@@ -179,7 +179,7 @@ public synchronized boolean commit(int xid) throws InvalidTransactionException,
179179 boolean receivedAllVotes = sem .tryAcquire (25 , TimeUnit .SECONDS ); // all participants must respond within 25 seconds
180180 if (!receivedAllVotes ) {
181181 System .out .println ("Timed out waiting for votes. Aborting" );
182- abort (xid , true );
182+ abort (xid , false );
183183 }
184184 } catch (Exception e ) { /* do nothing*/ }
185185
@@ -205,57 +205,64 @@ public void receiveVote(int xid, boolean voteYes, String rmName) throws InvalidT
205205 // TODO log ABORT
206206 System .out .println ("Received NO vote from " + rmName + ". Aborting" );
207207 crashIf (CrashMode .TM_BEFORE_SENDING_DECISION );
208- abort (xid , true );
208+ abort (xid , false );
209209 return ;
210210 } catch (InvalidTransactionException e ) {
211211 return ;
212212 } catch (RemoteException e ) { /* do nothing */ }
213213 }
214-
215- new Thread (() -> {
216- ArrayList <Name_RM_Vote > resourceManagers ;
217- synchronized (resourceManagerRecorder ) {
218- resourceManagers = resourceManagerRecorder .get (xid );
214+
215+ ArrayList <Name_RM_Vote > resourceManagers ;
216+ synchronized (resourceManagerRecorder ) {
217+ resourceManagers = resourceManagerRecorder .get (xid );
218+ }
219+
220+ int yesCount = 0 ;
221+ int numRMs = 0 ;
222+ synchronized (resourceManagers ) {
223+ numRMs = resourceManagers .size ();
224+ for (Name_RM_Vote name_rm_vote : resourceManagers ) {
225+ if (name_rm_vote .rmName .equals (rmName )) {
226+ name_rm_vote .votedYes = true ;
227+ }
228+ if (name_rm_vote .votedYes != null && name_rm_vote .votedYes ) {
229+ yesCount ++;
230+ if (yesCount == 2 ) {
231+ crashIf (CrashMode .TM_BEFORE_SOME_VOTE_REPLIES ); // crash at second yesVote recieved
232+ }
233+ }
219234 }
220-
235+ }
236+
237+ final int yesCount2 = yesCount ;
238+ final int numRMs2 = numRMs ;
239+ new Thread (() -> {
221240 Semaphore sem = null ; // this semaphore lets commit() complete
222241 synchronized (voteReplyWaitMap ) {
223242 sem = voteReplyWaitMap .get (xid );
224243 }
225-
226- synchronized (resourceManagers ) {
227- int numRMs = resourceManagers .size ();
228- int yesCount = 0 ;
229- for (Name_RM_Vote name_rm_vote : resourceManagers ) {
230- if (name_rm_vote .rmName .equals (rmName )) {
231- name_rm_vote .votedYes = true ;
232- }
233- if (name_rm_vote .votedYes != null && name_rm_vote .votedYes ) {
234- yesCount ++;
235- if (yesCount == 2 ) {
236- crashIf (CrashMode .TM_BEFORE_SOME_VOTE_REPLIES ); // crash at second yesVote recieved
237- }
244+
245+ if (yesCount2 == numRMs2 ) {
246+ System .out .println (GREEN .colorString ("Success: " ) + "received all yesses." );
247+ crashIf (CrashMode .TM_BEFORE_DECIDING );
248+ // TODO log COMMIT.
249+ // This is the point of no return.
250+ synchronized (transactionAges ) {
251+ // finally kill transaction state
252+ if (transactionAges .remove (xid ) == null ) {
253+ System .out .println ("Transaction not found - Whoopsie" );
254+ return ; // probably shouldn't happen
238255 }
239256 }
240- if (yesCount == numRMs ) {
241- System .out .println (GREEN .colorString ("Success: " ) + "received all yesses." );
242- crashIf (CrashMode .TM_BEFORE_DECIDING );
243- // TODO log COMMIT.
244- // This is the point of no return.
245- synchronized (transactionAges ) {
246- // finally kill transaction state
247- if (transactionAges .remove (xid ) == null ) {
248- System .out .println ("Transaction not found - Whoopsie" );
249- return ; // probably shouldn't happen
250- }
251- }
252- crashIf (CrashMode .TM_BEFORE_SENDING_DECISION );
253-
257+ crashIf (CrashMode .TM_BEFORE_SENDING_DECISION );
258+
259+ synchronized (resourceManagers ) {
260+
254261 for (int i =0 ; i <resourceManagers .size (); i ++) {
255262 if (i == resourceManagers .size () - 1 ) {
256263 crashIf (CrashMode .TM_BEFORE_SENDING_LAST_DECISION );
257264 }
258-
265+
259266 Name_RM_Vote name_rm_vote = resourceManagers .get (i );
260267 try {
261268 name_rm_vote .rm .doCommit (xid );
@@ -288,13 +295,14 @@ public void receiveVote(int xid, boolean voteYes, String rmName) throws InvalidT
288295 sem .release ();
289296 }
290297 }
291- // ------------- HERE ENDS 2PC -------------
292- return ;
298+ // ------------- HERE ENDS 2PC -------------
299+ return ;
293300 }).start ();
294301 }
295302
296303
297304 private boolean abort (int xid ) throws InvalidTransactionException , RemoteException {
305+ if (transactionAges .remove (xid ) == null ) throw new InvalidTransactionException ();
298306 ArrayList <Name_RM_Vote > resourceManagers = null ;
299307 Semaphore sem = null ;
300308
0 commit comments