Documentation
¶
Overview ¶
Package tenant provides the cross-cutting policy-callback layer (DESIGN.md §2, §8): a tenant.Resolver maps a tenant id to limits, retention, downsampling, and routing, hot-reloadable by the consumer. Not yet implemented (M3).
Index ¶
Constants ¶
const DefaultRecompressLevel = 9
DefaultRecompressLevel is the Zstandard level a Recompress policy uses when Level is unset: the archival end of the useful range, not the extreme one.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Downsample ¶
type Downsample struct {
// Tiers are the age-banded rollup resolutions, applied at merge time. Order does not
// matter; the engine assigns each sample to the coarsest applicable tier.
Tiers []DownsampleTier
}
Downsample is the per-tenant downsampling policy: a merge-time rollup reusing the same background-merge engine (no separate subsystem). Optional and per-tenant; an empty Tiers list disables downsampling (data stays raw until retention drops it).
func (Downsample) Validate ¶ added in v0.49.0
func (d Downsample) Validate() error
Validate reports whether the enabled tiers' Intervals nest and share one Agg (see DownsampleTier).
type DownsampleTier ¶ added in v0.4.0
type DownsampleTier struct {
// After is the age past which this tier applies (relative to now at merge time, the
// cutoff floored as described above).
After time.Duration
// Interval is the rollup bucket width. Zero ⇒ the tier is disabled.
Interval time.Duration
// Agg combines the samples in a bucket. The zero value is [signal.AggLast]. Every
// enabled tier of a policy must use the same Agg.
Agg signal.Aggregation
}
DownsampleTier rolls up samples once they reach a given age: every sample older than After is replaced, at merge time, by one representative per Interval-wide time bucket, the bucket's samples combined by Agg. A tier with Interval ≤ 0 is ignored.
Tiers coarsen old data: configure increasing After with increasing Interval (e.g. 5m buckets after 24h, 1h buckets after 7d). A sample is rolled up by the widest-Interval tier whose cutoff it is past; samples younger than every tier's cutoff stay raw. Buckets are aligned to absolute multiples of Interval (not to ingest time), so the rollup of a time range is independent of when the merge runs — repeated merges are stable.
Intervals must nest: ordered by Interval, each strictly divides the next (1m, 5m, 1h, 6h nests; 7m with 1h does not). A tier's cutoff is now − After floored to a multiple of Q = Interval × ⌈1h / Interval⌉: one hour for any Interval dividing an hour, Interval itself from 1h up. Nesting puts every cutoff on the grid of every finer tier, so no bucket straddles a cutoff: each is rolled up whole, once, by one tier, and a finer representative later coarsened lands in the coarse bucket its samples belong to. The cutoff moves once per Q rather than on every maintenance cycle. The cost is lag: a sample is rolled up between After and After + Q past its timestamp, plus up to one maintenance interval.
Every enabled tier must use the same Agg, so a coarser tier can re-aggregate the finer tier's representatives. Last, First, Min and Max keep the chosen sample, at its own timestamp, and coarsen exactly; Min and Max ignore NaN unless a bucket holds nothing else. Sum keeps the bucket total at the bucket start. Avg keeps the bucket mean at the bucket start, weighted by the bucket's population through the sample's scale factor (fetch.Batch.ScaleFactors). Count keeps the bucket's count at the bucket start. A coarser Sum or Avg equals one rollup of the raw samples up to floating-point grouping: the same sum, added in a different order. A different Agg per tier would aggregate the finer tier's results instead of the raw samples (a 1m Sum then a 1h Max is the max of per-minute sums), so such a policy is rejected.
Rolled data stays rolled. Removing or narrowing a tier un-rolls nothing, and a sample later written into a time range already rolled up is rolled by the layout that range has. A tier that conflicts with one already applied to stored data is not applied at all until retention drops that data; what it would roll stays raw meanwhile. A tier conflicts when its Interval does not nest with the applied one, or when its Agg differs, whatever its width. So changing Agg takes effect only once the data rolled with the old Agg has expired.
A sample written late into a bucket that is already rolled up combines with the bucket's representative as new data. It cannot replace a raw sample that is already rolled up, because that value is gone: a late write reusing its timestamp combines with the aggregate as one more sample. Set After beyond the ingest lateness the tenant must tolerate.
A policy that fails Downsample.Validate is rejected whole: the tenant is not downsampled at all, and the storage logs a warning once.
type Durability ¶ added in v0.32.0
type Durability struct {
// RF is the replication factor: the number of ring-owners for the tenant's shards
// (primary + RF-1 replicas). Zero ⇒ the cluster default; values are clamped to the
// membership size at lookup time.
RF int
// EC, when non-nil, erasure-codes the tenant's flushed parts instead of storing RF
// full copies. Nil ⇒ full-copy replication.
EC *ECScheme
}
Durability is the per-tenant replication policy for the cluster layer: how many ring-owners hold a tenant's data and how flushed parts are stored across them. Zero value ⇒ the cluster-wide defaults (cluster.Config.RF, full-copy). Ignored in single-node mode.
type ECScheme ¶ added in v0.32.0
type ECScheme struct {
// Data is the number of data shards (k). Must be ≥ 1.
Data int
// Parity is the number of parity shards (m); the scheme tolerates Parity node losses.
// Must be ≥ 1.
Parity int
// After is the age past which a fully-cold part is erasure-coded, at merge, in place of
// full-copy replication — recent parts stay full-copy for fast local reads, and only cold
// parts trade a reconstruct-on-read cost for the storage saving. It mirrors [Recompress.After]
// and rides the same background-merge engine. Zero ⇒ EC every part (accept the read cost
// everywhere for the cheapest storage).
After time.Duration
}
ECScheme is a Reed-Solomon erasure-coding scheme for a tenant's flushed parts: each part object is split into Data shards plus Parity parity shards, spread across Data+Parity distinct owners; any Data of them reconstruct the object. It trades reconstruct-on-miss read cost for storage: (Data+Parity)/Data of the logical size instead of RF full copies (e.g. {4,2} survives 2 node losses at 1.5x). Applies only to flushed, immutable parts — the unflushed head is always full-copy replicated. A nil *ECScheme means full-copy.
type Limits ¶
type Limits struct {
// IngestBytesPerSecond caps the per-tenant ingest rate (a token bucket whose burst is one
// second of budget). Bytes over budget are shed. Zero ⇒ unlimited.
IngestBytesPerSecond int64
// MaxInFlightBytes caps the unflushed in-flight bytes buffered for a tenant (memory
// backpressure): samples arriving while at the cap are shed until a flush drains the head.
// Zero ⇒ unlimited.
MaxInFlightBytes int64
// MaxSeries caps the active series cardinality per tenant: a sample that would mint a new
// series past the cap is shed (or routed to overflow — see MaxSeriesSoft); existing series
// are unaffected. It is the hard ceiling. Zero ⇒ unlimited.
MaxSeries int64
// MaxSeriesSoft, when 0 < MaxSeriesSoft <= MaxSeries, is a soft cardinality budget: past it a
// *new* series' samples are routed to a synthetic per-metric overflow series (keeping a tenant's
// aggregates approximately correct under a spike) rather than shed, until the hard MaxSeries is
// reached. Zero (or with no MaxSeries) ⇒ no soft budget: the cap is a hard reject at MaxSeries.
// Metrics only (collapsing a log/trace stream would lose its rows). Note: cardinality here is
// monotonic within an engine's lifetime, so once crossed the budget stays in overflow (no
// hysteresis); see docs/design/cardinality-overflow.md.
MaxSeriesSoft int64
// MaxPartSize caps a *flushed* part's (approximate, uncompressed) size: a flush splits its
// output so no single part exceeds it. It is a structural cap fixed when a tenant's engine is
// first created. Zero ⇒ unlimited (one part). It applies to every signal, exactly for metrics
// (fixed 32 B/row) and by measured row bytes for record signals (logs/traces/profiles/exemplars),
// whose rows are variable-width.
//
// It no longer bounds *merged* parts; see MaxMergePartSize.
MaxPartSize int64
// MaxMergePartSize caps a merged part's size **on disk** — compressed bytes, unlike
// MaxPartSize. Zero ⇒ derived from the backend's free space, which is the default and what
// makes part size track the deployment; negative ⇒ unlimited, never seal.
//
// Merged parts are sized separately because the two caps answer different questions: a flush is
// bounded so the head's rows land promptly, a merge so that part *count* stays low. Under a byte
// constant the time span a part covers is inversely proportional to active series, so a
// fixed-range query opens proportionally more parts the more series a tenant has.
MaxMergePartSize int64
}
Limits are the per-tenant operational limits enforced as lossless admission control: when a tenant exceeds one, the offending samples are shed and reported via OTLP partial-success (RESOURCE_EXHAUSTED), so an overload degrades rather than OOMs. They are resolved per write, so a consumer's hot-reloaded changes take effect immediately. Zero values mean "no limit".
type Policy ¶
type Policy struct {
Limits Limits
Retention Retention
Downsample Downsample
Sampling Sampling
Recompress Recompress
Precision Precision
Durability Durability
}
Policy is the resolved policy for a tenant: limits, retention, downsampling, sampling, recompression, durability, and routing. It is the return value of Resolver.Resolve.
type Precision ¶ added in v0.6.0
type Precision struct {
// Tiers are the age-banded precision budgets. Order does not matter; the engine assigns each
// part the most aggressive applicable tier.
Tiers []PrecisionTier
}
Precision is the per-tenant lossy float-compression policy: age-tiered value-column precision applied at merge, so recent data stays lossless and only old data trades accuracy for size. Optional and per-tenant; an empty Tiers list keeps every part lossless.
type PrecisionTier ¶ added in v0.6.0
type PrecisionTier struct {
// After is the age past which this tier applies (relative to now at merge time).
After time.Duration
// Bits is the significant mantissa bits to retain (1..63). 0 or ≥64 ⇒ lossless (ignored).
Bits uint8
}
PrecisionTier makes a part's value column lossy once its data reaches a given age: a part fully older than After is re-encoded, at merge, retaining only Bits significant mantissa bits (scaled-decimal). Fewer bits ⇒ better compression, less accuracy. A tier with Bits == 0 or Bits ≥ 64 is lossless and ignored.
Tiers coarsen old data (configure increasing After with decreasing Bits, e.g. 32 bits after 7d, 16 bits after 30d). A part takes the most aggressive tier whose After it has exceeded; data younger than every tier's After stays lossless. It rides the same background-merge engine — no separate subsystem — and is never worse than lossless Gorilla (the encoder keeps whichever is smaller), so a tier can only help size.
Bits is significant mantissa bits of the value, not decimal places: ~16 bits keeps roughly 4–5 significant decimal digits, ~24 bits ~7 digits. Counters and clean low-precision gauges are already near-lossless dense, so precision tiers mainly help noisy/high-entropy gauges where some accuracy on old data can be traded for size.
type Recompress ¶ added in v0.4.0
type Recompress struct {
// After is the age past which a fully-cold part is recompressed at merge. Zero ⇒ disabled.
After time.Duration
// Level is the Zstandard level (1 fastest … 19 best ratio). Zero ⇒ [DefaultRecompressLevel].
Level int
}
Recompress is the per-tenant cold-data recompression policy: once a part's data is older than After, the merge engine rewrites it at a higher Zstandard level than the size ladder would pick, trading merge CPU for storage. It is decode-transparent (the reader keys off the recorded algorithm), so it costs nothing to read. Optional; a zero After disables it — merged parts are still compressed, at the ladder level their size earns.
This is the *archival* tier and should be the only one: levels past ~9 buy single-digit percent for roughly an order of magnitude more CPU, which competes with merge and retention.
type Resolver ¶
type Resolver interface {
// Resolve returns the policy for a tenant. It must not be expensive (called on
// the hot ingest/query path); back hot values with a cache.
Resolve(t signal.TenantID) Policy
}
Resolver maps a signal.TenantID to a Policy (DESIGN.md §2, §8). It is the callback-based multi-tenancy mechanism: the consumer supplies the resolver, which may be backed by hot-reloaded overrides (Mimir-style). Implementations must be safe for concurrent use.
The zero value Resolver is not usable; consumers provide a function via ResolverFunc.
type ResolverFunc ¶
ResolverFunc adapts a function into a Resolver.
type Retention ¶
type Retention struct {
// MaxAge is the default age bound for signals without an override.
MaxAge time.Duration
// MaxBytes bounds this node's stored bytes pooled across all tenant signals and shards.
MaxBytes int64
// PerSignal overrides age and adds a byte bound for individual signals. Zero fields inherit.
PerSignal map[signal.Signal]SignalRetention
}
Retention bounds a tenant's stored data. Zero bounds retain forever.
type Sampling ¶ added in v0.4.0
type Sampling struct {
// MaxRowsPerSecond is the per-tenant ingest budget. When the observed rate exceeds it, the
// sampler keeps ~budget rows/second and scales the kept samples up to compensate. 0 ⇒ no
// sampling (lossless: every sample is stored with a scale factor of 1).
MaxRowsPerSecond int64
}
Sampling is the per-tenant lossy admission policy (DESIGN §8a): when ingest exceeds the budget, keep a representative subset rather than reject, tagging each kept sample with a scale factor (the inverse keep-probability) so an embedder's count/sum/rate stays unbiased. It is off by default (lossless) — sampling only ever activates under an explicit budget, and only for metrics (dropping a log or span would break a stream or trace). Sampling is deterministic by (series, timestamp), so the same point is consistently kept or dropped.
type SignalRetention ¶ added in v0.52.0
type SignalRetention struct {
// MaxAge replaces the tenant's default age, whether longer or shorter.
MaxAge time.Duration
// MaxBytes adds a byte bound beside the pooled limit. The newest part is retained even over budget.
// Pair it with [Limits.MaxPartSize], which is fixed when the engine is created, for a tighter floor.
MaxBytes int64
}
SignalRetention bounds one signal. Zero fields are unset.