Documentation
¶
Overview ¶
Package clickhouse implements store.Store over ClickHouse.
One table per dataset, partitioned by tenant. The tenant predicate is added by this package on every operation rather than by callers, so a query that forgets it does not exist.
Index ¶
- func WithDeduplicationToken(ctx context.Context, token string) context.Context
- type Config
- type Driver
- func (d *Driver) BulkWriter(dataset store.Dataset) (store.BulkWriter, error)
- func (d *Driver) Close() error
- func (d *Driver) Count(ctx context.Context, s store.Scope, filters []store.Filter) (int64, error)
- func (d *Driver) CountInWindow(ctx context.Context, s store.Scope, filters []store.Filter, ...) (int64, error)
- func (d *Driver) DescribeFields(ctx context.Context, s store.Scope) ([]store.Field, error)
- func (d *Driver) Drop(ctx context.Context, s store.Scope) (int64, error)
- func (d *Driver) EnableTiering(ctx context.Context, dataset store.Dataset, policy string, r store.Retention) error
- func (d *Driver) Exec(ctx context.Context, query string, args ...any) error
- func (d *Driver) FetchByID(ctx context.Context, s store.Scope, id string) (json.RawMessage, error)
- func (d *Driver) FetchN(ctx context.Context, s store.Scope, filters []store.Filter, n int) ([]json.RawMessage, error)
- func (d *Driver) FetchPage(ctx context.Context, s store.Scope, filters []store.Filter, page store.Page) ([]json.RawMessage, int64, error)
- func (d *Driver) FetchQuery(ctx context.Context, s store.Scope, sql string) ([]json.RawMessage, error)
- func (d *Driver) Flush(ctx context.Context, dataset store.Dataset) error
- func (d *Driver) GroupBy(ctx context.Context, s store.Scope, fields []string, filters []store.Filter, ...) ([]store.Group, error)
- func (d *Driver) Health(ctx context.Context) (store.Health, error)
- func (d *Driver) Insert(ctx context.Context, s store.Scope, id string, doc any) error
- func (d *Driver) Retention(ctx context.Context, dataset store.Dataset) (store.Retention, error)
- func (d *Driver) SetRetention(ctx context.Context, dataset store.Dataset, r store.Retention) error
- func (d *Driver) Timeline(ctx context.Context, s store.Scope, filters []store.Filter, ...) ([]store.Point, error)
- func (d *Driver) TimelineByField(ctx context.Context, s store.Scope, field string, filters []store.Filter, ...) ([]store.Series, error)
- func (d *Driver) TopValues(ctx context.Context, s store.Scope, field string, filters []store.Filter, ...) ([]store.Bucket, error)
- func (d *Driver) UpdateWhere(ctx context.Context, s store.Scope, filters []store.Filter, ...) (int64, error)
- func (d *Driver) Usage(ctx context.Context) ([]store.DatasetUsage, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
// Addr, Database and Auth build a connection when Conn is nil.
Addr []string
Database string
Username string
Password string
// Conn takes precedence, for callers that already hold one.
Conn driver.Conn
// Tables maps a dataset to its table. A dataset with no entry is an error
// at call time rather than a silent read of nothing.
Tables map[store.Dataset]string
// TenantColumn carries the tenant. It has no default: a driver that
// guessed it would silently return every tenant's rows when it guessed
// wrong.
TenantColumn string
// TextColumns names, per dataset, the column holding the whole record as
// text — what OpSearch looks in. A dataset with no entry cannot be searched
// that way, which is an error at call time rather than a silent no-match.
TextColumns map[store.Dataset]string
// IDColumns names, per dataset, a column that identifies a row. It is what
// a paged read narrows on before it materialises whole records: selecting
// every column is what lets a record keep the dynamic half of its shape,
// and it is also what stops ClickHouse deferring those columns until after
// the sort, so ordering a week of logs to return fifty of them reads the
// week. A dataset with no entry is read in one pass, which is correct and
// costs that.
IDColumns map[store.Dataset]string
// TimeColumn bounds Scope.From/To. Defaults to "@timestamp".
TimeColumn string
// DataTypeColumn narrows Scope.DataType. Empty means the dataset has no
// such dimension and a scoped DataType is an error.
DataTypeColumn string
// ColdVolume names the storage volume SetRetention moves data to. Defaults
// to "cold"; it has to match the storage policy the table was created with.
ColdVolume string
// DialTimeout and MaxOpenConns apply only when this package builds the
// connection.
DialTimeout time.Duration
MaxOpenConns int
// MaxRetries and RetryDelay bound how hard a BulkWriter tries a batch
// ClickHouse rejects before treating it as unrecoverable. Zero uses the
// package defaults (3 retries, 1s apart).
MaxRetries int
RetryDelay time.Duration
// OnReject is called for every row a BulkWriter permanently drops: one
// ClickHouse still refuses after MaxRetries, and, once a failing batch
// holds more than one row, after bisecting it down to the row(s) actually
// at fault. A single bad row would otherwise cost every row batched
// alongside it — ClickHouse parses one INSERT ... FORMAT JSONEachRow as
// one unit, so it rejects the whole thing for one bad line — and without
// this callback that loss leaves no trace beyond a row count in this
// package's own error log. Runs on the caller's goroutine (whichever
// called Flush/Close), never concurrently with itself. Nil is fine: the
// row is still dropped, just as silently as before.
OnReject func(row []byte, err error)
}
Config describes where the data lives. Table names are supplied rather than derived: which datasets exist and what they are called is product knowledge, and this package holds none.
type Driver ¶
type Driver struct {
// contains filtered or unexported fields
}
func (*Driver) BulkWriter ¶
BulkWriter batches rows into one INSERT. ClickHouse pays a fixed cost per insert regardless of size — every one creates a part that then has to be merged — so batching is not an optimisation here, it is how you avoid drowning the server in small parts.
func (*Driver) CountInWindow ¶
func (d *Driver) CountInWindow(ctx context.Context, s store.Scope, filters []store.Filter, window time.Duration) (int64, error)
CountInWindow counts a trailing window ending at Scope.To, or at now when it is unset. The bounds are pinned onto the scope so a caller that counts and then fetches sees the same window for both.
func (*Driver) DescribeFields ¶
DescribeFields lists what can be queried, and that is two things joined: the declared columns, and the paths ClickHouse discovered inside the JSON column. The second half is why a record can carry fields nobody declared and still be filterable.
func (*Driver) Drop ¶
Drop deletes what the scope covers and nothing else. It is a mutation, so it is asynchronous and the count is not knowable up front.
func (*Driver) EnableTiering ¶
func (d *Driver) EnableTiering(ctx context.Context, dataset store.Dataset, policy string, r store.Retention) error
EnableTiering points a dataset at a storage policy that has a cold volume, for an install that started without one and later added object storage.
It exists because the two halves cannot be done in one statement: the policy has to be in place before a TTL may name its volume. Doing them in the wrong order fails with an error about a volume that does not exist, which reads like a typo rather than an ordering problem.
The policy itself is server configuration and this package cannot create it. One requirement is worth knowing before you write it: ClickHouse only accepts a new policy that contains every VOLUME NAME of the old one, and a table with no policy is on "default", whose volume is also called "default". So the hot volume has to be named "default" — call it "hot" and the table can never be migrated without being rebuilt.
func (*Driver) FetchQuery ¶
func (d *Driver) FetchQuery(ctx context.Context, s store.Scope, sql string) ([]json.RawMessage, error)
FetchQuery runs caller-supplied ClickHouse SQL. It requires store.AllTenants because the driver cannot inject a tenant predicate into text it did not build — an aggregate query returns rows with no tenant column left to filter — so the caller must carry the predicate and acknowledge it is reading across tenants.
func (*Driver) Insert ¶
Insert writes one record. An explicit id upserts, which on MergeTree means the newest row wins at merge time rather than immediately — reads can see both until then, so callers that need immediacy should not rely on it.
func (*Driver) Retention ¶
Retention reads the TTL off the table itself, which is where it lives: in ClickHouse retention is part of the schema rather than a policy attached from outside.
func (*Driver) SetRetention ¶
SetRetention rewrites the whole TTL, because that is the only thing MODIFY TTL can do: it replaces the expression rather than amending it.
So a flat retention asked of a table that currently moves data to cold storage is refused. Accepting it would drop the move clause, and nothing would report that: the data would simply stop leaving the hot disk until it filled.