refactor: deduplicate event rows in ClickHouse#843
Open
Conversation
6c2a46a to
1319d1b
Compare
Gate event insertion on AttemptNumber <= 1 in InsertMany for chlogstore, pglogstore, and memlogstore. Retry attempts carry identical event data, so re-inserting is wasteful (and creates duplicates in ClickHouse's ReplacingMergeTree before background merge). Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Comprehensive test covering fanout (1 event → 3 destinations), retries (3 attempts for same event), and legacy duplicate rows injected via raw batch inserts. Verifies both write-path row counts and read-path ListEvent uniqueness via LIMIT 1 BY event_id. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1319d1b to
1d39e24
Compare
Each entry in the dataset is a unique event, not a retry — so attempt_number should be 1. The old cycling (i%4+1) was unrealistic and incompatible with the write-path fix that skips event inserts for AttemptNumber > 1. Updated derived totals and assertions: - first_attempt_count: 75 → 270 (300 - 30 manual) - retry_count: 225 → 0 - avg_attempt_number: 2.5 → 1.0 - by_attempt_number: 4 buckets × 75 → 1 bucket × 300 - filter_by_attempt_number=1: 75 → 300 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1d39e24 to
3696fab
Compare
Remove LIMIT 1 BY event_id/attempt_id from ListEvent/ListAttempt queries. Instead, deduplicate results in Go after fetching. If duplicates reduce the result count below the requested limit, the loop advances the cursor and fetches more rows. This avoids the performance penalty of LIMIT 1 BY (equivalent to GROUP BY) on large result sets while still hiding duplicates from unmerged ReplacingMergeTree parts. In practice, duplicates are rare after the write-path fix, so the loop almost never executes more than once. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Contributor
alexbouchardd
left a comment
There was a problem hiding this comment.
We're missing logic to handle the fan-out scenario, only one of the delivery task should be responsible for the event insert
Contributor
|
@alexluong The same dedup logic should apply for attempts, really, we have the same problem there we just haven't seen it because duplicates are rarer |
Collaborator
Author
|
@alexbouchardd yes it's already applied for Attempts as well |
When duplicates cause the first batch to yield fewer unique items, the next batch appends all its unique items without checking the limit — returning more than requested. This test exposes the bug by inserting 1 duplicated event + 3 unique events and requesting limit=2 via fetchAndDedup directly (bypassing pagination.Run which masks the issue with its own truncation). Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The dedup loop appends all unique items from each batch before rechecking the limit, so the last batch can overshoot. Truncate the result slice before returning. pagination.Run already truncates downstream, but fetchAndDedup should be correct on its own. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Add cursorPosition() methods to eventWithPosition and attemptRecordWithPosition so the cursor format string lives in one place. Previously getCursorPos lambdas duplicated the format from Cursor.Encode — if one changed, the dedup loop would silently advance to invalid cursor positions. Also update test comments/messages that still referenced LIMIT 1 BY (removed in the previous client-side dedup commit). Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Prevents runaway queries against pathological data with extreme duplication. In normal operation the loop executes once; with the write-path fix, duplicates only arise from unmerged ReplacingMergeTree parts. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
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.
Summary
attempt_number > 1) across all logstore backendsLIMIT 1 BY/GROUP BYperf penalty on large result sets.attempt_number=1for unique eventsTest plan
TestEventDedupintegration test (fanout, retries, legacy duplicates)🤖 Generated with Claude Code