public interface RaftServiceExecutor
extends io.atomix.utils.concurrent.ThreadContext
The state machine executor is responsible for managing input to and output from a RaftService.
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 ReadConsistency.
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 RaftOperation 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.
RaftService,
ServiceContext| Modifier and Type | Method and Description |
|---|---|
byte[] |
apply(Commit<byte[]> commit)
Applies the given commit to the executor.
|
default void |
close() |
void |
handle(OperationId operationId,
Function<Commit<byte[]>,byte[]> callback)
Registers a operation callback.
|
default void |
register(OperationId operationId,
Consumer<Commit<Void>> callback)
Registers a operation callback.
|
default <T> void |
register(OperationId operationId,
Function<byte[],T> decoder,
Consumer<Commit<T>> callback)
Registers a operation callback.
|
default <T,R> void |
register(OperationId operationId,
Function<byte[],T> decoder,
Function<Commit<T>,R> callback,
Function<R,byte[]> encoder)
Registers an operation callback.
|
default <R> void |
register(OperationId operationId,
Function<Commit<Void>,R> callback,
Function<R,byte[]> encoder)
Registers a operation callback.
|
default void |
register(OperationId operationId,
Runnable callback)
Registers a operation callback.
|
default <R> void |
register(OperationId operationId,
Supplier<R> callback,
Function<R,byte[]> encoder)
Registers a no argument operation callback.
|
byte[] apply(Commit<byte[]> commit)
commit - the commit to applyvoid handle(OperationId operationId, Function<Commit<byte[]>,byte[]> callback)
operationId - the operation identifiercallback - the operation callbackNullPointerException - if the operationId or callback is nulldefault void register(OperationId operationId, Runnable callback)
operationId - the operation identifiercallback - the operation callbackNullPointerException - if the operationId or callback is nulldefault <R> void register(OperationId operationId, Supplier<R> callback, Function<R,byte[]> encoder)
operationId - the operation identifiercallback - the operation callbackencoder - result encoderNullPointerException - if the operationId or callback is nulldefault void register(OperationId operationId, Consumer<Commit<Void>> callback)
operationId - the operation identifiercallback - the operation callbackNullPointerException - if the operationId or callback is nulldefault <R> void register(OperationId operationId, Function<Commit<Void>,R> callback, Function<R,byte[]> encoder)
operationId - the operation identifiercallback - the operation callbackencoder - result encoderNullPointerException - if the operationId or callback is nulldefault <T> void register(OperationId operationId, Function<byte[],T> decoder, Consumer<Commit<T>> callback)
operationId - the operation identifierdecoder - the operation decodercallback - the operation callbackNullPointerException - if the operationId or callback is nulldefault <T,R> void register(OperationId operationId, Function<byte[],T> decoder, Function<Commit<T>,R> callback, Function<R,byte[]> encoder)
operationId - the operation identifierdecoder - the operation decodercallback - the operation callbackencoder - the output encoderNullPointerException - if the operationId or callback is nulldefault void close()
close in interface AutoCloseableclose in interface io.atomix.utils.concurrent.ThreadContextCopyright © 2013–2017. All rights reserved.