You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/11/07 02:27:05 UTC

[GitHub] [pulsar] labuladong commented on a diff in pull request #18340: [fix][ml]fix wrong cursor state if repeat do close

labuladong commented on code in PR #18340:
URL: https://github.com/apache/pulsar/pull/18340#discussion_r1014954124


##########
managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java:
##########
@@ -2707,6 +2707,25 @@ public void operationFailed(ManagedLedgerException exception) {
         });
     }
 
+    /**
+     * Try set {@link #state} to {@link State#Closing}.
+     * @return false if the {@link #state} already is {@link State#Closing} or {@link State#Closed}.
+     */
+    private boolean trySetStateToClosing() {
+        final AtomicBoolean notClosing = new AtomicBoolean(false);
+        STATE_UPDATER.updateAndGet(this, state -> {
+            switch (state){
+                case Closing:
+                case Closed: return state;
+                default: {
+                    notClosing.set(true);
+                    return State.Closing;
+                }

Review Comment:
   [`updateAndGet` doc](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.html#updateAndGet-T-java.util.function.UnaryOperator-) says:
   
   > The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.
   
   So I guess `notClosing.set(true)` may cause tricky bugs. How about just 
    comparing the state that before and after `updateAndGet`?



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org