Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(clickhouse): gate schema convergence on native hclexp -exclude (#81174)

Paweł Szczurcd9acb38580b
diff --git a/posthog/clickhouse/hcl/README.md b/posthog/clickhouse/hcl/README.md
index 660588b0d84e..8800e495bc70 100644
--- a/posthog/clickhouse/hcl/README.md
+++ b/posthog/clickhouse/hcl/README.md
@@ -49,20 +49,22 @@ hcl/
`check.sh` is **offline** — it proves the HCL is internally consistent and that `golden/`/`sql/` are
fresh, but it never contacts a cluster, so it cannot tell whether the imperative migrations in
`posthog/clickhouse/migrations/` still produce the schema the HCL declares. That gap is how old
-migrations silently desynced the live OPS/LOGS schema from the HCL.
+migrations silently desynced the live schema from the HCL.
The convergence gate closes it, in **two steps** that run inside the multinode migration smoke
(`tools/infra-scripts/clickhouse-multinode/`, workflow `ci-clickhouse-multinode-migrations.yml`)
**after** `manage.py migrate_clickhouse`:
-1. **`dump-live.sh [outdir]`** — `hclexp introspect` each managed role's live node into
+1. **`dump-live.sh [outdir]`** — `hclexp introspect` each role's live node into
`<outdir>/<env>-<role>.hcl`, dropping unmanaged / transient objects via `exclude.hcl`. Needs the
cluster (a `--network host` container, or `HCLEXP_BIN` locally). Also writes
`<outdir>/hclexp-version.txt` (`hclexp -version`) recording the tool build that produced the dump —
informational provenance, not gated by `check-live.sh`.
-2. **`check-live.sh <dumpdir>`** — for each role, `hclexp diff -format json` the committed
- `golden/<env>/<role>.hcl` against the dump, drop the ignored operations (named_collections +
- `exclude.hcl` globs), and require nothing left. Offline — only needs `hclexp`.
+2. **`check-live.sh <dumpdir>`** — for each role, `hclexp diff -exclude exclude.hcl -format json` the
+ committed `golden/<env>/<role>.hcl` against the dump, and require an empty operation list.
+ The exclusion is native: `hclexp` drops named_collections (`object_types`) and unmanaged /
+ transient names (`patterns`) from both sides before diffing, so `check-live.sh` filters nothing
+ itself. Offline — only needs `hclexp`.
```bash
DUMP=$(bash posthog/clickhouse/hcl/dump-live.sh) # step 1 -> prints the dump dir
@@ -75,13 +77,16 @@ versa). Fix the migration to match the HCL, or — if the change is intended —
**enforced** (drift fails the smoke); export `VERIFY_LIVE_WARN=1` to make it informational while
reconciling a new role.
-Each managed role is compared against its `golden/local-multi/<role>.hcl`. The local LOGS node runs a
-partial/newer schema than the cloud logs nodes, so `local logs` composes a self-contained
-`roles/logs/local` (extracted from the live node) rather than the shared cloud layers.
+Every role `manifest.hcl` composes for the gate's env is dumped and gated — for `local-multi` that is
+each node the multinode stack runs, one per published port in `dump-live.sh`'s `ROLES`, compared
+against its `golden/local-multi/<role>.hcl` (`aux` is filed as `auxiliary`, see `golden_name` in
+`lib.sh`). The local LOGS node runs a partial/newer schema than the cloud logs nodes, so
+`local-multi logs` composes a self-contained `roles/logs/local` (extracted from the live node) rather
+than the shared cloud layers.
`node_roles` is **derived**: an object in `roles/shared/` appears in every node's composition →
-`node_roles` = every managed role (currently `[LOGS, OPS]`); an object under `roles/ops/` appears
-only in the ops nodes → `[OPS]`; one under `roles/logs/` → `[LOGS]`.
+`node_roles` = every role the manifest declares; an object under `roles/ops/` appears only in the ops
+nodes → `[OPS]`; one under `roles/logs/` → `[LOGS]`.
Per-node `{shard}` / `{replica}` stay as ClickHouse macros, so replicas collapse to one definition.
A cross-cluster Distributed proxy references a table on another cluster's composition; `check.sh`
diff --git a/posthog/clickhouse/hcl/check-live.sh b/posthog/clickhouse/hcl/check-live.sh
index 6d6d9b0f479c..539e2a0cef5d 100755
--- a/posthog/clickhouse/hcl/check-live.sh
+++ b/posthog/clickhouse/hcl/check-live.sh
@@ -1,15 +1,14 @@
#!/usr/bin/env bash
# Convergence gate, step 2 of 2: diff the live HCL dumps produced by dump-live.sh
-# against the committed golden per <env>-<role>, and require nothing but ignored
-# operations remain. Offline — needs only hclexp (via bin/hclexp), no cluster.
+# against the committed golden per <env>-<role>, and require the diff to come
+# back empty. Offline — needs only hclexp (via bin/hclexp), no cluster.
#
-# For each role it runs `hclexp diff -left golden -right <dump> -format json` and
-# drops the operations the gate intentionally ignores — named_collections (secret
-# Kafka broker config the schema golden never models) and objects whose name
-# matches an exclude.hcl glob (out-of-band-managed: real on prod but not created
-# by the local migrate path). Anything left is real drift: fix the migration to
-# match posthog/clickhouse/hcl/, or edit the HCL layer + rerun gen-golden.sh /
-# gen-sql.sh + add the migration. See README.md.
+# For each role it runs `hclexp diff -left golden -right <dump> -exclude
+# exclude.hcl -format json`. hclexp drops the excluded objects from both sides
+# itself — named_collections by object_type, out-of-band-managed and transient
+# objects by name glob — so everything the diff still reports is real drift: fix
+# the migration to match posthog/clickhouse/hcl/, or edit the HCL layer + rerun
+# gen-golden.sh / gen-sql.sh + add the migration. See README.md.
#
# Usage: check-live.sh [dumpdir] (dumpdir defaults to $LIVE_DUMP_DIR)
#
@@ -34,27 +33,14 @@ DUMPDIR="${1:-${LIVE_DUMP_DIR:?dump dir required (pass as arg1 or set LIVE_DUMP_
roles_lines="$(manifest_roles "$ENV")"
read -r -a ROLES <<< "${roles_lines//$'\n'/ }"
-# Object-name globs the gate ignores, parsed from exclude.hcl (the quoted glob
-# strings) — the same list dump-live.sh feeds hclexp -exclude, applied here to
-# the diff so golden-only out-of-band objects don't count as drift.
-GATE_IGNORE="$(grep -oE '"[^"]+"' "$EXCLUDE" 2>/dev/null | tr -d '"' | tr '\n' ' ')"
-
-# Read `hclexp diff -format json` on stdin, print the operations that count as
-# real drift (one per line), exit non-zero iff any remain. Drops named_collections
-# and objects matching a gate-ignore glob — both are workarounds for hclexp diff
-# lacking subset/exclude scoping (PostHog/chschema#75); remove once it lands.
-filter_drift() {
- # -c (not `- <<heredoc`) so stdin stays the piped JSON; globs arrive as argv.
+# Read `hclexp diff -format json` on stdin and report its operations one per
+# line, exit non-zero iff there are any. No filtering happens here: hclexp
+# already applied exclude.hcl to both sides, so every operation is drift.
+report_drift() {
+ # -c (not `- <<heredoc`) so stdin stays the piped JSON.
python3 -c '
-import sys, json, fnmatch
-globs = sys.argv[1:]
-ops = json.load(sys.stdin).get("operations", [])
-def ignored(o):
- if o.get("object_type") == "named_collection":
- return True
- name = o.get("object", "")
- return any(fnmatch.fnmatch(name, g) for g in globs)
-drift = [o for o in ops if not ignored(o)]
+import sys, json
+drift = json.load(sys.stdin).get("operations", [])
for o in drift:
db = o.get("database") or ""
obj = (db + "." + o["object"]) if db else o["object"]
@@ -63,7 +49,7 @@ for o in drift:
for line in (o.get("sql") or "").strip().splitlines():
print(" " + line)
sys.exit(1 if drift else 0)
-' "$@"
+'
}
rc=0
@@ -81,8 +67,7 @@ for role in "${ROLES[@]}"; do
fi
echo "== $ENV/$role: diff golden vs live dump =="
- # shellcheck disable=SC2086 # GATE_IGNORE is a deliberately word-split glob list
- if drift="$("$HCLEXP" diff -left "$golden" -right "$live" -format json | filter_drift $GATE_IGNORE)"; then
+ if drift="$("$HCLEXP" diff -left "$golden" -right "$live" -exclude "$EXCLUDE" -format json | report_drift)"; then
echo "no differences"
else
echo "DRIFT: $ENV/$role — migrations produced a schema that differs from the HCL golden"
diff --git a/posthog/clickhouse/hcl/dump-live.sh b/posthog/clickhouse/hcl/dump-live.sh
index 9a535b0a3693..0d882a751342 100755
--- a/posthog/clickhouse/hcl/dump-live.sh
+++ b/posthog/clickhouse/hcl/dump-live.sh
@@ -1,9 +1,10 @@
#!/usr/bin/env bash
-# Convergence gate, step 1 of 2: introspect the live OPS/LOGS nodes on a booted
-# multinode stack (see tools/infra-scripts/clickhouse-multinode/) into one HCL
-# dump per <env>-<role>. Step 2 (check-live.sh) diffs those dumps against the
-# committed golden — offline. Splitting keeps the cluster/network-dependent
-# capture separate from the deterministic comparison.
+# Convergence gate, step 1 of 2: introspect every node the booted multinode stack
+# runs (see tools/infra-scripts/clickhouse-multinode/) into one HCL dump per
+# <env>-<role> — the roles listed in ROLES below, one per published port. Step 2
+# (check-live.sh) diffs those dumps against the committed golden — offline.
+# Splitting keeps the cluster/network-dependent capture separate from the
+# deterministic comparison.
#
# Transient / unmanaged objects are dropped at introspect time via exclude.hcl.
#
diff --git a/posthog/clickhouse/hcl/exclude.hcl b/posthog/clickhouse/hcl/exclude.hcl
index 05d25aedefd4..902d327a2b56 100644
--- a/posthog/clickhouse/hcl/exclude.hcl
+++ b/posthog/clickhouse/hcl/exclude.hcl
@@ -1,14 +1,17 @@
-# Objects skipped when introspecting a live OPS/LOGS node for the convergence
-# gate (verify-live.sh). A name matching any glob (filepath.Match syntax) is
-# dropped before its DDL is parsed, so it neither lands in the introspected HCL
-# nor is compared against the golden. Patterns match both the bare name
-# ("events_main") and the db-qualified form ("posthog.events_main").
+# Objects the convergence gate skips on a live node. Both gate steps read this
+# one file: dump-live.sh passes it to `hclexp introspect -exclude` (a match is
+# dropped before its DDL is parsed, so it never lands in the dump) and
+# check-live.sh to `hclexp diff -exclude` (a match is dropped from both sides
+# before diffing, so a golden-only match is not drift either). Patterns are
+# filepath.Match globs against both the bare name ("events_main") and the
+# db-qualified form ("posthog.events_main"); object_types drops a whole kind.
#
-# Two classes live here:
-# 1. Transient objects (atomic-replace temporaries, migration/backfill scratch,
+# Three classes live here:
+# 1. Whole object kinds the goldens never model (object_types).
+# 2. Transient objects (atomic-replace temporaries, migration/backfill scratch,
# backups) — never part of the managed schema.
-# 2. Cross-cluster proxies present on the node but intentionally NOT authored in
-# the OPS/LOGS golden — they belong to another role's managed set (e.g. the
+# 3. Cross-cluster proxies present on the node but intentionally NOT authored in
+# that role's golden — they belong to another role's managed set (e.g. the
# events_* distributed proxies the OPS node carries so it can query the main
# cluster). These are the same names check.sh lists in its validate SKIP.
#
@@ -16,6 +19,9 @@
# node has that the golden intentionally omits goes here, with a one-line reason.
exclude {
+ # Secret Kafka broker/credential config; never modeled in the goldens.
+ object_types = ["named_collection"]
+
patterns = [
# --- transient (ClickHouse atomic CREATE-OR-REPLACE / EXCHANGE) ---
"_tmp_replace_*",
@@ -32,7 +38,7 @@ exclude {
"*_backfill",
# --- cross-cluster proxies carried by the node but owned elsewhere ---
- # Distributed proxies into the main event cluster; not part of OPS/LOGS.
+ # Distributed proxies into the main event cluster; owned by the data role.
"events_main",
"events_recent",
diff --git a/posthog/clickhouse/hcl/manifest.hcl b/posthog/clickhouse/hcl/manifest.hcl
index b5b3789dccbd..314e006ed6ce 100644
--- a/posthog/clickhouse/hcl/manifest.hcl
+++ b/posthog/clickhouse/hcl/manifest.hcl
@@ -15,15 +15,14 @@
# `shared` = all envs of that role and `prod` = both prod envs (e.g. the OPS metrics
# suite is prod-only but env-identical).
#
-# Both managed roles (OPS, LOGS) are modeled for all three cloud envs (dev, prod-us,
-# prod-eu); OPS additionally has `local`. OPS carries the env differences; LOGS carries
-# the shared managed subset (env-identical, but verified per env for fidelity).
+# OPS and LOGS are modeled for all three cloud envs (dev, prod-us, prod-eu). OPS carries
+# the env differences; LOGS carries the shared managed subset (env-identical, but verified
+# per env for fidelity). The satellite roles are modeled where a node of that role exists
+# to model: every role the multinode stack runs has a `local-multi` env block, and the
+# convergence gate (dump-live.sh + check-live.sh) dumps and gates all of them.
#
-# SCOPE: the pilot is OPS + LOGS. The other roles (data/endpoints/aux/ai_events/sessions)
-# also host the shared query_log_archive path + custom_metrics, but are intentionally
-# left out for now, so node_roles for shared objects derives to [OPS, LOGS] rather than
-# every role. To bring a role under management, add its env block and regenerate the
-# golden from a host of that role (codegen/README has the extraction).
+# SCOPE: to bring a further role or env under management, add its env block and regenerate
+# the golden from a host of that role (codegen/README has the extraction).
role "ops" {
env "local-multi" { layers = ["roles/shared", "roles/coshared/custom_metrics", "roles/ops/shared", "roles/ops/local"] }
diff --git a/tools/infra-scripts/clickhouse-multinode/multinode-migration-smoke b/tools/infra-scripts/clickhouse-multinode/multinode-migration-smoke
index 80f19686ba6f..8dcb33372986 100755
--- a/tools/infra-scripts/clickhouse-multinode/multinode-migration-smoke
+++ b/tools/infra-scripts/clickhouse-multinode/multinode-migration-smoke
@@ -203,12 +203,12 @@ echo "==> Verifying per-node table layout..."
python tools/infra-scripts/clickhouse-multinode/verify-multinode-clickhouse.py
# Convergence gate: the migrations just applied must reproduce the declarative
-# HCL golden for the managed OPS/LOGS roles. Two steps — (1) introspect those
-# nodes into HCL dumps, (2) diff the dumps against posthog/clickhouse/hcl/golden/.
+# HCL golden for every role this stack runs. Two steps — (1) introspect each
+# node into an HCL dump, (2) diff the dumps against posthog/clickhouse/hcl/golden/.
# Enforced by default (drift fails the smoke); export VERIFY_LIVE_WARN=1 to make
# it informational (report drift but pass) while reconciling.
SMOKE_STAGE="dump_live_hcl"
-echo "==> Dumping live OPS/LOGS schema..."
+echo "==> Dumping live schema from every node..."
LIVE_DUMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/ch-live-dump.XXXXXX")"
bash posthog/clickhouse/hcl/dump-live.sh "$LIVE_DUMP_DIR" >/dev/null