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 and finding the relevant files related to the
MessageDeduplicationclass and its recovery state machine.Assistant
Let me look at the main
MessageDeduplication.javafile.Assistant
Let me see the truncated portion of the file (the
replayCursormethod and surrounding code).Assistant
Let me look at the test files to understand the testing patterns and expected behavior.
Assistant
Let me look at the existing tests related to deduplication status and the
ManagedLedgerReplayTaskclass.Assistant
Let me look at where
checkStatus/checkDeduplicationStatusis called and look at the PersistentTopic and BrokerService.Assistant
Let me look at the
initTopicPolicy()and theonUpdate()flow in PersistentTopic.Assistant
Now let me look at the full
checkStatus()method in detail.Assistant
Let me look at the
ManagedLedgerUtilsforopenCursorandmarkDelete.Assistant
Let me look at the existing tests to understand testing patterns for deduplication recovery.
Assistant
Let me look at
checkMessageDeduplicationInfoin PersistentTopic and how the inactivity monitor calls it.Assistant
Let me first build and run the existing test to confirm the environment works.
Assistant
This is a Gradle project. Let me check the build setup and try to run the existing test.
Assistant
The build works. Now let me write a reproduction test to confirm the race condition. I'll create a temporary test file modeled after the existing
BrokerMessageDeduplicationTest.Assistant
Now let me create a temporary reproduction test file to confirm the bug.
Assistant
Let me check the
ManagedCursorinterface for the correct method name and look at how the existing test accesses status.Assistant
So the method is
hasMoreEntries()(plural), nothasMoreEntry(). But the existing test useshasMoreEntry()(singular)... let me re-check the existing test.Wait, let me re-read the existing test:
doReturn(true).when(cursor).hasMoreEntry();Hmm, but that would be a compile error too. But the build succeeded earlier. Let me re-check by looking at the actual file content.
Assistant
I had typos. Let me move the test to the
persistentpackage to access the package-privateStatusenum and fix the method name.Assistant
The first test confirms the bug (expected 1 but found 2 openCursor calls). The second test was skipped—let me check why.
Assistant
The first test confirms the race bug (2 openCursor calls instead of 1). The second test was skipped due to the first failure's retry. Let me run only the second test to confirm it also reproduces the retry gap.
Assistant
Both reproduction tests confirm the bug:
- Race:
concurrentCheckStatusShouldNotStartSecondReplay— 2 openCursor calls instead of 1. - Retry gap:
failedEnableShouldRetryOnLaterCheck— status staysFailedinstead of retrying toEnabled.
Now let me implement the fix. Let me open the file and look at the enable path.
- Race:
Assistant
Now let me implement the fix in the enable path.
Assistant
Now let me run the reproduction tests to confirm the fix works.
Assistant
Both reproduction tests pass now. Let me verify they actually ran (not skipped).
Assistant
Both tests pass with 0 skipped and 0 failures. Now let me remove the temporary test file and run the existing related tests to make sure nothing is broken.
Assistant
All three existing test classes pass. Let me also run the other deduplication-related tests to be thorough.
Assistant
The combined run timed out. Let me run the lighter
DeduplicationDisabledBrokerLevelTestseparately.Assistant
Let me review the full updated
checkStatus()method to verify correctness.Assistant
Let me verify there are no scheduled periodic calls to
checkStatus()that could cause issues, and check where the no-argtakeSnapshot()is called.Assistant
Let me look at the
takeSnapshot()call at PersistentTopic line 3785 to understand the scheduled snapshot.Assistant
Let me check the
BacklogQuotaManagercall totakeSnapshot().Assistant
Let me check where
getLastPublishedSequenceIdis called, since it reads the maps without anisEnabled()check.Assistant
Let me search for any tests that specifically check the
Recoveringstatus or status transitions, to ensure my change is consistent.Assistant
Let me check where
resetHighestSequenceIdPushedis called to ensure clearing maps duringRecoveringis safe.Assistant
All the methods that read the maps or
managedCursorcheckisEnabled()first, so clearing them duringRecoveringis safe. Let me view the final diff.Assistant
The diff looks clean. Let me re-create a comprehensive temporary test to verify the fix handles all the edge cases described in the issue.
Assistant
Both tests pass and confirm the fix, including the assertion that
Recoveringis set before replay starts. Let me remove the temporary test file.Assistant
Let me run the key existing dedup tests once more to confirm the final state is clean.
Assistant
All tests pass. Let me do a final verification that there are no leftover temporary files and the diff is exactly what I intend to submit.
Assistant
The diff is clean — only the source file change remains, with no leftover temporary files. Let me do a final review of the complete modified method to ensure correctness.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.