diff --git a/src/main/java/it/unitn/disi/ds1/qtop/Node.java b/src/main/java/it/unitn/disi/ds1/qtop/Node.java index 5ce719d..c9556e3 100644 --- a/src/main/java/it/unitn/disi/ds1/qtop/Node.java +++ b/src/main/java/it/unitn/disi/ds1/qtop/Node.java @@ -95,15 +95,19 @@ private void setGroup(@NotNull StartMessage sm) { private void multicast(Serializable m, boolean hasToCrash) { ArrayList 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; } @@ -113,6 +117,10 @@ private void multicast(Serializable m, boolean hasToCrash) { getSelf() ); } + if (hasToCrash) + { + System.out.println("-------------------------------------"); + } } /** @@ -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" @@ -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( @@ -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( diff --git a/src/main/java/it/unitn/disi/ds1/qtop/UserInterface.java b/src/main/java/it/unitn/disi/ds1/qtop/UserInterface.java index 5f8a757..079fbfe 100644 --- a/src/main/java/it/unitn/disi/ds1/qtop/UserInterface.java +++ b/src/main/java/it/unitn/disi/ds1/qtop/UserInterface.java @@ -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"); diff --git a/src/main/java/it/unitn/disi/ds1/qtop/Utils.java b/src/main/java/it/unitn/disi/ds1/qtop/Utils.java index 452b464..546c619 100644 --- a/src/main/java/it/unitn/disi/ds1/qtop/Utils.java +++ b/src/main/java/it/unitn/disi/ds1/qtop/Utils.java @@ -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; @@ -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)); + } } /**