Modifier and Type | Method and Description |
---|---|
void |
applyLogEntries()
Applies committed log entries between
lastApplied and commitIndex , if there's any available. |
void |
broadcastAppendRequest()
Broadcasts append-entries request to all group members according to their nextIndex parameters.
|
boolean |
canQueryLinearizable()
Returns true if a new query is currently allowed to be executed without
appending to the Raft log.
|
boolean |
canReplicateNewEntry(Object operation)
Returns true if a new entry with the operation is currently allowed to
be replicated.
|
void |
completeFuture(long entryIndex,
Object response)
Completes the future registered with
entryIndex . |
void |
execute(Runnable task)
Executes task using
RaftIntegration.execute(Runnable) . |
InternalCompletableFuture |
forceSetTerminatedStatus()
Sets node's status to
RaftNodeStatus.TERMINATED unconditionally
if it's not terminated or stepped down yet. |
Collection<RaftEndpoint> |
getAppliedMembers()
Returns the currently effective member list of the raft group this node
belongs to.
|
Collection<RaftEndpoint> |
getCommittedMembers()
Returns the last committed member list of the raft group this node
belongs to.
|
CPGroupId |
getGroupId()
Returns the groupId which this node belongs to.
|
Collection<RaftEndpoint> |
getInitialMembers()
Returns the initial member list of the Raft group this node belongs to.
|
RaftEndpoint |
getLeader()
Returns the known leader endpoint.
|
long |
getLeaderElectionTimeoutInMillis()
Returns a randomized leader election timeout in milliseconds based on configured timeout.
|
RaftEndpoint |
getLocalMember()
Returns the Raft endpoint for this node.
|
ILogger |
getLogger(Class clazz) |
RaftNodeStatus |
getStatus()
Returns the current status of this node.
|
void |
handleAppendRequest(AppendRequest request)
Handles
AppendRequest sent by leader. |
void |
handleAppendResponse(AppendFailureResponse response)
Handles
AppendFailureResponse for a previously sent
append request by this node. |
void |
handleAppendResponse(AppendSuccessResponse response)
Handles
AppendSuccessResponse for a previously sent
append request by this node. |
void |
handleInstallSnapshot(InstallSnapshot request)
Handles
InstallSnapshot sent by leader. |
void |
handlePreVoteRequest(PreVoteRequest request)
Handles
PreVoteRequest sent by another follower. |
void |
handlePreVoteResponse(PreVoteResponse response)
Handles
PreVoteResponse for a previously sent request by
this node. |
void |
handleTriggerLeaderElection(TriggerLeaderElection request) |
void |
handleVoteRequest(VoteRequest request)
Handles
VoteRequest sent by a candidate. |
void |
handleVoteResponse(VoteResponse response)
Handles
VoteResponse for a previously sent vote request by
this node. |
boolean |
installSnapshot(SnapshotEntry snapshot)
Restores the snapshot sent by the leader if it's not applied before.
|
void |
invalidateFuturesFrom(long entryIndex)
Invalidates futures registered with indexes
>= entryIndex . |
boolean |
isLinearizableReadOptimizationEnabled()
Returns true if the linearizable read optimization is enabled.
|
boolean |
isTerminatedOrSteppedDown()
Returns true if this node is
RaftNodeStatus.TERMINATED or
RaftNodeStatus.STEPPED_DOWN , false otherwise. |
long |
lastAppendEntriesTimestamp() |
void |
leader(RaftEndpoint endpoint)
Updates the known leader endpoint and calls
RaftIntegration.onNodeStatusChange(RaftNodeStatus) . |
static RaftNodeImpl |
newRaftNode(CPGroupId groupId,
RaftEndpoint localMember,
Collection<RaftEndpoint> members,
RaftAlgorithmConfig config,
RaftIntegration integration)
Creates a new Raft node with an empty initial state.
|
static RaftNodeImpl |
newRaftNode(CPGroupId groupId,
RaftEndpoint localMember,
Collection<RaftEndpoint> members,
RaftAlgorithmConfig config,
RaftIntegration integration,
RaftStateStore raftStateStore)
Creates a new Raft node with an empty initial state
and a
RaftStateStore to persist Raft state changes |
void |
printMemberState() |
InternalCompletableFuture |
query(Object operation,
QueryPolicy queryPolicy)
Executes the given operation on Raft group depending
on the
QueryPolicy |
void |
registerFuture(long entryIndex,
InternalCompletableFuture future)
Registers the future for the appended entry with its
entryIndex . |
InternalCompletableFuture |
replicate(Object operation)
Replicates the given operation to the Raft group.
|
InternalCompletableFuture |
replicateMembershipChange(RaftEndpoint member,
MembershipChangeMode mode)
Replicates the membership change to the Raft group.
|
InternalCompletableFuture |
replicateMembershipChange(RaftEndpoint member,
MembershipChangeMode mode,
long groupMembersCommitIndex)
Replicates the membership change to the Raft group, if expected members
commit index is equal to the actual one stored in Raft state.
|
void |
resetGroupMembers()
Resets Raft group members back.
|
static RaftNodeImpl |
restoreRaftNode(CPGroupId groupId,
RestoredRaftState restoredState,
RaftAlgorithmConfig config,
RaftIntegration integration)
Creates a new Raft node with restored Raft state
|
static RaftNodeImpl |
restoreRaftNode(CPGroupId groupId,
RestoredRaftState restoredState,
RaftAlgorithmConfig config,
RaftIntegration integration,
RaftStateStore raftStateStore)
Creates a new Raft node with restored Raft state
and a
RaftStateStore to persist Raft state changes |
void |
runQuery(Object operation,
InternalCompletableFuture resultFuture)
Executes query operation sets execution result to the future.
|
void |
schedule(Runnable task,
long delayInMillis)
Schedules task using
RaftIntegration.schedule(Runnable, long, TimeUnit) . |
void |
send(AppendFailureResponse response,
RaftEndpoint target) |
void |
send(AppendSuccessResponse response,
RaftEndpoint target) |
void |
send(PreVoteRequest request,
RaftEndpoint target) |
void |
send(PreVoteResponse response,
RaftEndpoint target) |
void |
send(TriggerLeaderElection request,
RaftEndpoint target) |
void |
send(VoteRequest request,
RaftEndpoint target) |
void |
send(VoteResponse response,
RaftEndpoint target) |
void |
sendAppendRequest(RaftEndpoint follower)
Sends an append-entries request to the follower member.
|
void |
setStatus(RaftNodeStatus newStatus) |
void |
start()
Starts the periodic tasks, such as voting, leader failure-detection, snapshot handling.
|
RaftState |
state() |
void |
toFollower(int term)
Switches this node to follower role by clearing the known leader
endpoint and (pre) candidate states, and updating the term.
|
void |
toLeader()
Switches this Raft node to the leader role by performing the following
steps:
Setting the local endpoint as the current leader,
Clearing (pre)candidate states,
Initializing the leader state for the current members,
Appending an operation to the Raft log if enabled,
Scheduling the periodic heartbeat task,
Printing the member state.
|
String |
toString() |
InternalCompletableFuture |
transferLeadership(RaftEndpoint endpoint)
Transfers group leadership to the given endpoint, if the local Raft node
is the leader with ACTIVE status and the endpoint is a group member.
|
boolean |
tryAdvanceCommitIndex() |
boolean |
tryRunQueries() |
void |
updateGroupMembers(long logIndex,
Collection<RaftEndpoint> members)
Updates Raft group members.
|
void |
updateLastAppendEntriesTimestamp() |
public static RaftNodeImpl newRaftNode(CPGroupId groupId, RaftEndpoint localMember, Collection<RaftEndpoint> members, RaftAlgorithmConfig config, RaftIntegration integration)
public static RaftNodeImpl newRaftNode(CPGroupId groupId, RaftEndpoint localMember, Collection<RaftEndpoint> members, RaftAlgorithmConfig config, RaftIntegration integration, RaftStateStore raftStateStore)
RaftStateStore
to persist Raft state changespublic static RaftNodeImpl restoreRaftNode(CPGroupId groupId, RestoredRaftState restoredState, RaftAlgorithmConfig config, RaftIntegration integration)
public static RaftNodeImpl restoreRaftNode(CPGroupId groupId, RestoredRaftState restoredState, RaftAlgorithmConfig config, RaftIntegration integration, RaftStateStore raftStateStore)
RaftStateStore
to persist Raft state changespublic CPGroupId getGroupId()
RaftNode
getGroupId
in interface RaftNode
public RaftEndpoint getLocalMember()
RaftNode
getLocalMember
in interface RaftNode
public RaftEndpoint getLeader()
RaftNode
public RaftNodeStatus getStatus()
RaftNode
public Collection<RaftEndpoint> getInitialMembers()
RaftNode
getInitialMembers
in interface RaftNode
public Collection<RaftEndpoint> getCommittedMembers()
RaftNode
getCommittedMembers
in interface RaftNode
public Collection<RaftEndpoint> getAppliedMembers()
RaftNode
getAppliedMembers
in interface RaftNode
public InternalCompletableFuture forceSetTerminatedStatus()
RaftNode
RaftNodeStatus.TERMINATED
unconditionally
if it's not terminated or stepped down yet.forceSetTerminatedStatus
in interface RaftNode
public void start()
public void handlePreVoteRequest(PreVoteRequest request)
RaftNode
PreVoteRequest
sent by another follower.handlePreVoteRequest
in interface RaftNode
public void handlePreVoteResponse(PreVoteResponse response)
RaftNode
PreVoteResponse
for a previously sent request by
this node.handlePreVoteResponse
in interface RaftNode
public void handleVoteRequest(VoteRequest request)
RaftNode
VoteRequest
sent by a candidate.handleVoteRequest
in interface RaftNode
public void handleVoteResponse(VoteResponse response)
RaftNode
VoteResponse
for a previously sent vote request by
this node.handleVoteResponse
in interface RaftNode
public void handleAppendRequest(AppendRequest request)
RaftNode
AppendRequest
sent by leader.handleAppendRequest
in interface RaftNode
public void handleAppendResponse(AppendSuccessResponse response)
RaftNode
AppendSuccessResponse
for a previously sent
append request by this node.handleAppendResponse
in interface RaftNode
public void handleAppendResponse(AppendFailureResponse response)
RaftNode
AppendFailureResponse
for a previously sent
append request by this node.handleAppendResponse
in interface RaftNode
public void handleInstallSnapshot(InstallSnapshot request)
RaftNode
InstallSnapshot
sent by leader.handleInstallSnapshot
in interface RaftNode
public void handleTriggerLeaderElection(TriggerLeaderElection request)
handleTriggerLeaderElection
in interface RaftNode
public InternalCompletableFuture replicate(Object operation)
RaftNode
Otherwise, if this node is not leader, or the leader is demoted before committing the operation, the returned future is notified with a related exception.
public InternalCompletableFuture replicateMembershipChange(RaftEndpoint member, MembershipChangeMode mode)
RaftNode
If this node is not leader, or the leader is demoted before committing the operation, or membership change is not committed for any reason, then the returned future is notified with a related exception.
replicateMembershipChange
in interface RaftNode
member
- member to add or removemode
- type of membership changepublic InternalCompletableFuture replicateMembershipChange(RaftEndpoint member, MembershipChangeMode mode, long groupMembersCommitIndex)
RaftNode
MismatchingGroupMembersCommitIndexException
.
For more info see RaftNode.replicate(Object)
.
replicateMembershipChange
in interface RaftNode
member
- member to add or removemode
- type of membership changegroupMembersCommitIndex
- expected members commit indexpublic InternalCompletableFuture query(Object operation, QueryPolicy queryPolicy)
RaftNode
QueryPolicy
public InternalCompletableFuture transferLeadership(RaftEndpoint endpoint)
RaftNode
Leadership transfer is considered to be completed when the local Raft node moves to a term that is bigger than its current term, and there is no strict guarantee that the given endpoint will be the new leader. However, it is very likely that the given endpoint will become the new leader.
The local Raft node will not replicate any new entry during a leadership
transfer and new calls to the RaftNode.replicate(Object)
method will
fail with CannotReplicateException
.
transferLeadership
in interface RaftNode
public boolean isTerminatedOrSteppedDown()
RaftNode
RaftNodeStatus.TERMINATED
or
RaftNodeStatus.STEPPED_DOWN
, false otherwise.
This method is essentially same as;
return status == TERMINATED || status == STEPPED_DOWN
isTerminatedOrSteppedDown
in interface RaftNode
public void setStatus(RaftNodeStatus newStatus)
public long getLeaderElectionTimeoutInMillis()
public boolean canReplicateNewEntry(Object operation)
Replication is not allowed, when;
RaftNodeStatus
.RaftAlgorithmConfig.getUncommittedEntryCountToRejectNewAppends()
.RaftGroupCmd
and there's an ongoing membership change in group.RaftIntegration.getAppendedEntryOnLeaderElection()
.public boolean canQueryLinearizable()
A new linearizable query execution is not allowed, when;
RaftNodeStatus
.RaftAlgorithmConfig.getUncommittedEntryCountToRejectNewAppends()
queries waiting to be executed.public boolean isLinearizableReadOptimizationEnabled()
public void send(PreVoteRequest request, RaftEndpoint target)
public void send(PreVoteResponse response, RaftEndpoint target)
public void send(VoteRequest request, RaftEndpoint target)
public void send(VoteResponse response, RaftEndpoint target)
public void send(AppendSuccessResponse response, RaftEndpoint target)
public void send(AppendFailureResponse response, RaftEndpoint target)
public void send(TriggerLeaderElection request, RaftEndpoint target)
public void broadcastAppendRequest()
public void sendAppendRequest(RaftEndpoint follower)
Log entries between follower's known nextIndex and latest appended entry index are sent in a batch.
Batch size can be RaftAlgorithmConfig.getAppendRequestMaxEntryCount()
at most.
If follower's nextIndex is behind the latest snapshot index, then InstallSnapshot
request is sent.
If leader doesn't know follower's matchIndex (if matchIndex == 0
), then an empty append-entries is sent
to save bandwidth until leader learns the matchIndex of the follower.
public void applyLogEntries()
lastApplied
and commitIndex
, if there's any available.
If new entries are applied, RaftState
's lastApplied
field is updated.RaftState.lastApplied()
,
RaftState.commitIndex()
public void updateLastAppendEntriesTimestamp()
public long lastAppendEntriesTimestamp()
public RaftState state()
public void runQuery(Object operation, InternalCompletableFuture resultFuture)
public void execute(Runnable task)
RaftIntegration.execute(Runnable)
.public void schedule(Runnable task, long delayInMillis)
RaftIntegration.schedule(Runnable, long, TimeUnit)
.public void registerFuture(long entryIndex, InternalCompletableFuture future)
entryIndex
.public void completeFuture(long entryIndex, Object response)
entryIndex
.public void invalidateFuturesFrom(long entryIndex)
>= entryIndex
. Note that entryIndex
is inclusive.
LeaderDemotedException
is set a result to futures.public boolean installSnapshot(SnapshotEntry snapshot)
public void printMemberState()
public void updateGroupMembers(long logIndex, Collection<RaftEndpoint> members)
public void resetGroupMembers()
RaftState.resetGroupMembers()
public void toFollower(int term)
LeaderDemotedException
. After the state switch,
RaftIntegration.onNodeStatusChange(RaftNodeStatus)
is called.term
- the new term to switchpublic void leader(RaftEndpoint endpoint)
RaftIntegration.onNodeStatusChange(RaftNodeStatus)
.endpoint
- the new leader endpointpublic void toLeader()
public boolean tryAdvanceCommitIndex()
public boolean tryRunQueries()
Copyright © 2019 Hazelcast, Inc.. All rights reserved.