Skip to content

fix(web-analytics): use lightweight replica sync before partition swap#62537

Closed
andyzzhao wants to merge 1 commit into
masterfrom
andy/web-preagg-lightweight-sync
Closed

fix(web-analytics): use lightweight replica sync before partition swap#62537
andyzzhao wants to merge 1 commit into
masterfrom
andy/web-preagg-lightweight-sync

Conversation

@andyzzhao

Copy link
Copy Markdown
Contributor

Problem

The web analytics pre-aggregation assets follow the pattern: recreate staging table, bulk insert, SYSTEM SYNC REPLICA on every data node, then ALTER TABLE ... REPLACE PARTITION FROM staging. The sync exists so that whichever host later executes the swap has all staging parts locally.

A plain SYSTEM SYNC REPLICA waits for the replica's entire replication queue, which right after a bulk insert includes the freshly scheduled background merges. The job blocks on .result() for the slowest replica, so every run stalls for tens of seconds (sometimes minutes) of pure waiting in system.query_log, with zero bytes read and zero CPU. This pattern showed up as one of the largest "long wall time, no work" query groups on the cluster, hundreds of executions per day across the hourly assets.

The merges are irrelevant to the swap: REPLACE PARTITION FROM clones the staging part set as it exists, merged or not, and readers of AggregatingMergeTree tables must aggregate across parts anyway.

Changes

sync_partitions_on_replicas now issues SYSTEM SYNC REPLICA <table> LIGHTWEIGHT. Lightweight mode waits only for data-movement queue entries (GET_PART, ATTACH_PART, DROP_RANGE, REPLACE_RANGE, DROP_PART), i.e. exactly "all inserted parts are present on this replica", and skips waiting on merges and mutations. ClickHouse itself uses lightweight mode internally for the same parts-must-be-present barrier, and its source notes that plain sync differs precisely in also waiting for merges.

The STRICT syncs in the deletes DAG and overrides manager are deliberate (one asserts an empty queue afterwards) and are not touched.

How did you test this code?

I am an agent; automated tests only:

  • New test_sync_partitions_on_replicas_uses_lightweight_sync in products/web_analytics/dags/tests/test_web_preaggregated_utils.py, following the file's mock-cluster pattern (asserts the exact SQL executed per host and that the job waits on the futures).
  • Full test_web_preaggregated_utils.py passes (7 tests).

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Docs update

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

  • Claude Code session: found this while sweeping query_log_archive for queries with long wall time but near-zero CPU and no cluster-load signature; the staging-table sync was the single most frequent such pattern. Verified in the ClickHouse 26.3 source that lightweight sync waits exactly for part fetches and that the subsequent REPLACE PARTITION does not depend on merges.
  • Considered and rejected: leaving full sync but adding a timeout (still wastes the wait and adds a failure mode), and dropping the sync entirely (the swap-executing host genuinely needs all staging parts present, and the executing host is chosen arbitrarily).

The pre-aggregation job bulk inserts into a staging table, syncs every
data replica, then swaps partitions into the live table with REPLACE
PARTITION. The swap clones staging parts as they are, merged or not, so
the sync only needs replicated part fetches to finish. A plain SYSTEM
SYNC REPLICA also waits for the background merges the bulk insert just
scheduled, which blocks each run on the slowest replica for no benefit.
SYNC REPLICA LIGHTWEIGHT waits exactly for the data-movement entries.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@andyzzhao andyzzhao self-assigned this Jun 9, 2026
@assign-reviewers-posthog assign-reviewers-posthog Bot requested a review from a team June 9, 2026 20:05
@greptile-apps

greptile-apps Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "fix(web-analytics): use lightweight repl..." | Re-trigger Greptile

@andyzzhao andyzzhao marked this pull request as draft June 9, 2026 20:08
@andyzzhao andyzzhao removed the request for review from a team June 9, 2026 20:46
@andyzzhao andyzzhao closed this Jun 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.

1 participant