Documentation
¶
Overview ¶
Package composite serves one logical store from two: a primary that takes every write and holds everything from Cutover on, and a read-only secondary store that holds what came before.
It serves an installation whose older data sits in a different store than its new data, for as long as that older data is kept. The split is by time, so a record is in exactly one of them and the two never need reconciling; what the composite does is route a query to whichever holds the range asked about, and merge when the range spans Cutover.
The invariant it relies on: the primary holds nothing older than Cutover and the secondary holds nothing newer. Cutover must be the instant the secondary stopped receiving writes.
Index ¶
- func PrefixMap(primary, secondary string) (toSecondary, fromSecondary func(string) string)
- func RenameDocKey(from, to string) func(json.RawMessage) json.RawMessage
- type Config
- type Store
- func (c *Store) BulkWriter(dataset store.Dataset) (store.BulkWriter, error)
- func (c *Store) Count(ctx context.Context, s store.Scope, filters []store.Filter) (int64, error)
- func (c *Store) CountInWindow(ctx context.Context, s store.Scope, filters []store.Filter, ...) (int64, error)
- func (c *Store) DescribeFields(ctx context.Context, s store.Scope) ([]store.Field, error)
- func (c *Store) Drop(ctx context.Context, s store.Scope) (int64, error)
- func (c *Store) FetchByID(ctx context.Context, s store.Scope, id string) (json.RawMessage, error)
- func (c *Store) FetchN(ctx context.Context, s store.Scope, filters []store.Filter, n int) ([]json.RawMessage, error)
- func (c *Store) FetchPage(ctx context.Context, s store.Scope, filters []store.Filter, page store.Page) ([]json.RawMessage, int64, error)
- func (c *Store) FetchQuery(ctx context.Context, s store.Scope, sql string) ([]json.RawMessage, error)
- func (c *Store) Flush(ctx context.Context, dataset store.Dataset) error
- func (c *Store) GroupBy(ctx context.Context, s store.Scope, fields []string, filters []store.Filter, ...) ([]store.Group, error)
- func (c *Store) Health(ctx context.Context) (store.Health, error)
- func (c *Store) Insert(ctx context.Context, s store.Scope, id string, doc any) error
- func (c *Store) Retention(ctx context.Context, dataset store.Dataset) (store.Retention, error)
- func (c *Store) SecondaryActive() bool
- func (c *Store) SetRetention(ctx context.Context, dataset store.Dataset, r store.Retention) error
- func (c *Store) Timeline(ctx context.Context, s store.Scope, filters []store.Filter, ...) ([]store.Point, error)
- func (c *Store) TimelineByField(ctx context.Context, s store.Scope, field string, filters []store.Filter, ...) ([]store.Series, error)
- func (c *Store) TopValues(ctx context.Context, s store.Scope, field string, filters []store.Filter, ...) ([]store.Bucket, error)
- func (c *Store) UpdateWhere(ctx context.Context, s store.Scope, filters []store.Filter, ...) (int64, error)
- func (c *Store) Usage(ctx context.Context) ([]store.DatasetUsage, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PrefixMap ¶
PrefixMap returns the two field-name translations a Config takes, for stores that differ only in what they call the object holding the parsed fields: the current store says "log.user" where the secondary says "event.user". Names that do not start with the prefix pass through unchanged, and so does the prefix alone.
func RenameDocKey ¶
func RenameDocKey(from, to string) func(json.RawMessage) json.RawMessage
RenameDocKey returns a Config.SecondaryDocument that renames one top-level key of a record. A record without the key, or that is not a JSON object, is returned as it came, and a key already present under the new name is left alone.
Types ¶
type Config ¶
type Config struct {
// Primary takes every write and is authoritative for records from Cutover.
Primary store.Store
// Secondary is only ever read. Nothing here writes to it.
Secondary store.Store
// Cutover is the instant the secondary stopped receiving writes.
Cutover time.Time
// SecondaryExpires is when the secondary store is switched off. From then on the
// composite never reaches it, so a query that would have crossed Cutover is
// answered from the primary alone instead of failing against a store that
// is gone. The zero value means it never expires. Set it to the cutover plus
// the secondary's retention.
SecondaryExpires time.Time
// SecondaryTenant is the one tenant the secondary data belongs to. A secondary store
// built for a single-tenant installation has no tenant field, so it cannot
// tell tenants apart: only this tenant (or store.AllTenants) may read it,
// and any other tenant sees the primary alone.
SecondaryTenant string
// SecondaryDatasets lists the datasets the secondary store serves. Anything else
// is answered from the primary only, so an unmapped dataset is not mistaken
// for an empty one.
SecondaryDatasets []store.Dataset
// TimeField is the field both stores order by. Defaults to "@timestamp".
TimeField string
// TolerateSecondary lets a failing secondary store degrade a read to the
// primary's answer instead of failing it. A partial answer can look like
// "nothing happened", so it is off unless the caller says otherwise, and
// OnSecondaryError is how it finds out.
TolerateSecondary bool
OnSecondaryError func(op string, err error)
// SecondaryField maps a field name as the composite's callers write it to the
// name the secondary store knows it by, and SecondaryFieldBack does the reverse.
// SecondaryDocument rewrites a secondary record into the shape the primary's
// records have. All three are optional.
SecondaryField func(string) string
SecondaryFieldBack func(string) string
SecondaryDocument func(json.RawMessage) json.RawMessage
// Now is the clock, replaceable in tests.
Now func() time.Time
}
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
func (*Store) BulkWriter ¶
func (*Store) CountInWindow ¶
func (*Store) DescribeFields ¶
func (*Store) Drop ¶
Drop removes records from the primary only; the secondary is read-only and its own retention ends it.
func (*Store) FetchN ¶
func (c *Store) FetchN(ctx context.Context, s store.Scope, filters []store.Filter, n int) ([]json.RawMessage, error)
FetchN returns the newest n records: the primary's first, and the secondary's only for what the primary could not fill.
func (*Store) FetchPage ¶
func (c *Store) FetchPage(ctx context.Context, s store.Scope, filters []store.Filter, page store.Page) ([]json.RawMessage, int64, error)
FetchPage pages across both stores. Sorted by time, which is the default, the two are consecutive runs — the primary's records are all newer than the secondary's — so the page is read from one or both without sorting anything. Any other order has to read offset+limit rows from each and merge them.
func (*Store) FetchQuery ¶
func (c *Store) FetchQuery(ctx context.Context, s store.Scope, sql string) ([]json.RawMessage, error)
FetchQuery runs the caller's SQL against every store the scope reaches and returns the primary's rows first, then the secondary's rewritten into the primary's shape. Both runs start together, and a secondary failure is tolerated like FetchPage's. SQL has no portable merge, so the runs concatenate: a query that is meaningful only as a whole (an aggregate, a join) should name its side by scoping From/To to one half of the cutover rather than relying on this.
func (*Store) Health ¶
Health is the primary's, degraded further when the secondary is not well. A secondary that is down must not make the installation look down.
func (*Store) SecondaryActive ¶
SecondaryActive reports whether the secondary store can still be reached: it is until SecondaryExpires, if one is set.
func (*Store) SetRetention ¶
func (*Store) Timeline ¶
func (c *Store) Timeline(ctx context.Context, s store.Scope, filters []store.Filter, interval store.Interval) ([]store.Point, error)
Timeline sums buckets with the same start. The bucket that holds Cutover is in both stores and is added up; both are expected to align their buckets the same way for a given interval.
func (*Store) TimelineByField ¶
func (*Store) TopValues ¶
func (c *Store) TopValues(ctx context.Context, s store.Scope, field string, filters []store.Filter, n int) ([]store.Bucket, error)
TopValues is exact when one store answers. When both do, each is asked for more than n values and the counts are summed, so a value that ranks high only by adding the two together can still be missed or undercounted.