public interface StateMachine extends Closeable
StateMachine implementation must be threadsafe.
For example, the applyTransaction(TransactionContext) method and the query(Message) method
can be invoked in parallel.| 限定符和类型 | 接口和说明 |
|---|---|
static interface |
StateMachine.DataApi
An optional API for managing data outside the raft log.
|
static interface |
StateMachine.DataChannel
For write state machine data.
|
static interface |
StateMachine.DataStream
For streaming state machine data.
|
static interface |
StateMachine.EventApi
An optional API for event notifications.
|
static interface |
StateMachine.FollowerEventApi
An optional API for follower-only event notifications.
|
static interface |
StateMachine.LeaderEventApi
An optional API for leader-only event notifications.
|
static interface |
StateMachine.Registry
A registry to support different state machines in multi-raft environment.
|
| 限定符和类型 | 字段和说明 |
|---|---|
static org.slf4j.Logger |
LOG |
| 限定符和类型 | 方法和说明 |
|---|---|
CompletableFuture<Message> |
applyTransaction(TransactionContext trx)
Apply a committed log entry to the state machine.
|
TransactionContext |
applyTransactionSerial(TransactionContext trx)
Called for transactions that have been committed to the RAFT log.
|
TransactionContext |
cancelTransaction(TransactionContext trx)
Called to notify the state machine that the Transaction passed cannot be appended (or synced).
|
default StateMachine.DataApi |
data()
Get the
StateMachine.DataApi object. |
default StateMachine.EventApi |
event()
Get the
StateMachine.EventApi object. |
default StateMachine.FollowerEventApi |
followerEvent()
Get the
StateMachine.FollowerEventApi object. |
TermIndex |
getLastAppliedTermIndex() |
SnapshotInfo |
getLatestSnapshot()
Returns the information for the latest durable snapshot.
|
LifeCycle.State |
getLifeCycleState()
Returns the lifecycle state for this StateMachine.
|
StateMachineStorage |
getStateMachineStorage() |
void |
initialize(RaftServer raftServer,
RaftGroupId raftGroupId,
RaftStorage storage)
Initializes the State Machine with the given parameter.
|
default StateMachine.LeaderEventApi |
leaderEvent()
Get the
StateMachine.LeaderEventApi object. |
void |
pause()
Pauses the state machine.
|
TransactionContext |
preAppendTransaction(TransactionContext trx)
This is called before the transaction passed from the StateMachine is appended to the raft log.
|
CompletableFuture<Message> |
query(Message request)
Query the state machine.
|
CompletableFuture<Message> |
queryStale(Message request,
long minIndex)
Query the state machine, provided minIndex <= commit index.
|
void |
reinitialize()
Re-initializes the State Machine in PAUSED state.
|
TransactionContext |
startTransaction(RaftClientRequest request)
Start a transaction for the given request.
|
default TransactionContext |
startTransaction(org.apache.ratis.proto.RaftProtos.LogEntryProto entry,
org.apache.ratis.proto.RaftProtos.RaftPeerRole role)
Start a transaction for the given log entry for non-leaders.
|
long |
takeSnapshot()
Dump the in-memory state into a snapshot file in the RaftStorage.
|
default String |
toStateMachineLogEntryString(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto proto)
Converts the given proto to a string.
|
default StateMachine.DataApi data()
StateMachine.DataApi object.
If this StateMachine chooses to support the optional StateMachine.DataApi,
it may either implement StateMachine.DataApi directly or override this method to return a StateMachine.DataApi object.
Otherwise, this StateMachine does not support StateMachine.DataApi.
Then, this method returns the default noop StateMachine.DataApi object.StateMachine.DataApi object.default StateMachine.EventApi event()
StateMachine.EventApi object.
If this StateMachine chooses to support the optional StateMachine.EventApi,
it may either implement StateMachine.EventApi directly or override this method to return an StateMachine.EventApi object.
Otherwise, this StateMachine does not support StateMachine.EventApi.
Then, this method returns the default noop StateMachine.EventApi object.StateMachine.EventApi object.default StateMachine.LeaderEventApi leaderEvent()
StateMachine.LeaderEventApi object.
If this StateMachine chooses to support the optional StateMachine.LeaderEventApi,
it may either implement StateMachine.LeaderEventApi directly
or override this method to return an StateMachine.LeaderEventApi object.
Otherwise, this StateMachine does not support StateMachine.LeaderEventApi.
Then, this method returns the default noop StateMachine.LeaderEventApi object.StateMachine.LeaderEventApi object.default StateMachine.FollowerEventApi followerEvent()
StateMachine.FollowerEventApi object.
If this StateMachine chooses to support the optional StateMachine.FollowerEventApi,
it may either implement StateMachine.FollowerEventApi directly
or override this method to return an StateMachine.FollowerEventApi object.
Otherwise, this StateMachine does not support StateMachine.FollowerEventApi.
Then, this method returns the default noop StateMachine.FollowerEventApi object.StateMachine.LeaderEventApi object.void initialize(RaftServer raftServer, RaftGroupId raftGroupId, RaftStorage storage) throws IOException
IOExceptionLifeCycle.State getLifeCycleState()
void pause()
void reinitialize()
throws IOException
IOExceptionlong takeSnapshot()
throws IOException
RaftServerConfigKeys.Snapshot.AUTO_TRIGGER_ENABLED_KEY is enabled.
The snapshot should include the latest raft configuration.IOExceptionStateMachineStorage getStateMachineStorage()
SnapshotInfo getLatestSnapshot()
CompletableFuture<Message> query(Message request)
CompletableFuture<Message> queryStale(Message request, long minIndex)
getLastAppliedTermIndex(),
the state machine may choose to either
(1) return exceptionally, or
(2) wait until minIndex <= getLastAppliedTermIndex() before running the query.TransactionContext startTransaction(RaftClientRequest request) throws IOException
RaftProtos.StateMachineLogEntryProto,
and then build a TransactionContext.
The implementation should also be light-weighted.IOException - thrown by the state machine while validationTransactionContext.Builderdefault TransactionContext startTransaction(org.apache.ratis.proto.RaftProtos.LogEntryProto entry, org.apache.ratis.proto.RaftProtos.RaftPeerRole role)
RaftProtos.StateMachineLogEntryProto,
and then build a TransactionContext.
The implementation should also be light-weighted.TransactionContext preAppendTransaction(TransactionContext trx) throws IOException
IOExceptionTransactionContext cancelTransaction(TransactionContext trx) throws IOException
trx - the transaction to cancelIOExceptionTransactionContext applyTransactionSerial(TransactionContext trx) throws org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException
trx - the transaction state including the log entry that has been committed to a quorum
of the raft peersorg.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferExceptionCompletableFuture<Message> applyTransaction(TransactionContext trx)
trx - the transaction state including the log entry that has been replicated to a majority of the raft peers.StateMachineException
and then replied to the client.TermIndex getLastAppliedTermIndex()
StateMachine.default String toStateMachineLogEntryString(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto proto)
proto - state machine protoCopyright © 2017–2025 The Apache Software Foundation. All rights reserved.