[ 
https://issues.apache.org/jira/browse/ZOOKEEPER-1863?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13882424#comment-13882424
 ] 

Camille Fournier commented on ZOOKEEPER-1863:
---------------------------------------------

OK Apologies, I (somewhat) understand why we don't care when nextPending 
doesn't match.

So, I've played around with reproducing this. When I change the 
FollowerRequestProcessor to do this:
  case OpCode.sync:
                    zks.pendingSyncs.add(request);
                    zks.getFollower().request(request);
                    zks.sync(); //this is what the leader will eventually cause 
us to call
                    break;
 , randomly sleep the commit processor, and run the async hammer test with some 
extra reads and sleeps (just trying to force a reproduction), I can get the xid 
out of sync exception:
java.io.IOException: Xid out of order. Got Xid 5 with err 0 expected Xid 1 for 
a packet with details: clientPath:/test- serverPath:/test- finished:false 
header:: 1,1  replyHeader:: 0,0,-4  request:: 
'/test-,,v{s{31,s{'world,'anyone}}},2  response::  
        at 
org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:914)
        at 
org.apache.zookeeper.ClientCnxnSocketNIO.doIO(ClientCnxnSocketNIO.java:99)
        at 
org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:362)
        at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1193)

Note that this fails in the same way with the same changes in 3.4, so I don't 
think this is a new thing.
Have yet to get it to hang or any other behavior except for the xid out of 
order.
Feels like we may need to do some additional tracking of syncs inside the 
commit processor...


> Race condition in commit processor leading to out of order request 
> completion, xid mismatch on client.
> ------------------------------------------------------------------------------------------------------
>
>                 Key: ZOOKEEPER-1863
>                 URL: https://issues.apache.org/jira/browse/ZOOKEEPER-1863
>             Project: ZooKeeper
>          Issue Type: Bug
>          Components: server
>    Affects Versions: 3.5.0
>            Reporter: Dutch T. Meyer
>            Priority: Blocker
>         Attachments: stack.17512
>
>
> In CommitProcessor.java processor, if we are at the primary request handler 
> on line 167:
> {noformat}
>                 while (!stopped && !isWaitingForCommit() &&
>                        !isProcessingCommit() &&
>                        (request = queuedRequests.poll()) != null) {
>                     if (needCommit(request)) {
>                         nextPending.set(request);
>                     } else {
>                         sendToNextProcessor(request);
>                     }
>                 }
> {noformat}
> A request can be handled in this block and be quickly processed and completed 
> on another thread. If queuedRequests is empty, we then exit the block. Next, 
> before this thread makes any more progress, we can get 2 more requests, one 
> get_children(say), and a sync placed on queuedRequests for the processor. 
> Then, if we are very unlucky, the sync request can complete and this object's 
> commit() routine is called (from FollowerZookeeperServer), which places the 
> sync request on the previously empty committedRequests queue. At that point, 
> this thread continues.
> We reach line 182, which is a check on sync requests.
> {noformat}
>                 if (!stopped && !isProcessingRequest() &&
>                     (request = committedRequests.poll()) != null) {
> {noformat}
> Here we are not processing any requests, because the original request has 
> completed. We haven't dequeued either the read or the sync request in this 
> processor. Next, the poll above will pull the sync request off the queue, and 
> in the following block, the sync will get forwarded to the next processor.
> This is a problem because the read request hasn't been forwarded yet, so 
> requests are now out of order.
> I've been able to reproduce this bug reliably by injecting a 
> Thread.sleep(5000) between the two blocks above to make the race condition 
> far more likely, then in a client program.
> {noformat}
>         zoo_aget_children(zh, "/", 0, getchildren_cb, NULL);
>         //Wait long enough for queuedRequests to drain
>         sleep(1);
>         zoo_aget_children(zh, "/", 0, getchildren_cb, &th_ctx[0]);
>         zoo_async(zh, "/", sync_cb, &th_ctx[0]);
> {noformat}
> When this bug is triggered, 3 things can happen:
> 1) Clients will see requests complete out of order and fail on xid mismatches.
> 2) Kazoo in particular doesn't handle this runtime exception well, and can 
> orphan outstanding requests.
> 3) I've seen zookeeper servers deadlock, likely because the commit cannot be 
> completed, which can wedge the commit processor.



--
This message was sent by Atlassian JIRA
(v6.1.5#6160)

Reply via email to