<commit_before>package fr.inria.jessy.transaction.termination;

import java.util.HashMap;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import net.sourceforge.fractal.membership.Group;
import fr.inria.jessy.communication.message.TerminateTransactionRequestMessage;
import fr.inria.jessy.communication.message.VoteMessage;
import fr.inria.jessy.consistency.Consistency.ConcernedKeysTarget;
import fr.inria.jessy.partitioner.Partitioner;
import fr.inria.jessy.transaction.ExecutionHistory;
import fr.inria.jessy.transaction.TransactionState;
import fr.inria.jessy.transaction.termination.vote.Vote;

public class TwoPhaseCommit extends AtomicCommit {

	String swid=""+jessy.manager.getSourceId();
	
	//TODO FIX ME THIS IS CRAP
	ConcurrentHashMap<UUID, Integer> causedTermination=new ConcurrentHashMap<UUID, Integer>(); 
	
	public TwoPhaseCommit(DistributedTermination termination) {
		super(termination);
	}

	@Override
	public boolean proceedToCertifyAndVote(
			TerminateTransactionRequestMessage msg) {
		synchronized(atomicDeliveredMessages){
			for (TerminateTransactionRequestMessage n : atomicDeliveredMessages) {
				if (n.equals(msg)) {
					continue;
				}
				if (!jessy.getConsistency().certificationCommute(
						n.getExecutionHistory(), msg.getExecutionHistory())) {
					
					UUID caused=n.getExecutionHistory().getTransactionHandler().getId();
					try{
					int ii=causedTermination.get(caused);
					System.out.println("XXXXXXX" + caused + " >> " + ii);
					}catch(Exception ex){
						ex.printStackTrace();
					}
					
					if (causedTermination.contains(caused)){
						int i=causedTermination.get(caused);
						System.out.println("XXXXXXX" + caused + " >> " + i);
						if (i>5)
						{
							causedTermination.remove(caused);
							atomicDeliveredMessages.remove(n);
							return true;
						}
						causedTermination.put(caused, i+1);
					}else{
						causedTermination.put(caused, 1);
					}
					
					System.out.println("Pre-emptive abort for " + msg.getExecutionHistory().getTransactionHandler().getId() + " because " + n.getExecutionHistory().getTransactionHandler().getId());
					return false;
				}
			}
		}
		return true;
	}
	
	@Override
	public void setVoters(TerminateTransactionRequestMessage msg ,Set<String> voteReceivers, AtomicBoolean voteReceiver, Set<String> voteSenders, AtomicBoolean voteSender){

		/*
		 * In 2pc, those that receive the transaction, always participate in voting.
		 * Hence, vote sender is always true.
		 */
		voteSender.set(true);
		
		if (isCoordinator(msg)){
			
			/*
			 * If this is coordinator, wait for the votes from every body.
			 */
			Set<String> keys_for_SendVotes =jessy
					.getConsistency().getConcerningKeys(
							msg.getExecutionHistory(),
							ConcernedKeysTarget.SEND_VOTES);
			
			Group g;<commit_msg>[FIX] Two Phase Commit<commit_after>package fr.inria.jessy.transaction.termination;

import java.util.HashMap;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import net.sourceforge.fractal.membership.Group;
import fr.inria.jessy.communication.message.TerminateTransactionRequestMessage;
import fr.inria.jessy.communication.message.VoteMessage;
import fr.inria.jessy.consistency.Consistency.ConcernedKeysTarget;
import fr.inria.jessy.partitioner.Partitioner;
import fr.inria.jessy.transaction.ExecutionHistory;
import fr.inria.jessy.transaction.TransactionState;
import fr.inria.jessy.transaction.termination.vote.Vote;

public class TwoPhaseCommit extends AtomicCommit {

	String swid=""+jessy.manager.getSourceId();
	
	//TODO FIX ME THIS IS CRAP
	ConcurrentHashMap<UUID, Integer> causedTermination=new ConcurrentHashMap<UUID, Integer>(); 
	
	public TwoPhaseCommit(DistributedTermination termination) {
		super(termination);
	}

	@Override
	public boolean proceedToCertifyAndVote(
			TerminateTransactionRequestMessage msg) {
		synchronized(atomicDeliveredMessages){
			for (TerminateTransactionRequestMessage n : atomicDeliveredMessages) {
				if (n.equals(msg)) {
					continue;
				}
				if (!jessy.getConsistency().certificationCommute(
						n.getExecutionHistory(), msg.getExecutionHistory())) {
					
					UUID caused=n.getExecutionHistory().getTransactionHandler().getId();
					
					if (causedTermination.containsKey(caused)){
						int i=causedTermination.get(caused);
						if (i>5)
						{
							causedTermination.remove(caused);
							atomicDeliveredMessages.remove(n);
							return true;
						}
						causedTermination.put(caused, i+1);
					}else{
						causedTermination.put(caused, 1);
					}
					
//					System.out.println("Pre-emptive abort for " + msg.getExecutionHistory().getTransactionHandler().getId() + " because " + n.getExecutionHistory().getTransactionHandler().getId());
					return false;
				}
			}
		}
		return true;
	}
	
	@Override
	public void setVoters(TerminateTransactionRequestMessage msg ,Set<String> voteReceivers, AtomicBoolean voteReceiver, Set<String> voteSenders, AtomicBoolean voteSender){

		/*
		 * In 2pc, those that receive the transaction, always participate in voting.
		 * Hence, vote sender is always true.
		 */
		voteSender.set(true);
		
		if (isCoordinator(msg)){
			
			/*
			 * If this is coordinator, wait for the votes from every body.
			 */
			Set<String> keys_for_SendVotes =jessy
					.getConsistency().getConcerningKeys(
							msg.getExecutionHistory(),
							ConcernedKeysTarget.SEND_VOTES);
			
			Group g;
{'fraction_non_alphanumeric': 0.10820025121119684, 'fraction_numerical': 0.0014354925533823794, 'mean_word_length': 4.170686456400742, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4440403', 'n_tokens_mistral': 2100, 'n_tokens_neox': 1682, 'n_words': 308}