[fix][broker] Avoid attaching a consumer to a migrated non-persistent topic on subscribe#26075
Merged
lhotari merged 1 commit intoJun 22, 2026
Merged
Conversation
… topic on subscribe Motivation PR apache#26051 changed NonPersistentTopic.internalSubscribe from a blocking, ordered migration redirect into a fire-and-forget async one: if (isMigrated()) { getMigratedClusterUrlAsync().thenAccept(consumer::topicMigrated); } addConsumerToSubscription(subscription, consumer).thenRun(...) getMigratedClusterUrlAsync() only invokes topicMigrated() (which sends the TopicMigrated redirect and disconnects the consumer) after a metadata-read hop on pulsar.getExecutor(), while addConsumerToSubscription runs immediately, so the two race. The downstream "if (!cnx.isActive())" guard does not mitigate it: for protocol >= v5, disconnect() -> closeConsumer() only sends a CloseConsumer command and never flips cnx.isActive() to false. As a result a consumer subscribing to a migrated non-persistent topic can be left attached to the subscription on the old cluster. Modifications Route the inline migration check through the existing Consumer.checkAndApplyTopicMigrationAsync() (already used by ServerCnx) and sequence addConsumerToSubscription behind it with thenCompose: when the topic is migrated, the consumer is redirected and disconnected and the add is skipped; otherwise the original add path runs unchanged. The check now also surfaces metadata-read failures through the existing exceptionally handler instead of swallowing them (only reachable for already-migrated topics, so the common subscribe path is unchanged). Usage count stays balanced: the migrated path's disconnect -> close -> removeConsumer already calls decrementUsageCount. Added NonPersistentTopicTest.testSubscribeOnMigratedTopicSkipsAddingConsumer, which fails on the previous code (the consumer is added via addConsumerToSubscription) and passes with the fix. Assisted-by: Claude Code (Claude Opus 4.8)
merlimat
approved these changes
Jun 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a follow-up regression fix for #26051.
Motivation
#26051 changed
NonPersistentTopic.internalSubscribefrom a blocking, ordered migration redirect into a fire-and-forget async one:getMigratedClusterUrlAsync()only invokestopicMigrated()— which sends theTopicMigratedredirect and disconnects the consumer — after a metadata-read hop onpulsar.getExecutor(), whileaddConsumerToSubscriptionruns immediately. The two therefore race.The later
if (!cnx.isActive())guard does not mitigate this, because for protocol versionv5and abovedisconnect()→closeConsumer()only sends aCloseConsumercommand and never flipscnx.isActive()tofalse. As a result, a consumer subscribing to a migrated non-persistent topic can be left attached to the subscription on the old (migrated) cluster instead of being cleanly redirected.Before #26051 this code was synchronous, so
topicMigrated()always completed beforeaddConsumerToSubscription; the async change introduced the race.This is specific to
NonPersistentTopic:PersistentTopic.internalSubscribehas no inline migration check and relies solely on the post-subscribe check inServerCnx.Modifications
Consumer.checkAndApplyTopicMigrationAsync()(already used byServerCnxon the subscribe-success path) and sequenceaddConsumerToSubscriptionbehind it withthenCompose:addConsumerToSubscriptionis skipped, so the consumer is never attached to the subscription on the old cluster;exceptionallyhandler instead of being silently swallowed by the fire-and-forgetthenAccept. This metadata read is only reached when the subscription is already migrated (checkAndApplyTopicMigrationAsync()short-circuits tofalseotherwise), so the common, non-migrated subscribe path is unchanged and gains no new failure mode.topicMigrated()→disconnect()→Consumer.close()→NonPersistentSubscription.removeConsumer()already callsdecrementUsageCount()(even when the consumer was never added), balancing the increment taken byhandleConsumerAdded().Verifying this change
This change added tests and can be verified as follows:
NonPersistentTopicTest.testSubscribeOnMigratedTopicSkipsAddingConsumer, a deterministic regression test that subscribes directly on a migrated non-persistent topic and asserts the consumer is never added to the subscription (verify(subscription, never()).addConsumer(...)), the migration redirect is sent, and the topic usage count returns to its pre-subscribe value. The test fails on the previous code (MockitoNeverWantedButInvoked:addConsumeris invoked viaaddConsumerToSubscription) and passes with the fix.ClusterMigrationTestand the fullNonPersistentTopicTestcontinue to pass.Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes