hanm commented on a change in pull request #1172: ZOOKEEPER-3594: Ability to 
skip proposing requests with error transactions
URL: https://github.com/apache/zookeeper/pull/1172#discussion_r360631264
 
 

 ##########
 File path: 
zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/SkippedRequestQueue.java
 ##########
 @@ -0,0 +1,78 @@
+package org.apache.zookeeper.server.quorum;
+
+import org.apache.zookeeper.server.ExitCode;
+import org.apache.zookeeper.server.Request;
+import org.apache.zookeeper.server.ServerMetrics;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.atomic.AtomicLong;
+
+public class SkippedRequestQueue {
+    private static final Logger LOG = 
LoggerFactory.getLogger(SkippedRequestQueue.class);
+
+    private final AtomicLong lastCommitted = new AtomicLong(-1);
+    private final LinkedBlockingQueue<Request> waitingRequests = new 
LinkedBlockingQueue<>();
+
+    /**
+     * Setter method for lastCommitted zxid
+     */
+    public void setLastCommitted(long zxid) {
+        lastCommitted.set(zxid);
+    }
+
+    public List<Request> setLastCommittedAndGet(long zxid) {
+        synchronized (this) {
+            setLastCommitted(zxid);
+            return getSkippedRequests();
+        }
+    }
+
+    /**
+     * Adds skipped request to the end of the queue that way we ensure the 
order.
+     * Returns all the skipped requests that are ready to be sent back to the 
origin,
+     * meaning if there was a COMMIT that they were waiting for
+     */
+    public List<Request> addAndGet(Request request) {
+        ServerMetrics.getMetrics().QUORUM_PEER_SKIP_QUEUE_SIZE.add(1);
+        synchronized (this) {
+            waitingRequests.add(request);
+            return getSkippedRequests();
+        }
+    }
+
+    /**
+     * This method flushed the error request queue. This method gets called 
from two different
+     * threads thorough skipProposing() and 
updateLastZxidAndGetSkippedRequests().
+     *
+     * Leader thread calls this method every time the leader processes a 
COMMIT in order
+     * to try to flush all the pending error requests that we short circuited.
+     *
+     * The RP chain thread will try to call this when short-circuiting requests
+     *
+     */
+    public List<Request> getSkippedRequests() {
+        List<Request> requestsToSkip = new ArrayList<>();
+        long lastZxid = lastCommitted.get();
+
+        while (!waitingRequests.isEmpty()) {
+            Request request = waitingRequests.peek();
+            if (lastZxid > request.zxid) {
 
 Review comment:
   might worth to comment on why it works this way (all requests in the queue 
should have same zxid as last committed zxid, otherwise we either bail out or 
report out of order error).

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to