Add defensive sequence number validation in replicated event persistence#2815
Draft
He-Pin wants to merge 1 commit intoapache:mainfrom
Draft
Add defensive sequence number validation in replicated event persistence#2815He-Pin wants to merge 1 commit intoapache:mainfrom
He-Pin wants to merge 1 commit intoapache:mainfrom
Conversation
Replace the FIXME (akka#29259) comment in handleExternalReplicatedEventPersist with a proper defensive validation of the replica sequence number before updating seenPerReplica. The validation logs a warning when the incoming event's originSequenceNr does not match the expected next sequence number for that replica. This covers the gap scenario where events from a replica may arrive out of order via the replication stream (onReplicatedEvent path). The event is still persisted for backward compatibility — rejecting it could stall the replication stream. Key design decisions (confirmed by cross-review from GPT-5.4 and Sonnet 4.6): - Only gap detection (seqNr > expected) can fire from current callers; onPublishedEvent filters both duplicates and gaps before calling. onReplicatedEvent filters duplicates via alreadySeen() but allows gaps. - Uses != check (not separate < and > branches) to avoid dead code. - Warning message includes the advancing seqNr to help operators diagnose potential event loss. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
afbf92c to
14f3ab7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
When
handleExternalReplicatedEventPersistreceives a replicated event, it updatesseenPerReplicawith the event'soriginSequenceNrwithout validating it matches the expected next sequence number. This was flagged as a FIXME referencing akka#29259.If events arrive out of order (e.g., seqNr 7 arrives when expecting 6),
seenPerReplicaadvances past the gap, and the missed event (seqNr 6) will later be filtered as "already seen" — permanently lost.Modification
originSequenceNr != expectedSeqNrfor the replicaonPublishedEvent: Rejects both duplicates and gaps before calling — only exact-match seqNr passesonReplicatedEvent: Filters duplicates viaalreadySeen(), but gaps may pass throughDesign Decision: Why
!=instead of separate<and>branchesAnalysis confirmed that:
< expectedSeqNr(duplicate) branch would be dead code — callers already filter duplicates> expectedSeqNr(gap) scenario can actually fire!=check avoids dead code while remaining defensive against future caller changesResult
References