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
The issue describes a race condition in the MessageDeduplication component where duplicate messages may be accepted after dedup recovery. Let me start by exploring the repository structure and finding the relevant code.
The issue mentions:
MessageDeduplicationclass defines aRecoveringstatus- The enable path starts dedup cursor replay without first changing the status to
Recovering - Another
checkStatus()call can still observeInitializedorDisabledand start a second replay - There's also an issue with retry gap after recovery failure
Let me first explore the repository structure and find the MessageDeduplication code.
Assistant
I found the MessageDeduplication.java file. Let me open it and examine the code to understand the issue.
Assistant
Let me open the MessageDeduplication.java file to understand the issue better.
Assistant
I need to see the full file to understand the issue better. Let me use bash to show me the specific section with the issue.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '1,250p' /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
Let me read the file properly using bash commands to see the relevant sections of the code.
Assistant
Now let me see the section where the enable path is handled (when status is Disabled or Initialized and shouldBeEnabled is true). This is the critical section that has the bug.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '210,280p' /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
Let me run the command properly to see the code section where the enable path is handled.
Assistant
Now I can see the issue clearly. Looking at lines 227-241:
java} else if ((status == Status.Disabled || status == Status.Initialized) && shouldBeEnabled) { // Enable deduping final var future = openCursor(managedLedger, PersistentTopic.DEDUPLICATION_CURSOR_NAME) .thenCompose(this::replayCursor); future.exceptionally(e -> { status = Status.Failed; log.error().exception(e).log("Failed to enable deduplication"); future.completeExceptionally(e); return null; }); return future; }The issue is that when the status is
DisabledorInitializedand we need to enable deduplication, the code starts the enable process (opening cursor and replaying) without first setting the status to . This means that if another call happens before the first replay completes, it will see or status and start another replay.Assistant
Let me search for the test files properly.
Assistant
Good. Now let me look at the MessageDuplicationTest.java to understand the testing patterns used.
<tool_call> <function=bash_terminal> <parameter=command> head -200 /pulsar/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/persistent/MessageDuplicationTest.java </tool_call>
Assistant
Let me run the command properly to view the test file.
Assistant
Now I have a good understanding of the issue. The problem is in the
checkStatus()method of theMessageDeduplicationclass. When enabling deduplication, the code starts the enable process (openCursor + replayCursor) without first setting the status toRecovering. This allows another concurrentcheckStatus()call to start another replay before the first one completes.Additionally, if deduplication fails (status becomes
Failed), subsequent checks will not retry enabling even when deduplication should still be enabled.Let me create the fix for this issue. The fix needs to:
- Set status to
Recoveringbefore starting the enable process (openCursor + replayCursor) - Handle the
Failedstatus case by retrying whenshouldBeEnabledis true
Let me first view the entire
checkStatusmethod to understand its structure better.<tool_call> <function=bash_terminal> <parameter=command> sed -n '155,250p' /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
- Set status to
Assistant
Let me run the bash command properly to view the code.
Assistant
Assistant
Let me try running the python script properly.
Assistant
The system doesn't have python3. Let me use sed or another approach to apply the fix. I'll need to manually apply the fix using sed or create a patch file.
Assistant
No python available either. Let me use the
editcommand or manually withsed.Assistant
Now I understand the exact lines I need to fix. The issue is on lines 226-237. I need to:
- Change line 226 to include
|| status == Status.Failedin the condition - Add
status = Status.Recovering;after the comment on line 227
Let me use the
editcommand to fix this:- Change line 226 to include
Assistant
Let me try the edit command with the proper format. I'll need to provide the line range and the replacement text properly.
Assistant
I need to first open the file using the special command, then use edit. But I realized the open command format was different from what I tried earlier.
Let me try opening the file first, then editing it.
Assistant
Let me look at the open command format again.
<tool_call> <function=bash_terminal> <parameter=command> open /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
Let me try the open command as a proper tool call.
Assistant
Let me verify the changes were applied correctly.
Assistant
I see the sed commands partially worked - the second comment and status line were added, but the first sed command to add
|| status == Status.Faileddidn't work. Let me check line 227 more carefully and fix it.<tool_call> <function=bash_terminal> <parameter=command> sed -n '227p' /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
The sed command didn't work because the condition is on a different line than expected. Let me check the exact line number again.
Assistant
The line is 227. Let me fix it by adding
|| status == Status.Failedto the condition.Assistant
Let me verify the changes were applied correctly.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '225,250p' /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
Let me check the sed command output properly.
Assistant
Assistant
Let me check if there's a gradle wrapper or build system in place.
Assistant
Good, there's a gradlew file. Let me try to compile and test the MessageDeduplication class to verify the fix.
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && ./gradlew :pulsar-broker:compileJava --no-daemon 2>&1 | tail -30 </tool_call>
Assistant
Let me run the gradle command properly.
Assistant
The code compiled successfully without any errors. The fix I applied is syntactically correct and the build was successful.
Now let me run the MessageDuplicationTest tests to make sure the fix doesn't break any existing functionality.
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && ./gradlew :pulsar-broker:test --tests "org.apache.pulsar.broker.service.persistent.MessageDuplicationTest" --no-daemon 2>&1 | tail -100 </tool_call>
Assistant
I need to run the gradle test command to verify my changes don't break existing tests.
Assistant
The tests passed. Let me also run the TopicDuplicationTest to make sure the fix doesn't break any related tests.
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && ./gradlew :pulsar-broker:test --tests "org.apache.pulsar.broker.service.persistent.TopicDuplicationTest" --no-daemon 2>&1 | tail -50 </tool_call>
Assistant
The tests passed. Let me run the TopicDuplicationTest to verify the fix doesn't break any related tests.
Assistant
The tests passed. Now let me also run some other deduplication-related tests to ensure the fix doesn't break anything.
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && ./gradlew :pulsar-broker:test --tests "Deduplication" --no-daemon 2>&1 | tail -50 </tool_call>
Assistant
Let me run the deduplication-related tests to ensure the fix works correctly.
Assistant
The test command timed out. Let me try a smaller subset of tests. Let me verify the fix by viewing the changes and making sure they are correct.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '165,250p' /pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java </tool_call>
Assistant
Let me view the changes I made to verify they are correct.
Assistant
The fix looks correct. Let me now analyze the changes:
-
Line 226: Changed the condition from
(status == Status.Disabled || status == Status.Initialized) && shouldBeEnabledto(status == Status.Disabled || status == Status.Initialized || status == Status.Failed) && shouldBeEnabled. This allows the code to retry enabling deduplication when the status isFailedbutshouldBeEnabledis true. -
Lines 227-229: Added a comment and
status = Status.Recovering;before starting the cursor open and replay. This ensures that the status is set toRecoveringbefore the asynchronous operations start, preventing concurrent recovery attempts.
Now let me verify the diff to make sure the changes 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
Let me run the git diff command to see the changes.
Assistant
Assistant
Let me run a command to find the test files related to MessageDeduplication.
Assistant
Let me run a quick test to verify my fix doesn't break existing functionality. I'll run the MessageDuplicationTest and a few other related tests.
Assistant
The tests passed. Let me also run the TopicDuplicationTest to make sure the fix doesn't break any related tests.
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && timeout 180 ./gradlew :pulsar-broker:test --tests "org.apache.pulsar.broker.service.persistent.TopicDuplicationTest" --no-daemon 2>&1 | tail -20 </tool_call>
Assistant
The test passed. Let me run the TopicDuplicationTest as well to verify the fix.
Assistant
The test passed (it was cached). Let me also run a quick check to see if the BrokerMessageDeduplicationTest passes.
<tool_call> <function=bash_terminal> <parameter=command> cd /pulsar && timeout 180 ./gradlew :pulsar-broker:test --tests "org.apache.pulsar.broker.BrokerMessageDeduplicationTest" --no-daemon 2>&1 | tail -30 </tool_call>
Assistant
Let me verify the fix is complete by reviewing the changes one more time.
Assistant
The fix looks correct. Let me summarize the changes:
-
Issue 1: Race condition during recovery - The original code started the cursor open and replay without first setting the status to
Recovering. This allowed concurrentcheckStatus()calls to start multiple recovery operations. The fix sets status toRecoveringbefore starting the recovery, which prevents concurrent recovery attempts because the initial check incheckStatus()will seeRecoveringstatus and schedule a retry. -
Issue 2: No retry when status is Failed - The original code didn't retry enabling deduplication when status was
FailedbutshouldBeEnabledwas true. The fix includesStatus.Failedin the condition, allowing the code to retry when deduplication should be enabled.
The fix is minimal and addresses both issues described in the bug report. Let me now submit the fix.
<tool_call> <function=submit> </tool_call>
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.