mattisonchao commented on code in PR #15021:
URL: https://github.com/apache/pulsar/pull/15021#discussion_r851873706


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/TransactionBufferHandlerImpl.java:
##########
@@ -222,6 +233,42 @@ public void handleEndTxnOnSubscriptionResponse(long 
requestId,
         }
     }
 
+    private void continueCheckOutstandingRequestIfTimeout() {
+        Map.Entry<Long, OpRequestSend> entry = 
outstandingRequests.firstEntry();
+        if (entry == null) {
+            timoutCheckIsRunning.set(false);
+            // To avoid race conditions, the scenarios are as follows.
+            // Thread A: after if(entry=null) => Thread B: put a new entry and 
then get timoutCheckIsRunning=true =>
+            // Thread A: set timoutCheckIsRunning = false
+            if (outstandingRequests.firstEntry() != null
+                    && timoutCheckIsRunning.compareAndSet(false, true)) {
+                 continueCheckOutstandingRequestIfTimeout();
+            }
+            return;
+        }
+        OpRequestSend op = entry.getValue();
+        final long finalRequestId = op.requestId;
+        if (!op.isTimeout(operationTimeoutInMills)) {
+            timer.newTimeout(task -> 
continueCheckOutstandingRequestIfTimeout(),
+                    outstandingRequests.firstEntry().getValue()
+                            .getTimeoutMs(operationTimeoutInMills), 
TimeUnit.MILLISECONDS);
+            return;
+        }
+        if (!op.tryOwnership(finalRequestId)) {
+            // Just safe check to ensure endless loop cause StackOverFlow,
+            // because other operation will remove this request firstly.
+            outstandingRequests.remove(finalRequestId);
+            // This operation already handle by other thread, we need to check 
next.
+            continueCheckOutstandingRequestIfTimeout();
+            return;
+        }
+        outstandingRequests.remove(entry.getKey());
+        op.cb.completeExceptionally(new TransactionBufferClientException

Review Comment:
   Hi, @congbobo184 
   We use `op.tryOwnership(finalRequestId)` to avoid recycle race conditions.
   This method will use the `requestId` to avoid trying to take ownership of 
the wrong `OpRequestSend`.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to