2016-12-25 00:40:28 +00:00
|
|
|
// Copyright 2014 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2017-10-04 19:04:15 +00:00
|
|
|
"context"
|
2016-12-25 00:40:28 +00:00
|
|
|
"errors"
|
2021-05-06 20:53:52 +00:00
|
|
|
"fmt"
|
2016-12-25 00:40:28 +00:00
|
|
|
|
2021-11-08 14:23:17 +00:00
|
|
|
"github.com/prometheus/prometheus/model/exemplar"
|
Style cleanup of all the changes in sparsehistogram so far
A lot of this code was hacked together, literally during a
hackathon. This commit intends not to change the code substantially,
but just make the code obey the usual style practices.
A (possibly incomplete) list of areas:
* Generally address linter warnings.
* The `pgk` directory is deprecated as per dev-summit. No new packages should
be added to it. I moved the new `pkg/histogram` package to `model`
anticipating what's proposed in #9478.
* Make the naming of the Sparse Histogram more consistent. Including
abbreviations, there were just too many names for it: SparseHistogram,
Histogram, Histo, hist, his, shs, h. The idea is to call it "Histogram" in
general. Only add "Sparse" if it is needed to avoid confusion with
conventional Histograms (which is rare because the TSDB really has no notion
of conventional Histograms). Use abbreviations only in local scope, and then
really abbreviate (not just removing three out of seven letters like in
"Histo"). This is in the spirit of
https://github.com/golang/go/wiki/CodeReviewComments#variable-names
* Several other minor name changes.
* A lot of formatting of doc comments. For one, following
https://github.com/golang/go/wiki/CodeReviewComments#comment-sentences
, but also layout question, anticipating how things will look like
when rendered by `godoc` (even where `godoc` doesn't render them
right now because they are for unexported types or not a doc comment
at all but just a normal code comment - consistency is queen!).
* Re-enabled `TestQueryLog` and `TestEndopints` (they pass now,
leaving them disabled was presumably an oversight).
* Bucket iterator for histogram.Histogram is now created with a
method.
* HistogramChunk.iterator now allows iterator recycling. (I think
@dieterbe only commented it out because he was confused by the
question in the comment.)
* HistogramAppender.Append panics now because we decided to treat
staleness marker differently.
Signed-off-by: beorn7 <beorn@grafana.com>
2021-10-09 13:57:07 +00:00
|
|
|
"github.com/prometheus/prometheus/model/histogram"
|
2021-11-08 14:23:17 +00:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
2022-07-19 08:58:52 +00:00
|
|
|
"github.com/prometheus/prometheus/model/metadata"
|
2020-02-06 15:58:38 +00:00
|
|
|
"github.com/prometheus/prometheus/tsdb/chunkenc"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/chunks"
|
2023-09-14 16:57:31 +00:00
|
|
|
"github.com/prometheus/prometheus/util/annotations"
|
2016-12-25 00:40:28 +00:00
|
|
|
)
|
|
|
|
|
2017-07-04 09:24:13 +00:00
|
|
|
// The errors exposed.
|
2016-12-25 00:40:28 +00:00
|
|
|
var (
|
2022-09-20 17:05:50 +00:00
|
|
|
ErrNotFound = errors.New("not found")
|
|
|
|
// ErrOutOfOrderSample is when out of order support is disabled and the sample is out of order.
|
|
|
|
ErrOutOfOrderSample = errors.New("out of order sample")
|
|
|
|
// ErrOutOfBounds is when out of order support is disabled and the sample is older than the min valid time for the append.
|
|
|
|
ErrOutOfBounds = errors.New("out of bounds")
|
|
|
|
// ErrTooOldSample is when out of order support is enabled but the sample is outside the time window allowed.
|
|
|
|
ErrTooOldSample = errors.New("too old sample")
|
|
|
|
// ErrDuplicateSampleForTimestamp is when the sample has same timestamp but different value.
|
2024-06-04 07:54:09 +00:00
|
|
|
ErrDuplicateSampleForTimestamp = errDuplicateSampleForTimestamp{}
|
2023-10-13 07:58:48 +00:00
|
|
|
ErrOutOfOrderExemplar = errors.New("out of order exemplar")
|
|
|
|
ErrDuplicateExemplar = errors.New("duplicate exemplar")
|
|
|
|
ErrExemplarLabelLength = fmt.Errorf("label length for exemplar exceeds maximum of %d UTF-8 characters", exemplar.ExemplarMaxLabelSetLength)
|
|
|
|
ErrExemplarsDisabled = fmt.Errorf("exemplar storage is disabled or max exemplars is less than or equal to 0")
|
|
|
|
ErrNativeHistogramsDisabled = fmt.Errorf("native histograms are disabled")
|
2023-12-11 08:43:42 +00:00
|
|
|
|
|
|
|
// ErrOutOfOrderCT indicates failed append of CT to the storage
|
|
|
|
// due to CT being older the then newer sample.
|
|
|
|
// NOTE(bwplotka): This can be both an instrumentation failure or commonly expected
|
|
|
|
// behaviour, and we currently don't have a way to determine this. As a result
|
|
|
|
// it's recommended to ignore this error for now.
|
|
|
|
ErrOutOfOrderCT = fmt.Errorf("created timestamp out of order, ignoring")
|
2016-12-25 00:40:28 +00:00
|
|
|
)
|
|
|
|
|
2021-11-06 10:10:04 +00:00
|
|
|
// SeriesRef is a generic series reference. In prometheus it is either a
|
|
|
|
// HeadSeriesRef or BlockSeriesRef, though other implementations may have
|
|
|
|
// their own reference types.
|
|
|
|
type SeriesRef uint64
|
|
|
|
|
2020-02-06 15:58:38 +00:00
|
|
|
// Appendable allows creating appenders.
|
|
|
|
type Appendable interface {
|
2020-07-31 07:33:56 +00:00
|
|
|
// Appender returns a new appender for the storage. The implementation
|
|
|
|
// can choose whether or not to use the context, for deadlines or to check
|
|
|
|
// for errors.
|
2020-07-24 14:10:51 +00:00
|
|
|
Appender(ctx context.Context) Appender
|
2020-02-06 15:58:38 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 13:41:52 +00:00
|
|
|
// SampleAndChunkQueryable allows retrieving samples as well as encoded samples in form of chunks.
|
|
|
|
type SampleAndChunkQueryable interface {
|
|
|
|
Queryable
|
|
|
|
ChunkQueryable
|
|
|
|
}
|
|
|
|
|
2016-12-25 00:40:28 +00:00
|
|
|
// Storage ingests and manages samples, along with various indexes. All methods
|
2021-03-16 09:47:45 +00:00
|
|
|
// are goroutine-safe. Storage implements storage.Appender.
|
2016-12-25 00:40:28 +00:00
|
|
|
type Storage interface {
|
2020-06-24 13:41:52 +00:00
|
|
|
SampleAndChunkQueryable
|
2020-02-06 15:58:38 +00:00
|
|
|
Appendable
|
2017-11-12 00:27:45 +00:00
|
|
|
|
2017-10-18 11:08:14 +00:00
|
|
|
// StartTime returns the oldest timestamp stored in the storage.
|
|
|
|
StartTime() (int64, error)
|
|
|
|
|
2016-12-25 00:40:28 +00:00
|
|
|
// Close closes the storage and all its underlying resources.
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2021-03-16 09:47:45 +00:00
|
|
|
// ExemplarStorage ingests and manages exemplars, along with various indexes. All methods are
|
|
|
|
// goroutine-safe. ExemplarStorage implements storage.ExemplarAppender and storage.ExemplarQuerier.
|
|
|
|
type ExemplarStorage interface {
|
|
|
|
ExemplarQueryable
|
|
|
|
ExemplarAppender
|
|
|
|
}
|
|
|
|
|
2017-11-12 00:27:45 +00:00
|
|
|
// A Queryable handles queries against a storage.
|
2020-03-24 20:15:47 +00:00
|
|
|
// Use it when you need to have access to all samples without chunk encoding abstraction e.g promQL.
|
2017-11-12 00:27:45 +00:00
|
|
|
type Queryable interface {
|
|
|
|
// Querier returns a new Querier on the storage.
|
2023-09-12 10:37:38 +00:00
|
|
|
Querier(mint, maxt int64) (Querier, error)
|
2017-11-12 00:27:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 00:16:46 +00:00
|
|
|
// A MockQueryable is used for testing purposes so that a mock Querier can be used.
|
|
|
|
type MockQueryable struct {
|
|
|
|
MockQuerier Querier
|
|
|
|
}
|
|
|
|
|
2023-09-12 10:37:38 +00:00
|
|
|
func (q *MockQueryable) Querier(int64, int64) (Querier, error) {
|
2022-03-29 00:16:46 +00:00
|
|
|
return q.MockQuerier, nil
|
|
|
|
}
|
|
|
|
|
2020-03-24 20:15:47 +00:00
|
|
|
// Querier provides querying access over time series data of a fixed time range.
|
2016-12-25 00:40:28 +00:00
|
|
|
type Querier interface {
|
2020-06-24 13:41:52 +00:00
|
|
|
LabelQuerier
|
2020-03-24 20:15:47 +00:00
|
|
|
|
2016-12-25 00:40:28 +00:00
|
|
|
// Select returns a set of series that matches the given label matchers.
|
2020-03-12 09:36:09 +00:00
|
|
|
// Caller can specify if it requires returned series to be sorted. Prefer not requiring sorting for better performance.
|
|
|
|
// It allows passing hints that can help in optimising select, but it's up to implementation how this is used if used at all.
|
2023-09-12 10:37:38 +00:00
|
|
|
Select(ctx context.Context, sortSeries bool, hints *SelectHints, matchers ...*labels.Matcher) SeriesSet
|
2020-03-24 20:15:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 00:16:46 +00:00
|
|
|
// MockQuerier is used for test purposes to mock the selected series that is returned.
|
|
|
|
type MockQuerier struct {
|
|
|
|
SelectMockFunction func(sortSeries bool, hints *SelectHints, matchers ...*labels.Matcher) SeriesSet
|
|
|
|
}
|
|
|
|
|
2023-09-14 16:57:31 +00:00
|
|
|
func (q *MockQuerier) LabelValues(context.Context, string, ...*labels.Matcher) ([]string, annotations.Annotations, error) {
|
2022-03-29 00:16:46 +00:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2023-09-14 16:57:31 +00:00
|
|
|
func (q *MockQuerier) LabelNames(context.Context, ...*labels.Matcher) ([]string, annotations.Annotations, error) {
|
2022-03-29 00:16:46 +00:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *MockQuerier) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-12 10:37:38 +00:00
|
|
|
func (q *MockQuerier) Select(_ context.Context, sortSeries bool, hints *SelectHints, matchers ...*labels.Matcher) SeriesSet {
|
2022-03-29 00:16:46 +00:00
|
|
|
return q.SelectMockFunction(sortSeries, hints, matchers...)
|
|
|
|
}
|
|
|
|
|
2020-03-24 20:15:47 +00:00
|
|
|
// A ChunkQueryable handles queries against a storage.
|
|
|
|
// Use it when you need to have access to samples in encoded format.
|
|
|
|
type ChunkQueryable interface {
|
|
|
|
// ChunkQuerier returns a new ChunkQuerier on the storage.
|
2023-09-12 10:37:38 +00:00
|
|
|
ChunkQuerier(mint, maxt int64) (ChunkQuerier, error)
|
2020-03-24 20:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChunkQuerier provides querying access over time series data of a fixed time range.
|
|
|
|
type ChunkQuerier interface {
|
2020-06-24 13:41:52 +00:00
|
|
|
LabelQuerier
|
2020-03-24 20:15:47 +00:00
|
|
|
|
|
|
|
// Select returns a set of series that matches the given label matchers.
|
|
|
|
// Caller can specify if it requires returned series to be sorted. Prefer not requiring sorting for better performance.
|
|
|
|
// It allows passing hints that can help in optimising select, but it's up to implementation how this is used if used at all.
|
2023-09-12 10:37:38 +00:00
|
|
|
Select(ctx context.Context, sortSeries bool, hints *SelectHints, matchers ...*labels.Matcher) ChunkSeriesSet
|
2020-03-24 20:15:47 +00:00
|
|
|
}
|
2020-01-17 11:21:44 +00:00
|
|
|
|
2020-06-24 13:41:52 +00:00
|
|
|
// LabelQuerier provides querying access over labels.
|
|
|
|
type LabelQuerier interface {
|
2016-12-25 00:40:28 +00:00
|
|
|
// LabelValues returns all potential values for a label name.
|
2021-05-31 22:49:29 +00:00
|
|
|
// It is not safe to use the strings beyond the lifetime of the querier.
|
2021-02-09 17:38:35 +00:00
|
|
|
// If matchers are specified the returned result set is reduced
|
|
|
|
// to label values of metrics matching the matchers.
|
2023-09-14 16:57:31 +00:00
|
|
|
LabelValues(ctx context.Context, name string, matchers ...*labels.Matcher) ([]string, annotations.Annotations, error)
|
2016-12-25 00:40:28 +00:00
|
|
|
|
2018-11-19 10:21:14 +00:00
|
|
|
// LabelNames returns all the unique label names present in the block in sorted order.
|
2021-07-20 12:38:08 +00:00
|
|
|
// If matchers are specified the returned result set is reduced
|
|
|
|
// to label names of metrics matching the matchers.
|
2023-09-14 16:57:31 +00:00
|
|
|
LabelNames(ctx context.Context, matchers ...*labels.Matcher) ([]string, annotations.Annotations, error)
|
2018-11-19 10:21:14 +00:00
|
|
|
|
2016-12-25 00:40:28 +00:00
|
|
|
// Close releases the resources of the Querier.
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2021-03-16 09:47:45 +00:00
|
|
|
type ExemplarQueryable interface {
|
|
|
|
// ExemplarQuerier returns a new ExemplarQuerier on the storage.
|
|
|
|
ExemplarQuerier(ctx context.Context) (ExemplarQuerier, error)
|
|
|
|
}
|
|
|
|
|
2021-05-08 03:45:29 +00:00
|
|
|
// ExemplarQuerier provides reading access to time series data.
|
2021-03-16 09:47:45 +00:00
|
|
|
type ExemplarQuerier interface {
|
|
|
|
// Select all the exemplars that match the matchers.
|
|
|
|
// Within a single slice of matchers, it is an intersection. Between the slices, it is a union.
|
|
|
|
Select(start, end int64, matchers ...[]*labels.Matcher) ([]exemplar.QueryResult, error)
|
|
|
|
}
|
|
|
|
|
2020-03-12 09:36:09 +00:00
|
|
|
// SelectHints specifies hints passed for data selections.
|
|
|
|
// This is used only as an option for implementation to use.
|
|
|
|
type SelectHints struct {
|
2018-07-18 03:58:00 +00:00
|
|
|
Start int64 // Start time in milliseconds for this select.
|
|
|
|
End int64 // End time in milliseconds for this select.
|
|
|
|
|
2018-01-09 16:44:23 +00:00
|
|
|
Step int64 // Query step size in milliseconds.
|
|
|
|
Func string // String representation of surrounding function or aggregation.
|
2019-12-05 14:06:28 +00:00
|
|
|
|
|
|
|
Grouping []string // List of label names used in aggregation.
|
|
|
|
By bool // Indicate whether it is without or by.
|
|
|
|
Range int64 // Range vector selector range in milliseconds.
|
2021-11-03 10:08:34 +00:00
|
|
|
|
2024-01-29 11:57:27 +00:00
|
|
|
// ShardCount is the total number of shards that series should be split into
|
|
|
|
// at query time. Then, only series in the ShardIndex shard will be returned
|
|
|
|
// by the query.
|
|
|
|
//
|
|
|
|
// ShardCount equal to 0 means that sharding is disabled.
|
|
|
|
ShardCount uint64
|
|
|
|
|
|
|
|
// ShardIndex is the series shard index to query. The index must be between 0 and ShardCount-1.
|
|
|
|
// When ShardCount is set to a value > 0, then a query will only process series within the
|
|
|
|
// ShardIndex's shard.
|
|
|
|
//
|
|
|
|
// Series are sharded by "labels stable hash" mod "ShardCount".
|
|
|
|
ShardIndex uint64
|
|
|
|
|
2021-11-03 10:08:34 +00:00
|
|
|
// DisableTrimming allows to disable trimming of matching series chunks based on query Start and End time.
|
|
|
|
// When disabled, the result may contain samples outside the queried time range but Select() performances
|
|
|
|
// may be improved.
|
|
|
|
DisableTrimming bool
|
2018-01-09 16:44:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 13:41:52 +00:00
|
|
|
// TODO(bwplotka): Move to promql/engine_test.go?
|
2017-11-12 00:27:45 +00:00
|
|
|
// QueryableFunc is an adapter to allow the use of ordinary functions as
|
|
|
|
// Queryables. It follows the idea of http.HandlerFunc.
|
2023-09-12 10:37:38 +00:00
|
|
|
type QueryableFunc func(mint, maxt int64) (Querier, error)
|
2017-11-12 00:27:45 +00:00
|
|
|
|
|
|
|
// Querier calls f() with the given parameters.
|
2023-09-12 10:37:38 +00:00
|
|
|
func (f QueryableFunc) Querier(mint, maxt int64) (Querier, error) {
|
|
|
|
return f(mint, maxt)
|
2017-11-12 00:27:45 +00:00
|
|
|
}
|
|
|
|
|
2016-12-25 00:40:28 +00:00
|
|
|
// Appender provides batched appends against a storage.
|
2020-02-07 16:24:17 +00:00
|
|
|
// It must be completed with a call to Commit or Rollback and must not be reused afterwards.
|
|
|
|
//
|
|
|
|
// Operations on the Appender interface are not goroutine-safe.
|
2021-11-26 12:13:27 +00:00
|
|
|
//
|
|
|
|
// The type of samples (float64, histogram, etc) appended for a given series must remain same within an Appender.
|
|
|
|
// The behaviour is undefined if samples of different types are appended to the same series in a single Commit().
|
2016-12-25 00:40:28 +00:00
|
|
|
type Appender interface {
|
2021-02-18 12:07:00 +00:00
|
|
|
// Append adds a sample pair for the given series.
|
2021-11-17 10:21:27 +00:00
|
|
|
// An optional series reference can be provided to accelerate calls.
|
|
|
|
// A series reference number is returned which can be used to add further
|
|
|
|
// samples to the given series in the same or later transactions.
|
2020-02-07 16:24:17 +00:00
|
|
|
// Returned reference numbers are ephemeral and may be rejected in calls
|
2021-02-18 12:07:00 +00:00
|
|
|
// to Append() at any point. Adding the sample via Append() returns a new
|
2020-02-07 16:24:17 +00:00
|
|
|
// reference number.
|
|
|
|
// If the reference is 0 it must not be used for caching.
|
2021-11-06 10:10:04 +00:00
|
|
|
Append(ref SeriesRef, l labels.Labels, t int64, v float64) (SeriesRef, error)
|
2016-12-25 00:40:28 +00:00
|
|
|
|
2020-03-09 17:24:18 +00:00
|
|
|
// Commit submits the collected samples and purges the batch. If Commit
|
|
|
|
// returns a non-nil error, it also rolls back all modifications made in
|
|
|
|
// the appender so far, as Rollback would do. In any case, an Appender
|
|
|
|
// must not be used anymore after Commit has been called.
|
2016-12-25 00:40:28 +00:00
|
|
|
Commit() error
|
2017-01-13 13:48:01 +00:00
|
|
|
|
2020-02-07 16:24:17 +00:00
|
|
|
// Rollback rolls back all modifications made in the appender so far.
|
2020-02-11 16:34:09 +00:00
|
|
|
// Appender has to be discarded after rollback.
|
2017-01-13 13:48:01 +00:00
|
|
|
Rollback() error
|
2022-08-10 15:54:37 +00:00
|
|
|
|
2021-03-16 09:47:45 +00:00
|
|
|
ExemplarAppender
|
2021-06-28 15:00:55 +00:00
|
|
|
HistogramAppender
|
2022-07-19 08:58:52 +00:00
|
|
|
MetadataUpdater
|
2023-12-11 08:43:42 +00:00
|
|
|
CreatedTimestampAppender
|
2021-03-16 09:47:45 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 19:28:55 +00:00
|
|
|
// GetRef is an extra interface on Appenders used by downstream projects
|
|
|
|
// (e.g. Cortex) to avoid maintaining a parallel set of references.
|
|
|
|
type GetRef interface {
|
2021-03-24 15:24:58 +00:00
|
|
|
// Returns reference number that can be used to pass to Appender.Append(),
|
|
|
|
// and a set of labels that will not cause another copy when passed to Appender.Append().
|
2021-03-19 19:28:55 +00:00
|
|
|
// 0 means the appender does not have a reference to this series.
|
2022-10-24 08:17:45 +00:00
|
|
|
// hash should be a hash of lset.
|
|
|
|
GetRef(lset labels.Labels, hash uint64) (SeriesRef, labels.Labels)
|
2021-03-19 19:28:55 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 09:47:45 +00:00
|
|
|
// ExemplarAppender provides an interface for adding samples to exemplar storage, which
|
|
|
|
// within Prometheus is in-memory only.
|
|
|
|
type ExemplarAppender interface {
|
|
|
|
// AppendExemplar adds an exemplar for the given series labels.
|
|
|
|
// An optional reference number can be provided to accelerate calls.
|
|
|
|
// A reference number is returned which can be used to add further
|
|
|
|
// exemplars in the same or later transactions.
|
|
|
|
// Returned reference numbers are ephemeral and may be rejected in calls
|
|
|
|
// to Append() at any point. Adding the sample via Append() returns a new
|
|
|
|
// reference number.
|
|
|
|
// If the reference is 0 it must not be used for caching.
|
|
|
|
// Note that in our current implementation of Prometheus' exemplar storage
|
|
|
|
// calls to Append should generate the reference numbers, AppendExemplar
|
|
|
|
// generating a new reference number should be considered possible erroneous behaviour and be logged.
|
2021-11-06 10:10:04 +00:00
|
|
|
AppendExemplar(ref SeriesRef, l labels.Labels, e exemplar.Exemplar) (SeriesRef, error)
|
2016-12-25 00:40:28 +00:00
|
|
|
}
|
|
|
|
|
Style cleanup of all the changes in sparsehistogram so far
A lot of this code was hacked together, literally during a
hackathon. This commit intends not to change the code substantially,
but just make the code obey the usual style practices.
A (possibly incomplete) list of areas:
* Generally address linter warnings.
* The `pgk` directory is deprecated as per dev-summit. No new packages should
be added to it. I moved the new `pkg/histogram` package to `model`
anticipating what's proposed in #9478.
* Make the naming of the Sparse Histogram more consistent. Including
abbreviations, there were just too many names for it: SparseHistogram,
Histogram, Histo, hist, his, shs, h. The idea is to call it "Histogram" in
general. Only add "Sparse" if it is needed to avoid confusion with
conventional Histograms (which is rare because the TSDB really has no notion
of conventional Histograms). Use abbreviations only in local scope, and then
really abbreviate (not just removing three out of seven letters like in
"Histo"). This is in the spirit of
https://github.com/golang/go/wiki/CodeReviewComments#variable-names
* Several other minor name changes.
* A lot of formatting of doc comments. For one, following
https://github.com/golang/go/wiki/CodeReviewComments#comment-sentences
, but also layout question, anticipating how things will look like
when rendered by `godoc` (even where `godoc` doesn't render them
right now because they are for unexported types or not a doc comment
at all but just a normal code comment - consistency is queen!).
* Re-enabled `TestQueryLog` and `TestEndopints` (they pass now,
leaving them disabled was presumably an oversight).
* Bucket iterator for histogram.Histogram is now created with a
method.
* HistogramChunk.iterator now allows iterator recycling. (I think
@dieterbe only commented it out because he was confused by the
question in the comment.)
* HistogramAppender.Append panics now because we decided to treat
staleness marker differently.
Signed-off-by: beorn7 <beorn@grafana.com>
2021-10-09 13:57:07 +00:00
|
|
|
// HistogramAppender provides an interface for appending histograms to the storage.
|
2021-06-28 15:00:55 +00:00
|
|
|
type HistogramAppender interface {
|
Style cleanup of all the changes in sparsehistogram so far
A lot of this code was hacked together, literally during a
hackathon. This commit intends not to change the code substantially,
but just make the code obey the usual style practices.
A (possibly incomplete) list of areas:
* Generally address linter warnings.
* The `pgk` directory is deprecated as per dev-summit. No new packages should
be added to it. I moved the new `pkg/histogram` package to `model`
anticipating what's proposed in #9478.
* Make the naming of the Sparse Histogram more consistent. Including
abbreviations, there were just too many names for it: SparseHistogram,
Histogram, Histo, hist, his, shs, h. The idea is to call it "Histogram" in
general. Only add "Sparse" if it is needed to avoid confusion with
conventional Histograms (which is rare because the TSDB really has no notion
of conventional Histograms). Use abbreviations only in local scope, and then
really abbreviate (not just removing three out of seven letters like in
"Histo"). This is in the spirit of
https://github.com/golang/go/wiki/CodeReviewComments#variable-names
* Several other minor name changes.
* A lot of formatting of doc comments. For one, following
https://github.com/golang/go/wiki/CodeReviewComments#comment-sentences
, but also layout question, anticipating how things will look like
when rendered by `godoc` (even where `godoc` doesn't render them
right now because they are for unexported types or not a doc comment
at all but just a normal code comment - consistency is queen!).
* Re-enabled `TestQueryLog` and `TestEndopints` (they pass now,
leaving them disabled was presumably an oversight).
* Bucket iterator for histogram.Histogram is now created with a
method.
* HistogramChunk.iterator now allows iterator recycling. (I think
@dieterbe only commented it out because he was confused by the
question in the comment.)
* HistogramAppender.Append panics now because we decided to treat
staleness marker differently.
Signed-off-by: beorn7 <beorn@grafana.com>
2021-10-09 13:57:07 +00:00
|
|
|
// AppendHistogram adds a histogram for the given series labels. An
|
|
|
|
// optional reference number can be provided to accelerate calls. A
|
|
|
|
// reference number is returned which can be used to add further
|
|
|
|
// histograms in the same or later transactions. Returned reference
|
|
|
|
// numbers are ephemeral and may be rejected in calls to Append() at any
|
|
|
|
// point. Adding the sample via Append() returns a new reference number.
|
2022-01-04 11:18:43 +00:00
|
|
|
// If the reference is 0, it must not be used for caching.
|
|
|
|
//
|
|
|
|
// For efficiency reasons, the histogram is passed as a
|
|
|
|
// pointer. AppendHistogram won't mutate the histogram, but in turn
|
|
|
|
// depends on the caller to not mutate it either.
|
2022-12-28 08:55:07 +00:00
|
|
|
AppendHistogram(ref SeriesRef, l labels.Labels, t int64, h *histogram.Histogram, fh *histogram.FloatHistogram) (SeriesRef, error)
|
2021-06-28 15:00:55 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 08:58:52 +00:00
|
|
|
// MetadataUpdater provides an interface for associating metadata to stored series.
|
|
|
|
type MetadataUpdater interface {
|
|
|
|
// UpdateMetadata updates a metadata entry for the given series and labels.
|
|
|
|
// A series reference number is returned which can be used to modify the
|
|
|
|
// metadata of the given series in the same or later transactions.
|
|
|
|
// Returned reference numbers are ephemeral and may be rejected in calls
|
|
|
|
// to UpdateMetadata() at any point. If the series does not exist,
|
|
|
|
// UpdateMetadata returns an error.
|
|
|
|
// If the reference is 0 it must not be used for caching.
|
|
|
|
UpdateMetadata(ref SeriesRef, l labels.Labels, m metadata.Metadata) (SeriesRef, error)
|
|
|
|
}
|
|
|
|
|
2023-12-11 08:43:42 +00:00
|
|
|
// CreatedTimestampAppender provides an interface for appending CT to storage.
|
|
|
|
type CreatedTimestampAppender interface {
|
|
|
|
// AppendCTZeroSample adds synthetic zero sample for the given ct timestamp,
|
|
|
|
// which will be associated with given series, labels and the incoming
|
|
|
|
// sample's t (timestamp). AppendCTZeroSample returns error if zero sample can't be
|
|
|
|
// appended, for example when ct is too old, or when it would collide with
|
|
|
|
// incoming sample (sample has priority).
|
|
|
|
//
|
|
|
|
// AppendCTZeroSample has to be called before the corresponding sample Append.
|
|
|
|
// A series reference number is returned which can be used to modify the
|
|
|
|
// CT for the given series in the same or later transactions.
|
|
|
|
// Returned reference numbers are ephemeral and may be rejected in calls
|
|
|
|
// to AppendCTZeroSample() at any point.
|
|
|
|
//
|
|
|
|
// If the reference is 0 it must not be used for caching.
|
|
|
|
AppendCTZeroSample(ref SeriesRef, l labels.Labels, t, ct int64) (SeriesRef, error)
|
|
|
|
}
|
|
|
|
|
2016-12-25 00:40:28 +00:00
|
|
|
// SeriesSet contains a set of series.
|
|
|
|
type SeriesSet interface {
|
|
|
|
Next() bool
|
2021-04-12 20:43:42 +00:00
|
|
|
// At returns full series. Returned series should be iterable even after Next is called.
|
2017-01-02 12:33:37 +00:00
|
|
|
At() Series
|
2020-06-09 16:57:31 +00:00
|
|
|
// The error that iteration as failed with.
|
|
|
|
// When an error occurs, set cannot continue to iterate.
|
2016-12-25 00:40:28 +00:00
|
|
|
Err() error
|
2020-06-09 16:57:31 +00:00
|
|
|
// A collection of warnings for the whole set.
|
|
|
|
// Warnings could be return even iteration has not failed with error.
|
2023-09-14 16:57:31 +00:00
|
|
|
Warnings() annotations.Annotations
|
2016-12-25 00:40:28 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 15:58:38 +00:00
|
|
|
var emptySeriesSet = errSeriesSet{}
|
|
|
|
|
|
|
|
// EmptySeriesSet returns a series set that's always empty.
|
|
|
|
func EmptySeriesSet() SeriesSet {
|
|
|
|
return emptySeriesSet
|
|
|
|
}
|
|
|
|
|
2022-03-29 00:16:46 +00:00
|
|
|
type testSeriesSet struct {
|
|
|
|
series Series
|
|
|
|
}
|
|
|
|
|
2023-09-14 16:57:31 +00:00
|
|
|
func (s testSeriesSet) Next() bool { return true }
|
|
|
|
func (s testSeriesSet) At() Series { return s.series }
|
|
|
|
func (s testSeriesSet) Err() error { return nil }
|
|
|
|
func (s testSeriesSet) Warnings() annotations.Annotations { return nil }
|
2022-03-29 00:16:46 +00:00
|
|
|
|
2023-10-03 20:09:25 +00:00
|
|
|
// TestSeriesSet returns a mock series set.
|
2022-03-29 00:16:46 +00:00
|
|
|
func TestSeriesSet(series Series) SeriesSet {
|
|
|
|
return testSeriesSet{series: series}
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:58:38 +00:00
|
|
|
type errSeriesSet struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2023-09-14 16:57:31 +00:00
|
|
|
func (s errSeriesSet) Next() bool { return false }
|
|
|
|
func (s errSeriesSet) At() Series { return nil }
|
|
|
|
func (s errSeriesSet) Err() error { return s.err }
|
|
|
|
func (s errSeriesSet) Warnings() annotations.Annotations { return nil }
|
2020-06-09 16:57:31 +00:00
|
|
|
|
|
|
|
// ErrSeriesSet returns a series set that wraps an error.
|
|
|
|
func ErrSeriesSet(err error) SeriesSet {
|
|
|
|
return errSeriesSet{err: err}
|
|
|
|
}
|
2020-02-06 15:58:38 +00:00
|
|
|
|
2020-06-24 13:41:52 +00:00
|
|
|
var emptyChunkSeriesSet = errChunkSeriesSet{}
|
|
|
|
|
|
|
|
// EmptyChunkSeriesSet returns a chunk series set that's always empty.
|
|
|
|
func EmptyChunkSeriesSet() ChunkSeriesSet {
|
|
|
|
return emptyChunkSeriesSet
|
|
|
|
}
|
|
|
|
|
|
|
|
type errChunkSeriesSet struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2023-09-14 16:57:31 +00:00
|
|
|
func (s errChunkSeriesSet) Next() bool { return false }
|
|
|
|
func (s errChunkSeriesSet) At() ChunkSeries { return nil }
|
|
|
|
func (s errChunkSeriesSet) Err() error { return s.err }
|
|
|
|
func (s errChunkSeriesSet) Warnings() annotations.Annotations { return nil }
|
2020-06-24 13:41:52 +00:00
|
|
|
|
|
|
|
// ErrChunkSeriesSet returns a chunk series set that wraps an error.
|
|
|
|
func ErrChunkSeriesSet(err error) ChunkSeriesSet {
|
|
|
|
return errChunkSeriesSet{err: err}
|
|
|
|
}
|
|
|
|
|
2020-03-24 20:15:47 +00:00
|
|
|
// Series exposes a single time series and allows iterating over samples.
|
2016-12-25 00:40:28 +00:00
|
|
|
type Series interface {
|
2020-03-24 20:15:47 +00:00
|
|
|
Labels
|
2021-04-12 20:43:42 +00:00
|
|
|
SampleIterable
|
2020-03-24 20:15:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 00:16:46 +00:00
|
|
|
type mockSeries struct {
|
|
|
|
timestamps []int64
|
|
|
|
values []float64
|
|
|
|
labelSet []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s mockSeries) Labels() labels.Labels {
|
|
|
|
return labels.FromStrings(s.labelSet...)
|
|
|
|
}
|
|
|
|
|
2022-09-20 17:16:45 +00:00
|
|
|
func (s mockSeries) Iterator(chunkenc.Iterator) chunkenc.Iterator {
|
2022-03-29 00:16:46 +00:00
|
|
|
return chunkenc.MockSeriesIterator(s.timestamps, s.values)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MockSeries returns a series with custom timestamps, values and labelSet.
|
|
|
|
func MockSeries(timestamps []int64, values []float64, labelSet []string) Series {
|
|
|
|
return mockSeries{
|
|
|
|
timestamps: timestamps,
|
|
|
|
values: values,
|
|
|
|
labelSet: labelSet,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 20:15:47 +00:00
|
|
|
// ChunkSeriesSet contains a set of chunked series.
|
|
|
|
type ChunkSeriesSet interface {
|
|
|
|
Next() bool
|
2021-04-12 20:43:42 +00:00
|
|
|
// At returns full chunk series. Returned series should be iterable even after Next is called.
|
2020-03-24 20:15:47 +00:00
|
|
|
At() ChunkSeries
|
2020-06-09 16:57:31 +00:00
|
|
|
// The error that iteration has failed with.
|
|
|
|
// When an error occurs, set cannot continue to iterate.
|
2020-03-24 20:15:47 +00:00
|
|
|
Err() error
|
2020-06-09 16:57:31 +00:00
|
|
|
// A collection of warnings for the whole set.
|
|
|
|
// Warnings could be return even iteration has not failed with error.
|
2023-09-14 16:57:31 +00:00
|
|
|
Warnings() annotations.Annotations
|
2020-03-24 20:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChunkSeries exposes a single time series and allows iterating over chunks.
|
|
|
|
type ChunkSeries interface {
|
|
|
|
Labels
|
2021-04-12 20:43:42 +00:00
|
|
|
ChunkIterable
|
2020-03-24 20:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Labels represents an item that has labels e.g. time series.
|
|
|
|
type Labels interface {
|
|
|
|
// Labels returns the complete set of labels. For series it means all labels identifying the series.
|
2016-12-25 00:40:28 +00:00
|
|
|
Labels() labels.Labels
|
2020-03-24 20:15:47 +00:00
|
|
|
}
|
2016-12-25 00:40:28 +00:00
|
|
|
|
2021-04-12 20:43:42 +00:00
|
|
|
type SampleIterable interface {
|
2022-09-20 17:16:45 +00:00
|
|
|
// Iterator returns an iterator of the data of the series.
|
2022-12-15 18:29:44 +00:00
|
|
|
// The iterator passed as argument is for re-use, if not nil.
|
2022-09-20 17:16:45 +00:00
|
|
|
// Depending on implementation, the iterator can
|
|
|
|
// be re-used or a new iterator can be allocated.
|
|
|
|
Iterator(chunkenc.Iterator) chunkenc.Iterator
|
2016-12-25 00:40:28 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 20:43:42 +00:00
|
|
|
type ChunkIterable interface {
|
2022-09-20 17:16:45 +00:00
|
|
|
// Iterator returns an iterator that iterates over potentially overlapping
|
2020-07-31 15:03:02 +00:00
|
|
|
// chunks of the series, sorted by min time.
|
2022-09-20 17:16:45 +00:00
|
|
|
Iterator(chunks.Iterator) chunks.Iterator
|
2020-03-24 20:15:47 +00:00
|
|
|
}
|