Skip to content

Conversation

@lhotari
Copy link
Member

@lhotari lhotari commented Jul 18, 2022

Fixes #10054

Motivation

There has been a long-standing issue (#10054) where replicated subscriptions were not working correctly across clusters. The test case testReplicatedSubscriptionAcrossTwoRegions demonstrates this problem:

public void testReplicatedSubscriptionAcrossTwoRegions() throws Exception {
String namespace = BrokerTestUtil.newUniqueName("pulsar/replicatedsubscription");
String topicName = "persistent://" + namespace + "/mytopic";
String subscriptionName = "cluster-subscription";
// Subscription replication produces duplicates, https://github.com/apache/pulsar/issues/10054
// TODO: duplications shouldn't be allowed, change to "false" when fixing the issue
boolean allowDuplicates = true;
// this setting can be used to manually run the test with subscription replication disabled
// it shows that subscription replication has no impact in behavior for this test case
boolean replicateSubscriptionState = true;

Previously, this test would only pass when configured to allow duplicates (allowDuplicates = true), indicating that subscription replication was not functioning as expected.

This PR (#16651) addresses this issue:

Race Condition: A race condition existed between snapshot creation and mark-delete position updates, which could cause synchronization failures. If the mark delete position got updated before the snapshot was completed, it wouldn't be used even when it would have been suitable for the snapshot.

Modifications

  • Handle ReplicatedSubscriptionsSnapshot on completion: When a snapshot is completed, it is now processed immediately, passing the current mark-delete position to trigger the update logic. This handles the case where the mark delete position was updated before the snapshot completed.

  • Refactor snapshot handling logic: The previous snapshot handling code has been moved to notifyTheMarkDeletePositionChanged. Since snapshot handling is idempotent, it's safe to handle snapshots both when they arrive and when the mark-delete position moves forward.

Documentation

  • doc
  • doc-required
  • doc-not-needed
  • doc-complete

@lhotari lhotari added type/enhancement The enhancements for the existing features or docs. e.g. reduce memory usage of the delayed messages area/geo-replication doc-not-needed Your PR changes do not impact docs labels Jul 18, 2022
@lhotari lhotari added this to the 2.11.0 milestone Jul 18, 2022
@lhotari lhotari self-assigned this Jul 18, 2022
@github-actions github-actions bot removed the doc-not-needed Your PR changes do not impact docs label Jul 18, 2022
@github-actions
Copy link

@lhotari Please provide a correct documentation label for your PR.
Instructions see Pulsar Documentation Label Guide.

@lhotari lhotari added the doc-not-needed Your PR changes do not impact docs label Jul 18, 2022
@lhotari lhotari force-pushed the lh-fix-replicated-subscription-advanced-markdelete-position branch from b034bc6 to a4401df Compare July 18, 2022 13:49
@lhotari lhotari changed the title Skip replicated subscription update if the previous update was for the same snapshot id Reactively update replicated subscription and skip replicated subscription update if the previous update was for the same snapshot id Jul 18, 2022
@codelipenghui codelipenghui modified the milestones: 2.11.0, 2.12.0 Jul 26, 2022
@github-actions
Copy link

The pr had no activity for 30 days, mark with Stale label.

@github-actions github-actions bot added the Stale label Aug 25, 2022
@lhotari lhotari force-pushed the lh-fix-replicated-subscription-advanced-markdelete-position branch from a4401df to baeba7b Compare January 3, 2023 15:45
@yangl
Copy link
Contributor

yangl commented Feb 28, 2023

@codelipenghui PTAL

@github-actions github-actions bot removed the Stale label Mar 1, 2023
@poorbarcode poorbarcode changed the title Reactively update replicated subscription and skip replicated subscription update if the previous update was for the same snapshot id [improve] [broker] Reactively update replicated subscription and skip replicated subscription update if the previous update was for the same snapshot id Apr 10, 2023
@poorbarcode
Copy link
Contributor

poorbarcode commented Apr 10, 2023

Since we will start the RC version of 3.0.0 on 2023-04-11, I will change the label/milestone of PR who have not been merged.

  • The PR of type feature/improve is deferred to 3.1.0
  • The PR of type fix is deferred to 3.0.1

So drag this PR to 3.0.1

@poorbarcode poorbarcode modified the milestones: 3.0.0, 3.1.0 Apr 10, 2023
@Technoboy- Technoboy- modified the milestones: 3.1.0, 3.2.0 Jul 31, 2023
@lhotari lhotari force-pushed the lh-fix-replicated-subscription-advanced-markdelete-position branch from baeba7b to 480b2e9 Compare November 24, 2023 07:28
@lhotari lhotari marked this pull request as ready for review December 8, 2025 11:25
@lhotari
Copy link
Member Author

lhotari commented Dec 8, 2025

Some assumptions aren't correct in this PR, will keep as draft until resolved.

I've validated the assumption and this PR makes sense. Before this PR a race condition is possible where the local cluster's mark delete position is updated before the snapshot is completed. That results in the case, that the most recent snapshot cannot immediately be used for sending the replicated subscription update back to the remote cluster (via writing the marker message to the topic).

@lhotari lhotari changed the title [improve] [broker] Handle ReplicatedSubscriptionsSnapshots when it arrives to fix issues in replicating subscription state to remote clusters [improve][broker] Handle ReplicatedSubscriptionsSnapshots when it arrives to fix issues in replicating subscription state to remote clusters Dec 8, 2025
@lhotari lhotari changed the title [improve][broker] Handle ReplicatedSubscriptionsSnapshots when it arrives to fix issues in replicating subscription state to remote clusters [improve][broker] Fix replicated subscriptions snapshot race condition and position handling Dec 8, 2025
@lhotari lhotari requested review from dao-jun and nodece December 8, 2025 12:50
@lhotari lhotari marked this pull request as draft December 8, 2025 16:07
@lhotari
Copy link
Member Author

lhotari commented Dec 8, 2025

There's another related flaw in the replicated subscription snapshots that should be fixed. The commit 50ca143 is already in this direction, but a similar one is needed for the responses. Fixing this would also avoid the current solution of two rounds of snapshot requests when there are more than 2 remote clusters.
In addition, it's still required to have something to track the last non-marker message before the snapshot request. That position should be used instead of the snapshot request's message id as the position for the completed snapshot. That would mean reverting some of the reverted changes in commit dae88dc

@lhotari lhotari marked this pull request as ready for review December 8, 2025 18:24
@lhotari
Copy link
Member Author

lhotari commented Dec 8, 2025

There's another related flaw in the replicated subscription snapshots that should be fixed. The commit 50ca143 is already in this direction, but a similar one is needed for the responses. Fixing this would also avoid the current solution of two rounds of snapshot requests when there are more than 2 remote clusters. In addition, it's still required to have something to track the last non-marker message before the snapshot request. That position should be used instead of the snapshot request's message id as the position for the completed snapshot. That would mean reverting some of the reverted changes in commit dae88dc

This turned out to be the incorrect direction. However, one change that continues to make sense is cb2d873. In the case of the replication snapshot response, the request position would be sent back instead of the latest topic position.

@lhotari lhotari changed the title [improve][broker] Fix replicated subscriptions snapshot race condition and position handling [improve][broker] Fix replicated subscriptions race condition with mark delete update and snapshot completion Dec 9, 2025
@lhotari
Copy link
Member Author

lhotari commented Dec 9, 2025

This turned out to be the incorrect direction. However, one change that continues to make sense is cb2d873. In the case of the replication snapshot response, the request position would be sent back instead of the latest topic position.

I reverted the change related to this since it doesn't change the behavior when the last position is used. The response will get appended at the end and the subscription update will get sent from the other cluster only after all messages have been acknowledged before that point. For more than 2 cluster configurations, the 2 rounds of snapshots solution is used to avoid message loss (acknowledging messages that haven't been processed).

@lhotari lhotari merged commit 89f6015 into apache:master Dec 9, 2025
95 of 97 checks passed
Technoboy- pushed a commit that referenced this pull request Dec 10, 2025
Technoboy- pushed a commit that referenced this pull request Dec 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/geo-replication cherry-picked/branch-4.0 cherry-picked/branch-4.1 doc-not-needed Your PR changes do not impact docs ready-to-test release/4.0.9 release/4.1.3 type/enhancement The enhancements for the existing features or docs. e.g. reduce memory usage of the delayed messages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Geo-replication] Subscription replication is not working across clusters

10 participants