public interface RaftAlgorithm
| Modifier and Type | Field and Description |
|---|---|
static Span |
DEFAULT_ELECTION_RANGE
The default timeout range for elections.
|
static org.slf4j.Logger |
log
An SLF4J Logger for this class.
|
static java.lang.Object |
summaryTiming
Timing statistics for Raft.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
bootstrap(boolean force)
Mark this node for bootstrapping.
|
void |
broadcastAppendEntries(long now)
Send out an append entries RPC (i.e., a heartbeat) to all listeners on the transport.
|
default Span |
electionTimeoutMillisRange()
The election timeout.
|
RaftTransport |
getTransport()
This gets the RaftTransport associated with this RaftAlgorithm.
|
void |
heartbeat(long now)
A method to be called on every heartbeat interval.
|
default long |
heartbeatMillis()
The interval between heartbeats for this particular Raft node / implementation.
|
boolean |
isRunning()
Check if the algorithm has already been stopped via
stop(boolean). |
java.util.Optional<RaftLifecycle> |
lifecycle()
Get the RaftLifecycle object that this Algorithm is registered for.
|
RaftState |
mutableState()
DANGEROUS: USE ONLY IF YOU KNOW WHAT YOU ARE DOING
get A REFERENCE TO the current Raft state.
|
RaftStateMachine |
mutableStateMachine()
DANGEROUS: USE ONLY IF YOU KNOW WHAT YOU ARE DOING
get A REFERENCE TO the current Raft state machine.
|
java.util.concurrent.CompletableFuture<EloquentRaftProto.RaftMessage> |
receiveAddServerRPC(EloquentRaftProto.AddServerRequest addServerRequest,
long now)
A request has been received to add a server to the cluster.
|
void |
receiveAppendEntriesReply(EloquentRaftProto.AppendEntriesReply reply,
long now)
We received an asynchronous heartbeat from a server.
|
void |
receiveAppendEntriesRPC(EloquentRaftProto.AppendEntriesRequest heartbeat,
java.util.function.Consumer<EloquentRaftProto.RaftMessage> replyLeader,
long now)
Receive a request to apply a transition or multiple transitions.
|
java.util.concurrent.CompletableFuture<EloquentRaftProto.RaftMessage> |
receiveApplyTransitionRPC(EloquentRaftProto.ApplyTransitionRequest transition,
long now)
Apply a transition to Raft.
|
void |
receiveBadRequest(EloquentRaftProto.RaftMessage message)
A bad RPC call was received.
|
void |
receiveInstallSnapshotReply(EloquentRaftProto.InstallSnapshotReply reply,
long now)
We received an asynchronous snapshot reply from a server.
|
void |
receiveInstallSnapshotRPC(EloquentRaftProto.InstallSnapshotRequest snapshot,
java.util.function.Consumer<EloquentRaftProto.RaftMessage> replyLeader,
long now)
Receive a snapshot request RPC call.
|
default void |
receiveMessage(EloquentRaftProto.RaftMessage messageProto,
java.util.function.Consumer<EloquentRaftProto.RaftMessage> replySender,
long now)
Receive a non-blocking RPC in the form of a
EloquentRaftProto.RaftMessage proto. |
java.util.concurrent.CompletableFuture<EloquentRaftProto.RaftMessage> |
receiveRemoveServerRPC(EloquentRaftProto.RemoveServerRequest removeServerRequest,
long now)
A request has been received to remove a server to the cluster.
|
void |
receiveRequestVoteRPC(EloquentRaftProto.RequestVoteRequest voteRequest,
java.util.function.Consumer<EloquentRaftProto.RaftMessage> replyLeader,
long now)
Clearly an election has been initiated, and some candidate is requesting votes to become the new leader.
|
void |
receiveRequestVotesReply(EloquentRaftProto.RequestVoteReply reply,
long now)
We received votes from another server.
|
default java.util.concurrent.CompletableFuture<EloquentRaftProto.RaftMessage> |
receiveRPC(EloquentRaftProto.RaftMessage messageProto,
long now)
Receive a blocking RPC in the form of a
EloquentRaftProto.RaftMessage proto. |
void |
sendAppendEntries(java.lang.String target,
long nextIndex)
Send an append entries request to a particular server.
|
java.lang.String |
serverName()
The name of this server.
|
static void |
shutdown(RaftAlgorithm raft,
RaftTransport transport,
boolean allowClusterDeath)
Shutdown the argument Raft algorithm, using the time given in the argument transport.
|
RaftState |
state()
get A COPY OF the current Raft state.
|
default void |
stop(boolean kill)
A method, called primarily from unit tests, to stop this algorithm and clean up the underlying transport,
if appropriate.
|
long |
term()
The term this node sees.
|
void |
triggerElection(long now)
Signal to the cluster that we are a candidate for an election, and we are soliciting votes
|
static final org.slf4j.Logger log
static final java.lang.Object summaryTiming
static final Span DEFAULT_ELECTION_RANGE
default void receiveMessage(EloquentRaftProto.RaftMessage messageProto, java.util.function.Consumer<EloquentRaftProto.RaftMessage> replySender, long now)
EloquentRaftProto.RaftMessage proto.messageProto - The EloquentRaftProto.RaftMessage we have received.replySender - A callback for sending a message to the sender on the underlying transport.now - The timestamp of the current time. In non-mock cases, this is always System.currentTimeMillis().default java.util.concurrent.CompletableFuture<EloquentRaftProto.RaftMessage> receiveRPC(EloquentRaftProto.RaftMessage messageProto, long now)
EloquentRaftProto.RaftMessage proto.
This function will return a CompletableFuture that is completed when the RPC completes.messageProto - The EloquentRaftProto.RaftMessage we have received.now - The timestamp of the current time. In non-mock cases, this is always System.currentTimeMillis().CompletableFuture that completes when the RPC completes.RaftState state()
RaftState mutableState()
RaftStateMachine mutableStateMachine()
long term()
RaftState.currentTermjava.lang.String serverName()
RaftState.serverNamevoid broadcastAppendEntries(long now)
now - The current time. This is usually System.currentTimeMillis(), but can
be mocked by unit tests.void sendAppendEntries(java.lang.String target,
long nextIndex)
target - The server we're sending the append entries RPC to.nextIndex - The nextIndex value on the target machine.void receiveAppendEntriesRPC(EloquentRaftProto.AppendEntriesRequest heartbeat, java.util.function.Consumer<EloquentRaftProto.RaftMessage> replyLeader, long now)
heartbeat - The request body, doubling both as a heartbeat and a request to mutate the
state machine.replyLeader - The method for replying to the leader with an ACK of the request.now - The timestamp of the current time. In non-mock cases, this is always System.currentTimeMillis().void receiveAppendEntriesReply(EloquentRaftProto.AppendEntriesReply reply, long now)
reply - The heartbeat ACK from the follower node.now - The timestamp of the current time. In non-mock cases, this is always System.currentTimeMillis().void receiveInstallSnapshotRPC(EloquentRaftProto.InstallSnapshotRequest snapshot, java.util.function.Consumer<EloquentRaftProto.RaftMessage> replyLeader, long now)
snapshot - The snapshot to install.replyLeader - The method for replying to the leader with an ACK of the request.now - The timestamp of the current time. In non-mock cases, this is always System.currentTimeMillis().void receiveInstallSnapshotReply(EloquentRaftProto.InstallSnapshotReply reply, long now)
reply - The snapshot reply from the follower node.now - The timestamp of the current time. In non-mock cases, this is always System.currentTimeMillis().void triggerElection(long now)
void receiveRequestVoteRPC(EloquentRaftProto.RequestVoteRequest voteRequest, java.util.function.Consumer<EloquentRaftProto.RaftMessage> replyLeader, long now)
voteRequest - The request for votes for a particular server.replyLeader - The method for replying to the leader with an ACK of the request.now - The timestamp of the current time. In non-mock cases, this is always System.currentTimeMillis().void receiveRequestVotesReply(EloquentRaftProto.RequestVoteReply reply, long now)
reply - The vote from the server, which we should count to see if we have a
majority.now - The timestamp of the current time. In non-mock cases, this is always System.currentTimeMillis().java.util.concurrent.CompletableFuture<EloquentRaftProto.RaftMessage> receiveAddServerRPC(EloquentRaftProto.AddServerRequest addServerRequest, long now)
A request has been received to add a server to the cluster. This is only well-defined for the leader -- if the request was not received by the leader, then it should forward to the leader.
The algorithm for this is the following:
addServerRequest - The request to add the server.now - The timestamp of the current time. In non-mock cases, this is always System.currentTimeMillis().java.util.concurrent.CompletableFuture<EloquentRaftProto.RaftMessage> receiveRemoveServerRPC(EloquentRaftProto.RemoveServerRequest removeServerRequest, long now)
removeServerRequest - The snapshot to install.now - The timestamp of the current time. In non-mock cases, this is always System.currentTimeMillis().java.util.concurrent.CompletableFuture<EloquentRaftProto.RaftMessage> receiveApplyTransitionRPC(EloquentRaftProto.ApplyTransitionRequest transition, long now)
transition - The transition to applynow - The timestamp of the current time. In non-mock cases, this is always System.currentTimeMillis().boolean bootstrap(boolean force)
RaftState.bootstrap(boolean), with
some error checks beforehand.
If this node cannot bootstrap, we return false.default void stop(boolean kill)
kill - If true, kill the server unceremoniously, without waiting for a handoff.boolean isRunning()
stop(boolean).default long heartbeatMillis()
electionTimeoutMillisRange()default Span electionTimeoutMillisRange()
heartbeatMillis()void heartbeat(long now)
void receiveBadRequest(EloquentRaftProto.RaftMessage message)
message - The raw message received.java.util.Optional<RaftLifecycle> lifecycle()
RaftTransport getTransport()
static void shutdown(RaftAlgorithm raft, RaftTransport transport, boolean allowClusterDeath)
raft - The implementing Raft algorithm.transport - The transport to run timing on, where necessary.allowClusterDeath - If true, allow the cluster to lose state and completely shut down.
Otherwise, we wait for another live node to show up before shutting
down (the default).