Skip to content

StateMachine

xnnyygn edited this page Aug 27, 2018 · 1 revision

In xraft, StateMachine is implemented by service and registered into xraft.

lastApplied

The difference with other raft implementation is, lastApplied is managed by StateMachine, in other word, it's service's responsibility to advance lastApplied.

The abstract implementation provided by xraft

  • AbstractDirectStateMachine
  • AbstractSingleThreadStateMachine

AbstractDirectStateMachine will run StateMachine in node thread, advance lastApplied when commitIndex increased.

AbstractSingleThreadStateMachine will run StateMachine in a separated thread, so lastApplied could be advanced in any time after commitIndex's increment.

Snapshot Generation

For service with special snapshot generation policy, xraft provides StateMachineContext#generateSnapshot(int) to notify node to generate snapshot.

A general policy of snapshot generation maybe check the first log index and last applied log index with threshold.

applyLog in StateMachine

void applyLog(StateMachineContext context, int index, @Nonnull byte[] commandBytes, int firstLogIndex);

In addition, service cannot get the log index when append log using Node API since appending log is asynchronous. A feasible method is to add request id or something like into command and check it after log is applied.

Clone this wiki locally