private CompletableFuture<RaftClientReply> watchAsync(RaftClientRequest request) { return role.getLeaderState() .map(ls -> ls.addWatchReqeust(request)) .orElseGet(() -> CompletableFuture.completedFuture( new RaftClientReply(request, generateNotLeaderException(), getCommitInfos()))); }
/** * The leader state is stopped. Send NotLeaderException to all the pending * requests since they have not got applied to the state machine yet. */ void sendNotLeaderResponses() throws IOException { LOG.info("{} sends responses before shutting down PendingRequestsHandler", server.getId()); // notify the state machine about stepping down final NotLeaderException nle = server.generateNotLeaderException(); server.getStateMachine().notifyNotLeader(pendingRequests.setNotLeaderException(nle)); if (pendingSetConf != null) { pendingSetConf.setNotLeaderException(nle); } delayedReplies.failReplies(); }
void stop() { this.running = false; // do not interrupt event processor since it may be in the middle of logSync senders.forEach(LogAppender::stopAppender); final NotLeaderException nle = server.generateNotLeaderException(); final Collection<CommitInfoProto> commitInfos = server.getCommitInfos(); try { final Collection<TransactionContext> transactions = pendingRequests.sendNotLeaderResponses(nle, commitInfos); server.getStateMachine().notifyNotLeader(transactions); watchRequests.failWatches(nle); } catch (IOException e) { LOG.warn(server.getId() + ": Caught exception in sendNotLeaderResponses", e); } }
public void failClientRequest(LogEntryProto logEntry) { if (logEntry.getLogEntryBodyCase() == LogEntryProto.LogEntryBodyCase.SMLOGENTRY) { final ClientId clientId = ClientId.valueOf(logEntry.getClientId()); final RetryCache.CacheEntry cacheEntry = getRetryCache().get(clientId, logEntry.getCallId()); if (cacheEntry != null) { final RaftClientReply reply = new RaftClientReply(clientId, getId(), getGroupId(), logEntry.getCallId(), false, null, generateNotLeaderException(), getCommitInfos()); cacheEntry.failWithReply(reply); } } }
public void failClientRequest(LogEntryProto logEntry) { if (logEntry.hasStateMachineLogEntry()) { final StateMachineLogEntryProto smLog = logEntry.getStateMachineLogEntry(); final ClientId clientId = ClientId.valueOf(smLog.getClientId()); final long callId = smLog.getCallId(); final RetryCache.CacheEntry cacheEntry = getRetryCache().get(clientId, callId); if (cacheEntry != null) { final RaftClientReply reply = new RaftClientReply(clientId, getId(), getGroupId(), callId, false, null, generateNotLeaderException(), logEntry.getIndex(), getCommitInfos()); cacheEntry.failWithReply(reply); } } }
/** * @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; }
/** * @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; }