ISSUE DESCRIPTION [Bug] Broker may accept duplicate messages after dedup recovery race…
- User
ISSUE DESCRIPTION
[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
I'll start by exploring the repository structure to understand the codebase and find the relevant files.
Assistant
I'll start by exploring the repository structure to find the relevant files.
Assistant
Let me open the MessageDeduplication.java file to understand its structure.
Assistant
Let me see the truncated middle portion of the file (lines 256-460).
Assistant
Now let me look at the existing tests for MessageDeduplication to understand the testing patterns.
Assistant
Now let me look at how the issue describes the fix. Let me check the referenced PR #25953 and understand the
checkStatusflow. Let me look at the relevantopenCursorutility.Assistant
Let me look at the
ManagedLedgerReplayTaskto understand the replay flow.Assistant
Now let me check the
readEntriesutility to understand howasyncReadEntriesis called.Assistant
Now I have a clear understanding of the bug. Let me open the file at the
checkStatus()method.Assistant
Now I understand the bug completely. Let me make the fix to the enable path:
- Set
status = Status.Recoveringbefore starting the cursor replay (so concurrentcheckStatus()calls see the in-progress recovery). - Add
Status.Failedto the enable condition so enable is retried after a transient failure when policy still requires deduplication.
- Set
Assistant
The edit looks good. Now let me write a reproduction test to verify the fix. Let me first create a temporary test that reproduces the race condition described in the issue.
Assistant
Let me look at the
asyncReadEntriessignature used and the imports needed.Assistant
Now let me create a reproduction test to verify the fix works correctly.
Assistant
Let me verify the exception class exists before running.
Assistant
Now let me run the reproduction test. This is a Maven project, let me check how tests are run.
Assistant
This is a Gradle project. Let me run the reproduction test using gradle.
Assistant
The
Statusenum is package-private. Let me move my test to the same package asMessageDeduplication.Assistant
Let me check if the test ran by looking at the test reports.
Assistant
The test compiled but no test result XML was generated, meaning the test execution timed out or didn't complete. Let me check the gradle test report directory.
Assistant
The test was skipped/timed out. The issue is that my mock completes
asyncOpenCursorsynchronously, which causescheckStatus()to block on the test thread. Let me fix the test to complete the cursor open asynchronously.Assistant
The test report directory may have a different ID now. Let me look for the latest report.
Assistant
The build is still in progress (there's an in-progress results file). The gradle daemon is likely still running. Let me check for running gradle/java processes and kill them, then try a more targeted approach.
Actually, the issue is that running the full
:pulsar-broker:testtask triggers a lot of test setup/teardown which is slow. Also, the gradle daemon may be stuck. Let me check running processes.Assistant
Let me kill the hanging gradle daemon first, then reconsider the test approach.
Assistant
Awaitility is available. Let me rewrite the test to be robust using Awaitility for the async transitions, avoiding fixed sleeps.
Assistant
Now let me run the test with the fix applied. I'll redirect output to a file so I can check results even if the command times out.
Assistant
The test is running. Let me wait for it to complete.
Assistant
The test task is still running. Let me wait more and check the process status.
Assistant
The test worker is running. Let me wait for it to finish.
Assistant
The test task is still running. Let me check the test report directly for results.
Assistant
The worker is still alive. Let me wait more. The test might be hanging again. Let me check the worker's thread dump or just wait. Actually, let me check if the test has produced any output. Let me look at the in-progress binary results.
Actually, let me wait a bit more and check. The test might just be slow due to TestNG setup.
Assistant
The test worker has been alive for a while. Let me take a thread dump to see what it's doing.
Assistant
I see the issue - the test is hanging. Let me check the line numbers in my test to understand the stack trace.
Assistant
I understand the issue now - the
openCursorfuture completes too quickly on the executor, causingreplayCursorto run synchronously on the test thread and block. Let me kill the hanging test and rewrite with a deterministic approach where I capture the cursor-open callback and control its completion explicitly.Assistant
Now let me run the test again with the deterministic approach.
Assistant
The test passed with the fix (BUILD SUCCESSFUL). Let me check the test report to confirm the test actually ran and passed.
Assistant
The test passes with the fix. Now let me confirm this is a valid reproduction by temporarily reverting the fix and verifying the test fails. Let me check the current diff first.
Assistant
Now let me temporarily revert the fix to confirm the test fails (reproducing the bug).
Assistant
The test fails without the fix (BUILD FAILED), confirming it's a valid reproduction. Let me check the failure details.
Assistant
The test fails without the fix with exactly the expected error: "Status should be Recovering while replay is in progress expected [Recovering] but found [Initialized]". This confirms the bug reproduction. Now let me re-apply the fix.
Assistant
The fix is re-applied. Now let me run the existing
BrokerMessageDeduplicationTestto ensure I didn't break existing tests (especiallycheckStatusFail).Assistant
The existing
BrokerMessageDeduplicationTestpasses. Let me verify the test report to confirm all tests in that class ran and passed.Assistant
All 3 existing tests pass. Let me also check the
MessageDuplicationTestand search for any other tests that reference dedup status behavior that might be affected.Assistant
Those are mostly generic matches. Let me narrow to tests that specifically reference
MessageDeduplicationorcheckDeduplicationStatus.Assistant
Let me run the most directly relevant unit tests:
MessageDuplicationTestandTopicDuplicationTest.Assistant
TopicDuplicationTestis an integration test exercising the full enable/disable/recovery flow - very relevant to my change. Let me run it along withMessageDuplicationTest.Assistant
MessageDuplicationTest.testIsDuplicatemocksisEnabled()and testsisDuplicatelogic - not affected by my change.TopicDuplicationTestexercises the fullcheckStatusflow with a real broker. Let me run it.Assistant
All 13 tests in
TopicDuplicationTestpass (includingtestFinishTakeSnapshotWhenTopicLoadingrelevant to topic loading). The fix is correct and doesn't break existing tests.Now I need to remove my temporary reproduction test before submitting (per the instructions not to add new tests to the repository).
Assistant
Now let me review the final state of the fix one more time to ensure correctness.
Assistant
Let me view the full
checkStatus()method one final time to ensure the state machine is consistent.Assistant
The fix is complete and correct. Let me do a final review of the complete state machine logic by examining the
replayCursorsuccess/failure transitions to confirm consistency.Assistant
The state machine is consistent:
checkStatus()enable path setsstatus = Recoveringsynchronously before starting the async recovery chain.- Success path (
replayCursor's.thenRun()) setsstatus = Enabled. - Failure path (
checkStatus()'sfuture.exceptionally()) setsstatus = Failed. - The
Recoveringguard at the top prevents concurrent transitions. Failedis now included in the enable condition so enable is retried when policy still requires deduplication.
Let me clean up temp files and confirm the final git state.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.