Skip to content

Commit 723ed81

Browse files
Neil DEVASNeil DEVAS
authored andcommitted
implement crash API
1 parent a71f84b commit 723ed81

11 files changed

Lines changed: 335 additions & 80 deletions

configs/remote_rmi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
MID_RM cs-10 2005
22
CAR_RM cs-11 2000
3-
FLIGHT_RM cs-12 2001
3+
FLIGHT_RM cs-15 2001
44
ROOM_RM cs-13 2002
5-
CUST_RM cs-14 2003
5+
CUST_RM cs-16 2003

src/main/java/group25/Client/Client.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package group25.Client;
22

3+
import group25.Server.Interface.IAbstractRMHashMapManager;
34
import group25.Server.Interface.IMiddlewareResourceManager;
45
import group25.Server.LockManager.DeadlockException;
56
import static group25.Utils.AnsiColors.*;
@@ -174,6 +175,18 @@ private void execute(Command cmd, Vector<String> arguments) throws RemoteExcepti
174175
System.exit(0);
175176
break;
176177
}
178+
case CrashMiddleware: {
179+
crashMiddleware(arguments);
180+
break;
181+
}
182+
case CrashResourceManager: {
183+
crashResourceManager(arguments);
184+
break;
185+
}
186+
case ResetCrashes: {
187+
resetCrashes(arguments);
188+
break;
189+
}
177190
}
178191
}
179192

@@ -553,6 +566,33 @@ private void bundle(Vector<String> arguments) throws RemoteException, DeadlockEx
553566
}
554567
}
555568

569+
private void crashMiddleware(Vector<String> arguments) throws RemoteException {
570+
checkArgumentsCount(2, arguments.size());
571+
572+
int mode = toInt(arguments.get(1));
573+
m_resourceManager.crashMiddleware(mode);
574+
System.out.println("Set middleware crash mode to " + mode);
575+
}
576+
577+
private void crashResourceManager(Vector<String> arguments) throws RemoteException {
578+
checkArgumentsCount(3, arguments.size());
579+
580+
int mode = toInt(arguments.get(2));
581+
String rmName = arguments.get(1);
582+
m_resourceManager.crashResourceManager(rmName, mode);
583+
System.out.println("Set " + rmName + " resource manager crash mode to " + mode);
584+
}
585+
586+
private void resetCrashes(Vector<String> arguments) throws RemoteException {
587+
checkArgumentsCount(1, arguments.size());
588+
589+
m_resourceManager.crashMiddleware(0);
590+
m_resourceManager.crashResourceManager("car", 0);
591+
m_resourceManager.crashResourceManager("customer", 0);
592+
m_resourceManager.crashResourceManager("flight", 0);
593+
m_resourceManager.crashResourceManager("room", 0);
594+
}
595+
556596
private static Vector<String> parse(String command) {
557597
Vector<String> arguments = new Vector<String>();
558598
StringTokenizer tokenizer = new StringTokenizer(command, ",");

src/main/java/group25/Client/Command.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public enum Command {
3333

3434
Bundle("Book N flight numbers, and optionally a room and/or car at a location", "<xid>,<CustomerID>,<FlightNumber1>...<FlightNumberN>,<Location>,<Car-Y/N>,<Room-Y/N>"),
3535

36+
CrashMiddleware("Crash the middleware with a specified mode", "<mode>"),
37+
CrashResourceManager("Crash a resource manager with a specified mode", "<rmName=(car,flight,room,customer)>,<mode>"),
38+
ResetCrashes("Reset crashes", ""),
39+
3640
Shutdown("Shutdown all servers", ""),
3741
Quit("Exit the client application", "");
3842

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

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
import group25.Utils.CrashMode;
55
import group25.Utils.XMLPersistor;
66
import static group25.Utils.AnsiColors.RED;
7+
import static group25.Utils.AnsiColors.BLUE;
78

89
import java.util.*;
10+
11+
import javax.transaction.InvalidTransactionException;
12+
913
import java.rmi.RemoteException;
1014
import java.io.*;
1115

@@ -41,21 +45,36 @@ public AbstractRMHashMapManager(String p_name, String filename1, String filename
4145
}
4246

4347
public void crashResourceManager(CrashMode cm) {
48+
System.out.println("crash mode " + cm);
4449
if (cm.toString().substring(0,2).equals("TM")) {
4550
System.out.println(RED.colorString("ERROR: ")+"Invalid crash mode for RM: "+cm.toString());
4651
return;
4752
}
53+
System.out.println("setting crash mode");
4854
crashMode = cm;
4955
}
5056

57+
private void crashIf(CrashMode cm) {
58+
if (crashMode == cm) {
59+
System.out.println(BLUE.colorString("CRASH: ")+cm.toString());
60+
shutdown();
61+
}
62+
}
63+
5164
public void vote(int xid) throws RemoteException {
65+
System.out.println(getName() + " Voting");
66+
crashIf(CrashMode.RM_BEFORE_DECIDING_VOTE);
5267
new Thread(() -> {
5368
if (!transactionExists(xid)) {
5469
try {
70+
// TODO log ABORT maybe
71+
crashIf(CrashMode.RM_AFTER_DECIDING_VOTE);
5572
middlewareRM.receiveVote(xid, false, this.m_name);
73+
abort(xid);
74+
crashIf(CrashMode.RM_AFTER_VOTING);
5675
return;
5776
} catch (RemoteException e) {
58-
// TODO Auto-generated catch block
77+
// TODO handle this
5978
e.printStackTrace();
6079
}
6180
return;
@@ -65,24 +84,62 @@ public void vote(int xid) throws RemoteException {
6584
boolean gotLock = globalLock.lock(xid);
6685
if (!gotLock) {
6786
try {
68-
middlewareRM.receiveVote(xid, false, this.m_name);
87+
// TODO log ABORT maybe
88+
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+
96+
abort(xid);
97+
crashIf(CrashMode.RM_AFTER_VOTING);
6998
} catch (RemoteException e) {
70-
// TODO Auto-generated catch block
99+
// TODO handle this
71100
e.printStackTrace();
72101
}
73102
}
74-
103+
75104
updateThenPersistGlobalState(xid);
76-
try {
77-
middlewareRM.receiveVote(xid, true, this.m_name);
78-
} catch (RemoteException e) {
79-
// TODO Auto-generated catch block
80-
e.printStackTrace();
81-
}
105+
// TODO log YES
106+
crashIf(CrashMode.RM_AFTER_DECIDING_VOTE);
107+
new Thread(() -> {
108+
try {
109+
middlewareRM.receiveVote(xid, true, this.m_name);
110+
}
111+
catch (InvalidTransactionException ite) {
112+
System.out.println("Invalid transaction! Cannot vote.");
113+
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+
}
130+
try {
131+
Thread.sleep(2000);
132+
} catch (InterruptedException e1) { /* do nothing */}
133+
}
134+
}
135+
}).start();
136+
crashIf(CrashMode.RM_AFTER_VOTING);
82137
}).start();
83138
}
84139

85140
public boolean doCommit(int xid) throws RemoteException {
141+
System.out.println(getName() + " Committing");
142+
crashIf(CrashMode.RM_AFTER_RECEIVING_DECISION);
86143
// update pointer file
87144
if (currentCommitFile.equals(filename1)) {
88145
xmlPersistor.writeObject(filename2, pointerFile);
@@ -102,6 +159,8 @@ public boolean doCommit(int xid) throws RemoteException {
102159
}
103160

104161
public boolean abort(int xid) throws RemoteException {
162+
System.out.println(getName() + " aborting");
163+
crashIf(CrashMode.RM_AFTER_RECEIVING_DECISION);
105164
if (globalLock.getLockOwner() == xid) {
106165
synchronized(globalState) {
107166
removeTransactionState(xid);
@@ -253,6 +312,7 @@ public String getName() throws RemoteException {
253312
}
254313

255314
public void shutdown() {
315+
System.out.println(BLUE.colorString(m_name + " is shutting down."));
256316
System.exit(0);
257317
}
258318

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.rmi.RemoteException;
1111
import group25.Server.LockManager.*;
12+
import group25.Utils.CrashMode;
1213

1314
public abstract class MiddlewareResourceManager implements IMiddlewareResourceManager {
1415
protected ICarResourceManager carRM;
@@ -32,7 +33,7 @@ public boolean commit(int xid) throws RemoteException, InvalidTransactionExcepti
3233
}
3334

3435
public boolean abort(int xid) throws RemoteException, InvalidTransactionException {
35-
return transactionManager.abort(xid);
36+
return transactionManager.abort(xid, false);
3637
}
3738

3839
// Create a new flight, or add seats to existing flight
@@ -163,38 +164,46 @@ public boolean bundle(int xid, int customerID, Vector<Integer> flightNumbers, St
163164
for (int flightNumber : flightNumbers) {
164165
boolean flightReserved = transactionManager.reserveFlight(xid, customerID, flightNumber);
165166
if (!flightReserved) {
166-
transactionManager.abort(xid);
167+
transactionManager.abort(xid, false);
167168
return false;
168169
}
169170
}
170171

171172
if (car) {
172173
boolean carReserved = transactionManager.reserveCar(xid, customerID, location);
173174
if (!carReserved) {
174-
transactionManager.abort(xid);
175+
transactionManager.abort(xid, false);
175176
return false;
176177
}
177178
}
178179

179180
if (room) {
180181
boolean roomReserved = transactionManager.reserveRoom(xid, customerID, location);
181182
if (!roomReserved) {
182-
transactionManager.abort(xid);
183+
transactionManager.abort(xid, false);
183184
return false;
184185
}
185186
}
186187

187188
return true;
188189
}
189190

190-
public void receiveVote(int xid, boolean voteYes, String rmName) {
191+
public void receiveVote(int xid, boolean voteYes, String rmName) throws InvalidTransactionException, RemoteException {
191192
transactionManager.receiveVote(xid, voteYes, rmName);
192193
}
193194

194195
public String getName() throws RemoteException, DeadlockException {
195196
return m_name;
196197
}
197198

199+
public void crashMiddleware(int mode) throws RemoteException {
200+
transactionManager.crashMiddleware(mode);
201+
}
202+
203+
public void crashResourceManager(String rmName, int mode) throws RemoteException {
204+
transactionManager.crashResourceManager(rmName, mode);
205+
}
206+
198207
@Override
199208
public void shutdown() {
200209
transactionManager.shutdownAllResourceManagers();

0 commit comments

Comments
 (0)