Skip to content

Commit 0780256

Browse files
Neil DEVASNeil DEVAS
authored andcommitted
Crash API fixes
1 parent db135dc commit 0780256

2 files changed

Lines changed: 48 additions & 38 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public void vote(int xid) throws RemoteException {
108108
System.out.println(GREEN.colorString("Voting yes."));
109109
try {
110110
middlewareRM.receiveVote(xid, true, this.m_name);
111+
crashIf(CrashMode.RM_AFTER_VOTING);
111112
return;
112113
} catch (InvalidTransactionException ite) {
113114
System.out.println("TM says invalid transaction. Abort.");
@@ -121,6 +122,7 @@ public void vote(int xid) throws RemoteException {
121122
try {
122123
System.out.println(GREEN.colorString("Try again to vote yes."));
123124
middlewareRM.receiveVote(xid, true, this.m_name);
125+
crashIf(CrashMode.RM_AFTER_VOTING);
124126
return;
125127
} catch (InvalidTransactionException ite) {
126128
System.out.println("Invalid transaction! Abort.");

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

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)