From a9162f8ea916550fc27c5d969eab97ae1b0949bb Mon Sep 17 00:00:00 2001 From: Simon Massey Date: Sun, 17 Nov 2024 17:47:57 +0000 Subject: [PATCH] tidy-up --- .../main/java/com/github/trex_paxos/Pickle.java | 1 - .../main/java/com/github/trex_paxos/TrexNode.java | 11 ----------- .../src/main/java/com/github/trex_paxos/Vote.java | 8 +++++++- .../com/github/trex_paxos/msg/AbstractCommand.java | 3 ++- .../com/github/trex_paxos/msg/BallotNumber.java | 1 - .../com/github/trex_paxos/msg/CatchupResponse.java | 1 - .../java/com/github/trex_paxos/msg/Command.java | 1 - .../java/com/github/trex_paxos/msg/Commit.java | 2 -- .../java/com/github/trex_paxos/msg/Prepare.java | 6 +++++- .../com/github/trex_paxos/msg/PrepareResponse.java | 8 +++++--- .../java/com/github/trex_paxos/msg/Progress.java | 10 ++++++---- .../java/com/github/trex_paxos/Simulation.java | 14 +------------- .../java/com/github/trex_paxos/SimulationTest.java | 2 +- 13 files changed, 27 insertions(+), 41 deletions(-) diff --git a/trex2-lib/src/main/java/com/github/trex_paxos/Pickle.java b/trex2-lib/src/main/java/com/github/trex_paxos/Pickle.java index b07a1f7..124daec 100644 --- a/trex2-lib/src/main/java/com/github/trex_paxos/Pickle.java +++ b/trex2-lib/src/main/java/com/github/trex_paxos/Pickle.java @@ -6,7 +6,6 @@ import java.util.ArrayList; import java.util.List; import java.util.stream.IntStream; - /// Pickle is a utility class for serializing and deserializing record types. /// Java serialization is famously broken but the Java Platform team are working on it. /// This class does things the boilerplate way. diff --git a/trex2-lib/src/main/java/com/github/trex_paxos/TrexNode.java b/trex2-lib/src/main/java/com/github/trex_paxos/TrexNode.java index 9b0ef09..fc49364 100644 --- a/trex2-lib/src/main/java/com/github/trex_paxos/TrexNode.java +++ b/trex2-lib/src/main/java/com/github/trex_paxos/TrexNode.java @@ -421,17 +421,6 @@ public long highestCommitted() { return progress.highestCommittedIndex(); } - public long highestFixedLogIndex() { - assert role == LEAD : "role=" + role; - Optional> first = this.acceptVotesByLogIndex - .reversed() - .entrySet() - .stream() - .filter(e -> e.getValue().chosen()) - .findFirst(); - return first.isEmpty() ? progress.highestCommittedIndex() : first.get().getKey(); - } - public byte nodeIdentifier() { return nodeIdentifier; } diff --git a/trex2-lib/src/main/java/com/github/trex_paxos/Vote.java b/trex2-lib/src/main/java/com/github/trex_paxos/Vote.java index d54d78a..292ab17 100644 --- a/trex2-lib/src/main/java/com/github/trex_paxos/Vote.java +++ b/trex2-lib/src/main/java/com/github/trex_paxos/Vote.java @@ -3,5 +3,11 @@ import com.github.trex_paxos.msg.BallotNumber; /// Nodes in a cluster vote for whether an accept message is chosen or not. This object tracks such votes. -public record Vote(byte from, byte to, long logIndex, boolean vote, BallotNumber number) { +public record Vote( + byte from, + byte to, + long logIndex, + boolean vote, + BallotNumber number +) { } diff --git a/trex2-lib/src/main/java/com/github/trex_paxos/msg/AbstractCommand.java b/trex2-lib/src/main/java/com/github/trex_paxos/msg/AbstractCommand.java index 674f347..45b874f 100644 --- a/trex2-lib/src/main/java/com/github/trex_paxos/msg/AbstractCommand.java +++ b/trex2-lib/src/main/java/com/github/trex_paxos/msg/AbstractCommand.java @@ -1,5 +1,6 @@ package com.github.trex_paxos.msg; -/// There are two types of commands. The NOOP which is used to speed up recovery and real commands sent by clients of the host applicationd +/// There are two types of commands. The NOOP which is used to speed up recovery and real commands sent by clients of +/// the host application public sealed interface AbstractCommand extends Message permits NoOperation, Command { } diff --git a/trex2-lib/src/main/java/com/github/trex_paxos/msg/BallotNumber.java b/trex2-lib/src/main/java/com/github/trex_paxos/msg/BallotNumber.java index 4758e37..10b6104 100644 --- a/trex2-lib/src/main/java/com/github/trex_paxos/msg/BallotNumber.java +++ b/trex2-lib/src/main/java/com/github/trex_paxos/msg/BallotNumber.java @@ -1,5 +1,4 @@ package com.github.trex_paxos.msg; - /// A ballot number is the proposal number used in the Paxos algorithm. Here we are using five bytes. The most significant /// are incremented as an integer when a node wishes to become a leader. We encode the /// nodeIdentifier in the least significant fifth byte. This works as long as we make the nodeIdentifier unique within the cluster diff --git a/trex2-lib/src/main/java/com/github/trex_paxos/msg/CatchupResponse.java b/trex2-lib/src/main/java/com/github/trex_paxos/msg/CatchupResponse.java index 4c584f1..ab0f4e0 100644 --- a/trex2-lib/src/main/java/com/github/trex_paxos/msg/CatchupResponse.java +++ b/trex2-lib/src/main/java/com/github/trex_paxos/msg/CatchupResponse.java @@ -1,7 +1,6 @@ package com.github.trex_paxos.msg; import java.util.List; - /// CatchupResponse is a message sent by the leader to a replica in response to a Catchup message. /// It will only return committed slots that the replica has requested. This is to avoid sending /// lots of uncommitted messages during a partition where an old leader is not yet aware of a new leader. diff --git a/trex2-lib/src/main/java/com/github/trex_paxos/msg/Command.java b/trex2-lib/src/main/java/com/github/trex_paxos/msg/Command.java index acd98e5..dc11db3 100644 --- a/trex2-lib/src/main/java/com/github/trex_paxos/msg/Command.java +++ b/trex2-lib/src/main/java/com/github/trex_paxos/msg/Command.java @@ -1,7 +1,6 @@ package com.github.trex_paxos.msg; import java.util.Arrays; - /// A client command to be executed by the state machine. As this library is neutral /// to the application, the command is completely opaque to the library. The /// application is responsible for encoding and decoding the commands from and to byte array. diff --git a/trex2-lib/src/main/java/com/github/trex_paxos/msg/Commit.java b/trex2-lib/src/main/java/com/github/trex_paxos/msg/Commit.java index 1b63c8b..0411070 100644 --- a/trex2-lib/src/main/java/com/github/trex_paxos/msg/Commit.java +++ b/trex2-lib/src/main/java/com/github/trex_paxos/msg/Commit.java @@ -9,8 +9,6 @@ /// it does not have the correct accept. /// @param committedLogIndex The highest contiguous log index that the leader has learnt to have been fixed and so /// committed. -/// @param highestFixedLogIndex The highest log index that the leader has learnt to have been fixed. This may be the -/// same as committedLogIndex. Due to lost messages there may be a gap between the two. public record Commit( byte from, BallotNumber number, diff --git a/trex2-lib/src/main/java/com/github/trex_paxos/msg/Prepare.java b/trex2-lib/src/main/java/com/github/trex_paxos/msg/Prepare.java index 5254f49..e957250 100644 --- a/trex2-lib/src/main/java/com/github/trex_paxos/msg/Prepare.java +++ b/trex2-lib/src/main/java/com/github/trex_paxos/msg/Prepare.java @@ -1,4 +1,8 @@ package com.github.trex_paxos.msg; -public record Prepare(byte from, long logIndex, BallotNumber number) implements TrexMessage, BroadcastMessage { +public record Prepare( + byte from, + long logIndex, + BallotNumber number +) implements TrexMessage, BroadcastMessage { } diff --git a/trex2-lib/src/main/java/com/github/trex_paxos/msg/PrepareResponse.java b/trex2-lib/src/main/java/com/github/trex_paxos/msg/PrepareResponse.java index b30b6e4..6de0c46 100644 --- a/trex2-lib/src/main/java/com/github/trex_paxos/msg/PrepareResponse.java +++ b/trex2-lib/src/main/java/com/github/trex_paxos/msg/PrepareResponse.java @@ -11,10 +11,12 @@ // FIXME seems unsafe or hard to deal with logging if adding in the Ballot Number. An AcceptResponse transmits such info as progress and I think we should add it here at the very least to log. /// A PrepareResponse is a response to a Prepare message. It contains the vote and the highest uncommitted log entry if any. -public record PrepareResponse(Vote vote, - long highestCommittedIndex, - Optional highestUncommitted +public record PrepareResponse( + Vote vote, + long highestCommittedIndex, + Optional highestUncommitted ) implements TrexMessage, DirectMessage { + public byte from() { return vote.from(); } diff --git a/trex2-lib/src/main/java/com/github/trex_paxos/msg/Progress.java b/trex2-lib/src/main/java/com/github/trex_paxos/msg/Progress.java index eab5ed2..401b169 100644 --- a/trex2-lib/src/main/java/com/github/trex_paxos/msg/Progress.java +++ b/trex2-lib/src/main/java/com/github/trex_paxos/msg/Progress.java @@ -15,10 +15,12 @@ * @param highestCommittedIndex The highest log index that has been learnt to have been fixed and so committed. * @param highestAcceptedIndex The highest log index that has been accepted. */ -public record Progress(byte nodeIdentifier, - BallotNumber highestPromised, - long highestCommittedIndex, - long highestAcceptedIndex) { +public record Progress( + byte nodeIdentifier, + BallotNumber highestPromised, + long highestCommittedIndex, + long highestAcceptedIndex +) { /** * When an application initializes an empty journal it has to have a NIL value. diff --git a/trex2-lib/src/test/java/com/github/trex_paxos/Simulation.java b/trex2-lib/src/test/java/com/github/trex_paxos/Simulation.java index 8b9e63b..5f9cef4 100644 --- a/trex2-lib/src/test/java/com/github/trex_paxos/Simulation.java +++ b/trex2-lib/src/test/java/com/github/trex_paxos/Simulation.java @@ -56,13 +56,11 @@ static RandomGenerator repeatableRandomGenerator(long seed) { Map nodeTimeouts = new HashMap<>(); - public ArrayList run( + public void run( int iterations, boolean makeClientMessages, BiFunction> nemesis ) { - final var allMessages = new ArrayList(); - if (makeClientMessages) { makeClientDataEvents(iterations, eventQueue); } @@ -136,9 +134,6 @@ public ArrayList run( final var nextTimeList = this.eventQueue.computeIfAbsent(nextTime, _ -> new ArrayList<>()); nextTimeList.add(new Send(newMessages)); } - - // add the messages to the all messages list - allMessages.addAll(newMessages); }); // if the event queue is empty we are done var finished = this.eventQueue.isEmpty(); @@ -172,7 +167,6 @@ public ArrayList run( } return finished; }); - return allMessages; } static OptionalLong inconsistentCommittedIndex(TreeMap c1, @@ -425,11 +419,5 @@ public Optional loadAccept(long logIndex) { public void sync() { // no-op } - - public Optional getCommitted(Long logIndex) { - return logIndex <= this.progress.highestCommittedIndex() ? - Optional.ofNullable(fakeJournal.get(logIndex)) : - Optional.empty(); - } } } diff --git a/trex2-lib/src/test/java/com/github/trex_paxos/SimulationTest.java b/trex2-lib/src/test/java/com/github/trex_paxos/SimulationTest.java index 7c9f12d..de24a38 100644 --- a/trex2-lib/src/test/java/com/github/trex_paxos/SimulationTest.java +++ b/trex2-lib/src/test/java/com/github/trex_paxos/SimulationTest.java @@ -187,7 +187,7 @@ public void testWorkRotationNetworkPartition100() { max.set(min); } }); - assertThat(max.get()).isGreaterThan(0); + assertThat(max.get()).isGreaterThan(30); } @Test