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 trace
MessageDeduplicationand its status-check callers, identify the async recovery transition and failure handling, then make the smallest state-machine fix. I’ll run focused tests (and broader relevant tests if practical), inspect the diff, and only then submit. - Assistant
The race is in the enable branch: it constructs the async chain while leaving the status unchanged. I’ll atomically set
Recoveringinside the existing synchronized section before opening the cursor, and includeFailedin the enable states so a subsequent policy check retries a failed enable. I’ll also verify whether completion/error callbacks can leave an incorrect state. - Assistant
The code now transitions to
Recoveringunder the lock beforeopenCursor, andFailedis eligible for a later enable retry. This preserves the existing behavior for policy-disabled topics while closing both reported gaps. I’ll compile the broker module and run the existing dedup-focused test class. - Assistant
Compilation and both existing dedup test classes pass. The focused tests don’t provide a deterministic delayed-replay race test in this checkout, but the synchronization/state transition is directly covered by the control flow: any second call sees
Recoveringand cannot enter enable. I’ll do a final diff/status review, then submit. - Assistant
Implemented the deduplication recovery race fix.
Changes:
- Set status to
Recoveringbefore starting asynchronous cursor recovery, preventing overlapping replay attempts. - Allow a later
checkStatus()call to retry enabling deduplication when status isFailedand policy still requires it.
Validation:
./gradlew :pulsar-broker:compileJava— passed.- Existing
MessageDuplicationTestandTopicDuplicationTest— passed. git diff --check— passed.
Checklist:
- Reproduced/analyzed the reported error path through the state machine. A dedicated delayed-replay reproduction was not present, but the race was confirmed from the implementation.
- Confirmed the fix prevents concurrent recovery by transitioning to
Recoveringunder synchronization. - Considered edge cases, including transient enable failures and subsequent retries.
- Ran affected existing tests successfully.
- Set status to
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.