public interface StateMachineExecutor extends ThreadContext
The state machine executor is responsible for managing input to and output from a StateMachine.
As operations are committed to the Raft log, the executor is responsible for applying them to the state machine.
commands are guaranteed to be applied to the state machine in the order in which
they appear in the Raft log and always in the same thread, so state machines don't have to be thread safe.
queries are not generally written to the Raft log and will instead be applied according to their
Query.ConsistencyLevel.
State machines can use the executor to provide deterministic scheduling during the execution of command callbacks.
private Object putWithTtl(Commit<PutWithTtl> commit) {
map.put(commit.operation().key(), commit);
executor.schedule(Duration.ofMillis(commit.operation().ttl()), () -> {
map.remove(commit.operation().key()).close();
});
}
As with all state machine callbacks, the executor will ensure scheduled callbacks are executed sequentially and
deterministically. As long as state machines schedule callbacks deterministically, callbacks will be executed
deterministically. Internally, the state machine executor triggers callbacks based on various timestamps in the
Raft log. This means the scheduler is dependent on internal or user-defined operations being written to the log.
Prior to the execution of a command, any expired scheduled callbacks will be executed based on the command's
logged timestamp.
It's important to note that callbacks can only be scheduled during Command operations or by recursive
scheduling. If a state machine attempts to schedule a callback via the executor during the execution of a
Query, a IllegalStateException will be thrown. This is because queries are usually only applied
on a single state machine within the cluster, and so scheduling callbacks in reaction to query execution would
not be deterministic.
| Modifier and Type | Method and Description |
|---|---|
default void |
block() |
default void |
close() |
StateMachineContext |
context()
Returns the state machine context.
|
default boolean |
isBlocked() |
<T extends io.atomix.copycat.Operation<Void>> |
register(Class<T> type,
Consumer<Commit<T>> callback)
Registers a void operation callback.
|
<T extends io.atomix.copycat.Operation<U>,U> |
register(Class<T> type,
Function<Commit<T>,U> callback)
Registers an operation callback.
|
default void |
unblock() |
checkThread, currentContext, currentContextOrThrow, execute, execute, executor, isCurrentContext, logger, schedule, schedule, serializerStateMachineContext context()
The context is reflective of the current position and state of the Raft state machine. In particular,
it exposes the current approximate time and all open
Sessions.
default boolean isBlocked()
isBlocked in interface ThreadContextdefault void block()
block in interface ThreadContextdefault void unblock()
unblock in interface ThreadContext<T extends io.atomix.copycat.Operation<Void>> StateMachineExecutor register(Class<T> type, Consumer<Commit<T>> callback)
The registered callback will be called when operations of type are applied to the state machine.
Because no return value is provided for void callbacks, the output of the operation will be null.
The callback is guaranteed to always be executed in the same thread.
T - The operation type.type - The operation type.callback - The operation callback.NullPointerException - if type or callback are null<T extends io.atomix.copycat.Operation<U>,U> StateMachineExecutor register(Class<T> type, Function<Commit<T>,U> callback)
The registered callback will be called when operations of type are applied to the state machine.
The return value of the provided callback must be synchronous (not a Future or
CompletableFuture) and will be sent back to the client as the operation output.
The callback is guaranteed to always be executed in the same thread.
T - The operation type.type - The operation type.callback - The operation callback.NullPointerException - if type or callback are nulldefault void close()
close in interface AutoCloseableclose in interface ThreadContextCopyright © 2013–2016. All rights reserved.