PendingRequest addConfRequest(SetConfigurationRequest request) { Preconditions.assertTrue(pendingSetConf == null); pendingSetConf = new PendingRequest(request); return pendingSetConf; }
void failReplies() { synchronized (q) { for(; !q.isEmpty();) { q.poll().failDelayedReply(); } } } }
TransactionContext setNotLeaderException(NotLeaderException nle, Collection<CommitInfoProto> commitInfos) { setReply(new RaftClientReply(getRequest(), nle, commitInfos)); return getEntry(); }
void replySetConfiguration(Supplier<Collection<CommitInfoProto>> getCommitInfos) { // we allow the pendingRequest to be null in case that the new leader // commits the new configuration while it has not received the retry // request from the client if (pendingSetConf != null) { final RaftClientRequest request = pendingSetConf.getRequest(); LOG.debug("{}: sends success for {}", name, request); // for setConfiguration we do not need to wait for statemachine. send back // reply after it's committed. pendingSetConf.setReply(new RaftClientReply(request, getCommitInfos.get())); pendingSetConf = null; } }
boolean delay(PendingRequest request, RaftClientReply reply) { if (request.getIndex() <= allAckedIndex.get()) { return false; // delay is not required. } LOG.debug("{}: delay request {}", name, request); request.setDelayedReply(reply); final boolean offered; synchronized (q) { offered = q.offer(request); } Preconditions.assertTrue(offered); return true; }
pending = new PendingRequest(request); pending.setReply(new RaftClientReply(request, getCommitInfos())); return pending.getFuture(); return pending.getFuture();
boolean replyPendingRequest(long index, RaftClientReply reply) { final PendingRequest pending = pendingRequests.remove(index); if (pending != null) { Preconditions.assertTrue(pending.getIndex() == index); final ReplicationLevel replication = pending.getRequest().getType().getWrite().getReplication(); if (replication == ReplicationLevel.ALL) { if (delayedReplies.delay(pending, reply)) { return false; } } pending.setReply(reply); } return true; }
void update(final long allAcked) { final long old = allAckedIndex.getAndUpdate(n -> allAcked > n? allAcked : n); if (allAcked <= old) { return; } LOG.debug("{}: update allAckedIndex {} -> {}", name, old, allAcked); for(;;) { final PendingRequest polled; synchronized (q) { final PendingRequest peeked = q.peek(); if (peeked == null || peeked.getIndex() > allAcked) { return; } polled = q.poll(); Preconditions.assertTrue(polled == peeked); } LOG.debug("{}: complete delay request {}", name, polled); polled.completeDelayedReply(); } }
PendingRequest addPendingRequest(long index, RaftClientRequest request, TransactionContext entry) { // externally synced for now Preconditions.assertTrue(request.is(RaftClientRequestProto.TypeCase.WRITE)); if (last != null && !(last.getRequest() instanceof SetConfigurationRequest)) { Preconditions.assertTrue(index == last.getIndex() + 1, () -> "index = " + index + " != last.getIndex() + 1, last=" + last); } return add(index, request, entry); }
Collection<TransactionContext> setNotLeaderException(NotLeaderException nle) { LOG.debug("{}: PendingRequests.setNotLeaderException", name); try { return map.values().stream() .map(p -> p.setNotLeaderException(nle)) .collect(Collectors.toList()); } finally { map.clear(); } } }
void failSetConfiguration(RaftException e) { Preconditions.assertTrue(pendingSetConf != null); pendingSetConf.setException(e); pendingSetConf = null; }
TransactionContext getTransactionContext(long index) { PendingRequest pendingRequest = pendingRequests.get(index); // it is possible that the pendingRequest is null if this peer just becomes // the new leader and commits transactions received by the previous leader return pendingRequest != null ? pendingRequest.getEntry() : null; }
leaderState.notifySenders(); return pending.getFuture();
pending = new PendingRequest(request); pending.setReply(new RaftClientReply(request, getCommitInfos())); return pending.getFuture(); return pending.getFuture();
void replySetConfiguration() { // we allow the pendingRequest to be null in case that the new leader // commits the new configuration while it has not received the retry // request from the client if (pendingSetConf != null) { // for setConfiguration we do not need to wait for statemachine. send back // reply after it's committed. pendingSetConf.setReply(new RaftClientReply(pendingSetConf.getRequest(), server.getCommitInfos())); pendingSetConf = null; } }
Collection<TransactionContext> setNotLeaderException(NotLeaderException nle, Collection<CommitInfoProto> commitInfos) { LOG.debug("{}: PendingRequests.setNotLeaderException", name); try { return map.values().stream() .map(p -> p.setNotLeaderException(nle, commitInfos)) .collect(Collectors.toList()); } finally { map.clear(); } } }
void failSetConfiguration(RaftException e) { Preconditions.assertTrue(pendingSetConf != null); pendingSetConf.setException(e); pendingSetConf = null; }
TransactionContext getTransactionContext(long index) { PendingRequest pendingRequest = pendingRequests.get(index); // it is possible that the pendingRequest is null if this peer just becomes // the new leader and commits transactions received by the previous leader return pendingRequest != null ? pendingRequest.getEntry() : null; }
leaderState.notifySenders(); return pending.getFuture();