Untitled conversation
- User
[Bug] Broker may accept duplicate messages after dedup recovery race
Issue Description
Message deduplication recovery can be started more than once for the same topic before the first recovery replay completes.
MessageDeduplicationdefines aRecoveringstatus to prevent overlapping transitions, but the enable path starts dedup cursor replay without first changing the status toRecovering.Because of that, another
checkStatus()call can still observeInitializedorDisabledand start a second replay for the same dedup cursor.One practical trigger path is topic load with existing topic-level policies:
Time Flow A: existing topic-level policy during load Flow B: normal topic load chain Dedup status / effect T1 initTopicPolicy()loads existing topic policiesdedup status is InitializedorDisabledT2 onUpdate()applies topic policies and callscheckDeduplicationStatus()first dedup recovery replay starts T3 first replay is still running asynchronously status is still InitializedorDisabledT4 topic load continues and BrokerServiceexplicitly callscheckDeduplicationStatus()T5 second check also enters the enable path second replay starts for the same dedup cursor T6 first replay rebuilds producer sequence state second replay also rebuilds producer sequence state overlapping replay can advance shared replay/cursor state Dedup recovery rebuilds producer sequence information from the dedup cursor. If overlapping replay leaves the recovered sequence state incomplete or inconsistent, the broker may fail to identify already-published messages as duplicates.
The impact is that duplicate messages can be accepted after topic load or policy refresh even though message deduplication is enabled.
There is also a retry gap after recovery failure. If enabling deduplication fails transiently, the status moves to
Failed, but later checks do not retry enabling even when the topic policy still requires deduplication. - Assistant
Now I'll apply the fix to
checkStatus(): - Assistant
Now check
ManagedLedgerReplayTask.replayto understand what a mocked cursor needs for the test, and check thegetStatusvisibility: - Assistant
While the test runs, let me check whether existing tests depend on the old behavior:
- Assistant
While waiting, check lint commands:
- Assistant
The test output is piped through
tailso it will appear when the run completes. Waiting on the monitors now. - Assistant
The diff is complete. Waiting on the two background runs (tests + checkstyle/spotless) to report results.
- Assistant
The lint run failed only due to Gradle lock contention with the concurrent test run — I'll re-run it after the tests finish. Waiting for the test run to complete.
- Assistant
Tests passed. Let me confirm both new test methods actually executed, and re-run the lint that hit the Gradle lock:
- Assistant
Both new tests ran and passed (
tests="2" failures="0" errors="0"). Lint is re-running now that the Gradle lock is free. - Assistant
Lint passes. Let me also run the pre-existing tests that exercise
checkStatus()directly, to verify no regression: - Assistant
Running the related regression suites (
BrokerMessageDeduplicationTest, fullMessageDuplicationTest,TopicDuplicationTest) in the background — I'll report the outcome when it completes. - Assistant
Running
TopicDuplicationTestin isolation now; will run the other two classes after. - Assistant
TopicDuplicationTest: 13/13 passed in isolation. Now the remaining two classes: - Assistant
Waiting for the final regression run.
- Assistant
All verification is complete and green. Summary of the resolved task:
Fix
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java:227— the enable path incheckStatus()now:- Transitions to
Recoveringinside the synchronized block before starting the asyncopenCursor(...).thenCompose(this::replayCursor)recovery. Any concurrentcheckStatus()(e.g. topic-policy during racing with 's explicit check on topic load) now hits the existing guard and reschedules a later check instead of starting a second overlapping replay of the same dedup cursor.
- Transitions to
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.