Skip to content

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
Signed-off-by: OneSizeFitQuorum <[email protected]>
  • Loading branch information
OneSizeFitsQuorum committed Aug 23, 2023
1 parent 95b51e5 commit 5f5c3df
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ interface EventApi {
*/
default void notifyLeaderChanged(RaftGroupMemberId groupMemberId, RaftPeerId newLeaderId) {}

/**
* Notify the {@link StateMachine} that current node has become the leader and recovered successfully.
* Note that only leader will call this function.
*
*/
default void notifyLeaderReady() {}

/**
* Notify the {@link StateMachine} a term-index update event.
* This method will be invoked when a {@link MetadataProto}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,11 @@ CompletableFuture<Message> applyLogToStateMachine(LogEntryProto next) throws Raf
// the reply should have already been set. only need to record
// the new conf in the metadata file and notify the StateMachine.
state.writeRaftConfiguration(next);
stateMachine.event().notifyConfigurationChanged(next.getTerm(), next.getIndex(), next.getConfigurationEntry());
stateMachine.event().notifyConfigurationChanged(next.getTerm(), next.getIndex(),
next.getConfigurationEntry());
if (isFirstLogAppliedByCurrentLeaderInCurrentTerm(next.getTerm())) {
stateMachine.event().notifyLeaderReady();
}
} else if (next.hasStateMachineLogEntry()) {
// check whether there is a TransactionContext because we are the leader.
TransactionContext trx = role.getLeaderState()
Expand All @@ -1825,6 +1829,11 @@ CompletableFuture<Message> applyLogToStateMachine(LogEntryProto next) throws Raf
return null;
}

boolean isFirstLogAppliedByCurrentLeaderInCurrentTerm(long term) {
return term != state.getLastEntry().getTerm()
&& role.getLeaderState().map(leader -> leader.getCurrentTerm() == term).orElse(false);
}

/**
* The given log entry is being truncated.
* Fail the corresponding client request, if there is any.
Expand Down

0 comments on commit 5f5c3df

Please sign in to comment.