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
- func SecondaryValueRewrite(m map[string]map[string]string) func(store.Filter) store.Filter
- type Config
- type DocRewrite
- 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.
func SecondaryValueRewrite ¶ added in v2.1.0
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
// SecondaryWriteDoc rewrites a record into the secondary's shape just before it
// is written to the secondary, so a composite caller keeps sending the primary's
// form in both directions, like SecondaryDocument does for reads. Optional.
SecondaryWriteDoc func(json.RawMessage) json.RawMessage
// Now is the clock, replaceable in tests.
Now func() time.Time
}
type DocRewrite ¶ added in v2.1.2
type DocRewrite struct {
// RenameFrom and RenameTo swap one top-level key, the secondary's name for
// the primary's. RenameTo empty means no rename; a key already present
// under RenameTo wins.
RenameFrom, RenameTo string
// TimeField and TimeLayout reformat one field's RFC 3339 date into the
// layout the primary renders ("2006-01-02 15:04:05.000"). TimeField empty
// means no reformat; a value that does not parse passes through.
TimeField, TimeLayout string
// ReverseTime runs the reformat the other way: from TimeLayout back to RFC
// 3339, so a doc that came back remapped is valid for the secondary again
// before it is written.
ReverseTime bool
// Values maps a field's value, as the secondary keeps it, to the form the
// primary uses, so records read like the primary's.
Values map[string]map[string]string
}
DocRewrite is what a Config.SecondaryDocument can do to a record coming back from the secondary, all in one decode pass: rename the top-level key the stores disagree on, reformat a date field into the primary's layout, and swap the values of fields the two stores name the same but keep in different forms. A field or key absent from a record leaves it alone; a record that is not a JSON object comes back as it came.
func (DocRewrite) Apply ¶ added in v2.1.2
func (r DocRewrite) Apply(doc json.RawMessage) json.RawMessage
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.
func (*Store) UpdateWhere ¶
func (c *Store) UpdateWhere(ctx context.Context, s store.Scope, filters []store.Filter, patch map[string]any) (int64, error)
UpdateWhere reaches whichever store holds the records in the scope's window and sums their updated counts. A record older than the cutover lives in the secondary; a newer one in the primary; a window that spans the cutover updates both.