Skip to main content

Crate libch

Crate libch 

Source
Expand description

libch runs ClickHouse’s insert and merge code as explicit operations on a tenant’s BigHouse inodedb disk.

Its parts:

  • tenant: where a tenant’s disk lives, in InodeDB and in S3.
  • keys: keys::KeyCache, which mints, caches and revokes the keys operations run under.
  • catalog: a tenant’s databases, tables, parts and coordination files, read from InodeDB.
  • part: part names as ClickHouse parses them, and the active set they form.
  • writer: the Writer trait, which every way of writing a tenant’s disk implements.
  • stock: stock ClickHouse servers from the BigHouse fork: the reference stock::StockWriter, the stock::Exclusive writer for DDL and mutations, and the read-only stock::Fleet.
  • worker: worker::OpWriter, which runs each operation in a worker process of its own.
  • pool: pool::Pool, which keeps workers started ahead of demand and bounds how many operations run at once.
  • observe: observe::Observer, which hears of each operation as it ends, and the observe::Report it gets.
  • metrics, with the feature of that name: an observer that records the reports through the metrics crate.
  • check: the invariant checker.

§An insert, a plan and its merges

Each call runs in a worker process that attaches the tenant, does its work, answers and exits. The pool starts workers before they are needed, and the key cache mints each tenant a short-lived key.

use bytes::Bytes;
use libch::keys::{KeyCache, KeyPolicy};
use libch::pool::{Pool, PoolOptions};
use libch::worker::WorkerCommand;
use libch::writer::{Budget, Insert, TableName};
use libch::{Outcome, S3Location, Tenant, Writer};

let pool = Pool::new(WorkerCommand::clickhouse("/usr/bin/clickhouse"), PoolOptions { waiting: 8, concurrency: 16 })?;
let keys = KeyCache::new(admin, KeyPolicy::default())?;
let tenant = Tenant {
    store: "127.0.0.1:7170".into(),
    root: "tenants/acme/disk".into(),
    s3: S3Location {
        endpoint: "http://127.0.0.1:11211".into(),
        bucket: "analytics".into(),
        prefix: "tenants/acme".into(),
        region: "us-east-1".into(),
        access_key: "…".into(),
        secret_key: "…".into(),
    },
};
let writer = pool.writer(&tenant, keys.key(&tenant).await?);
let events = TableName::new("d", "events");

// A batch lands once, however often it is sent under its token.
let batch = Bytes::from_static(b"{\"ts\":\"2026-11-01 00:00:00\",\"kind\":\"view\",\"v\":1}\n");
match writer.insert(Insert { table: &events, format: "JSONEachRow", token: "batch-1", data: batch }).await? {
    Outcome::Committed(_) | Outcome::Duplicate => {}
    other => return Err(libch::Error::Other(format!("the batch ended {}", other.name()))),
}

// ClickHouse's selector proposes merges, and each runs as an operation of its own.
for planned in writer.plan(&events, &Budget::default()).await? {
    writer.merge(&events, &planned.candidate).await?;
}
// Housekeeping removes the merged parts once queries that began before the merge have had their time.
writer.housekeep(&events).await?;

Re-exports§

pub use error::Error;
pub use part::PartInfo;
pub use tenant::S3Location;
pub use tenant::Tenant;
pub use tenant::TenantKey;
pub use writer::Outcome;
pub use writer::Writer;
pub use inodedb;

Modules§

catalog
A tenant’s catalog, read from InodeDB: databases, tables, parts and coordination files.
check
The invariant checker: what must hold of a tenant’s disk whoever wrote it.
disk
The format of a file’s value on an inodedb disk, as the BigHouse fork writes it (InodeDBValue.h and DiskObjectStorageMetadata.cpp).
error
keys
Keys for tenants’ operations: minted with a TTL, cached, and revoked on demand.
metrics
An observer that records each operation through the metrics crate’s facade, beside the pool’s and key caches’ figures.
observe
What libch reports about each operation as it ends, for a service to count, bill and alert on.
part
Part names as ClickHouse’s MergeTreePartInfo parses them, and the active set a table’s parts form.
pool
Workers started ahead of demand, and a bound on how many operations run at once.
s3
The few S3 calls libch makes itself: checking that blobs exist, reading a spilled record, and clearing a prefix.
stock
Stock ClickHouse servers from the BigHouse fork.
tenant
Where a tenant’s disk lives.
worker
The crate’s side of a worker: one process for each operation.
writer
The Writer trait: what every way of writing a tenant’s disk offers.