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
Let me analyze this issue. The bug is about message deduplication recovery in what appears to be Apache Pulsar. The key problems are:
- The enable path starts dedup cursor replay without first changing the status to
Recovering - This allows another
checkStatus()call to start a second replay Failedstatus doesn't retry enable when deduplication should still be enabled
Let me first explore the repository structure to understand the codebase and find the relevant files.
- The enable path starts dedup cursor replay without first changing the status to
Assistant
The user is asking me to open a file. Let me use the correct syntax for the open command without the line_number parameter as a separate argument.
Assistant
I need to use the correct syntax. Let me check the function definition again - it seems the tool expects positional arguments. Let me try with just the file path as a positional argument.
Assistant
Let me see the full file to understand the complete flow, especially the
replayCursormethod:Assistant
Now I can see the issue clearly. Let me see the rest of the
replayCursormethod:Assistant
Now I understand the issue clearly. Let me analyze:
The Bug:
In
checkStatus()at line 227-237, when enabling deduplication:- The code checks
(status == Status.Disabled || status == Status.Initialized) && shouldBeEnabled - But it does NOT set
status = Status.Recoveringbefore starting the async replay - The
status = Status.Enabledis only set at the end ofreplayCursor()(line 275) - So while replay is in progress, status remains
DisabledorInitialized - A concurrent call to
checkStatus()can seeDisabled/Initializedand start another replay
The Fix:
- Set
status = Status.Recoveringbefore starting the async replay - Handle
Status.Failedby allowing retry when deduplication should still be enabled
Let me also look at the test file to understand the expected behavior better:
- The code checks
Assistant
Now let me look at the existing tests to understand the testing patterns:
Assistant
Now let me look at the full
checkStatusmethod more carefully to understand the fix I need to make:Assistant
Now I understand the issue and the fix needed. The problems are:
-
Line 227-237: When enabling deduplication, the status is NOT set to
Recoveringbefore starting the async cursor open and replay. So anothercheckStatus()call can seeDisabledorInitializedand start another replay. -
Missing Failed status handling: When status is
Failed, subsequentcheckStatus()calls don't retry enabling even whenshouldBeEnabledis true.
Here's my fix plan:
- Set
status = Status.Recoveringbefore starting the async operations in the enable path - Add handling for
Status.Failedto retry enabling when deduplication should still be enabled
Let me now implement the fix:
-
Assistant
Now I'll apply the fix. The key changes are:
- In the enable path (line 227-237), set
status = Status.Recoveringbefore starting async operations - Add handling for
Status.Failedto retry enabling when deduplication should still be enabled
- In the enable path (line 227-237), set
Assistant
Now I need to replace the enable path to:
- Include
Status.Failedin the condition - Set
status = Status.Recoveringbefore starting async operations
- Include
Assistant
Now I'll apply the fix using sed. I need to:
- Change
(status == Status.Disabled || status == Status.Initialized)to(status == Status.Disabled || status == Status.Initialized || status == Status.Failed)to allow retrying after failure - Add
status = Status.Recovering;before starting the async operations
- Change
Assistant
Now I need to add
status = Status.Recovering;before theopenCursorcall. Let me verify the change was applied and see the exact line:Assistant
Good, the first change is applied. Now I need to add
status = Status.Recovering;after the comment// Enable dedupingand before theopenCursorcall. Let me use sed to do this:Assistant
The changes look correct. Now let me verify the full
checkStatusmethod to ensure it's properly structured:Assistant
The changes look correct. Let me verify my changes by looking at the diff:
Assistant
The diff shows exactly the two changes I made:
-
Added
|| status == Status.Failedto the condition, so that when deduplication is inFailedstate but should still be enabled, a retry will happen. -
Added
status = Status.Recovering;before starting the async cursor operations, so that concurrentcheckStatus()calls will be blocked (they'll hit theif (status == Status.Recovering || status == Status.Removing)guard at line 162 and schedule a later retry).
Now let me run the existing tests to make sure the changes don't break anything:
-
Assistant
Let me first try to compile the changed file to make sure there are no syntax errors:
Assistant
Let me check the project structure to find the correct module name:
Assistant
command 0}
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && ls -la </tool_call>
Assistant
Let me try again with the correct syntax.
Assistant
This is a Gradle project. Let me compile using Gradle:
Assistant
The code compiled successfully. Now let me run the relevant tests to verify the fix doesn't break anything:
Assistant
The test passed (no errors at the end). Let me also run the BrokerMessageDeduplicationTest to make sure:
Assistant
The tests passed. Now let me also run the TopicDuplicationTest to make sure nothing is broken.
Assistant
All tests passed. Let me verify the final state of my changes and clean up:
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.