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 start by exploring the repository structure to understand the codebase and find the relevant files related to message deduplication.
Assistant
Now let me look at the main MessageDeduplication.java file to understand the issue better:
<tool_call> <function=bash_terminal> <parameter=command> cat /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
I need to use the bash_terminal function to run the command. Let me try again.
Assistant
Let me view the full file to see the complete
checkStatus()method and thereplayCursormethod:<tool_call> <function=bash_terminal> <parameter=command> head -250 /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
Let me use the bash_terminal function correctly this time to get more lines from the file.
Assistant
Now I can see the issue clearly. Looking at lines 212-220, when enabling deduplication (status is
DisabledorInitializedandshouldBeEnabledis true), the code directly starts the async cursor open and replay without first setting the status toRecovering. This allows anothercheckStatus()call to also enter this enable path and start a second replay.The fix should be to set
status = Status.Recovering;before starting the async operations when enabling deduplication.Let me also look at what happens after the replay completes to understand the complete flow:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '240,320p' /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
I need to use the bash_terminal function correctly. Let me try again.
Assistant
Now I understand the issue completely:
-
In
checkStatus(), when status isDisabledorInitializedandshouldBeEnabledis true (lines 212-220), the code starts the async cursor open and replay without first settingstatus = Status.Recovering. -
The
replayCursormethod setsstatus = Status.Enabledat the end (line 275), but there's a race window between starting the async operations and whenstatusbecomesRecovering. -
If another
checkStatus()call happens while the first replay is in progress, it can also enter the enable path and start a second replay because the status is stillDisabledorInitialized. -
Additionally, when status is
Failed, there's no retry logic - the code doesn't check ifshouldBeEnabledis true and try to re-enable.
The fix should:
- Set
status = Status.Recoveringbefore starting the async cursor open operation when enabling deduplication.
-
Assistant
I need to use bash_terminal function properly to run the command.
Assistant
The grep didn't find any matches, but the command had a non-zero exit code which might just mean no files matched. Let me try a different approach.
Assistant
Now let me understand the exact fix needed. Looking at the code again:
- In the enable path (lines 212-220), we need to set
status = Status.RecoveringBEFORE starting the async operations - We also need to add retry logic when status is
Failed
Let me first look at the exact lines I need to modify:
- In the enable path (lines 212-220), we need to set
Assistant
Now I can see the exact code block. The fix should:
- Set
status = Status.Recovering;before starting the async cursor open operation - Add a condition to check for
status == Status.Failedand retry whenshouldBeEnabledis true
Let me see the full else-if block for enabling:
- Set
Assistant
Now I understand the issue completely. The fix requires:
- Setting
status = Status.Recovering;before starting the async cursor open operation in the enable path - Adding a condition to handle
status == Status.Failedand retry whenshouldBeEnabledis true
Let me implement the fix:
- Setting
Assistant
I need to apply the patch differently. Let me use the edit command directly instead of patch.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '223,232p' /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
I keep forgetting to use the bash_terminal function properly. Let me use it correctly this time.
Assistant
Now I can see the exact code that needs to be modified. The line at position 227 is:
} else if ((status == Status.Disabled || status == Status.Initialized) && shouldBeEnabled) {And immediately after the comment "Enable deduping", we need to add
status = Status.Recovering;.I'll use the edit function to make the changes.
Assistant
Now I can see the exact code. I need to:
- Modify the condition on line 226 to also check for
Status.Failed - Add
status = Status.Recovering;after the comment on line 227
Let me make the edit:
- Modify the condition on line 226 to also check for
Assistant
I see there's a duplicate line 232. Let me fix that:
Assistant
There's a duplicate line. Let me fix it:
Assistant
Now the changes look correct. Let me verify the fix by looking at the modified section:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '218,245p' /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
I need to use the bash_terminal function properly to run the command.
Assistant
The fix looks correct. Now I've made two key changes:
- Added
|| status == Status.Failedto the condition to allow retry when deduplication was previously failed - Added
status = Status.Recovering;before starting the async cursor open operation
Now let me verify the fix compiles and run the existing tests:
- Added
Assistant
Let me try a different approach to run the tests:
<tool_call> <function=bash_terminal> <parameter=command> ls /pulsar </tool_call>
Assistant
I need to use bash_terminal to execute commands. Let me check the project structure first.
Assistant
This is a Gradle-based project. Let me try to build and test using Gradle:
Assistant
I need to actually run a command, not just describe my plan. Let me compile and test the changes.
Assistant
It seems the gradle command is running but it skipped the tests with
-x test. Let me try to compile the code first to make sure there are no compilation errors, then run the actual tests.Assistant
I see there's a compilation error at line 244: "missing return statement". This suggests that there may be an issue with my change. Let me look at the code around that line to see what's happening.
Assistant
The code looks fine. The error message mentions line 244, and it says "missing return statement". However, I don't see what could be causing this. Let me check the full method to see if there's a syntax issue.
Let me look at the full
checkStatusmethod:Assistant
I see the issue! Look at lines 193-195. The second
ifblock (the one that handles disabling deduplication when status isEnabledand!shouldBeEnabled) is missing areturn future;at the end. My edit may have affected the structure. Let me check what the original code looked like.Wait, looking at line 225, the closing brace
}, null);doesn't havereturn future;before it. That's the issue - the code at line 225 should havereturn future;before the closing brace.But wait, let me check the original code again by looking at the git history or by examining the issue more carefully. Looking at the code:
if (status == Status.Enabled && !shouldBeEnabled) { // Disabled deduping CompletableFuture<Void> future = new CompletableFuture<>(); status = Status.Removing; managedLedger.asyncDeleteCursor(..., new DeleteCursorCallback() {...}, null); } else if ...The
ifblock doesn't returnfuture- this looks like it was the original issue! The original code was missingreturn future;at line 225.Assistant
I need to use the bash_terminal function to run the git diff command.
Assistant
The git diff command returned with exit code 0 but no output. This means my changes are staged. Let me see what changes were made.
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && git status </tool_call>
Assistant
I need to use bash_terminal function properly to run the git status command.
Assistant
Let me check the actual changes I made.
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && git diff pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
I need to use bash_terminal to run git diff. Let me try that.
Assistant
I see that my edit changed the structure incorrectly. The original code had
return future;on line 226, and my edit accidentally removed it when I replaced lines 226-229. Let me fix this by restoring thereturn future;statement.Looking at the git diff:
- Original line 226:
return future; - Original line 227:
} else if ((status == Status.Disabled || status == Status.Initialized) && shouldBeEnabled) {
After my edit:
- Line 226:
} else if ((status == Status.Disabled || status == Status.Initialized - Line 227:
|| status == Status.Failed) && shouldBeEnabled) { - Line 228:
// Enable deduping - Line 229:
status = Status.Recovering;
I need to add back the
return future;for the previousifblock (the one that handles disabling deduplication when status isEnabledand!shouldBeEnabled). Let me look at the current state of the code and fix it.<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && git checkout -- pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
- Original line 226:
Assistant
Let me first restore the file and then make the correct edit.
Assistant
Now let me view the original code again to make the correct edit:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '218,235p' /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
Let me use the bash_terminal function to run the sed command to view the original code.
Assistant
Now I can see the original code correctly. The
return future;at line 226 is correct. I need to:- Modify line 227 to add
|| status == Status.Failedto the condition - Add
status = Status.Recovering;after the comment on line 228
Let me make the correct edit:
<tool_call> <function=bash_terminal> <parameter=command> edit --file /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java 227:231 << 'EOF' } else if ((status == Status.Disabled || status == Status.Initialized || status == Status.Failed) && shouldBeEnabled) { // Enable deduping status = Status.Recovering; final var future = openCursor(managedLedger, PersistentTopic.DEDUPLICATION_CURSOR_NAME) .thenCompose(this::replayCursor); EOF </tool_call>
- Modify line 227 to add
Assistant
I need to use the bash_terminal function to run the edit command.
Assistant
The changes look correct. Let me verify by checking the diff and then compiling the code:
Assistant
The diff looks mostly correct, but I see there might be an issue with line 233. Let me check the full code around that area:
Assistant
I see that the
future.exceptionally(e -> {line was accidentally removed. Let me restore the file and make a more careful edit:Assistant
Let me view the original code first and then use sed to make the changes more precisely.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '225,240p' /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
Let me try to run the command using bash_terminal function properly.
Assistant
I see the original code. Now I need to make two changes:
- Change line 226 to also include
|| status == Status.Failed - Add
status = Status.Recovering;after line 227 (the comment "// Enable deduping")
Let me use sed to make these changes:
<tool_call> <function=bash_terminal> <parameter=command> sed -i '226s/(status == Status.Disabled || status == Status.Initialized)/(status == Status.Disabled || status == Status.Initialized || status == Status.Failed)/' /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
- Change line 226 to also include
Assistant
I need to use bash_terminal to run the sed command. Let me try that.
Assistant
Now let me add the
status = Status.Recovering;line after line 227:Assistant
Now let me verify the changes:
Assistant
I see the changes aren't correct. Let me fix this by restoring the file and using the correct approach:
Assistant
Let me view the exact original lines again and make a more precise edit:
Assistant
Let me use sed to make both changes properly. I'll first modify line 227 to add the
Status.Failedcheck:Assistant
Now let me add the
status = Status.Recovering;line after line 228 (the comment "// Enable deduping"):Assistant
Now let me verify the changes:
Assistant
The changes look correct now! Let me verify by checking the git diff:
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && git diff pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
I need to use the bash_terminal function properly to run the git diff command.
Assistant
Now let me compile the code to make sure there are no compilation errors:
Assistant
The compilation was successful! Now let me run the existing tests to make sure my changes don't break anything:
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && ./gradlew :pulsar-broker:test --tests "org.apache.pulsar.broker.service.persistent.MessageDuplicationTest" 2>&1 | tail -50 </tool_call>
Assistant
Let me run the command properly using bash_terminal.
Assistant
The test completed successfully. Let me also run the TopicDuplicationTest to make sure the changes don't break other deduplication-related tests:
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && timeout 300 ./gradlew :pulsar-broker:test --tests "org.apache.pulsar.broker.service.persistent.TopicDuplicationTest" 2>&1 | tail -50 </tool_call>
Assistant
I need to use the bash_terminal function properly.
Assistant
The tests passed successfully. Let me also run the BrokerMessageDeduplicationTest to make sure the fix works:
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && timeout 300 ./gradlew :pulsar-broker:test --tests "org.apache.pulsar.broker.BrokerMessageDeduplicationTest" 2>&1 | tail -50 </tool_call>
Assistant
The command didn't run because I forgot to use bash_terminal. Let me run it again.
Assistant
All tests passed! Let me also run the OneWayReplicatorDeduplicationTest to make sure the fix doesn't break any replication-related deduplication functionality:
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && timeout 300 ./gradlew :pulsar-broker:test --tests "org.apache.pulsar.broker.service.OneWayReplicatorDeduplicationTest" 2>&1 | tail -30 </tool_call>
Assistant
I need to use the bash_terminal function to run the command properly.
Assistant
The test timed out. Let me just verify that my fix is correct by looking at the code once more and then run a few more quick tests to ensure nothing is broken.
Let me also run the DeduplicationDisabledBrokerLevelTest:
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && timeout 120 ./gradlew :pulsar-broker:test --tests "org.apache.pulsar.broker.service.DeduplicationDisabledBrokerLevelTest" 2>&1 | tail -30 </tool_call>
Assistant
Let me use bash_terminal to run the command.
Assistant
The test passed successfully. Let me now verify the final state of the changes and review them one more time to ensure they are correct.
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && git diff pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
I need to use the bash_terminal function to run the git diff command.
Assistant
The changes look correct. Let me summarize what the fix does:
-
Added
status == Status.Failedto the condition: This allows the deduplication to be re-enabled even if it previously failed. Without this fix, if enabling deduplication failed transiently, subsequent calls tocheckStatus()would not retry enabling deduplication even if the topic policy still required it. -
Added
status = Status.Recovering;before starting the async cursor open operation: This is the key fix for the race condition. By setting the status toRecoveringbefore starting the async operations, we ensure that any subsequent calls tocheckStatus()will see the status asRecoveringand will schedule themselves to retry later (via the existing check at the beginning ofcheckStatus()) instead of starting another concurrent replay.
Now let me verify that all the related tests pass by running a broader set of deduplication-related tests:
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && timeout 180 ./gradlew :pulsar-broker:test --tests "Deduplication" 2>&1 | tail -30 </tool_call>
-
Assistant
Let me use bash_terminal to run the command.
Assistant
The test timed out or was terminated. This is expected as some tests may take a long time to run. Let me verify the fix once more by reviewing the complete
checkStatus()method to ensure all edge cases are handled correctly.<tool_call> <function=bash_terminal> <parameter=command> sed -n '155,245p' /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
I need to use bash_terminal to run the sed command to view the checkStatus() method.
Assistant
The fix looks correct. Let me verify that the changes handle all the edge cases mentioned in the issue:
-
Race condition fix: By setting
status = Status.Recoveringbefore starting the async cursor open operation, subsequent calls tocheckStatus()will see the status asRecoveringand will schedule themselves to retry later instead of starting another concurrent replay. This is correctly implemented at line 229. -
Retry after failure: By adding
|| status == Status.Failedto the condition, the code can retry enabling deduplication even if it previously failed. This is correctly implemented at line 227. -
Existing status checks: The check at the beginning of
checkStatus()(lines 162-165) will catch any concurrent status checks when the status isRecoveringorRemovingand schedule them to retry later.
The fix addresses both issues mentioned in the issue description:
- Deduplication now enters
Recoveringbefore starting recovery replay. - Concurrent status checks won't start another recovery while replay is in progress.
-
Assistant
I need to use the submit command to submit my changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.