You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "Denovo1998 (via GitHub)" <gi...@apache.org> on 2026/06/29 12:17:54 UTC

[PR] [fix][broker] Prevent repeated consumer close on topic migration [pulsar]

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

   ### Motivation
   
   PR #26075 fixed the non-persistent topic subscribe race by redirecting a migrated consumer before adding it to the subscription. However, that path still completes the subscribe future with the same consumer, and `ServerCnx.handleSubscribe` runs the generic post-subscribe migration check again.
   
   That second check can redirect and disconnect the same consumer a second time. For non-persistent topics this can double-decrement the topic usage count and emit duplicate `TopicMigrated` commands.
   
   ### Modifications
   
   - Make `Consumer.topicMigrated(...)` idempotent for each consumer with an atomic once guard.
   - Keep the first migration handling unchanged: send `TopicMigrated` and disconnect the consumer.
   - Ignore repeated migration handling for the same consumer so the `ServerCnx` post-subscribe check remains safe after an inline non-persistent redirect.
   - Add a regression test that simulates the post-subscribe migration check after `NonPersistentTopic.internalSubscribe` already redirected the consumer, verifying one redirect and balanced topic usage count.
   
   ### Verifying this change
   
   - [x] Make sure that the change passes the CI checks.
   
   - `./gradlew :pulsar-broker:test --tests org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopicTest.testPostSubscribeMigrationCheckDoesNotDoubleCloseRedirectedConsumer`
   - `./gradlew :pulsar-broker:test --tests org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopicTest.testSubscribeOnMigratedTopicSkipsAddingConsumer --tests org.apache.pulsar.broker.service.nonpersistent.NonPersistentTopicTest.testPostSubscribeMigrationCheckDoesNotDoubleCloseRedirectedConsumer`
   - `./gradlew :pulsar-broker:checkstyleMain :pulsar-broker:checkstyleTest`
   
   ### Does this pull request potentially affect one of the following parts:
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   *If the box was checked, please highlight the changes*
   
   - [ ] Dependencies (add or upgrade a dependency)
   - [ ] The public API
   - [ ] The schema
   - [ ] The default values of configurations
   - [ ] The threading model
   - [ ] The binary protocol
   - [ ] The REST endpoints
   - [ ] The admin CLI options
   - [ ] The metrics
   - [ ] Anything that affects deployment


-- 
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] [fix][broker] Prevent repeated consumer close on topic migration [pulsar]

Posted by "merlimat (via GitHub)" <gi...@apache.org>.
merlimat closed pull request #26111: [fix][broker] Prevent repeated consumer close on topic migration
URL: https://github.com/apache/pulsar/pull/26111


-- 
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] [fix][broker] Prevent repeated consumer close on topic migration [pulsar]

Posted by "Denovo1998 (via GitHub)" <gi...@apache.org>.
Denovo1998 commented on PR #26111:
URL: https://github.com/apache/pulsar/pull/26111#issuecomment-4843691189

   > It seems like the original logic before #26075 is incorrect. `Consumer.topicMigrated` should only get called by this code:
   > 
   > https://github.com/apache/pulsar/blob/808133dfca01a0b94b7295314778e654613ca322/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/ServerCnx.java#L2017-L2027
   > 
   > One possibility would be to add this method to `Consumer` so that it could be used in `NonPersistentTopic.internalSubscribe` instead of using `checkAndApplyTopicMigrationAsync`
   > 
   > ```java
   >     public CompletableFuture<Boolean> checkTopicMigrationAsync() {
   >         if (!subscription.isSubscriptionMigrated()) {
   >             return CompletableFuture.completedFuture(false);
   >         }
   >         return AbstractTopic.getMigratedClusterUrlAsync(cnx.getBrokerService().getPulsar(), topicName)
   >                 .thenApply(clusterUrl -> {
   >                     return clusterUrl.isPresent();
   >                 });
   >     }
   > ```
   
   @lhotari 
   Thanks for the suggestion. I updated the PR to keep `Consumer.topicMigrated(...)` applied only from the common `ServerCnx` post-subscribe path.
   
   `NonPersistentTopic.internalSubscribe` now uses a new pure `Consumer.checkTopicMigrationAsync()` helper to detect migrated subscriptions and skip `addConsumerToSubscription` without sending the redirect or closing the consumer. The existing `checkAndApplyTopicMigrationAsync()` still owns the actual redirect/close behavior.


-- 
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] [fix][broker] Prevent repeated consumer close on topic migration [pulsar]

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on PR #26111:
URL: https://github.com/apache/pulsar/pull/26111#issuecomment-6167813936

   Thanks, this looks right. The topic only checks now, and `ServerCnx` is the one place that sends the redirect and closes the consumer. The updated test covers that sequence.


-- 
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] [fix][broker] Prevent repeated consumer close on topic migration [pulsar]

Posted by "merlimat (via GitHub)" <gi...@apache.org>.
merlimat commented on PR #26111:
URL: https://github.com/apache/pulsar/pull/26111#issuecomment-6381469381

   Thanks @Denovo1998, and thanks @lhotari for the review. This is now superseded by #27003, which you followed up with and which was merged on 2026-10-22. On current master, `NonPersistentTopic.internalSubscribe` checks migration before it constructs a `Consumer` and fails the subscribe with `TopicMigratedException`. `ServerCnx` then redirects the request once through `redirectOrFailMigratedConsumer`. No consumer is created on a migrated subscription, so the post-subscribe `checkAndApplyTopicMigrationAsync()` never runs for it and the double close can't happen. The usage-count leak for Exclusive/Failover that Lari noted is fixed there too. This branch now conflicts with master in that same block, and after resolving it, `checkTopicMigrationAsync()` would have no callers. I'm closing this in favour of #27003. Please reopen if you see a case it doesn't cover. Thanks again for following it through.


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