Skip to content

feat(onchain-analytics): check merge-key duplicates after every ingestion run - #79

Closed
thalescb wants to merge 5 commits into
masterfrom
feat/onchain-range-dup-check
Closed

thalescb wants to merge 5 commits into
masterfrom
feat/onchain-range-dup-check

Conversation

@thalescb

@thalescb thalescb commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Adds a merge-key duplicate check that runs after every daily ingestion, backfill and repair, scoped to the keys that run wrote.

This settles how often the duplicate check runs. Every ingestion checks its own range, and the full-history scan (verifyMergeKeyUniqueness) is only needed by hand after a migration or the first full-history load.

What changes

Before After
After a daily run, backfill or repair No duplicate check The keys the run wrote are counted over a window wider than the MERGE window. A repeated key, or a check that cannot run, fails the run (non-zero exit and the existing Slack failure alert).
Long backfill or repair n/a One statement per month that received rows, so each stays under the 10 GiB per-query ceiling and empty months in between are never scanned

The comment on twinWindowFor in window.ts explains why the window reaches past the MERGE window and how far. No schema change: it uses existing columns on the base tables and does not need the all-history views.

Cost

Estimated, to be confirmed with a dry run once RawLogs exists. The project's estimate for RawLogs is 719.4M rows for the full history of all four chains. Without Fuse, now out of scope, that is 584M rows, and that is a ceiling. Spread over about six years (~8M rows a month), at ~100 bytes read per row:

  • Daily run: ~5 GB across RawLogs and Transactions (~10 GB if recent months are twice the average), $0.03 to $0.06 a run.
  • Backfill or repair: ~7 GB per month written.
  • Manual full-history scan: ~88 GB, which needs MAX_BYTES_BILLED_PER_JOB raised for that one run.

Transactions is assumed to hold at most half as many rows as RawLogs.

The check runs in two steps. Finding the keys a run wrote filters to the run's own time span and block range. On RawLogs, block_number is a clustering column, so for a daily run that step should read about the day rather than the whole month. Transactions clusters by tx_hash first, so on that table it still reads about the month. Looking for a second copy of each key reads the run's month and the two before it, because a copy the MERGE missed can only sit outside its window. The figures above assumed the whole month for both steps, so treat them as an upper bound until the dry run after the trial load.

Two schema options would cut this further, and both are cheapest before RawLogs is created: storing tx_hash as 32 bytes instead of a hex string (~36% less read on every query), and an 8-byte merge-key fingerprint column (full-history scan of RawLogs from ~58 GB to ~9 GB). Neither is included here.

Validation

  • npm run verify:local in pipeline-v5: lint, typecheck, build check, 560 unit and 21 known-failure tests pass.
  • test/unit/range-duplicate-check.test.ts seeds an April copy of a key the run writes in June. It fails the run for daily, backfill and repair. Another test writes January 2025 and June 2026 and checks that only those two months are queried. Two more make a run throw after it has written rows, once in a daily run and once in a repair, and check the duplicate check still runs. The tests go red when the check is unwired, when the window is narrowed to the MERGE window, and when the month split is removed.
  • npm run gate:dup-check runs the check against live BigQuery in a disposable sandbox. It builds RawLogs and Transactions with the production partitioning, clustering and require_partition_filter, creates a real duplicate through a real MERGE (an April row, then a June write of the same key), and runs the check's actual statements. Run on Nov 4: on both tables the statements were accepted by the partition guard, the check reported 1 phantom row (2 keys held by 3 rows), a clean write passed, and both sandbox datasets were proven absent afterwards.
  • Not yet measured: bytes at production scale. The sandbox holds a few rows, so the dry run after the trial load gives the real number. Until then every statement carries the 10 GiB per-job cap, so an oversized check is refused before it bills and fails the run.
  • Nothing runs in production until daily ingestion is commissioned.

Start review at verifyWrittenRangeUniqueness and the run-scoped branch of duplicateReport in src/bq.ts, then twinWindowFor and windowsByMonth in src/window.ts. Most of the line count is the test file.

Refs #40.

…tion run

After a daily run, backfill or repair, count every copy of the keys the run wrote over a window wider than the MERGE window, one statement per written month. A repeated key, or a check that cannot run, fails the run. The full-history scan stays a manual step after a migration or the first full load.
@thalescb thalescb mentioned this pull request Oct 28, 2026
6 of 9 tasks
@thalescb
thalescb requested review from sirpy and a balanced review from Copilot October 28, 2026 21:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Duplicate verification can falsely pass when a concurrent writer overwrites the run ID after the merge lease is released, leaving duplicate rows undetected.

2 open issues
What changed in this PR

Adds post-ingestion duplicate-key checks to the onchain analytics pipeline, helping detect duplicate warehouse rows after daily runs, backfills, and repairs.

Changes:

  • Checks run-written keys across a wider lookup window, split into monthly queries.
  • Marks runs unsuccessful when duplicates are found or verification fails.
  • Extends simulator support and tests for duplicate detection, window boundaries, and repair behavior.
File Description
projects/​onchain-analytics/​pipeline-v5/​test/​unit/​range-duplicate-check.test.ts Tests detection, failure handling, monthly splitting, and lookup limits.
projects/​onchain-analytics/​pipeline-v5/​test/​helpers/​bq-simulator.ts Simulates run-scoped duplicate queries with separate windows.
projects/​onchain-analytics/​pipeline-v5/​src/​window.ts Adds wider lookup windows and calendar-month slicing.
projects/​onchain-analytics/​pipeline-v5/​src/​repair.ts Checks written ranges after gap re-reads.
projects/​onchain-analytics/​pipeline-v5/​src/​pipeline.ts Tracks writes and adds verification failures to run outcomes.
projects/​onchain-analytics/​pipeline-v5/​src/​bq.ts Implements run-scoped duplicate queries and aggregates monthly reports.

Review effort: Balanced

The duplicate check used to slice the span between a run's earliest and
latest written row into months, so a repair writing two far-apart months
checked every month between them. The run now records the months that
actually received rows, per table and chain, and checks only those.

Also documents, and pins with a test, how a key re-stamped by a second
writer before this run's check is still reported by that writer's check.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

A post-write coverage-read failure can bypass duplicate-key checks for repaired ranges, so cleanup must reliably drain written-range checks and include a regression test.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Check written ranges even when post-repair coverage loading fails

projects/​onchain-analytics/​pipeline-v5/​src/​repair.ts:197

If the post-repair loadCoverage call throws after processTarget has merged rows, runRepair exits before checking those writes. If the capture already closed the gap, a later repair can skip that range and leave its duplicate keys unchecked. Drain checkWrittenRanges in a finally around the gap-repair phase while preserving the original error, and add a regression test for a post-write coverage-read failure.

2 resolved since last review

Review effort: Balanced

Daily, backfill and repair runs could throw after rows were merged, for
example when a later coverage read or write failed, and exit before the
duplicate check. Those captures were already recorded complete, so no
later run would read the range again and its keys went unchecked.

runPipeline and runRepair now run checkWrittenRanges in a finally around
their capture loops. The original error still propagates. A regression
test covers each path and fails on the previous code.
@thalescb

thalescb commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author

Re the repair.ts:197 point from the second Copilot review: fixed in c94df1a. runRepair now runs checkWrittenRanges in a finally around the gap-repair loop, so a re-read that merged rows is checked even when the coverage read after it throws. The original error still propagates, since checkWrittenRanges records its own failures as outcomes instead of throwing.

runPipeline had the same gap for daily and backfill: a later chain throwing, for example on writing its coverage row, skipped the check for chains that had already written. Same fix there.

Added a regression test for each path. Both fail on the previous code with no duplicate check run, and pass now. npm run verify:local: lint, typecheck, build, 559 unit and 21 known-failure tests pass.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The new BigQuery SQL has only simulator validation, leaving partition-filter acceptance and representative daily/backfill scan estimates against the 10 GiB per-job ceiling unverified.

No open issues 🎉

Review effort: Balanced

…onths

The duplicate check's first step found the keys a run wrote by reading
every row of the run's calendar months. It now filters to the run's own
time span and block range per month. block_number is a clustering column
on RawLogs, so a daily run's check can skip most of the month there.
Transactions clusters by tx_hash first, so that step still reads about
the whole month on it. Both are to be measured with a dry run after the
trial load. Counting the copies keeps its wider window.
@thalescb

thalescb commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author

Following the review on the 5 GB figure: in 95ae326 the step that finds the keys a run wrote no longer reads the run's whole months. It filters to the run's own time span and block range. block_number is a clustering column on RawLogs, so there a daily run should read about the day. Transactions clusters by tx_hash first, so on that table this step still reads about the month. The second step, looking for another copy of each key, still reads the run's month and the two before it, because a copy the MERGE missed can only be outside its window.

None of this is measured yet. RawLogs doesn't exist, so I'll dry-run the check on real rows after the trial load and post the bytes here before anything is scheduled. Cost section of the description updated to match.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Simulator-only validation leaves BigQuery partition-filter acceptance and scan estimates below the 10 GiB per-job ceiling unverified, requiring a real dry run before rollout.

No open issues 🎉

Review effort: Balanced

Adds `npm run gate:dup-check`, which builds RawLogs and Transactions with
the production partitioning, clustering and require_partition_filter in
a disposable sandbox, creates a real duplicate through a real MERGE, and
runs the check's real statements. It must report the duplicate, pass a
clean write, and leave both sandbox datasets proven absent.

It does not measure bytes at production scale; that needs the trial load.
@thalescb

thalescb commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

On Copilot's note that the SQL was only checked against the simulator: 37520de adds npm run gate:dup-check, which runs the check against live BigQuery in a disposable sandbox with the production table settings, including require_partition_filter. It creates a real duplicate through a real MERGE and runs the check's actual statements. Ran it today: on RawLogs and Transactions the statements were accepted, the check found the 1 phantom row (2 keys, 3 rows), a clean write passed, and both sandbox datasets were proven gone after.

What it can't show is bytes at production scale, since the sandbox holds a few rows. That comes from the dry run after the trial load. Until then every statement carries the 10 GiB per-job cap, so an oversized check is refused before billing and fails the run.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the run-scoped merge-key duplicate checks.

No open issues 🎉

Review effort: Balanced

@thalescb

Copy link
Copy Markdown
Collaborator Author

Closing this. The duplicate check moves into the save itself: rows are only inserted, and a daily check over the previous day looks for duplicates and missing events (agreed with Hadar on Nov 11). The measurements on this PR fed that design.

@thalescb thalescb closed this Nov 12, 2026
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