public class NodeImpl extends Object implements Node, RaftServerService
Describer.DefaultPrinter, Describer.Printer| Modifier and Type | Field and Description |
|---|---|
static AtomicInteger |
GLOBAL_NUM_NODES |
protected Lock |
readLock |
static RaftTimerFactory |
TIMER_FACTORY |
protected Lock |
writeLock |
| Constructor and Description |
|---|
NodeImpl() |
NodeImpl(String groupId,
PeerId serverId) |
| Modifier and Type | Method and Description |
|---|---|
void |
addLearners(List<PeerId> learners,
Closure done)
Add some new learners to the raft group.
|
void |
addPeer(PeerId peer,
Closure done)
Add a new peer to the raft group.
|
void |
addReplicatorStateListener(Replicator.ReplicatorStateListener replicatorStateListener)
SOFAJRaft users can implement the ReplicatorStateListener interface by themselves.
|
void |
apply(Task task)
[Thread-safe and wait-free]
Apply task to the replicated-state-machine
About the ownership:
|task.data|: for the performance consideration, we will take away the
content.
|
boolean |
bootstrap(BootstrapOptions opts) |
void |
changePeers(Configuration newPeers,
Closure done)
Change the configuration of the raft group to |newPeers| , done.run()
would be invoked after this operation finishes, describing the detailed result.
|
void |
clearReplicatorStateListeners()
Remove all the ReplicatorStateListeners which have been added by users.
|
void |
describe(Describer.Printer out) |
Configuration |
getCurrentConf()
Retrieve current configuration this node seen so far.
|
String |
getGroupId()
Get the raft group id.
|
long |
getLastAppliedLogIndex()
Retrieve the last log index that applied to state machine.
|
long |
getLastCommittedIndex()
Retrieve the last log index that committed to the raft group.
|
long |
getLastLogIndex()
Retrieve the last log index in log storage.
|
PeerId |
getLeaderId()
Get the leader peer id for redirect, null if absent.
|
NodeId |
getNodeId()
Get current node id.
|
NodeMetrics |
getNodeMetrics()
Returns the node metrics.
|
State |
getNodeState()
Get the node's state.
|
int |
getNodeTargetPriority()
Get the node's target election priority value.
|
NodeOptions |
getOptions()
Get the node options.
|
RaftOptions |
getRaftOptions()
Get the raft options
|
List<Replicator.ReplicatorStateListener> |
getReplicatorStatueListeners()
Get the ReplicatorStateListeners which have been added by users.
|
RaftClientService |
getRpcService() |
PeerId |
getServerId() |
JRaftServiceFactory |
getServiceFactory()
Returns the JRaft service factory for current node.
|
Scheduler |
getTimerManager() |
com.google.protobuf.Message |
handleAppendEntriesRequest(RpcRequests.AppendEntriesRequest request,
RpcRequestClosure done)
Handle append-entries request, return response message or
called done.run() with response.
|
com.google.protobuf.Message |
handleInstallSnapshot(RpcRequests.InstallSnapshotRequest request,
RpcRequestClosure done)
Handle install-snapshot request, return response message or
called done.run() with response.
|
com.google.protobuf.Message |
handlePreVoteRequest(RpcRequests.RequestVoteRequest request)
Handle pre-vote request.
|
void |
handlePreVoteResponse(PeerId peerId,
long term,
RpcRequests.RequestVoteResponse response) |
void |
handleReadIndexRequest(RpcRequests.ReadIndexRequest request,
RpcResponseClosure<RpcRequests.ReadIndexResponse> done)
Handle read index request.
|
com.google.protobuf.Message |
handleRequestVoteRequest(RpcRequests.RequestVoteRequest request)
Handle request-vote request.
|
void |
handleRequestVoteResponse(PeerId peerId,
long term,
RpcRequests.RequestVoteResponse response) |
com.google.protobuf.Message |
handleTimeoutNowRequest(RpcRequests.TimeoutNowRequest request,
RpcRequestClosure done)
Handle time-out-now request, return response message or
called done.run() with response.
|
boolean |
init(NodeOptions opts)
Initialize the service.
|
boolean |
isLeader()
Returns true when the node is leader.
|
boolean |
isLeader(boolean blocking)
Returns true when the node is leader.
|
void |
join()
Block the thread until the node is successfully stopped.
|
List<PeerId> |
listAliveLearners()
List all alive learners of this raft group, only leader returns.
|
List<PeerId> |
listAlivePeers()
List all alive peers of this raft group, only leader returns.
|
List<PeerId> |
listLearners()
List all learners of this raft group, only leader returns.
|
List<PeerId> |
listPeers()
List peers of this raft group, only leader returns.
|
void |
onConfigurationChangeDone(long term) |
void |
onError(RaftException error) |
UserLog |
readCommittedUserLog(long index)
Read the first committed user log from the given index.
|
void |
readIndex(byte[] requestContext,
ReadIndexClosure done)
[Thread-safe and wait-free]
Starts a linearizable read-only query request with request context(optional,
such as request id etc.) and closure.
|
void |
readIndex(ReadOnlyOption readOnlyOptions,
byte[] requestContext,
ReadIndexClosure done)
[Thread-safe and wait-free]
Specify the ReadOnlyOption to execute a linearizable read-only query request.
|
void |
removeLearners(List<PeerId> learners,
Closure done)
Remove some learners from the raft group.
|
void |
removePeer(PeerId peer,
Closure done)
Remove the peer from the raft group.
|
void |
removeReplicatorStateListener(Replicator.ReplicatorStateListener replicatorStateListener)
End User can remove their implement the ReplicatorStateListener interface by themselves.
|
void |
resetElectionTimeoutMs(int electionTimeoutMs)
Reset the election_timeout for the every node.
|
void |
resetLearners(List<PeerId> learners,
Closure done)
Reset learners in the raft group.
|
Status |
resetPeers(Configuration newPeers)
Reset the configuration of this node individually, without any replication
to other peers before this node becomes the leader.
|
void |
shutdown()
Dispose the resources for service.
|
void |
shutdown(Closure done)
Shutdown local replica node.
|
void |
snapshot(Closure done)
Start a snapshot immediately if possible.
|
void |
snapshotSync(Closure done)
Start to snapshot StateMachine immediately with the latest log applied to state machine.
|
String |
toString() |
Status |
transferLeadershipTo(PeerId peer)
Try transferring leadership to |peer|.
|
void |
updateConfigurationAfterInstallingSnapshot() |
public static final RaftTimerFactory TIMER_FACTORY
public static final AtomicInteger GLOBAL_NUM_NODES
protected final Lock writeLock
protected final Lock readLock
public boolean bootstrap(BootstrapOptions opts) throws InterruptedException
InterruptedExceptionpublic boolean init(NodeOptions opts)
Lifecycleinit in interface Lifecycle<NodeOptions>public NodeMetrics getNodeMetrics()
getNodeMetrics in interface Nodepublic JRaftServiceFactory getServiceFactory()
public void readIndex(byte[] requestContext,
ReadIndexClosure done)
NodeReadOnlyOption.ReadOnlySafe.
The closure will be called when the request is completed, and user can read
data from state machine if the result status is OK.public void readIndex(ReadOnlyOption readOnlyOptions, byte[] requestContext, ReadIndexClosure done)
Nodepublic void handleReadIndexRequest(RpcRequests.ReadIndexRequest request, RpcResponseClosure<RpcRequests.ReadIndexResponse> done)
handleReadIndexRequest in interface RaftServerServicerequest - data of the readIndex readdone - callbackpublic void apply(Task task)
NodeStateMachine.onApply(Iterator).
Otherwise we will specify the error and call it.public com.google.protobuf.Message handlePreVoteRequest(RpcRequests.RequestVoteRequest request)
RaftServerServicehandlePreVoteRequest in interface RaftServerServicerequest - data of the pre votepublic com.google.protobuf.Message handleRequestVoteRequest(RpcRequests.RequestVoteRequest request)
RaftServerServicehandleRequestVoteRequest in interface RaftServerServicerequest - data of the votepublic com.google.protobuf.Message handleAppendEntriesRequest(RpcRequests.AppendEntriesRequest request, RpcRequestClosure done)
RaftServerServicehandleAppendEntriesRequest in interface RaftServerServicerequest - data of the entries to appenddone - callbackpublic NodeOptions getOptions()
NodegetOptions in interface Nodepublic Scheduler getTimerManager()
public RaftOptions getRaftOptions()
NodegetRaftOptions in interface Nodepublic void shutdown()
Lifecycleshutdown in interface Lifecycle<NodeOptions>public void onConfigurationChangeDone(long term)
public PeerId getLeaderId()
NodegetLeaderId in interface Nodepublic String getGroupId()
NodegetGroupId in interface Nodepublic PeerId getServerId()
public RaftClientService getRpcService()
public void onError(RaftException error)
public void handleRequestVoteResponse(PeerId peerId, long term, RpcRequests.RequestVoteResponse response)
public void handlePreVoteResponse(PeerId peerId, long term, RpcRequests.RequestVoteResponse response)
public boolean isLeader()
Nodepublic boolean isLeader(boolean blocking)
Nodepublic void shutdown(Closure done)
Nodepublic void join()
throws InterruptedException
Nodejoin in interface NodeInterruptedException - if the current thread is interrupted
while waitingpublic Configuration getCurrentConf()
listPeers() instead.public List<PeerId> listPeers()
NodeNode.addPeer(PeerId, Closure)/Node.removePeer(PeerId, Closure),
maybe return peers is staled. Because Node.addPeer(PeerId, Closure)/Node.removePeer(PeerId, Closure)
immediately modify configuration in memorypublic List<PeerId> listAlivePeers()
NodelistAlivePeers in interface Nodepublic List<PeerId> listLearners()
NodeNode.addLearners(List, Closure)/Node.removeLearners(List, Closure)/Node.resetLearners(List, Closure),
maybe return peers is staled. Because Node.addLearners(List, Closure)/Node.removeLearners(List, Closure)/Node.resetLearners(List, Closure)
immediately modify configuration in memorylistLearners in interface Nodepublic List<PeerId> listAliveLearners()
NodeNode.addLearners(List, Closure)/Node.removeLearners(List, Closure)/Node.resetLearners(List, Closure),
maybe return peers is staled. Because Node.addLearners(List, Closure)/Node.removeLearners(List, Closure)/Node.resetLearners(List, Closure)
immediately modify configuration in memorylistAliveLearners in interface Nodepublic void addPeer(PeerId peer, Closure done)
Nodepublic void removePeer(PeerId peer, Closure done)
NoderemovePeer in interface Nodepeer - peer to removedone - callbackpublic void changePeers(Configuration newPeers, Closure done)
NodechangePeers in interface NodenewPeers - new peers to changedone - callbackpublic Status resetPeers(Configuration newPeers)
NoderesetPeers in interface NodenewPeers - new peerspublic void addLearners(List<PeerId> learners, Closure done)
NodeaddLearners in interface Nodelearners - learners to adddone - callbackpublic void removeLearners(List<PeerId> learners, Closure done)
NoderemoveLearners in interface Nodelearners - learners to removedone - callbackpublic void resetLearners(List<PeerId> learners, Closure done)
NoderesetLearners in interface Nodelearners - learners to setdone - callbackpublic void snapshot(Closure done)
Nodepublic void snapshotSync(Closure done)
NodeStateMachine callback methods to trigger a snapshot synchronously, otherwise throws IllegalStateException.
And done.run() would be invoked when the snapshot finishes, describing the detailed result.snapshotSync in interface Nodedone - snapshot callbackpublic void resetElectionTimeoutMs(int electionTimeoutMs)
NoderesetElectionTimeoutMs in interface NodeelectionTimeoutMs - the timeout millis of electionpublic Status transferLeadershipTo(PeerId peer)
NodetransferLeadershipTo in interface Nodepeer - the target peer of new leaderpublic com.google.protobuf.Message handleTimeoutNowRequest(RpcRequests.TimeoutNowRequest request, RpcRequestClosure done)
RaftServerServicehandleTimeoutNowRequest in interface RaftServerServicerequest - data of the timeout now requestdone - callbackpublic com.google.protobuf.Message handleInstallSnapshot(RpcRequests.InstallSnapshotRequest request, RpcRequestClosure done)
RaftServerServicehandleInstallSnapshot in interface RaftServerServicerequest - data of the install snapshot requestdone - callbackpublic void updateConfigurationAfterInstallingSnapshot()
public UserLog readCommittedUserLog(long index)
NodereadCommittedUserLog in interface Nodeindex - log indexpublic void addReplicatorStateListener(Replicator.ReplicatorStateListener replicatorStateListener)
NodeaddReplicatorStateListener in interface NodereplicatorStateListener - added ReplicatorStateListener which is implemented by users.public void removeReplicatorStateListener(Replicator.ReplicatorStateListener replicatorStateListener)
NoderemoveReplicatorStateListener in interface NodereplicatorStateListener - need to remove the ReplicatorStateListener which has been added by users.public void clearReplicatorStateListeners()
NodeclearReplicatorStateListeners in interface Nodepublic List<Replicator.ReplicatorStateListener> getReplicatorStatueListeners()
NodegetReplicatorStatueListeners in interface Nodepublic int getNodeTargetPriority()
NodegetNodeTargetPriority in interface Nodepublic State getNodeState()
NodegetNodeState in interface Nodepublic long getLastLogIndex()
NodegetLastLogIndex in interface Nodepublic long getLastCommittedIndex()
NodegetLastCommittedIndex in interface Nodepublic long getLastAppliedLogIndex()
NodegetLastAppliedLogIndex in interface Nodepublic void describe(Describer.Printer out)
Copyright © 2024. All rights reserved.