ClickHouseSink
The Sink of one aggregate type into ClickHouse: each record one row of the schema's target table (ClickHouseSinkSchema.target, ClickHouseRows), from which ClickHouse's materialized views fill the current state and the events.
Batches: the sink subscription hands write the records of every aggregate's commits waiting together, at most maxBatch (batchSize): their runs, each aggregate's in version order (Sink.maxBatch). A write is one
INSERT(more only past batchSize, which the subscription does not hand it), one at a time per sink, and returns once ClickHouse has acknowledged its records.Paced (minWriteInterval, insertInterval): an insert starts no sooner than insertInterval after the previous one started, unless batchSize records are waiting, and the commits arriving meanwhile join it. Each insert makes a part in each table it reaches (the base table and the two views' tables, per month touched), so one insert per commit or per few would have ClickHouse make and merge thousands of parts a minute: unpaced, the inserts took 7 ms under the rc drill's load, 3,250 a minute of a few rows each, which a 2Gi ClickHouse refused (#6494). Paced, a node inserts at most once per interval per aggregate type under load; an idle sink inserts at once, and a backlog, in full batches, at full speed.
Idempotent by
ref@version: each insert carries a deduplication token, the record's id for a batch of one, else a digest of the batch's ids, so a retry of the same batch within the tables' deduplication window is dropped at once, the views' included. Paced, that window of 1,000 inserts covers about five minutes of them at one a second per node and type, where unpaced it covered some 18 s. Beyond it, and for a batch made up differently (a retry that other commits joined), the history tables areReplacingMergeTrees keyed byref@version(ClickHouseSinkSchema): a replayed record is merged away, and a read withFINALnever sees it twice.DDL: before its first write, and again once recheckEvery has passed since, the Sink creates or validates its tables as ddl says; a failure fails the write, which the subscription retries. The check repeated finds what changed since: a table dropped or altered fails the writes until it is as the Sink expects. A table or view is the Sink's when it has at least the columns the Sink expects, each of its type, and its comment says the Sink's format (ClickHouseSinkSchema.FORMAT_COMMENT); other columns, an operator's own or a later version's within the format, are no reason.
autocreates what is missing with it, adds to an existing table only the columns Wow added within the format (ClickHouseColumn.added) and the computed ones, and leaves it otherwise as it is, so one of another format, or of another column's type or a column of the format missing, is refused.The flat view across versions: during a rolling upgrade, writers of two state schemas run at once. The view's comment keeps the fingerprints of the definitions it was created with (FlatLineage): a writer replaces it only when its own definition is newer than every one before it, so an older instance that restarts leaves a newer one's view as it is, and validates it by existence only.
Catalog: once its tables are created or validated (at once with
ddl=none), the Sink appends the catalog entries of its tables to the database'swow_catalog(ClickHouseCatalog), each with the table's current uuid;autocreateswow_catalogwhen it is missing. A table whose uuid the account cannot read gets no row, and a failed write is logged and never fails the Sink's write.
Properties
The least time between the starts of two inserts: insertInterval.
Functions
Creates or validates the tables as ddl says, before the first write and again once recheckEvery has passed since the last time.