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; }
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(); } }
void replyPendingRequest(long index, RaftClientReply reply) { final PendingRequest pending = pendingRequests.remove(index); if (pending != null) { Preconditions.assertTrue(pending.getIndex() == index); pending.setReply(reply); } }
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); }
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; }