1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! 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.
//!
//! ```no_run
//! 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};
//!
//! # async fn recipe(admin: libch::inodedb::Client) -> Result<(), libch::Error> {
//! 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?;
//! # Ok(())
//! # }
//! ```
/// The protocol between the crate and a worker. It is public for `libch-testkit`'s stand-in worker, and it
/// changes with the worker, outside the crate's versioned API.
pub use Error;
pub use PartInfo;
pub use ;
pub use ;
pub use inodedb;