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 locating the MessageDeduplication-related code mentioned in the issue.
- Assistant
Let me examine the MessageDeduplication class, focusing on status handling and the checkStatus/enable paths.
- Assistant
rg is not available, let me use grep instead.
- Assistant
Let me read the full checkStatus method and the related enable logic.
- Assistant
Let me read the checkStatus method specifically.
- Assistant
Need to use goto or open with line number correctly.
- Assistant
I can see the bug—status isn't set to Recovering on enable. Let me also inspect the replay path and any related PR or tests for context.
- Assistant
The issue references PR #25953. I'll inspect related tests and any local history that might reveal the intended fix.
- Assistant
I'll check how related past fixes handled the status machine, and inspect git blame around the enable path.
- Assistant
Let me look at the older version of the code before the ManagedLedgerReplayTask refactor to see if Recovering was set.
- Assistant
Let me also check the status field declaration and related fields for thread-safety.
- Assistant
I'll implement the fix: set status to
Recoveringbefore starting recovery, and allow retry fromFailedwhen enable is still required. - Assistant
I need to navigate to that section before editing.
- Assistant
Now applying the fix.
- Assistant
When retrying after a Failed recovery, I should clear the sequence maps so a partial prior replay can't leave stale state.
- Assistant
I'll write a temporary unit test to reproduce the race and verify the fix. First, checking how
openCursorand the existing tests are structured. - Assistant
I'll set up a temporary verification test and try compiling only the broker module tests that matter.
- Assistant
There's already a MessageDuplicationTest — I'll inspect it and add a temporary verification there.
- Assistant
I'll add a temporary self-contained unit test based on the simpler mocked BrokerMessageDeduplicationTest.
- Assistant
Running the temporary race test to verify the fix.
- Assistant
There are compilation errors—I'll inspect them.
- Assistant
Status is package-private, so I'll put the temp test in the same package.
- Assistant
Also need to check - is Status accessible as MessageDeduplication.Status? It's package-private enum not public, but within same package it should be accessible as MessageDeduplication.Status.
- Assistant
Build succeeded. Verifying the test results in more detail:
- Assistant
All four tests passed. Removing the temporary test and reviewing the final diff.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.