summaryrefslogtreecommitdiffstats
path: root/src/main/java/derms/ReplicaManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/derms/ReplicaManager.java')
-rw-r--r--src/main/java/derms/ReplicaManager.java41
1 files changed, 35 insertions, 6 deletions
diff --git a/src/main/java/derms/ReplicaManager.java b/src/main/java/derms/ReplicaManager.java
index 83c1897..64ceb04 100644
--- a/src/main/java/derms/ReplicaManager.java
+++ b/src/main/java/derms/ReplicaManager.java
@@ -10,6 +10,7 @@ import derms.net.runicast.ReliableUnicastSender;
import derms.net.tomulticast.TotalOrderMulticastSender;
import derms.replica1.Replica1;
import derms.replica2.Replica2;
+import derms.util.*;
import java.io.IOException;
import java.net.InetAddress;
@@ -21,7 +22,7 @@ import java.io.ObjectInputStream;
import java.util.logging.Logger;
public class ReplicaManager {
- public static final String usage = "Usage: java ReplicaManager <replicaId> <city> <frontEndIP>";
+ public static final String usage = "Usage: java ReplicaManager <replicaId> <city> <frontEndIP> <byzantine(0 or 1)> <crash(0 or 1)>";
private final int replicaId;
private final String city;
private Replica replica;
@@ -31,12 +32,12 @@ public class ReplicaManager {
private ReliableUnicastSender<Response> unicastSender;
private TotalOrderMulticastReceiver multicastReceiver;
- public ReplicaManager(int replicaId, String city, InetAddress frontEndIP) throws IOException {
+ public ReplicaManager(int replicaId, String city, InetAddress frontEndIP, int byzantine, int crash) throws IOException {
this.replicaId = replicaId;
this.city = city;
this.log = Logger.getLogger(getClass().getName());
initUnicastSender(frontEndIP);
- initReplica();
+ initReplica(byzantine, crash);
initMulticastReceiver();
startHeartbeatThread();
}
@@ -47,7 +48,7 @@ public class ReplicaManager {
unicastSender = new ReliableUnicastSender<>(frontEndAddress);
}
- private void initReplica() throws IOException {
+ private void initReplica(int byzantine, int crash) throws IOException {
switch (replicaId) {
case 1:
replica = new Replica1(this);
@@ -62,7 +63,21 @@ public class ReplicaManager {
replica = new derms.replica2.Replica2(city, this);
break;
}
- replica.startProcess();
+
+ // [TEST] Logging
+ if (byzantine == 0) {
+ TestLogger.log("REPLICA " + replicaId + ": {BYZANTINE: FALSE}");
+ } else {
+ TestLogger.log("REPLICA " + replicaId + ": {BYZANTINE: TRUE}");
+ }
+
+ if (crash == 0) {
+ TestLogger.log("REPLICA " + replicaId + ": {CRASH: FALSE}");
+ } else {
+ TestLogger.log("REPLICA " + replicaId + ": {CRASH: TRUE}");
+ }
+
+ replica.startProcess(byzantine, crash);
}
private void initMulticastReceiver() throws IOException {
@@ -92,8 +107,12 @@ public class ReplicaManager {
new Thread(() -> {
while (true) {
if (!replica.isAlive()) {
+ // [TEST] Logging
+ TestLogger.log("REPLICA " + replicaId + ": {CRASH: DETECTED}");
+
informFrontEndRmIsDown(replica.getId());
replica.restart();
+ //TestLogger.log("REPLICA " + replicaId + ": {RESTARTED}");
}
try {
Thread.sleep(5000); // Example 5 seconds.
@@ -114,8 +133,13 @@ public class ReplicaManager {
public void handleByzantineFailure() {
log.severe("Byzantine failure detected in Replica " + replica.getId());
+
+ // [TEST] Logging
+ TestLogger.log("REPLICA " + replicaId + ": {BYZANTINE: DETECTED}");
+
replica.restart();
informFrontEndRmHasBug(replica.getId());
+ //TestLogger.log("REPLICA " + replicaId + ": {RESTARTED}");
}
private void informFrontEndRmIsDown(int replicaId) {
@@ -124,6 +148,7 @@ public class ReplicaManager {
out.writeObject("RM_DOWN:" + replicaId);
} catch (IOException e) {
log.severe("Failed to inform FE that RM is down: " + e.getMessage());
+ TestLogger.log("[FAILED TO INFORM FE (RM IS DOWN)]");
}
}
@@ -133,6 +158,7 @@ public class ReplicaManager {
out.writeObject("RM_BUG:" + replicaId);
} catch (IOException e) {
log.severe("Failed to inform FE that RM has a bug: " + e.getMessage());
+ TestLogger.log("[FAILED TO INFORM FE (RM HAS A BUG)]");
}
}
@@ -146,10 +172,13 @@ public class ReplicaManager {
int replicaId = Integer.parseInt(args[0]);
String city = args[1];
InetAddress frontEndIP = InetAddress.getByName(args[2]);
- ReplicaManager replicaManager = new ReplicaManager(replicaId, city, frontEndIP);
+ int byzantine = Integer.parseInt(args[3]);
+ int crash = Integer.parseInt(args[4]);
+ ReplicaManager replicaManager = new ReplicaManager(replicaId, city, frontEndIP, byzantine, crash);
System.out.println("ReplicaManager " + replicaId + " is running.");
} catch (IOException e) {
System.err.println("Failed to start ReplicaManager: " + e.getMessage());
+ TestLogger.log("[FAILED TO START RM]");
e.printStackTrace();
}
}