Skip to content

Commit

Permalink
feat: check to forward in Utils.Election
Browse files Browse the repository at this point in the history
  • Loading branch information
elblasco committed Jul 31, 2024
1 parent 7f32e96 commit 5199d00
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
25 changes: 18 additions & 7 deletions src/main/java/it/unitn/disi/ds1/qtop/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,19 @@ private void setGroup(@NotNull StartMessage sm) {
private void multicast(Serializable m, boolean hasToCrash) {
ArrayList<ActorRef> groupCopy = new ArrayList<>(this.group);
Collections.shuffle(groupCopy);
if (hasToCrash)
{
System.out.println("The coordinator is multicasting " + m + " to the group");
}
for (ActorRef node : groupCopy)
{
if (hasToCrash)
{
System.out.println("Destination for this message " + node);
}
if (hasToCrash && (rand.nextInt(100) < 10) && node != this.getSelf())
if (hasToCrash && (rand.nextInt(100) < 20) && node != this.getSelf())
{
System.out.println(node + "will not receive the message");
System.out.println("Bye Bye\n-------------------------------------");
this.coordinatorCrash();
return;
}
Expand All @@ -113,6 +117,10 @@ private void multicast(Serializable m, boolean hasToCrash) {
getSelf()
);
}
if (hasToCrash)
{
System.out.println("-------------------------------------");
}
}

/**
Expand Down Expand Up @@ -577,7 +585,6 @@ private void onElection(@NotNull Election msg) {
this.timeOutManager.endElectionState();
this.history.add(new ArrayList<>());
this.numbersOfWrites = 0;
//this.quorum = (this.group.size() / 2) + 1;
logger.log(
LogLevel.INFO,
"[NODE-" + this.nodeId + "] elected as coordinator"
Expand All @@ -595,8 +602,10 @@ private void onElection(@NotNull Election msg) {
);
}
else if (
(msg.highestEpoch() > nodeLatest.e() && msg.highestIteration() > nodeLatest.i()) ||
(msg.highestEpoch() == nodeLatest.e() && msg.highestIteration() == nodeLatest.i() && msg.bestCandidateId() < this.nodeId)
msg.isGreaterThanLocalData(
this.nodeId,
nodeLatest
)
)
{
this.forwardPreviousElectionMessage(
Expand All @@ -618,8 +627,10 @@ else if (
this.timeOutManager.startElectionState();
this.isElection = true;
if (
(msg.highestEpoch() > nodeLatest.e() && msg.highestIteration() > nodeLatest.i()) ||
(msg.highestEpoch() == nodeLatest.e() && msg.highestIteration() == nodeLatest.i() && msg.bestCandidateId() < this.nodeId)
msg.isGreaterThanLocalData(
this.nodeId,
nodeLatest
)
)
{
this.forwardPreviousElectionMessage(
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/it/unitn/disi/ds1/qtop/UserInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void start() {
int numberOfClients = 3;
int decisionTimeout = 2000;
int voteTimeout = 1000;
int writeTimeout = 20000;
int writeTimeout = 2000;

System.out.println("Qtop - DS Project 2023/2024 - Blascovich Alessio, Cereser Lorenzo \n");

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/it/unitn/disi/ds1/qtop/Utils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.unitn.disi.ds1.qtop;

import akka.actor.ActorRef;
import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.HashMap;
Expand Down Expand Up @@ -277,6 +278,11 @@ public record EpochPair(int e, int i) implements Serializable {
* @param bestCandidateId node that have that EpochPair
*/
public record Election(int highestEpoch, int highestIteration, int bestCandidateId) implements Serializable {
public boolean isGreaterThanLocalData(int nodeId, @NotNull EpochPair ep){
return ((this.highestEpoch() > ep.e()) ||
(this.highestEpoch() == ep.e() && this.highestIteration() > ep.i()) ||
(this.highestEpoch() == ep.e() && this.highestIteration() == ep.i() && this.bestCandidateId() < nodeId));
}
}

/**
Expand Down

0 comments on commit 5199d00

Please sign in to comment.