public synchronized long[] getFollowerNextIndices() { LeaderState s = this.leaderState; if (s == null || !isLeader()) { return null; } return s.getFollowerNextIndices(); }
public synchronized void submitLocalSyncEvent() { if (isLeader() && leaderState != null) { leaderState.submitUpdateStateEvent(LeaderState.UPDATE_COMMIT_EVENT); } }
public long[] getFollowerNextIndices() { if (!isLeader()) { return null; } return role.getLeaderState().map(LeaderState::getFollowerNextIndices).orElse(null); }
private boolean shouldWithholdVotes(long candidateTerm) { if (state.getCurrentTerm() < candidateTerm) { return false; } else if (isLeader()) { return true; } else { // following a leader and not yet timeout return isFollower() && state.hasLeader() && role.getFollowerState().map(FollowerState::shouldWithholdVotes).orElse(false); } }
Collection<CommitInfoProto> getCommitInfos() { final List<CommitInfoProto> infos = new ArrayList<>(); // add the commit info of this server infos.add(commitInfoCache.update(getPeer(), state.getLog().getLastCommittedIndex())); // add the commit infos of other servers if (isLeader()) { Optional.ofNullable(leaderState).ifPresent( leader -> leader.updateFollowerCommitInfos(commitInfoCache, infos)); } else { getRaftConf().getPeers().stream() .filter(p -> !p.getId().equals(state.getSelfId())) .map(RaftPeer::getId) .map(commitInfoCache::get) .filter(i -> i != null) .forEach(infos::add); } return infos; }
Collection<CommitInfoProto> getCommitInfos() { final List<CommitInfoProto> infos = new ArrayList<>(); // add the commit info of this server infos.add(commitInfoCache.update(getPeer(), state.getLog().getLastCommittedIndex())); // add the commit infos of other servers if (isLeader()) { role.getLeaderState().ifPresent( leader -> leader.updateFollowerCommitInfos(commitInfoCache, infos)); } else { getRaftConf().getPeers().stream() .filter(p -> !p.getId().equals(state.getSelfId())) .map(RaftPeer::getId) .map(commitInfoCache::get) .filter(i -> i != null) .forEach(infos::add); } return infos; }
private boolean shouldWithholdVotes(long candidateTerm) { if (state.getCurrentTerm() < candidateTerm) { return false; } else if (isLeader()) { return true; } else { return isFollower() && state.hasLeader() && heartbeatMonitor.shouldWithholdVotes(); } }
/** * check if the remote peer is not included in the current conf * and should shutdown. should shutdown if all the following stands: * 1. this is a leader * 2. current conf is stable and has been committed * 3. candidate id is not included in conf * 4. candidate's last entry's index < conf's index */ private boolean shouldSendShutdown(RaftPeerId candidateId, TermIndex candidateLastEntry) { return isLeader() && getRaftConf().isStable() && getState().isConfCommitted() && !getRaftConf().containsInConf(candidateId) && candidateLastEntry.getIndex() < getRaftConf().getLogEntryIndex() && role.getLeaderState().map(ls -> !ls.isBootStrappingPeer(candidateId)).orElse(false); }
/** * check if the remote peer is not included in the current conf * and should shutdown. should shutdown if all the following stands: * 1. this is a leader * 2. current conf is stable and has been committed * 3. candidate id is not included in conf * 4. candidate's last entry's index < conf's index */ private boolean shouldSendShutdown(RaftPeerId candidateId, TermIndex candidateLastEntry) { return isLeader() && getRaftConf().isStable() && getState().isConfCommitted() && !getRaftConf().containsInConf(candidateId) && candidateLastEntry.getIndex() < getRaftConf().getLogEntryIndex() && !leaderState.isBootStrappingPeer(candidateId); }
cacheEntry.failWithReply(exceptionReply); if (isLeader() && leaderState != null) { leaderState.submitStepDownEvent();
if (!raftServer.isLeader()) { ((SimpleStateMachine4Testing) raftServer.getStateMachine()).setBlockAppend(true);
/** * @return null if the server is in leader state. */ private CompletableFuture<RaftClientReply> checkLeaderState( RaftClientRequest request, RetryCache.CacheEntry entry) { try { assertGroup(request.getRequestorId(), request.getRaftGroupId()); } catch (GroupMismatchException e) { return RetryCache.failWithException(e, entry); } if (!isLeader()) { NotLeaderException exception = generateNotLeaderException(); final RaftClientReply reply = new RaftClientReply(request, exception, getCommitInfos()); return RetryCache.failWithReply(reply, entry); } final LeaderState leaderState = role.getLeaderState().orElse(null); if (leaderState == null || !leaderState.isReady()) { RetryCache.CacheEntry cacheEntry = retryCache.get(request.getClientId(), request.getCallId()); if (cacheEntry != null && cacheEntry.isCompletedNormally()) { return cacheEntry.getReplyFuture(); } return RetryCache.failWithException(new LeaderNotReadyException(getId()), entry); } return null; }
.filter(impl -> !impl.isLeader()) .map(SimpleStateMachine4Testing::get) .forEach(SimpleStateMachine4Testing::blockWriteStateMachineData); .filter(impl -> !impl.isLeader()) .map(SimpleStateMachine4Testing::get) .forEach(SimpleStateMachine4Testing::unblockWriteStateMachineData); cluster.getServerAliveStream().filter(impl -> !impl.isLeader()).forEach(raftServer -> JavaUtils.runAsUnchecked(() -> JavaUtils.attempt(() -> { final long leaderNextIndex = leaderLog.getNextIndex();
if (isLeader() && leaderState != null) { // is leader and is running leaderState.replyPendingRequest(logIndex, r);
if (isLeader() && leaderState != null) { // is leader and is running leaderState.replyPendingRequest(logEntry.getIndex(), r);
/** * @return null if the server is in leader state. */ private CompletableFuture<RaftClientReply> checkLeaderState( RaftClientRequest request, RetryCache.CacheEntry entry) { try { assertGroup(request.getRequestorId(), request.getRaftGroupId()); } catch (GroupMismatchException e) { return RetryCache.failWithException(e, entry); } if (!isLeader()) { NotLeaderException exception = generateNotLeaderException(); final RaftClientReply reply = new RaftClientReply(request, exception, getCommitInfos()); return RetryCache.failWithReply(reply, entry); } else if (leaderState == null || !leaderState.isReady()) { RetryCache.CacheEntry cacheEntry = retryCache.get(request.getClientId(), request.getCallId()); if (cacheEntry != null && cacheEntry.isCompletedNormally()) { return cacheEntry.getReplyFuture(); } return RetryCache.failWithException(new LeaderNotReadyException(getId()), entry); } return null; }