Skip to content

Commit

Permalink
Merge pull request #30 from elblasco/crash-on-election
Browse files Browse the repository at this point in the history
Crash on election
  • Loading branch information
lcereser6 authored Aug 10, 2024
2 parents 128aba6 + 05a65fd commit 73d1b9c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/main/java/it/unitn/disi/ds1/qtop/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,7 @@ private void onReadAck(@NotNull Utils.ReadValue msg) {
);
LOGGER.log(
LogLevel.INFO,
"[CLIENT-" + (this.clientId - this.numberOfNodes) + "] read done from node " + getSender() + " of " +
"value " + value + ", local operation id: " + msg.nRequest()
"[CLIENT-" + (this.clientId - this.numberOfNodes) + "] read done from " + Utils.matchNodeID(this.getSender()) + " of " + "value " + value + ", local operation id: " + msg.nRequest()
);
}

Expand Down
24 changes: 19 additions & 5 deletions src/main/java/it/unitn/disi/ds1/qtop/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -553,10 +553,14 @@ private void onTimeOut(@NotNull TimeOut msg) {
private void onElection(@NotNull Election msg) {
logger.log(
LogLevel.DEBUG,
"[NODE-" + this.nodeId + "] received election message from [NODE-" + this.getSender() + "] with " +
"params < e:" + msg.highestEpoch() + ", i:" + msg.highestIteration() + ">, best " + "candidate"
+ " received:" + msg.bestCandidateId()
"[NODE-" + this.nodeId + "] received election message from " + Utils.matchNodeID(this.getSender()) +
" with " + "params < e:" + msg.highestEpoch() + ", i:" + msg.highestIteration() + ">, best " + "candidate" + " received:" + msg.bestCandidateId()
);
if (this.crashType == CrashType.NODE_BEFORE_ELECTION_ACK)
{
this.crash();
return;
}
this.tell(
this.getSender(),
new ElectionACK(),
Expand Down Expand Up @@ -601,7 +605,7 @@ else if (msg.isBetterThanLocalData(
{
logger.log(
LogLevel.DEBUG,
"[NODE-" + this.nodeId + "] is not going to forward election message from " + this.getSender()
"[NODE-" + this.nodeId + "] is not going to forward election message from " + Utils.matchNodeID(this.getSender())
);
}
}
Expand Down Expand Up @@ -629,6 +633,10 @@ else if (msg.isBetterThanLocalData(
);
}
}
if (this.crashType == CrashType.NODE_AFTER_ELECTION_MESSAGE)
{
this.crash();
}
}

/**
Expand Down Expand Up @@ -658,6 +666,10 @@ private void startElection() {
latest,
idDest
);
if (this.crashType == CrashType.NODE_AFTER_ELECTION_MESSAGE)
{
this.crash();
}
}
}

Expand All @@ -673,7 +685,7 @@ private void onElectionAck(ElectionACK msg) {
);
logger.log(
LogLevel.DEBUG,
"[NODE-" + this.nodeId + "] received election ACK from [NODE-" + this.getSender() + "]"
"[NODE-" + this.nodeId + "] received election ACK from " + Utils.matchNodeID(this.getSender())
);
}

Expand Down Expand Up @@ -859,6 +871,8 @@ private void onCrashRequest(@NotNull CrashRequest msg) {
case NODE_AFTER_WRITE_REQUEST:
case NODE_AFTER_VOTE_REQUEST:
case NODE_AFTER_VOTE_CAST:
case NODE_BEFORE_ELECTION_ACK:
case NODE_AFTER_ELECTION_MESSAGE:
this.crashType = msg.crashType();
logger.log(
LogLevel.INFO,
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/it/unitn/disi/ds1/qtop/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ public void addCrashNode(int crashType) {
case 2 -> Utils.CrashType.NODE_AFTER_WRITE_REQUEST;
case 3 -> Utils.CrashType.NODE_AFTER_VOTE_REQUEST;
case 4 -> Utils.CrashType.NODE_AFTER_VOTE_CAST;
case 5 -> Utils.CrashType.COORDINATOR_ON_VOTE_REQUEST;
case 6 -> Utils.CrashType.COORDINATOR_ON_DECISION_RESPONSE;
case 5 -> Utils.CrashType.NODE_BEFORE_ELECTION_ACK;
case 6 -> Utils.CrashType.NODE_AFTER_ELECTION_MESSAGE;
case 7 -> Utils.CrashType.COORDINATOR_ON_VOTE_REQUEST;
case 8 -> Utils.CrashType.COORDINATOR_ON_DECISION_RESPONSE;
default -> Utils.CrashType.NO_CRASH;
};
// Crash messages are instantaneous
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/it/unitn/disi/ds1/qtop/UserInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@ public void clientMenu() {
System.out.println("2. NODE - After write request");
System.out.println("3. NODE - After vote request received");
System.out.println("4. NODE - After vote casted");
System.out.println("5. COORDINATOR - During the multicast of a vote request");
System.out.println("6. COORDINATOR - During the multicast of a decision response");
System.out.println("5. NODE - Before an election ack is sent");
System.out.println("6. NODE - After an election message is sent/forwarded");
System.out.println("7. COORDINATOR - During the multicast of a vote request");
System.out.println("8. COORDINATOR - During the multicast of a decision response");
int crashType = scanner.nextInt();
controller.crashNode(crashType);
break;
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/it/unitn/disi/ds1/qtop/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Class that contains all the utility classes and messages used by the actors.
Expand Down Expand Up @@ -149,6 +151,15 @@ public enum CrashType {
* Receiver crash after casting a vote.
*/
NODE_AFTER_VOTE_CAST,
/**
* Receiver crash when a node receives an election message.
* Does NOT reply with an ACK
*/
NODE_BEFORE_ELECTION_ACK,
/**
* Receiver crash after a node sends an Election message.
*/
NODE_AFTER_ELECTION_MESSAGE,
/**
* Probabilistic crash during a vote request multicast.
*/
Expand All @@ -159,6 +170,31 @@ public enum CrashType {
COORDINATOR_ON_DECISION_RESPONSE,
}

/**
* Method to match the ID of a node and build a fancy string out of it.
* There are two cases:
* <ul>
* <li> if the actor reference is a node, it will return [NODE-xyz]
* <li> if the actor reference is the coordinator, it will return [COORDINATOR]
* </ul>
*
* @param actorRefToMatch the actor reference to match
*
* @return the fancy String.
*/
public static String matchNodeID(ActorRef actorRefToMatch) {
Pattern pattern = Pattern.compile(".*node(\\d+)#.*");
Matcher m = pattern.matcher("" + actorRefToMatch);
if (m.matches())
{
return "[NODE-" + m.group(1) + "]";
}
else
{
return "[COORDINATOR]";
}
}

/**
* Start message that sends the list of participants to everyone.
*
Expand Down

0 comments on commit 73d1b9c

Please sign in to comment.