# libch
libch runs ClickHouse's insert and merge code as explicit, short-lived operations on a tenant's disk, so one pool of workers serves a very large number of tenants. The disks are [BigHouse](https://github.com/danthegoodman1/BigHouse) `inodedb` disks: MergeTree tables whose blobs live in S3 and whose file tree lives in [InodeDB](https://github.com/danthegoodman1/inodedb). A disk libch writes is a stock BigHouse disk: read-only ClickHouse servers query it, and a stock writer can take it over, at any time.
A Rust crate drives a worker program built from the BigHouse fork of ClickHouse. Each operation starts from the tenant's key and the state in InodeDB, does one thing, commits and exits:
| Operation | What it does |
| --- | --- |
| `insert` | Commits a batch to a table and to every materialized view's target, once, however often it is sent under its token. |
| `plan` | Proposes merges with ClickHouse's own selector. |
| `merge` | Merges named parts into one. |
| `housekeep` | Removes what merges and dead workers leave behind. |
| `describe` | Lists a tenant's tables and parts as the worker's engine sees them. |
| `share` | Hands a disk back to operations once the tenant's exclusive writer has stopped. |
## Why operations
Writing a tenant through stock ClickHouse means booting a process for each batch or keeping one running for every tenant. A stock process loads every part of every table it attaches, so its boot costs many times the insert and grows with the table. **A libch operation's boot and insert cost about the same at any table size**, because a worker loads none of a table's parts: 38 ms of CPU through a worker of its own, and 14 to 15 ms through a forking worker, from 10 parts a table to 10,000.
<picture>
<source media="(prefers-color-scheme: dark)" srcset="docs/insert-cpu-dark.svg">
<img alt="CPU for one 1,000-row insert by parts a table: a stock server and clickhouse local grow about linearly, to 1,039 and 913 ms at 500 parts, while libch stays at 38 ms with a worker per operation and 14 to 15 ms with a forking worker." src="docs/insert-cpu.svg">
</picture>
| Parts a table | Insert into a running writer | Stock server: boot, insert, stop | `clickhouse local` | libch, a worker per operation | libch, a forking worker |
| --- | --: | --: | --: | --: | --: |
| 10 | 5.8 ms | 231 ms (40×) | 145 ms (25×) | 38 ms (7×) | 13.7 ms (2.4×) |
| 100 | 5.8 ms | 419 ms (72×) | 317 ms (55×) | 38 ms (7×) | 13.7 ms (2.4×) |
| 500 | 5.3 ms | 1,039 ms (196×) | 913 ms (172×) | 38 ms (7×) | 15.2 ms (2.9×) |
Each figure is the CPU for one 1,000-row insert through a projection and a materialized view, with its multiple of the first column: the same insert into a writer that is already running. The stock processes run with background merges off, as a process that serves one batch would. A forking worker starts the engine once and serves each operation in a child it forks. A stock server holds 145 to 205 MB and over 200 threads and answers in 259 to 361 ms; a worker holds 30 MB and 20 threads and answers in 18 to 22 ms.
A worker also builds only the tables its operation reaches. [BENCHMARKS.md](BENCHMARKS.md) has the method and every operation's cost.
## The recipe
```rust
let pool = Pool::new(WorkerCommand::clickhouse("/usr/bin/clickhouse"), PoolOptions { waiting: 8, concurrency: 16 })?;
let keys = KeyCache::new(admin, KeyPolicy::default())?;
let writer = pool.writer(&tenant, keys.key(&tenant).await?);
writer.insert(Insert { table: &events, format: "JSONEachRow", token: "batch-1", data: batch }).await?;
for planned in writer.plan(&events, &Budget::default()).await? {
writer.merge(&events, &planned.candidate).await?;
}
writer.housekeep(&events).await?;
```
A pool made with `Pool::with_observer` reports every operation as it ends: its outcome, its times, what the worker spent and what it wrote. The `metrics` feature records those reports through the `metrics` crate, for the exporter a service installs ([OPERATING.md](OPERATING.md), section Reports and metrics).
[`demo/`](demo/README.md) runs the whole recipe for many tenants: batching, one insert in flight for each tenant, retries under a token, compaction from the catalog, housekeeping, and queries through read-only servers that belong to no tenant. [OPERATING.md](OPERATING.md) says what each call can answer and what a service around libch decides.
## Releases
libch is published to crates.io as [`libch`](https://crates.io/crates/libch) from every merge to `main` ([RELEASING.md](RELEASING.md)), with its documentation on [docs.rs](https://docs.rs/libch). Each release's notes name the commit of the fork's `libch-worker` branch it speaks to: build the fork's `clickhouse` at that commit for its workers.
## Limits
- **DDL and mutations run in a stock exclusive writer.** `stock::Exclusive::run` boots one for a tenant, runs statements and stops. From its start the tenant's operations end `Fenced`, so none interrupts it, and they run again once `share` hands the disk back.
- **A view reads the rows it is fed.** A worker refuses an insert whose view also reads stored rows, names a table function or reads a dictionary.
- **Exactly-once needs tokens.** Every table an insert reaches keeps deduplication tokens (`non_replicated_deduplication_window`), or the writer waives exactly-once.
- **A source part and its views' parts commit one after another.** A retry under the token completes an insert that died between them.
- **`plan` loads every part of its table.** That costs about half a millisecond of CPU a part on every call, where a stock server pays it once at boot, so a caller plans the tables the catalog names as crowded and keeps them compacted.
- **The sandbox needs Linux 6.12.** It confines a worker by file, port, signal and system call; which hosts a worker reaches is for the network to say.
[PLAN.md](PLAN.md) holds the goal, the phases, and the evidence for each.
## What is here
| Path | Contents |
| --- | --- |
| `crates/libch` | The crate: `catalog`, `part`, `disk`, the `Writer` trait, `stock` servers, `check`, a small S3 client, `worker` (the client that runs each operation in a worker process, and the sandbox), `pool` (workers started ahead of demand), `keys` (tenants' keys, minted, cached and revoked), `observe` (a report of each operation as it ends) and, with the `metrics` feature, `metrics` (those reports as metrics). |
| `proto/libch.proto` | The protocol between the crate and a worker, which `libch::proto` compiles in as generated code. |
| `crates/libch-testkit` | What tests, benchmarks and the demo share: the dev stack, the preflight, a tenant fixture, the conformance scenarios, the differential harness and its workload generator, a proxy that fails calls to the store, process measurements, and a stand-in worker for protocol tests. |
| `crates/libch/tests` | Integration tests against the dev stack. |
| `crates/libch/benches` | The benchmarks that write [BENCHMARKS.md](BENCHMARKS.md). |
| `demo` | `libch-demo`, the high-tenant recipe end to end, and `demo/run`, which starts its large runs. |
| `scripts/dev-up`, `scripts/dev-down` | libch's own dev stack: InodeDB on port 7170 and s3proxy on port 11211. |
| `scripts/fork-setup`, `scripts/fork-build` | The worker's branch of the fork: a git worktree at `fork/ClickHouse` and its build at `fork/build`. |
| `scripts/soak`, `scripts/mutants` | The differential soak, and planted bugs that the tests must catch. |
| `bighouse.rev`, `fork.rev`, `inodedb.rev` | The BigHouse commit, the commit of the fork's `libch-worker` branch, and the InodeDB commit whose server the dev stack runs, that this code runs against. |
| `.github/workflows`, `scripts/ci-test` | CI, and a release to crates.io on every merge to `main` ([RELEASING.md](RELEASING.md)). |
## Running the tests
The tests need:
- a BigHouse checkout beside this repository (or at `BIGHOUSE_DIR`), at the commit in `bighouse.rev` or later, with the fork's ClickHouse built (`scripts/clickhouse-build`) and InodeDB built (`scripts/inodedb-build`);
- the worker's branch of the fork, `libch-worker`, in that checkout's `ClickHouse` repository, built here;
- Docker, for s3proxy;
- `protoc`, which `libch-testkit`'s `tests/proto.rs` runs.
```sh
scripts/fork-setup # makes fork/ClickHouse, a worktree of the fork on the worker's branch
scripts/fork-build # builds fork/build/programs/clickhouse, the stock server and the worker in one binary
scripts/dev-up # starts InodeDB and s3proxy, with their state in .dev/
cargo test
scripts/dev-down # add --wipe to delete the state
```
The first `scripts/fork-build` compiles all of ClickHouse, about an hour on 24 cores. `LIBCH_CLICKHOUSE` points the tests at a binary built elsewhere.
`scripts/ci-test` runs the tests that need neither the stack nor the fork: the crate's unit and doc tests, and the tests against a stand-in worker. CI runs it.
Every test that needs the stack runs a preflight first and fails with what to start or build. Each test writes a tenant of its own and removes it, so tests run side by side and leave the stack empty.
| Command | What it runs |
| --- | --- |
| `cargo test` | The suite: about six minutes on a freshly wiped stack. |
| `LIBCH_SANDBOX=1 cargo test` | The suite with every worker in the sandbox. |
| `LIBCH_FORK=1 cargo test` | The suite with every worker forking a child for each operation. |
| `cargo test -p libch --test pool_tenants -- --ignored` | Sixty-four tenants at once through one pool. |
| `LIBCH_KILL_ROUNDS=1000 cargo test -p libch --test worker_kill_loop` | The kill loop at the plan's length. |
| `LIBCH_FORK_OPERATIONS=10000 cargo test -p libch --test worker_fork a_parent_serves` | Ten thousand operations through one forking worker. |
| `PARTS=10000 cargo test -p libch --test plan_beside_stock_boot -- --ignored --nocapture` | A `plan` beside a stock server's boot on a table of 10,000 parts. |
| `scripts/soak --minutes 60` | The differential soak. |
| `scripts/mutants` | Each planted bug in turn, about an hour of builds. |
| `demo/run gate`, `demo/run kills`, `demo/run wide` | The demo's large runs, each from an empty stack: 1,000 tenants and 100,000,000 rows, the same with insert workers killed mid-operation, and 10,000 tenants. `--tmpfs` keeps the InodeDB store in memory. `LIBCH_INODEDB_BIN` runs another InodeDB server in place of BigHouse's. |
| `cargo bench --bench stock`, `cargo bench --bench operations` | The benchmarks. They refuse to record on a busy machine. |
## License
libch is under the [libch Development License](LICENSE). It permits developing libch or software that works with it, fixing its bugs, and reviewing its security, and grants no commercial rights. The license is provisional: it changes at libch's official release. The worker is under the fork's `LICENSE-BIGHOUSE`.