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 to understand the codebase and locate the relevant files for message deduplication.
- Assistant
Let me examine the main MessageDeduplication.java file to understand the issue.
- Assistant
Let me look at the MessageDeduplication.java file to understand the current code structure and identify the issue.
- Assistant
Let me read the rest of the file to understand the checkStatus method and the issue.
- Assistant
Let me continue reading to see the enable path and the issue described.
- Assistant
Now I can see the issue! In the
checkStatus()method, when enabling deduplication (lines 227-237), the code does not set the status toRecoveringbefore starting the async operation. Let me look at the full method to understand the logic better: - Assistant
Now I understand the issue. The problem is in the
checkStatus()method where when enabling deduplication (line 227), the status is not set toRecoveringbefore starting the async operation. Let me examine the exact lines around line 227: - Assistant
Now I understand the issue. Looking at the
checkStatus()method, the problem is on line 227-237:- When enabling deduplication (
status == Disabled || status == InitializedandshouldBeEnabled), the status is NOT set toRecoveringbefore starting the async operation - This means during async replay, another
checkStatus()call can see the status as stillInitializedorDisabledand start another replay - Also, when status is
Failed, it doesn't get retried even when deduplication should still be enabled
Let me also check how the
openCursorandreplayCursormethods work: - When enabling deduplication (
- Assistant
Let me also look for existing tests to understand how the deduplication is tested:
- Assistant
Now let me look at the existing tests:
- Assistant
Let me look at the existing tests:
- Assistant
Now I have a clear understanding of the issue. Let me create a reproduction script first:
- Assistant
Let me look at the exact issue more carefully:
- Assistant
Now I understand the issue clearly:
- When
checkStatus()enters the enable path (status ==InitializedorDisabled, shouldBeEnabled == true), it starts asyncopenCursor/replayCursorbut does NOT setstatus = Status.Recoveringfirst. - While the replay is in progress,
statusis stillInitializedorDisabled, so anothercheckStatus()call can start another replay. - Also, when
status == Failed, the condition doesn't match for retry, so it silently does nothing.
The fix needs to:
- Set
status = Status.Recoveringbefore starting the async replay - Include
Status.Failedin the enable path condition for retry
Let me also check the
exceptionallycallback more carefully: - When
- Assistant
Now I have a clear understanding. Let me also look at the
openCursormethod andreplayCursorto see how errors are handled: - Assistant
Now I understand the issue completely. Let me apply the fix:
- Assistant
Let me use the edit command directly instead:
- Assistant
Now I can see the exact issue. I need to make three changes:
- Add
Status.Failedto the condition to allow retry after failure - Set
status = Status.Recoveringbefore starting async operation to prevent duplicate replays - The
future.completeExceptionally(e)in the exceptionally handler is also problematic (it's callingcompleteExceptionallyon an already-failed future, which is a no-op but confusing)
Let me apply the fix:
- Add
- Assistant
Let me use the edit command correctly with the file parameter.
- Assistant
Let me use the correct syntax for the edit command.
- Assistant
The fix has been applied. Now let me verify the changes look correct and run the existing tests to make sure nothing is broken:
- Assistant
The diff looks correct. The changes are:
- Added
Status.Failedto the enable condition - allows retry when deduplication should be enabled but previously failed - Set
status = Status.Recoveringbefore starting async operation - prevents concurrentcheckStatus()calls from starting a second replay - Removed redundant
future.completeExceptionally(e)call - the future is already completed exceptionally at that point
Let me now run the existing deduplication tests to make sure nothing is broken:
- Added
- Assistant
Let me check the Maven project structure.
- Assistant
Let me check what the current directory is.
- Assistant
Let me check the repository structure.
- Assistant
It's a Gradle project. Let me check if there's a gradle wrapper.
- Assistant
Let me try running the tests with Gradle:
- Assistant
The build seems to be still running. Let me wait and check the output:
- Assistant
The tests show as UP-TO-DATE which means they were cached. Let me clean the build cache and run again to make sure:
- Assistant
The build is UP-TO-DATE, which means the changes were already compiled. Let me clean and rebuild to make sure the changes are properly compiled:
- Assistant
Good, the code compiles. Now let me run the MessageDuplicationTest:
- Assistant
All tests passed. Let me also run the other deduplication-related tests:
- Assistant
All tests pass. Let me review the fix one more time to make sure it's complete and correct:
- Assistant
Let me verify the full
checkStatus()method is correct by viewing the whole method: - Assistant
Let me verify the fix is complete and correct. Let me also run the TopicDuplicationTest which is another related test:
- Assistant
All tests pass. Let me verify the final diff one more time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.