Skip to content

Commit

Permalink
tidy-up
Browse files Browse the repository at this point in the history
  • Loading branch information
simbo1905 committed Nov 17, 2024
1 parent 2126cc8 commit a9162f8
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 41 deletions.
1 change: 0 additions & 1 deletion trex2-lib/src/main/java/com/github/trex_paxos/Pickle.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 0 additions & 11 deletions trex2-lib/src/main/java/com/github/trex_paxos/TrexNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,17 +421,6 @@ public long highestCommitted() {
return progress.highestCommittedIndex();
}

public long highestFixedLogIndex() {
assert role == LEAD : "role=" + role;
Optional<Map.Entry<Long, AcceptVotes>> 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;
}
Expand Down
8 changes: 7 additions & 1 deletion trex2-lib/src/main/java/com/github/trex_paxos/Vote.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 0 additions & 2 deletions trex2-lib/src/main/java/com/github/trex_paxos/msg/Commit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Accept> highestUncommitted
public record PrepareResponse(
Vote vote,
long highestCommittedIndex,
Optional<Accept> highestUncommitted
) implements TrexMessage, DirectMessage {

public byte from() {
return vote.from();
}
Expand Down
10 changes: 6 additions & 4 deletions trex2-lib/src/main/java/com/github/trex_paxos/msg/Progress.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 1 addition & 13 deletions trex2-lib/src/test/java/com/github/trex_paxos/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ static RandomGenerator repeatableRandomGenerator(long seed) {

Map<Byte, Long> nodeTimeouts = new HashMap<>();

public ArrayList<Message> run(
public void run(
int iterations,
boolean makeClientMessages,
BiFunction<Send, Long, Stream<TrexMessage>> nemesis
) {
final var allMessages = new ArrayList<Message>();

if (makeClientMessages) {
makeClientDataEvents(iterations, eventQueue);
}
Expand Down Expand Up @@ -136,9 +134,6 @@ public ArrayList<Message> 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();
Expand Down Expand Up @@ -172,7 +167,6 @@ public ArrayList<Message> run(
}
return finished;
});
return allMessages;
}

static OptionalLong inconsistentCommittedIndex(TreeMap<Long, AbstractCommand> c1,
Expand Down Expand Up @@ -425,11 +419,5 @@ public Optional<Accept> loadAccept(long logIndex) {
public void sync() {
// no-op
}

public Optional<Accept> getCommitted(Long logIndex) {
return logIndex <= this.progress.highestCommittedIndex() ?
Optional.ofNullable(fakeJournal.get(logIndex)) :
Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void testWorkRotationNetworkPartition100() {
max.set(min);
}
});
assertThat(max.get()).isGreaterThan(0);
assertThat(max.get()).isGreaterThan(30);
}

@Test
Expand Down

0 comments on commit a9162f8

Please sign in to comment.