composite

package
v2.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrefixMap

func PrefixMap(primary, secondary string) (toSecondary, fromSecondary func(string) string)

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.

func SecondaryValueRewrite added in v2.1.0

func SecondaryValueRewrite(m map[string]map[string]string) func(store.Filter) store.Filter

SecondaryValueRewrite returns a Config.SecondaryValue that swaps a filter's value when its field is named in m and that value has an entry: "Open" becomes "2" before it reaches a secondary that keeps status as a numeric code. A field not in m, or a value with no entry, passes through unchanged, so the hook is safe to install even when nothing needs remapping. The map keys on the secondary's field name, since the hook runs after SecondaryField has remapped the name; only a field's own table is consulted for its values.

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

	// SecondaryValue rewrites one filter before it reaches the secondary store, and
	// returns it unchanged when the filter is not one it maps. It exists for the two
	// stores agreeing on a field name but not its value: the v11 alerts index keeps
	// status as a numeric code (2 = "Open") where the primary keeps the string.
	// Applied in SecondaryField's order, after the field name is already remapped,
	// so the hook keys on the secondary's name. Optional.
	SecondaryValue func(store.Filter) store.Filter

	// Now is the clock, replaceable in tests.
	Now func() time.Time
}

type Store

type Store struct {
	// contains filtered or unexported fields
}

func New

func New(cfg Config) (*Store, error)

func (*Store) BulkWriter

func (c *Store) BulkWriter(dataset store.Dataset) (store.BulkWriter, error)

func (*Store) Count

func (c *Store) Count(ctx context.Context, s store.Scope, filters []store.Filter) (int64, error)

func (*Store) CountInWindow

func (c *Store) CountInWindow(ctx context.Context, s store.Scope, filters []store.Filter, window time.Duration) (int64, error)

func (*Store) DescribeFields

func (c *Store) DescribeFields(ctx context.Context, s store.Scope) ([]store.Field, error)

func (*Store) Drop

func (c *Store) Drop(ctx context.Context, s store.Scope) (int64, error)

Drop removes records from the primary only; the secondary is read-only and its own retention ends it.

func (*Store) FetchByID

func (c *Store) FetchByID(ctx context.Context, s store.Scope, id string) (json.RawMessage, error)

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) Flush

func (c *Store) Flush(ctx context.Context, dataset store.Dataset) error

func (*Store) GroupBy

func (c *Store) GroupBy(ctx context.Context, s store.Scope, fields []string, filters []store.Filter, opts store.GroupOpts) ([]store.Group, error)

func (*Store) Health

func (c *Store) Health(ctx context.Context) (store.Health, error)

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) Insert

func (c *Store) Insert(ctx context.Context, s store.Scope, id string, doc any) error

func (*Store) Retention

func (c *Store) Retention(ctx context.Context, dataset store.Dataset) (store.Retention, error)

func (*Store) SecondaryActive

func (c *Store) SecondaryActive() bool

SecondaryActive reports whether the secondary store can still be reached: it is until SecondaryExpires, if one is set.

func (*Store) SetRetention

func (c *Store) SetRetention(ctx context.Context, dataset store.Dataset, r store.Retention) error

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 (c *Store) TimelineByField(ctx context.Context, s store.Scope, field string, filters []store.Filter, interval store.Interval, n int) ([]store.Series, error)

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.

func (*Store) UpdateWhere

func (c *Store) UpdateWhere(ctx context.Context, s store.Scope, filters []store.Filter, patch map[string]any) (int64, error)

func (*Store) Usage

func (c *Store) Usage(ctx context.Context) ([]store.DatasetUsage, error)

Usage lists both stores' datasets, the secondary's after the primary's.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL