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


##########
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:
   Thread A=> firstEntry() 
   Thread B=> remove() and recycle this OpRequestSend
   Thread timeout => op.cb is not Thread A OpRequestSend, but Thread B 
OpRequestSend may completeExceptionally



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