Skip to content

Commit db135dc

Browse files
Neil DEVASNeil DEVAS
authored andcommitted
make fixes to crash API
1 parent 723ed81 commit db135dc

4 files changed

Lines changed: 140 additions & 170 deletions

File tree

src/main/java/group25/Server/Common/AbstractRMHashMapManager.java

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import group25.Utils.XMLPersistor;
66
import static group25.Utils.AnsiColors.RED;
77
import static group25.Utils.AnsiColors.BLUE;
8+
import static group25.Utils.AnsiColors.GREEN;
89

910
import java.util.*;
1011

@@ -62,78 +63,80 @@ private void crashIf(CrashMode cm) {
6263
}
6364

6465
public void vote(int xid) throws RemoteException {
65-
System.out.println(getName() + " Voting");
66-
crashIf(CrashMode.RM_BEFORE_DECIDING_VOTE);
66+
System.out.println(getName() + " got vote request.");
67+
68+
// do this all in a new thread since we want vote() to return immediately in the TM
6769
new Thread(() -> {
70+
crashIf(CrashMode.RM_BEFORE_DECIDING_VOTE);
6871
if (!transactionExists(xid)) {
6972
try {
70-
// TODO log ABORT maybe
73+
// TODO log abort
7174
crashIf(CrashMode.RM_AFTER_DECIDING_VOTE);
75+
System.out.println("Transaction not found. Voting no.");
7276
middlewareRM.receiveVote(xid, false, this.m_name);
73-
abort(xid);
7477
crashIf(CrashMode.RM_AFTER_VOTING);
75-
return;
7678
} catch (RemoteException e) {
77-
// TODO handle this
78-
e.printStackTrace();
79+
System.out.println("Could not send vote to TM");
7980
}
8081
return;
8182
}
8283

83-
// get global lock
8484
boolean gotLock = globalLock.lock(xid);
8585
if (!gotLock) {
8686
try {
87-
// TODO log ABORT maybe
87+
// TODO log abort
8888
crashIf(CrashMode.RM_AFTER_DECIDING_VOTE);
89-
new Thread(() -> {
90-
try {
91-
middlewareRM.receiveVote(xid, false, this.m_name);
92-
} catch (Exception e) {
93-
}
94-
}).start();
95-
9689
abort(xid);
90+
System.out.println("Could not get global RM persistence lock. Voting no.");
91+
middlewareRM.receiveVote(xid, false, this.m_name);
9792
crashIf(CrashMode.RM_AFTER_VOTING);
9893
} catch (RemoteException e) {
99-
// TODO handle this
100-
e.printStackTrace();
94+
System.out.println("Could not send vote to TM");
10195
}
96+
return;
10297
}
10398

99+
// all good, we will vote YES
104100
updateThenPersistGlobalState(xid);
105-
// TODO log YES
106-
crashIf(CrashMode.RM_AFTER_DECIDING_VOTE);
107-
new Thread(() -> {
101+
102+
// TODO log YES
103+
crashIf(CrashMode.RM_AFTER_DECIDING_VOTE);
104+
105+
// uncertainty phase. Send Yes, and wait for decision
106+
boolean sentVote = false;
107+
new Thread(() -> {
108+
System.out.println(GREEN.colorString("Voting yes."));
109+
try {
110+
middlewareRM.receiveVote(xid, true, this.m_name);
111+
return;
112+
} catch (InvalidTransactionException ite) {
113+
System.out.println("TM says invalid transaction. Abort.");
108114
try {
109-
middlewareRM.receiveVote(xid, true, this.m_name);
110-
}
111-
catch (InvalidTransactionException ite) {
112-
System.out.println("Invalid transaction! Cannot vote.");
115+
abort(xid);
116+
return;
117+
} catch (RemoteException e) { /* can't happen, do nothing */ }
118+
} catch (RemoteException re) {
119+
System.out.println("Could not send vote to Coordinator. Sending again.");
120+
while (true) { // keep sending indefinitely
113121
try {
114-
abort(xid);
115-
} catch (RemoteException e) { }
116-
} catch (RemoteException e) {
117-
System.out.println("Could not send vote request to Coordinator. Sending again.");
118-
while (true) {
119-
try {
120-
middlewareRM.receiveVote(xid, true, this.m_name);
121-
break;
122-
} catch (InvalidTransactionException ee) {
123-
try {
124-
abort(xid);
125-
break;
126-
} catch (RemoteException e1) { }
127-
} catch (RemoteException eee) {
128-
System.out.println("Could not send vote request to Coordinator. Sending again.");
129-
}
122+
System.out.println(GREEN.colorString("Try again to vote yes."));
123+
middlewareRM.receiveVote(xid, true, this.m_name);
124+
return;
125+
} catch (InvalidTransactionException ite) {
126+
System.out.println("Invalid transaction! Abort.");
130127
try {
128+
abort(xid);
129+
return;
130+
} catch (RemoteException e) { }
131+
} catch (RemoteException re2) {
132+
System.out.println("Could not send vote request to Coordinator. Sending again.");
133+
}
134+
try {
131135
Thread.sleep(2000);
132136
} catch (InterruptedException e1) { /* do nothing */}
133-
}
134137
}
135-
}).start();
136-
crashIf(CrashMode.RM_AFTER_VOTING);
138+
}
139+
}).start();
137140
}).start();
138141
}
139142

src/main/java/group25/Server/Common/FairWaitInterruptibleLock.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public boolean lock(int xid) {
1818
synchronized(this) {
1919
xids.addLast(xid);
2020
xidToSemaphore.put(xid, new Semaphore(1));
21-
System.out.println("getting xids in lock " + xids);
2221
try {
2322
if (xids.peekFirst() != xid) {// not first in line
2423
xidToSemaphore.get(xid).acquire();// next acquire() fails

0 commit comments

Comments
 (0)