Skip to content

Commit

Permalink
RATIS-1874. Add notifyLeaderReady function in IStateMachine (apache#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
OneSizeFitsQuorum authored and symious committed Mar 20, 2024
1 parent 467043d commit 4954867
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ default void notifyFollowerSlowness(RoleInfoProto leaderInfo) {}
* Notify {@link StateMachine} that this server is no longer the leader.
*/
default void notifyNotLeader(Collection<TransactionContext> pendingEntries) throws IOException {}

/**
* Notify the {@link StateMachine} that this server becomes ready after changed to leader.
*/
default void notifyLeaderReady() {}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ private List<FollowerInfo> getFollowerInfos(PeerConfiguration peers) {

private final int stagingCatchupGap;
private final long placeHolderIndex;
private final AtomicBoolean isReady = new AtomicBoolean();
private final RaftServerMetricsImpl raftServerMetrics;
private final LogAppenderMetrics logAppenderMetrics;
private final long followerMaxGapThreshold;
Expand Down Expand Up @@ -382,7 +383,14 @@ LogEntryProto start() {
}

boolean isReady() {
return server.getState().getLastAppliedIndex() >= placeHolderIndex;
return isReady.get();
}

void checkReady(LogEntryProto entry) {
if (entry.getTerm() == currentTerm && entry.getIndex() == placeHolderIndex) {
isReady.set(true);
server.getStateMachine().leaderEvent().notifyLeaderReady();
}
}

void stop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1802,6 +1802,7 @@ CompletableFuture<Message> applyLogToStateMachine(LogEntryProto next) throws Raf
// the new conf in the metadata file and notify the StateMachine.
state.writeRaftConfiguration(next);
stateMachine.event().notifyConfigurationChanged(next.getTerm(), next.getIndex(), next.getConfigurationEntry());
role.getLeaderState().ifPresent(leader -> leader.checkReady(next));
} else if (next.hasStateMachineLogEntry()) {
// check whether there is a TransactionContext because we are the leader.
TransactionContext trx = role.getLeaderState()
Expand Down

1 comment on commit 4954867

@symious
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. When RaftServer becomes leader, it will append a new ConfigurationEntry, so at this point, the notifyLeaderReady is invoked.

Please sign in to comment.