You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "poorbarcode (via GitHub)" <gi...@apache.org> on 2024/08/13 09:43:45 UTC

[PR] [improve] [broker] Avoid subsscription fenced error that introduced by consumer.seek whenever possible [pulsar]

poorbarcode opened a new pull request, #23163:
URL: https://github.com/apache/pulsar/pull/23163

   ### Motivation
   
   After a consumer calls `seek`, the operation `reconnect` will get a fenced error, which leads to more unnecessary reconnects. The steps to produce are as follows:
   
   - `client-side`: consumer connected
   - `client-side`: calls `consumer.seek` or `seekAsync`
   - `broker-side`: mark subscription as `fenced`
     - remove all consumer
     - do reset the cursor internally
   -`client-side`: consumer tries to reconnect
     - get an error Subscription was fenced due to the task `reset cursor` not finished yet.
   - `broker-side`: `reset cursor` is completed
   - `client-side`: The consumer tries to reconnect, and it will finish at this time.
   
   
   ### Modifications
   
   - This PR fixes the issue that a consumer fenced itself.
   - This PR can probably fix the issue that a `consumer.seek` fenced other consumers under the same subscription.
     - This PR does not fix all scenarios in which a `consumer.seek` fenced other consumers under the same subscription. See the following example:
   
   | time | `broker-side: consumer_1` | `broker-side: consumer_2` |
   | --- | --- | --- |
   | 1 | | check whether an in-progress reset cursor exists: `false` |
   | 2 | | start to add `consumer_2` |
   | 3 | `reset cursor` started |
   | 4 | mark subscription as `fenced` |
   | 5 | | get a fenced error | 
   
   I do not want to improve the behavior above because it needs a lock. Since it does not cause critical errors, it just causes reconnection and the possibility of it occurring is low, we can leave it there.
   
   
   ### Documentation
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   - [ ] `doc` <!-- Your PR contains doc changes. -->
   - [ ] `doc-required` <!-- Your PR changes impact docs and you will update later -->
   - [x] `doc-not-needed` <!-- Your PR changes do not impact docs -->
   - [ ] `doc-complete` <!-- Docs have been already added -->
   
   ### Matching PR in forked repository
   
   PR in forked repository: x
   


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


Re: [PR] [improve] [broker] Avoid subscription fenced error with consumer.seek whenever possible [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on code in PR #23163:
URL: https://github.com/apache/pulsar/pull/23163#discussion_r1715030323


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentSubscription.java:
##########
@@ -794,18 +806,13 @@ public void findEntryFailed(ManagedLedgerException exception,
     }
 
     @Override
-    public CompletableFuture<Void> resetCursor(Position position) {
-        CompletableFuture<Void> future = new CompletableFuture<>();
-        resetCursor(position, future);
-        return future;
-    }
-
-    private void resetCursor(Position finalPosition, CompletableFuture<Void> future) {
+    public CompletableFuture<Void> resetCursor(Position finalPosition) {
         if (!IS_FENCED_UPDATER.compareAndSet(PersistentSubscription.this, FALSE, TRUE)) {
-            future.completeExceptionally(new SubscriptionBusyException("Failed to fence subscription"));
-            return;
+            return CompletableFuture.failedFuture(new SubscriptionBusyException("Failed to fence subscription"));
         }
 
+        final CompletableFuture<Void> future = new CompletableFuture<>();;

Review Comment:
   extra semicolon at the end



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentSubscription.java:
##########
@@ -220,6 +221,16 @@ public boolean setReplicated(boolean replicated) {
 
     @Override
     public CompletableFuture<Void> addConsumer(Consumer consumer) {
+        CompletableFuture<Void> inProgressResetCursorFuture = this.inProgressResetCursorFuture;
+        if (inProgressResetCursorFuture != null) {
+            return inProgressResetCursorFuture.thenCompose(ignore -> addConsumerInternal(consumer))
+                    .exceptionallyCompose(ex -> addConsumerInternal(consumer));

Review Comment:
   There's code duplication in this style and the `exceptionallyCompose` cannot differentiate the source of the exception. 
   instead of chaining `.thenCompose` and `.exceptionallyCompose`, I think it's better to use `.exceptionally(e -> null).thenCompose(...)` to achieve the intended result of ignoring exceptions. 
   



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


Re: [PR] [improve] [broker] Avoid subscription fenced error with consumer.seek whenever possible [pulsar]

Posted by "poorbarcode (via GitHub)" <gi...@apache.org>.
poorbarcode commented on code in PR #23163:
URL: https://github.com/apache/pulsar/pull/23163#discussion_r1715048757


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentSubscription.java:
##########
@@ -794,18 +806,13 @@ public void findEntryFailed(ManagedLedgerException exception,
     }
 
     @Override
-    public CompletableFuture<Void> resetCursor(Position position) {
-        CompletableFuture<Void> future = new CompletableFuture<>();
-        resetCursor(position, future);
-        return future;
-    }
-
-    private void resetCursor(Position finalPosition, CompletableFuture<Void> future) {
+    public CompletableFuture<Void> resetCursor(Position finalPosition) {
         if (!IS_FENCED_UPDATER.compareAndSet(PersistentSubscription.this, FALSE, TRUE)) {
-            future.completeExceptionally(new SubscriptionBusyException("Failed to fence subscription"));
-            return;
+            return CompletableFuture.failedFuture(new SubscriptionBusyException("Failed to fence subscription"));
         }
 
+        final CompletableFuture<Void> future = new CompletableFuture<>();;

Review Comment:
   fixed



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentSubscription.java:
##########
@@ -220,6 +221,16 @@ public boolean setReplicated(boolean replicated) {
 
     @Override
     public CompletableFuture<Void> addConsumer(Consumer consumer) {
+        CompletableFuture<Void> inProgressResetCursorFuture = this.inProgressResetCursorFuture;
+        if (inProgressResetCursorFuture != null) {
+            return inProgressResetCursorFuture.thenCompose(ignore -> addConsumerInternal(consumer))
+                    .exceptionallyCompose(ex -> addConsumerInternal(consumer));

Review Comment:
   Improved



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


Re: [PR] [improve] [broker] Avoid subscription fenced error with consumer.seek whenever possible [pulsar]

Posted by "poorbarcode (via GitHub)" <gi...@apache.org>.
poorbarcode commented on code in PR #23163:
URL: https://github.com/apache/pulsar/pull/23163#discussion_r1715100565


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentSubscription.java:
##########
@@ -220,6 +221,16 @@ public boolean setReplicated(boolean replicated) {
 
     @Override
     public CompletableFuture<Void> addConsumer(Consumer consumer) {
+        CompletableFuture<Void> inProgressResetCursorFuture = this.inProgressResetCursorFuture;
+        if (inProgressResetCursorFuture != null) {
+            return inProgressResetCursorFuture.thenApply(ignore -> null)
+                    .exceptionally(ex -> null).thenCompose(ignore -> addConsumerInternal(consumer));

Review Comment:
   Improved, thanks for your suggestion



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


Re: [PR] [improve] [broker] Avoid subscription fenced error with consumer.seek whenever possible [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on code in PR #23163:
URL: https://github.com/apache/pulsar/pull/23163#discussion_r1715054550


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentSubscription.java:
##########
@@ -220,6 +221,16 @@ public boolean setReplicated(boolean replicated) {
 
     @Override
     public CompletableFuture<Void> addConsumer(Consumer consumer) {
+        CompletableFuture<Void> inProgressResetCursorFuture = this.inProgressResetCursorFuture;
+        if (inProgressResetCursorFuture != null) {
+            return inProgressResetCursorFuture.thenApply(ignore -> null)
+                    .exceptionally(ex -> null).thenCompose(ignore -> addConsumerInternal(consumer));

Review Comment:
   `.thenApply(ignore -> null).exceptionally(ex -> null)` could be replaced with `.handle((ignore, ex) -> null)` to make it shorter.



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


Re: [PR] [improve] [broker] Avoid subscription fenced error with consumer.seek whenever possible [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari merged PR #23163:
URL: https://github.com/apache/pulsar/pull/23163


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