Skip to content

refactor: deduplicate event rows in ClickHouse#843

Open
alexluong wants to merge 8 commits intomainfrom
fix/dedup-event-writes
Open

refactor: deduplicate event rows in ClickHouse#843
alexluong wants to merge 8 commits intomainfrom
fix/dedup-event-writes

Conversation

@alexluong
Copy link
Copy Markdown
Collaborator

@alexluong alexluong commented Apr 17, 2026

Summary

  • Skip event row insert for retry attempts (attempt_number > 1) across all logstore backends
  • Deduplicate ListEvent/ListAttempt results client-side: fetch, dedupe by ID, re-fetch if short of limit. Avoids LIMIT 1 BY / GROUP BY perf penalty on large result sets.
  • Fix metrics test dataset to use realistic attempt_number=1 for unique events

Test plan

  • TestEventDedup integration test (fanout, retries, legacy duplicates)
  • Conformance suite passes (chlogstore, pglogstore, memlogstore)

🤖 Generated with Claude Code

@alexluong alexluong force-pushed the fix/dedup-event-writes branch from 6c2a46a to 1319d1b Compare April 17, 2026 09:13
alexluong and others added 2 commits April 17, 2026 16:24
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]>
@alexluong alexluong force-pushed the fix/dedup-event-writes branch from 1319d1b to 1d39e24 Compare April 17, 2026 09:24
@alexluong alexluong changed the title fix: deduplicate event rows in ClickHouse (write + read path) refactor: deduplicate event rows in ClickHouse Apr 17, 2026
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]>
@alexluong alexluong force-pushed the fix/dedup-event-writes branch from 1d39e24 to 3696fab Compare April 17, 2026 09:29
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]>
Copy link
Copy Markdown
Contributor

@alexbouchardd alexbouchardd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're missing logic to handle the fan-out scenario, only one of the delivery task should be responsible for the event insert

@alexbouchardd
Copy link
Copy Markdown
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

@alexluong
Copy link
Copy Markdown
Collaborator Author

@alexbouchardd yes it's already applied for Attempts as well

alexluong and others added 4 commits April 17, 2026 21:43
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants