Class ContainerStateMachine
- All Implemented Interfaces:
Closeable,AutoCloseable,org.apache.ratis.statemachine.StateMachine,org.apache.ratis.statemachine.StateMachine.DataApi,org.apache.ratis.statemachine.StateMachine.EventApi,org.apache.ratis.statemachine.StateMachine.FollowerEventApi,org.apache.ratis.statemachine.StateMachine.LeaderEventApi
StateMachine for containers,
which is responsible for handling different types of container requests.
The container requests can be divided into readonly request, WriteChunk request and other write requests.
- Read only requests (see HddsUtils.isReadOnly(org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ContainerCommandRequestProtoOrBuilder)) are handled by query(Message).
- WriteChunk request contains user data
- Other write request does not contain user data.
In order to optimize the write throughput, a WriteChunk request is processed :
(1) startTransaction(RaftClientRequest) separate user data from the client request
(2) the user data is written directly into the state machine via write(org.apache.ratis.proto.RaftProtos.LogEntryProto, org.apache.ratis.statemachine.TransactionContext)
(3) transaction is committed via applyTransaction(TransactionContext)
For the other write requests,
the transaction is directly committed via applyTransaction(TransactionContext).
There are 2 ordering operation which are enforced right now in the code, 1) WriteChunk must be executed after the CreateContainer; otherwise, WriteChunk will fail with container not found. 2) WriteChunk commit is executed after WriteChunk write. Then, WriteChunk commit and CreateContainer will be executed in the same order.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.ratis.statemachine.StateMachine
org.apache.ratis.statemachine.StateMachine.DataApi, org.apache.ratis.statemachine.StateMachine.DataChannel, org.apache.ratis.statemachine.StateMachine.DataStream, org.apache.ratis.statemachine.StateMachine.EventApi, org.apache.ratis.statemachine.StateMachine.FollowerEventApi, org.apache.ratis.statemachine.StateMachine.LeaderEventApi, org.apache.ratis.statemachine.StateMachine.Registry -
Field Summary
Fields inherited from interface org.apache.ratis.statemachine.StateMachine.DataApi
DEFAULTFields inherited from interface org.apache.ratis.statemachine.StateMachine.EventApi
DEFAULTFields inherited from interface org.apache.ratis.statemachine.StateMachine.FollowerEventApi
DEFAULTFields inherited from interface org.apache.ratis.statemachine.StateMachine.LeaderEventApi
DEFAULT -
Constructor Summary
ConstructorsConstructorDescriptionContainerStateMachine(HddsDatanodeService hddsDatanodeService, org.apache.ratis.protocol.RaftGroupId gid, ContainerDispatcher dispatcher, ContainerController containerController, List<ThreadPoolExecutor> chunkExecutors, XceiverServerRatis ratisServer, org.apache.hadoop.hdds.conf.ConfigurationSource conf, String threadNamePrefix) -
Method Summary
Modifier and TypeMethodDescriptionCompletableFuture<org.apache.ratis.protocol.Message> applyTransaction(org.apache.ratis.statemachine.TransactionContext trx) voidbuildMissingContainerSet(File snapshotFile) voidclose()voidflush(long index) Returns the combined future of all the writeChunks till the given log index.org.apache.ratis.statemachine.StateMachineStoragevoidinitialize(org.apache.ratis.server.RaftServer server, org.apache.ratis.protocol.RaftGroupId id, org.apache.ratis.server.storage.RaftStorage raftStorage) booleanlink(org.apache.ratis.statemachine.StateMachine.DataStream stream, org.apache.ratis.proto.RaftProtos.LogEntryProto entry) voidnotifyExtendedNoLeader(org.apache.ratis.proto.RaftProtos.RoleInfoProto roleInfoProto) voidnotifyFollowerSlowness(org.apache.ratis.proto.RaftProtos.RoleInfoProto roleInfoProto, org.apache.ratis.protocol.RaftPeer follower) voidCompletableFuture<org.apache.ratis.server.protocol.TermIndex> notifyInstallSnapshotFromLeader(org.apache.ratis.proto.RaftProtos.RoleInfoProto roleInfoProto, org.apache.ratis.server.protocol.TermIndex firstTermIndexInLog) voidnotifyLeaderChanged(org.apache.ratis.protocol.RaftGroupMemberId groupMemberId, org.apache.ratis.protocol.RaftPeerId raftPeerId) voidnotifyLogFailed(Throwable t, org.apache.ratis.proto.RaftProtos.LogEntryProto failedEntry) voidnotifyNotLeader(Collection<org.apache.ratis.statemachine.TransactionContext> pendingEntries) voidnotifyServerShutdown(org.apache.ratis.proto.RaftProtos.RoleInfoProto roleInfo, boolean allServer) voidnotifyTermIndexUpdated(long term, long index) Notifies the state machine about index updates because of entries which do not cause state machine update, i.e. conf entries, metadata entriesvoidAs a part of taking snapshot with Ratis StateMachine, it will persist the existing container set in the snapshotFile.CompletableFuture<org.apache.ratis.protocol.Message> query(org.apache.ratis.protocol.Message request) CompletableFuture<org.apache.ratis.thirdparty.com.google.protobuf.ByteString> read(org.apache.ratis.proto.RaftProtos.LogEntryProto entry, org.apache.ratis.statemachine.TransactionContext trx) This method is used by the Leader to read state machine data for sending appendEntries to followers.static StringsmProtoToString(org.apache.ratis.protocol.RaftGroupId gid, ContainerController containerController, org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto proto) org.apache.ratis.statemachine.TransactionContextstartTransaction(org.apache.ratis.proto.RaftProtos.LogEntryProto entry, org.apache.ratis.proto.RaftProtos.RaftPeerRole role) For applying log entry.org.apache.ratis.statemachine.TransactionContextstartTransaction(org.apache.ratis.protocol.RaftClientRequest request) For the Leader to serve the given client request.CompletableFuture<org.apache.ratis.statemachine.StateMachine.DataStream> stream(org.apache.ratis.protocol.RaftClientRequest request) longtoStateMachineLogEntryString(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto proto) truncate(long index) CompletableFuture<org.apache.ratis.protocol.Message> write(org.apache.ratis.proto.RaftProtos.LogEntryProto entry, org.apache.ratis.statemachine.TransactionContext trx) writeStateMachineData(org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ContainerCommandRequestProto, long, long, long)calls are not synchronized with each other and also withapplyTransaction(TransactionContext).Methods inherited from class org.apache.ratis.statemachine.impl.BaseStateMachine
applyTransactionSerial, cancelTransaction, getGroupId, getId, getLastAppliedTermIndex, getLatestSnapshot, getLifeCycle, getLifeCycleState, getServer, pause, preAppendTransaction, queryStale, reinitialize, setLastAppliedTermIndex, toString, updateLastAppliedTermIndex, updateLastAppliedTermIndexMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.ratis.statemachine.StateMachine
data, event, followerEvent, leaderEventMethods inherited from interface org.apache.ratis.statemachine.StateMachine.DataApi
read, writeMethods inherited from interface org.apache.ratis.statemachine.StateMachine.EventApi
notifyConfigurationChanged, notifyServerShutdown, notifySnapshotInstalledMethods inherited from interface org.apache.ratis.statemachine.StateMachine.LeaderEventApi
notifyFollowerSlowness, notifyLeaderReady
-
Constructor Details
-
ContainerStateMachine
public ContainerStateMachine(HddsDatanodeService hddsDatanodeService, org.apache.ratis.protocol.RaftGroupId gid, ContainerDispatcher dispatcher, ContainerController containerController, List<ThreadPoolExecutor> chunkExecutors, XceiverServerRatis ratisServer, org.apache.hadoop.hdds.conf.ConfigurationSource conf, String threadNamePrefix)
-
-
Method Details
-
getStateMachineStorage
public org.apache.ratis.statemachine.StateMachineStorage getStateMachineStorage()- Specified by:
getStateMachineStoragein interfaceorg.apache.ratis.statemachine.StateMachine- Overrides:
getStateMachineStoragein classorg.apache.ratis.statemachine.impl.BaseStateMachine
-
getMetrics
-
initialize
public void initialize(org.apache.ratis.server.RaftServer server, org.apache.ratis.protocol.RaftGroupId id, org.apache.ratis.server.storage.RaftStorage raftStorage) throws IOException - Specified by:
initializein interfaceorg.apache.ratis.statemachine.StateMachine- Overrides:
initializein classorg.apache.ratis.statemachine.impl.BaseStateMachine- Throws:
IOException
-
buildMissingContainerSet
- Throws:
IOException
-
persistContainerSet
As a part of taking snapshot with Ratis StateMachine, it will persist the existing container set in the snapshotFile.- Parameters:
out- OutputStream mapped to the Ratis snapshot file- Throws:
IOException
-
isStateMachineHealthy
public boolean isStateMachineHealthy() -
takeSnapshot
- Specified by:
takeSnapshotin interfaceorg.apache.ratis.statemachine.StateMachine- Overrides:
takeSnapshotin classorg.apache.ratis.statemachine.impl.BaseStateMachine- Throws:
IOException
-
startTransaction
public org.apache.ratis.statemachine.TransactionContext startTransaction(org.apache.ratis.proto.RaftProtos.LogEntryProto entry, org.apache.ratis.proto.RaftProtos.RaftPeerRole role) For applying log entry. -
startTransaction
public org.apache.ratis.statemachine.TransactionContext startTransaction(org.apache.ratis.protocol.RaftClientRequest request) throws IOException For the Leader to serve the given client request.- Specified by:
startTransactionin interfaceorg.apache.ratis.statemachine.StateMachine- Overrides:
startTransactionin classorg.apache.ratis.statemachine.impl.BaseStateMachine- Throws:
IOException
-
stream
public CompletableFuture<org.apache.ratis.statemachine.StateMachine.DataStream> stream(org.apache.ratis.protocol.RaftClientRequest request) -
link
public CompletableFuture<?> link(org.apache.ratis.statemachine.StateMachine.DataStream stream, org.apache.ratis.proto.RaftProtos.LogEntryProto entry) -
write
public CompletableFuture<org.apache.ratis.protocol.Message> write(org.apache.ratis.proto.RaftProtos.LogEntryProto entry, org.apache.ratis.statemachine.TransactionContext trx) writeStateMachineData(org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ContainerCommandRequestProto, long, long, long)calls are not synchronized with each other and also withapplyTransaction(TransactionContext). -
query
public CompletableFuture<org.apache.ratis.protocol.Message> query(org.apache.ratis.protocol.Message request) - Specified by:
queryin interfaceorg.apache.ratis.statemachine.StateMachine- Overrides:
queryin classorg.apache.ratis.statemachine.impl.BaseStateMachine
-
flush
Returns the combined future of all the writeChunks till the given log index. The Raft log worker will wait for the stateMachineData to complete flush as well.- Parameters:
index- log index till which the stateMachine data needs to be flushed- Returns:
- Combined future of all writeChunks till the log index given.
-
read
public CompletableFuture<org.apache.ratis.thirdparty.com.google.protobuf.ByteString> read(org.apache.ratis.proto.RaftProtos.LogEntryProto entry, org.apache.ratis.statemachine.TransactionContext trx) This method is used by the Leader to read state machine data for sending appendEntries to followers. It will first get the data fromstateMachineDataCache. If the data is not in the cache, it will read from the file by dispatching a command- Parameters:
trx- the transaction context, which can be null if this method is invoked afterapplyTransaction(TransactionContext).
-
notifyTermIndexUpdated
public void notifyTermIndexUpdated(long term, long index) Notifies the state machine about index updates because of entries which do not cause state machine update, i.e. conf entries, metadata entries- Specified by:
notifyTermIndexUpdatedin interfaceorg.apache.ratis.statemachine.StateMachine.EventApi- Overrides:
notifyTermIndexUpdatedin classorg.apache.ratis.statemachine.impl.BaseStateMachine- Parameters:
term- term of the log entryindex- index of the log entry
-
notifyServerShutdown
public void notifyServerShutdown(org.apache.ratis.proto.RaftProtos.RoleInfoProto roleInfo, boolean allServer) -
applyTransaction
public CompletableFuture<org.apache.ratis.protocol.Message> applyTransaction(org.apache.ratis.statemachine.TransactionContext trx) - Specified by:
applyTransactionin interfaceorg.apache.ratis.statemachine.StateMachine- Overrides:
applyTransactionin classorg.apache.ratis.statemachine.impl.BaseStateMachine
-
notifyNotLeader
public void notifyNotLeader(Collection<org.apache.ratis.statemachine.TransactionContext> pendingEntries) -
truncate
-
evictStateMachineCache
public void evictStateMachineCache() -
notifyFollowerSlowness
public void notifyFollowerSlowness(org.apache.ratis.proto.RaftProtos.RoleInfoProto roleInfoProto, org.apache.ratis.protocol.RaftPeer follower) -
notifyExtendedNoLeader
public void notifyExtendedNoLeader(org.apache.ratis.proto.RaftProtos.RoleInfoProto roleInfoProto) -
notifyLogFailed
public void notifyLogFailed(Throwable t, org.apache.ratis.proto.RaftProtos.LogEntryProto failedEntry) -
notifyInstallSnapshotFromLeader
public CompletableFuture<org.apache.ratis.server.protocol.TermIndex> notifyInstallSnapshotFromLeader(org.apache.ratis.proto.RaftProtos.RoleInfoProto roleInfoProto, org.apache.ratis.server.protocol.TermIndex firstTermIndexInLog) -
notifyGroupRemove
public void notifyGroupRemove() -
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classorg.apache.ratis.statemachine.impl.BaseStateMachine
-
notifyLeaderChanged
public void notifyLeaderChanged(org.apache.ratis.protocol.RaftGroupMemberId groupMemberId, org.apache.ratis.protocol.RaftPeerId raftPeerId) -
toStateMachineLogEntryString
public String toStateMachineLogEntryString(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto proto) -
smProtoToString
public static String smProtoToString(org.apache.ratis.protocol.RaftGroupId gid, ContainerController containerController, org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto proto)
-