Skip to content

Schema migrations - #522

Open
aagbsn wants to merge 30 commits into
schema_sql_current_statefrom
schema_migrations
Open

aagbsn wants to merge 30 commits into
schema_sql_current_statefrom
schema_migrations

Conversation

@aagbsn

@aagbsn aagbsn commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Adds a base schema (current state), a migrations directory for changes to prod, and a script that ensures they stay in sync. schema.sql will be used for ooni/backend's CI fixtures, so that there is a single source of truth for database tables. Related is #520, for backing up (soon, also restoring from) clickhouse backups. For handling of some tables, e.g. the EmbeddedRocksDB types, this is convoluted, so migrating them to replicated types is desirable.

aagbsn added 30 commits October 23, 2026 08:39
Run every statement through clickhouse-format (24.8), the layout SHOW CREATE
and production's DDL use, so the per-table commits that follow show only real
changes. Comments are kept. No statement changes: formatting the old and new
files gives identical output.
Move each statement next to the others of its table, tables by name, ALTERs
after their CREATE. Only the order changes.
Add the columns production has: top_{dns,tcp,tls}_rule_id, probe_id and
analysis_rules_version, and its minmax indexes on measurement_start_time,
probe_cc and probe_asn.

Like every table from here on, it's now created as ooni.<table> with IF NOT
EXISTS, as production reports it.
Replicated at /clickhouse/{cluster}/tables/ooni/asnmeta on every replica,
as ooni/data#190 made it (and OOPRIV-181 restored on data1).
No change but the explicit index_granularity production reports.
Production's path has no /{shard} suffix. A replica created with the old path
wouldn't join production's table.
Production doesn't have it. Only the legacy citizenlab updater used it, and
ooni/data's updater (ooni/data#190) swaps citizenlab without it.
Production has none of the 17 *_obs_w_sum, *_w_sum, *_s_pos, *_s_neg,
last_ts and current_mean columns, and no partitioning. It has current_state,
and block_type in the CREATE instead of a later ALTER. float is now spelled
Float32, the same type.
Production's table is a plain ReplacingMergeTree(ts), not replicated or
partitioned, with *_current_state instead of *_obs_w_sum and *_w_sum. The
*_last_change and *_last_ts columns are data1's, the only replica with rows.
No change but the explicit index_granularity production reports.
anomaly, confirmed and msm_failure are LowCardinality(String) (ooni/backend#1279),
input has a bloom filter index (ooni/backend#1292), and is_verified is in the
CREATE. The ALTERs that added and then dropped nym, zkp_request, age_range and
msm_range are gone.

data1 and data3's definition: data2 alone also has
deduplicate_merge_projection_mode = 'rebuild'.
No change but the explicit index_granularity production reports.
An EmbeddedRocksDB table, so it isn't replicated: production has it on data1
and data2 only, and ON CLUSTER creates it, empty, on every node.
An EmbeddedRocksDB table, not replicated, like fingerprints_dns.
No change but IF NOT EXISTS.
A local MergeTree. data1's definition, the only replica with rows and with
failed_fastpath.
Replicated at production's path, msmt_feedback_new.
Replicated at production's path, obs_web_repl.
A Join table, local to each node.
Its inner table keeps production's Keeper path, so a new replica joins the
existing one. Production has the view on data1 and data3.
A header with the dump date, which replica's definition is used where they
differ, and what's left out: the updaters' *_tmp tables, msmt_feedback_old,
session_expunge (unused since ooni/backend#972) and the leftover inner table of
the dropped counters_test_list view. Also CREATE DATABASE, which the ooni.
names need on a new cluster.
ClickHouse fills in {database} when it creates a table, so in production
the paths are unchanged: /clickhouse/{cluster}/tables/ooni/<table>/{shard}.
A second database on the same cluster, e.g. a scratch or CI one, now gets
paths of its own instead of joining production's replicas.

counters_asn_test_list's path keeps production's name for its hidden table,
.inner_id.6a72ceec-…, with a comment saying why: a replica added later joins
production's table whatever UUID its view gets. With {table} there, it
would start a separate, empty path unless its CREATE copied the view's UUID.
Table names are unqualified, so clickhouse-client --database decides where
the file loads, and CREATE DATABASE moves to the header's instructions: it
has to exist on every node first, or the nodes without it fail with
UNKNOWN_DATABASE while the others create the table.

Tested on a two-node 24.8.6.70 cluster with Keeper. The node that receives
an ON CLUSTER query qualifies its names with the client's database before
passing it on, so every node uses that database, not its own default.
Loaded into ooni, this file gives exactly the definitions and Keeper paths
of the previous commit's file on both nodes; loaded again into ooni_test on
the same cluster, it shares no path with ooni, and its view reads
ooni_test's tables.
schema.sql stays the schema as it is now. A change to an existing cluster
goes in a numbered file in migrations/, applied once, in order, on one node.
000_baseline.sql is schema.sql as of this PR and is never edited; the
README says how to run, document and record a migration. schema.sql's
header points to it.
test/check.sh starts a one-node oonidata_cluster with Keeper at production's
version (24.8.6.70), loads schema.sql into one database and every migration,
in order, into another, and compares each table's definition and Keeper path
with the database name and UUIDs blanked. It takes about 10 seconds. CI runs
it when schema.sql, the migrations, the test or the workflow change.

It fails when a migration is missing, and when schema.sql changes without
one: both checked by hand before this commit.
The view kept its rows in a hidden table, .inner_id.<uuid>, sorted by
(probe_cc, probe_asn, input). It's a SummingMergeTree, so merges add up the
weeks of each key and keep one arbitrary week. Its reader, ooniprobe's
test-list/urls (common/prio.py), sums the current and previous week, so it
misses every count whose merged row says an older week, which in production
is most of them. Tested on 24.8: 2 rows from last week and 5 from this one
merge into one row of 7 that says last week.

The view now writes TO counters_asn_test_list_data, sorted by (probe_cc,
probe_asn, input, week), with a plain {database} Keeper path. It keeps its
name and column types, so the reader's query doesn't change. The table is
backed up and restored by name, like any other table, by the
clickhouse_backup role's "*" job; the view's backup holds only its
definition. Restore the table before the view: a view restored without its
table makes every insert into fastpath fail.

Migration 001 does it in production. The old rows are wrong, so it doesn't
copy them: it counts the two weeks the reader uses again from fastpath, from
the rows inserted before the new view existed (fastpath's update_time), and
the new view counts the rest. That also counts uploads that came more than
8 days late, which the view leaves out. It isn't a mutation. Queries on the
view fail between the DROP and the RENAME, for about a second; from then on
test-list/urls gets the corrected counts.

Tested on a two-node 24.8.6.70 cluster set up like production (the view on
one node, its rows already merged): the weeks come out right, a late upload
is counted once, later inserts are counted once on both nodes, and
test/check.sh passes.
@aagbsn
aagbsn marked this pull request as draft October 23, 2026 11:33
This was referenced Oct 28, 2026
@aagbsn
aagbsn force-pushed the schema_sql_current_state branch from 8506762 to 6ace7fb Compare November 5, 2026 18:02
@aagbsn
aagbsn marked this pull request as ready for review November 25, 2026 09:57
aagbsn added a commit that referenced this pull request Nov 25, 2026
…sql has them

#521's schema.sql defines counters_asn_test_list as production has it: a
materialized view without TO, whose rows are in a hidden
.inner_id.<view UUID> table. On #522's schema.sql the view had a TO table,
so the replicas test passed there and failed once #528 was based on #521:

- "has the 21 tables and views of schema.sql (22)": system.tables lists the
  hidden table too. It's no longer counted.
- "data4 has every replicated table": each node creates the view itself,
  with its own UUID, so the hidden table's name differs between nodes.
  Replicated tables are now compared by Keeper path, which is the same on
  every replica (schema.sql keeps production's path for the hidden table).

Replication itself was fine: "Keeper lists replica 04 for every replicated
table" and "data4 catches up" passed in the failing run.
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