2017-04-19 05:43:09 -07:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
2016-12-24 16:40:28 -08:00
|
|
|
package storage
|
|
|
|
|
2017-06-12 22:22:27 -07:00
|
|
|
import (
|
2021-11-28 23:54:23 -08:00
|
|
|
"fmt"
|
2017-06-12 22:22:27 -07:00
|
|
|
"math"
|
2020-02-06 07:58:38 -08: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 06:57:07 -07:00
|
|
|
"github.com/prometheus/prometheus/model/histogram"
|
2020-02-06 07:58:38 -08:00
|
|
|
"github.com/prometheus/prometheus/tsdb/chunkenc"
|
2023-08-24 06:21:17 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb/chunks"
|
2017-06-12 22:22:27 -07:00
|
|
|
)
|
2016-12-25 02:34:22 -08:00
|
|
|
|
|
|
|
// BufferedSeriesIterator wraps an iterator with a look-back buffer.
|
|
|
|
type BufferedSeriesIterator struct {
|
2024-01-23 08:02:14 -08:00
|
|
|
hReader histogram.Histogram
|
|
|
|
fhReader histogram.FloatHistogram
|
|
|
|
|
2020-02-06 07:58:38 -08:00
|
|
|
it chunkenc.Iterator
|
2018-07-18 03:14:02 -07:00
|
|
|
buf *sampleRing
|
|
|
|
delta int64
|
2016-12-25 02:34:22 -08:00
|
|
|
|
2021-11-28 23:54:23 -08:00
|
|
|
lastTime int64
|
|
|
|
valueType chunkenc.ValueType
|
2016-12-25 02:34:22 -08:00
|
|
|
}
|
2016-12-24 16:40:28 -08:00
|
|
|
|
|
|
|
// NewBuffer returns a new iterator that buffers the values within the time range
|
2018-07-17 21:10:28 -07:00
|
|
|
// of the current element and the duration of delta before, initialized with an
|
|
|
|
// empty iterator. Use Reset() to set an actual iterator to be buffered.
|
|
|
|
func NewBuffer(delta int64) *BufferedSeriesIterator {
|
2020-02-11 08:34:09 -08:00
|
|
|
return NewBufferIterator(chunkenc.NewNopIterator(), delta)
|
2018-07-17 21:10:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBufferIterator returns a new iterator that buffers the values within the
|
|
|
|
// time range of the current element and the duration of delta before.
|
2020-02-06 07:58:38 -08:00
|
|
|
func NewBufferIterator(it chunkenc.Iterator, delta int64) *BufferedSeriesIterator {
|
2017-03-14 02:57:34 -07:00
|
|
|
bit := &BufferedSeriesIterator{
|
2022-12-08 15:44:48 -08:00
|
|
|
buf: newSampleRing(delta, 0, chunkenc.ValNone),
|
2018-07-18 03:14:02 -07:00
|
|
|
delta: delta,
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
|
|
|
bit.Reset(it)
|
2017-03-14 02:57:34 -07:00
|
|
|
|
|
|
|
return bit
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
2018-07-18 03:14:02 -07:00
|
|
|
// Reset re-uses the buffer with a new iterator, resetting the buffered time
|
|
|
|
// delta to its original value.
|
2020-02-06 07:58:38 -08:00
|
|
|
func (b *BufferedSeriesIterator) Reset(it chunkenc.Iterator) {
|
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
|
|
|
b.it = it
|
|
|
|
b.lastTime = math.MinInt64
|
|
|
|
b.buf.reset()
|
2018-07-18 03:14:02 -07:00
|
|
|
b.buf.delta = b.delta
|
2021-11-28 23:54:23 -08:00
|
|
|
b.valueType = it.Next()
|
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
|
|
|
}
|
|
|
|
|
2018-07-18 03:14:02 -07:00
|
|
|
// ReduceDelta lowers the buffered time delta, for the current SeriesIterator only.
|
|
|
|
func (b *BufferedSeriesIterator) ReduceDelta(delta int64) bool {
|
2018-12-18 03:25:45 -08:00
|
|
|
return b.buf.reduceDelta(delta)
|
2018-07-18 03:14:02 -07:00
|
|
|
}
|
|
|
|
|
2017-05-23 09:01:54 -07:00
|
|
|
// PeekBack returns the nth previous element of the iterator. If there is none buffered,
|
2016-12-24 16:40:28 -08:00
|
|
|
// ok is false.
|
2023-08-24 06:21:17 -07:00
|
|
|
func (b *BufferedSeriesIterator) PeekBack(n int) (sample chunks.Sample, ok bool) {
|
2022-12-08 04:31:08 -08:00
|
|
|
return b.buf.nthLast(n)
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
|
|
|
// Buffer returns an iterator over the buffered data. Invalidates previously
|
|
|
|
// returned iterators.
|
2023-12-11 02:12:11 -08:00
|
|
|
func (b *BufferedSeriesIterator) Buffer() *SampleRingIterator {
|
2016-12-24 16:40:28 -08:00
|
|
|
return b.buf.iterator()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek advances the iterator to the element at time t or greater.
|
2021-11-28 23:54:23 -08:00
|
|
|
func (b *BufferedSeriesIterator) Seek(t int64) chunkenc.ValueType {
|
2016-12-24 16:40:28 -08:00
|
|
|
t0 := t - b.buf.delta
|
|
|
|
|
|
|
|
// If the delta would cause us to seek backwards, preserve the buffer
|
2018-09-13 03:34:10 -07:00
|
|
|
// and just continue regular advancement while filling the buffer on the way.
|
2021-12-15 04:49:33 -08:00
|
|
|
if b.valueType != chunkenc.ValNone && t0 > b.lastTime {
|
2016-12-24 16:40:28 -08:00
|
|
|
b.buf.reset()
|
|
|
|
|
2021-11-28 23:54:23 -08:00
|
|
|
b.valueType = b.it.Seek(t0)
|
|
|
|
switch b.valueType {
|
|
|
|
case chunkenc.ValNone:
|
|
|
|
return chunkenc.ValNone
|
2023-08-16 23:42:18 -07:00
|
|
|
case chunkenc.ValFloat, chunkenc.ValHistogram, chunkenc.ValFloatHistogram:
|
|
|
|
b.lastTime = b.AtT()
|
2021-11-28 23:54:23 -08:00
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("BufferedSeriesIterator: unknown value type %v", b.valueType))
|
2021-11-02 08:01:32 -07:00
|
|
|
}
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.lastTime >= t {
|
2021-11-28 23:54:23 -08:00
|
|
|
return b.valueType
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
2021-11-28 23:54:23 -08:00
|
|
|
for {
|
|
|
|
if b.valueType = b.Next(); b.valueType == chunkenc.ValNone || b.lastTime >= t {
|
|
|
|
return b.valueType
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next advances the iterator to the next element.
|
2021-11-28 23:54:23 -08:00
|
|
|
func (b *BufferedSeriesIterator) Next() chunkenc.ValueType {
|
2016-12-24 16:40:28 -08:00
|
|
|
// Add current element to buffer before advancing.
|
2021-11-28 23:54:23 -08:00
|
|
|
switch b.valueType {
|
|
|
|
case chunkenc.ValNone:
|
|
|
|
return chunkenc.ValNone
|
|
|
|
case chunkenc.ValFloat:
|
2022-12-08 04:31:08 -08:00
|
|
|
t, f := b.it.At()
|
2022-12-08 15:44:48 -08:00
|
|
|
b.buf.addF(fSample{t: t, f: f})
|
2021-11-28 23:54:23 -08:00
|
|
|
case chunkenc.ValHistogram:
|
2024-01-23 08:02:14 -08:00
|
|
|
t, h := b.it.AtHistogram(&b.hReader)
|
2022-12-08 15:44:48 -08:00
|
|
|
b.buf.addH(hSample{t: t, h: h})
|
2021-11-28 23:54:23 -08:00
|
|
|
case chunkenc.ValFloatHistogram:
|
2024-01-23 08:02:14 -08:00
|
|
|
t, fh := b.it.AtFloatHistogram(&b.fhReader)
|
2022-12-08 15:44:48 -08:00
|
|
|
b.buf.addFH(fhSample{t: t, fh: fh})
|
2021-11-28 23:54:23 -08:00
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("BufferedSeriesIterator: unknown value type %v", b.valueType))
|
2021-11-02 08:01:32 -07:00
|
|
|
}
|
2016-12-24 16:40:28 -08:00
|
|
|
|
2021-11-28 23:54:23 -08:00
|
|
|
b.valueType = b.it.Next()
|
2021-11-30 08:19:06 -08:00
|
|
|
if b.valueType != chunkenc.ValNone {
|
|
|
|
b.lastTime = b.AtT()
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
2021-11-28 23:54:23 -08:00
|
|
|
return b.valueType
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
2021-11-29 02:53:04 -08:00
|
|
|
// At returns the current float element of the iterator.
|
|
|
|
func (b *BufferedSeriesIterator) At() (int64, float64) {
|
2017-01-02 04:33:37 -08:00
|
|
|
return b.it.At()
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
2021-11-29 02:53:04 -08:00
|
|
|
// AtHistogram returns the current histogram element of the iterator.
|
2024-01-23 08:02:14 -08:00
|
|
|
func (b *BufferedSeriesIterator) AtHistogram(fh *histogram.Histogram) (int64, *histogram.Histogram) {
|
|
|
|
return b.it.AtHistogram(fh)
|
2021-11-02 08:01:32 -07:00
|
|
|
}
|
|
|
|
|
2021-11-29 02:53:04 -08:00
|
|
|
// AtFloatHistogram returns the current float-histogram element of the iterator.
|
2024-01-23 08:02:14 -08:00
|
|
|
func (b *BufferedSeriesIterator) AtFloatHistogram(fh *histogram.FloatHistogram) (int64, *histogram.FloatHistogram) {
|
|
|
|
return b.it.AtFloatHistogram(fh)
|
2021-11-02 08:01:32 -07:00
|
|
|
}
|
|
|
|
|
2021-11-30 08:19:06 -08:00
|
|
|
// AtT returns the current timestamp of the iterator.
|
|
|
|
func (b *BufferedSeriesIterator) AtT() int64 {
|
|
|
|
return b.it.AtT()
|
|
|
|
}
|
|
|
|
|
2016-12-24 16:40:28 -08:00
|
|
|
// Err returns the last encountered error.
|
|
|
|
func (b *BufferedSeriesIterator) Err() error {
|
|
|
|
return b.it.Err()
|
|
|
|
}
|
|
|
|
|
2022-12-08 04:31:08 -08:00
|
|
|
type fSample struct {
|
|
|
|
t int64
|
|
|
|
f float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s fSample) T() int64 {
|
|
|
|
return s.t
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:50:13 -07:00
|
|
|
func (s fSample) F() float64 {
|
2022-12-08 04:31:08 -08:00
|
|
|
return s.f
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s fSample) H() *histogram.Histogram {
|
|
|
|
panic("H() called for fSample")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s fSample) FH() *histogram.FloatHistogram {
|
|
|
|
panic("FH() called for fSample")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s fSample) Type() chunkenc.ValueType {
|
|
|
|
return chunkenc.ValFloat
|
|
|
|
}
|
|
|
|
|
|
|
|
type hSample struct {
|
|
|
|
t int64
|
|
|
|
h *histogram.Histogram
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s hSample) T() int64 {
|
|
|
|
return s.t
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:50:13 -07:00
|
|
|
func (s hSample) F() float64 {
|
2022-12-08 04:31:08 -08:00
|
|
|
panic("F() called for hSample")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s hSample) H() *histogram.Histogram {
|
|
|
|
return s.h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s hSample) FH() *histogram.FloatHistogram {
|
2023-11-29 06:15:57 -08:00
|
|
|
return s.h.ToFloat(nil)
|
2022-12-08 04:31:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s hSample) Type() chunkenc.ValueType {
|
|
|
|
return chunkenc.ValHistogram
|
|
|
|
}
|
|
|
|
|
|
|
|
type fhSample struct {
|
2021-11-28 23:54:23 -08:00
|
|
|
t int64
|
|
|
|
fh *histogram.FloatHistogram
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
2022-12-08 04:31:08 -08:00
|
|
|
func (s fhSample) T() int64 {
|
2020-03-24 13:15:47 -07:00
|
|
|
return s.t
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:50:13 -07:00
|
|
|
func (s fhSample) F() float64 {
|
2022-12-08 04:31:08 -08:00
|
|
|
panic("F() called for fhSample")
|
2020-03-24 13:15:47 -07:00
|
|
|
}
|
|
|
|
|
2022-12-08 04:31:08 -08:00
|
|
|
func (s fhSample) H() *histogram.Histogram {
|
|
|
|
panic("H() called for fhSample")
|
2021-11-02 08:01:32 -07:00
|
|
|
}
|
|
|
|
|
2022-12-08 04:31:08 -08:00
|
|
|
func (s fhSample) FH() *histogram.FloatHistogram {
|
2021-11-28 23:54:23 -08:00
|
|
|
return s.fh
|
|
|
|
}
|
|
|
|
|
2022-12-08 04:31:08 -08:00
|
|
|
func (s fhSample) Type() chunkenc.ValueType {
|
|
|
|
return chunkenc.ValFloatHistogram
|
2021-11-28 23:54:23 -08:00
|
|
|
}
|
|
|
|
|
2016-12-24 16:40:28 -08:00
|
|
|
type sampleRing struct {
|
|
|
|
delta int64
|
|
|
|
|
2023-05-03 11:06:12 -07:00
|
|
|
// Lookback buffers. We use iBuf for mixed samples, but one of the three
|
2022-12-08 15:44:48 -08:00
|
|
|
// concrete ones for homogenous samples. (Only one of the four bufs is
|
|
|
|
// allowed to be populated!) This avoids the overhead of the interface
|
|
|
|
// wrapper for the happy (and by far most common) case of homogenous
|
|
|
|
// samples.
|
2023-08-24 06:21:17 -07:00
|
|
|
iBuf []chunks.Sample
|
2023-05-03 11:06:12 -07:00
|
|
|
fBuf []fSample
|
|
|
|
hBuf []hSample
|
|
|
|
fhBuf []fhSample
|
|
|
|
bufInUse bufType
|
2022-12-08 15:44:48 -08:00
|
|
|
|
|
|
|
i int // Position of most recent element in ring buffer.
|
|
|
|
f int // Position of first element in ring buffer.
|
|
|
|
l int // Number of elements in buffer.
|
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
|
|
|
|
2023-12-11 02:12:11 -08:00
|
|
|
it SampleRingIterator
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
2023-05-03 11:06:12 -07:00
|
|
|
type bufType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
noBuf bufType = iota // Nothing yet stored in sampleRing.
|
|
|
|
iBuf
|
|
|
|
fBuf
|
|
|
|
hBuf
|
|
|
|
fhBuf
|
|
|
|
)
|
|
|
|
|
2022-12-08 15:44:48 -08:00
|
|
|
// newSampleRing creates a new sampleRing. If you do not know the prefereed
|
|
|
|
// value type yet, use a size of 0 (in which case the provided typ doesn't
|
|
|
|
// matter). On the first add, a buffer of size 16 will be allocated with the
|
|
|
|
// preferred type being the type of the first added sample.
|
|
|
|
func newSampleRing(delta int64, size int, typ chunkenc.ValueType) *sampleRing {
|
|
|
|
r := &sampleRing{delta: delta}
|
2016-12-24 16:40:28 -08:00
|
|
|
r.reset()
|
2022-12-08 15:44:48 -08:00
|
|
|
if size <= 0 {
|
|
|
|
// Will initialize on first add.
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
switch typ {
|
|
|
|
case chunkenc.ValFloat:
|
|
|
|
r.fBuf = make([]fSample, size)
|
|
|
|
case chunkenc.ValHistogram:
|
|
|
|
r.hBuf = make([]hSample, size)
|
|
|
|
case chunkenc.ValFloatHistogram:
|
|
|
|
r.fhBuf = make([]fhSample, size)
|
|
|
|
default:
|
storage: Fix mixed samples handling in sampleRing
Two issues are fixed here, that lead to the same problem:
1. If `newSampleRing` is called with an unknown ValueType including
ValueNone, we have initialized the interface buffer (`iBuf`).
However, we would still use a specialized buffer for the first
sample, opportunistically assuming that we might still not
encounter mixed samples and we should go down the more efficient
road.
2. If the `sampleRing` is `reset`, we leave all buffers alone,
including `iBuf`, which is generally fine, but not for `iBuf`, see
below.
In both cases, `iBuf` already contains values, but we will fill one of
the specialized buffers first. Once we then actually encounter mixed
samples, the content of the specialized buffer is copied into `iBuf`
using `append`. That's by itself the right idea because `iBuf` might
be `nil`, and even if not, it might or might not have the right
capacity. However, this approach assumes that `iBuf` is empty, or more
precisely has a length of zero.
This commit makes sure that `iBuf` does not get needlessly initialized
in `newSampleRing` and that it is emptied upon `reset`.
A test case is added to demonstrate both issues above.
Signed-off-by: beorn7 <beorn@grafana.com>
2023-10-31 06:50:26 -07:00
|
|
|
// Do not initialize anything because the 1st sample will be
|
|
|
|
// added to one of the other bufs anyway.
|
2022-12-08 15:44:48 -08:00
|
|
|
}
|
2016-12-24 16:40:28 -08:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *sampleRing) reset() {
|
|
|
|
r.l = 0
|
|
|
|
r.i = -1
|
|
|
|
r.f = 0
|
2023-05-03 11:06:12 -07:00
|
|
|
r.bufInUse = noBuf
|
storage: Fix mixed samples handling in sampleRing
Two issues are fixed here, that lead to the same problem:
1. If `newSampleRing` is called with an unknown ValueType including
ValueNone, we have initialized the interface buffer (`iBuf`).
However, we would still use a specialized buffer for the first
sample, opportunistically assuming that we might still not
encounter mixed samples and we should go down the more efficient
road.
2. If the `sampleRing` is `reset`, we leave all buffers alone,
including `iBuf`, which is generally fine, but not for `iBuf`, see
below.
In both cases, `iBuf` already contains values, but we will fill one of
the specialized buffers first. Once we then actually encounter mixed
samples, the content of the specialized buffer is copied into `iBuf`
using `append`. That's by itself the right idea because `iBuf` might
be `nil`, and even if not, it might or might not have the right
capacity. However, this approach assumes that `iBuf` is empty, or more
precisely has a length of zero.
This commit makes sure that `iBuf` does not get needlessly initialized
in `newSampleRing` and that it is emptied upon `reset`.
A test case is added to demonstrate both issues above.
Signed-off-by: beorn7 <beorn@grafana.com>
2023-10-31 06:50:26 -07:00
|
|
|
|
|
|
|
// The first sample after the reset will always go to a specialized
|
|
|
|
// buffer. If we later need to change to the interface buffer, we'll
|
|
|
|
// copy from the specialized buffer to the interface buffer. For that to
|
|
|
|
// work properly, we have to reset the interface buffer here, too.
|
|
|
|
r.iBuf = r.iBuf[:0]
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
2018-11-27 08:44:29 -08:00
|
|
|
// Returns the current iterator. Invalidates previously returned iterators.
|
2023-12-11 02:12:11 -08:00
|
|
|
func (r *sampleRing) iterator() *SampleRingIterator {
|
Optimise PromQL (#3966)
* Move range logic to 'eval'
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make aggregegate range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* PromQL is statically typed, so don't eval to find the type.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Extend rangewrapper to multiple exprs
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Start making function evaluation ranged
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make instant queries a special case of range queries
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Eliminate evalString
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Evaluate range vector functions one series at a time
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make unary operators range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make binops range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Pass time to range-aware functions.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple _over_time functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce allocs when working with matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add basic benchmark for range evaluation
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse objects for function arguments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Do dropmetricname and allocating output vector only once.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add range-aware support for range vector functions with params
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise holt_winters, cut cpu and allocs by ~25%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make rate&friends range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware. Document calling convention.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make date functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make simple math functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Convert more functions to be range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make more functions range aware
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Specialcase timestamp() with vector selector arg for range awareness
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove transition code for functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the rest of the engine transition code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove more obselete code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove the last uses of the eval* functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove engine finalizers to prevent corruption
The finalizers set by matrixSelector were being called
just before the value they were retruning to the pool
was then being provided to the caller. Thus a concurrent query
could corrupt the data that the user has just been returned.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add new benchmark suite for range functinos
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Migrate existing benchmarks to new system
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand promql benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simply test by removing unused range code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* When testing instant queries, check range queries too.
To protect against subsequent steps in a range query being
affected by the previous steps, add a test that evaluates
an instant query that we know works again as a range query
with the tiimestamp we care about not being the first step.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse ring for matrix iters. Put query results back in pool.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse buffer when iterating over matrix selectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Unary minus should remove metric name
Cut down benchmarks for faster runs.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reduce repetition in benchmark test cases
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Work series by series when doing normal vectorSelectors
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise benchmark setup, cuts time by 60%
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Have rangeWrapper use an evalNodeHelper to cache across steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use evalNodeHelper with functions
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cache dropMetricName within a node evaluation.
This saves both the calculations and allocs done by dropMetricName
across steps.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse input vectors in rangewrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Reuse the point slices in the matrixes input/output by rangeWrapper
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make benchmark setup faster using AddFast
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Simplify benchmark code.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add caching in VectorBinop
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Use xor to have one-level resultMetric hash key
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Add more benchmarks
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Call Query.Close in apiv1
This allows point slices allocated for the response data
to be reused by later queries, saving allocations.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise histogram_quantile
It's now 5-10% faster with 97% less garbage generated for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make the input collection in rangeVector linear rather than quadratic
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_replace, for 1k steps 15x fewer allocs and 3x faster
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Optimise label_join, 1.8x faster and 11x less memory for 1k steps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Expand benchmarks, cleanup comments, simplify numSteps logic.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Fabian's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Comments from Alin.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address jrv's comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Remove dead code
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Address Simon's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Rename populateIterators, pre-init some sizes
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Handle case where function has non-matrix args first
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Split rangeWrapper out to rangeEval function, improve comments
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Cleanup and make things more consistent
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Make EvalNodeHelper public
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
* Fabian's comments.
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
2018-06-04 06:47:45 -07:00
|
|
|
r.it.r = r
|
|
|
|
r.it.i = -1
|
|
|
|
return &r.it
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
2023-12-12 23:30:02 -08:00
|
|
|
// SampleRingIterator is returned by BufferedSeriesIterator.Buffer() and can be
|
|
|
|
// used to iterate samples buffered in the lookback window.
|
2023-12-11 02:12:11 -08:00
|
|
|
type SampleRingIterator struct {
|
2022-11-01 10:34:47 -07:00
|
|
|
r *sampleRing
|
|
|
|
i int
|
|
|
|
t int64
|
2022-12-08 15:44:48 -08:00
|
|
|
f float64
|
2022-11-01 10:34:47 -07:00
|
|
|
h *histogram.Histogram
|
|
|
|
fh *histogram.FloatHistogram
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
2023-12-11 02:12:11 -08:00
|
|
|
func (it *SampleRingIterator) Next() chunkenc.ValueType {
|
2016-12-24 16:40:28 -08:00
|
|
|
it.i++
|
2021-11-28 23:54:23 -08:00
|
|
|
if it.i >= it.r.l {
|
|
|
|
return chunkenc.ValNone
|
|
|
|
}
|
2023-05-03 11:06:12 -07:00
|
|
|
switch it.r.bufInUse {
|
|
|
|
case fBuf:
|
2022-12-08 15:44:48 -08:00
|
|
|
s := it.r.atF(it.i)
|
|
|
|
it.t = s.t
|
|
|
|
it.f = s.f
|
|
|
|
return chunkenc.ValFloat
|
2023-05-03 11:06:12 -07:00
|
|
|
case hBuf:
|
2022-12-08 15:44:48 -08:00
|
|
|
s := it.r.atH(it.i)
|
|
|
|
it.t = s.t
|
|
|
|
it.h = s.h
|
|
|
|
return chunkenc.ValHistogram
|
2023-05-03 11:06:12 -07:00
|
|
|
case fhBuf:
|
2022-12-08 15:44:48 -08:00
|
|
|
s := it.r.atFH(it.i)
|
|
|
|
it.t = s.t
|
|
|
|
it.fh = s.fh
|
|
|
|
return chunkenc.ValFloatHistogram
|
|
|
|
}
|
2021-11-28 23:54:23 -08:00
|
|
|
s := it.r.at(it.i)
|
2022-12-08 04:31:08 -08:00
|
|
|
it.t = s.T()
|
|
|
|
switch s.Type() {
|
|
|
|
case chunkenc.ValHistogram:
|
|
|
|
it.h = s.H()
|
2023-04-14 02:59:30 -07:00
|
|
|
it.fh = nil
|
2021-11-28 23:54:23 -08:00
|
|
|
return chunkenc.ValHistogram
|
2022-12-08 04:31:08 -08:00
|
|
|
case chunkenc.ValFloatHistogram:
|
|
|
|
it.fh = s.FH()
|
2023-04-14 02:59:30 -07:00
|
|
|
it.h = nil
|
2021-11-28 23:54:23 -08:00
|
|
|
return chunkenc.ValFloatHistogram
|
|
|
|
default:
|
2023-03-30 10:50:13 -07:00
|
|
|
it.f = s.F()
|
2021-11-28 23:54:23 -08:00
|
|
|
return chunkenc.ValFloat
|
|
|
|
}
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
2023-12-12 23:35:02 -08:00
|
|
|
// At returns the current float element of the iterator.
|
2023-12-11 02:12:11 -08:00
|
|
|
func (it *SampleRingIterator) At() (int64, float64) {
|
2022-12-08 15:44:48 -08:00
|
|
|
return it.t, it.f
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
2023-12-12 23:35:02 -08:00
|
|
|
// AtHistogram returns the current histogram element of the iterator.
|
2023-12-11 02:12:11 -08:00
|
|
|
func (it *SampleRingIterator) AtHistogram() (int64, *histogram.Histogram) {
|
2022-11-01 10:34:47 -07:00
|
|
|
return it.t, it.h
|
2021-06-30 07:48:13 -07:00
|
|
|
}
|
|
|
|
|
2023-12-12 23:35:02 -08:00
|
|
|
// AtFloatHistogram returns the current histogram element of the iterator. If the
|
|
|
|
// current sample is an integer histogram, it will be converted to a float histogram.
|
|
|
|
// An optional histogram.FloatHistogram can be provided to avoid allocating a new
|
|
|
|
// object for the conversion.
|
2023-12-11 02:12:11 -08:00
|
|
|
func (it *SampleRingIterator) AtFloatHistogram(fh *histogram.FloatHistogram) (int64, *histogram.FloatHistogram) {
|
2022-11-01 10:34:47 -07:00
|
|
|
if it.fh == nil {
|
2023-12-10 12:07:19 -08:00
|
|
|
return it.t, it.h.ToFloat(fh)
|
2021-11-28 23:54:23 -08:00
|
|
|
}
|
2024-01-23 08:02:14 -08:00
|
|
|
if fh != nil {
|
|
|
|
it.fh.CopyTo(fh)
|
|
|
|
return it.t, fh
|
|
|
|
}
|
|
|
|
return it.t, it.fh.Copy()
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
2023-12-11 02:12:11 -08:00
|
|
|
func (it *SampleRingIterator) AtT() int64 {
|
2022-11-01 10:34:47 -07:00
|
|
|
return it.t
|
2021-11-02 08:01:32 -07:00
|
|
|
}
|
|
|
|
|
2023-08-24 06:21:17 -07:00
|
|
|
func (r *sampleRing) at(i int) chunks.Sample {
|
2023-05-03 11:06:12 -07:00
|
|
|
j := (r.f + i) % len(r.iBuf)
|
|
|
|
return r.iBuf[j]
|
2021-11-02 08:01:32 -07:00
|
|
|
}
|
|
|
|
|
2022-12-08 15:44:48 -08:00
|
|
|
func (r *sampleRing) atF(i int) fSample {
|
|
|
|
j := (r.f + i) % len(r.fBuf)
|
|
|
|
return r.fBuf[j]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *sampleRing) atH(i int) hSample {
|
|
|
|
j := (r.f + i) % len(r.hBuf)
|
|
|
|
return r.hBuf[j]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *sampleRing) atFH(i int) fhSample {
|
|
|
|
j := (r.f + i) % len(r.fhBuf)
|
|
|
|
return r.fhBuf[j]
|
|
|
|
}
|
|
|
|
|
|
|
|
// add adds a sample to the ring buffer and frees all samples that fall out of
|
|
|
|
// the delta range. Note that this method works for any sample
|
|
|
|
// implementation. If you know you are dealing with one of the implementations
|
|
|
|
// from this package (fSample, hSample, fhSample), call one of the specialized
|
|
|
|
// methods addF, addH, or addFH for better performance.
|
2023-08-24 06:21:17 -07:00
|
|
|
func (r *sampleRing) add(s chunks.Sample) {
|
2023-05-03 11:06:12 -07:00
|
|
|
if r.bufInUse == noBuf {
|
|
|
|
// First sample.
|
|
|
|
switch s := s.(type) {
|
|
|
|
case fSample:
|
|
|
|
r.bufInUse = fBuf
|
|
|
|
r.fBuf = addF(s, r.fBuf, r)
|
|
|
|
case hSample:
|
|
|
|
r.bufInUse = hBuf
|
|
|
|
r.hBuf = addH(s, r.hBuf, r)
|
|
|
|
case fhSample:
|
|
|
|
r.bufInUse = fhBuf
|
|
|
|
r.fhBuf = addFH(s, r.fhBuf, r)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.bufInUse != iBuf {
|
2022-12-08 15:44:48 -08:00
|
|
|
// Nothing added to the interface buf yet. Let's check if we can
|
|
|
|
// stay specialized.
|
|
|
|
switch s := s.(type) {
|
|
|
|
case fSample:
|
2023-05-03 11:06:12 -07:00
|
|
|
if r.bufInUse == fBuf {
|
2023-04-13 08:42:40 -07:00
|
|
|
r.fBuf = addF(s, r.fBuf, r)
|
2022-12-08 15:44:48 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case hSample:
|
2023-05-03 11:06:12 -07:00
|
|
|
if r.bufInUse == hBuf {
|
2023-04-13 08:42:40 -07:00
|
|
|
r.hBuf = addH(s, r.hBuf, r)
|
2022-12-08 15:44:48 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case fhSample:
|
2023-05-03 11:06:12 -07:00
|
|
|
if r.bufInUse == fhBuf {
|
2023-04-13 08:42:40 -07:00
|
|
|
r.fhBuf = addFH(s, r.fhBuf, r)
|
2022-12-08 15:44:48 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The new sample isn't a fit for the already existing
|
|
|
|
// ones. Copy the latter into the interface buffer where needed.
|
storage: Fix mixed samples handling in sampleRing
Two issues are fixed here, that lead to the same problem:
1. If `newSampleRing` is called with an unknown ValueType including
ValueNone, we have initialized the interface buffer (`iBuf`).
However, we would still use a specialized buffer for the first
sample, opportunistically assuming that we might still not
encounter mixed samples and we should go down the more efficient
road.
2. If the `sampleRing` is `reset`, we leave all buffers alone,
including `iBuf`, which is generally fine, but not for `iBuf`, see
below.
In both cases, `iBuf` already contains values, but we will fill one of
the specialized buffers first. Once we then actually encounter mixed
samples, the content of the specialized buffer is copied into `iBuf`
using `append`. That's by itself the right idea because `iBuf` might
be `nil`, and even if not, it might or might not have the right
capacity. However, this approach assumes that `iBuf` is empty, or more
precisely has a length of zero.
This commit makes sure that `iBuf` does not get needlessly initialized
in `newSampleRing` and that it is emptied upon `reset`.
A test case is added to demonstrate both issues above.
Signed-off-by: beorn7 <beorn@grafana.com>
2023-10-31 06:50:26 -07:00
|
|
|
// The interface buffer is assumed to be of length zero at this point.
|
2023-05-03 11:06:12 -07:00
|
|
|
switch r.bufInUse {
|
|
|
|
case fBuf:
|
2022-12-08 15:44:48 -08:00
|
|
|
for _, s := range r.fBuf {
|
2023-05-03 11:06:12 -07:00
|
|
|
r.iBuf = append(r.iBuf, s)
|
2022-12-08 15:44:48 -08:00
|
|
|
}
|
|
|
|
r.fBuf = nil
|
2023-05-03 11:06:12 -07:00
|
|
|
case hBuf:
|
2022-12-08 15:44:48 -08:00
|
|
|
for _, s := range r.hBuf {
|
2023-05-03 11:06:12 -07:00
|
|
|
r.iBuf = append(r.iBuf, s)
|
2022-12-08 15:44:48 -08:00
|
|
|
}
|
|
|
|
r.hBuf = nil
|
2023-05-03 11:06:12 -07:00
|
|
|
case fhBuf:
|
2022-12-08 15:44:48 -08:00
|
|
|
for _, s := range r.fhBuf {
|
2023-05-03 11:06:12 -07:00
|
|
|
r.iBuf = append(r.iBuf, s)
|
2022-12-08 15:44:48 -08:00
|
|
|
}
|
|
|
|
r.fhBuf = nil
|
|
|
|
}
|
2023-05-03 11:06:12 -07:00
|
|
|
r.bufInUse = iBuf
|
2022-12-08 15:44:48 -08:00
|
|
|
}
|
2023-05-03 11:06:12 -07:00
|
|
|
r.iBuf = addSample(s, r.iBuf, r)
|
2022-12-08 15:44:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// addF is a version of the add method specialized for fSample.
|
|
|
|
func (r *sampleRing) addF(s fSample) {
|
2023-05-03 11:06:12 -07:00
|
|
|
switch r.bufInUse {
|
|
|
|
case fBuf: // Add to existing fSamples.
|
|
|
|
r.fBuf = addF(s, r.fBuf, r)
|
|
|
|
case noBuf: // Add first sample.
|
|
|
|
r.fBuf = addF(s, r.fBuf, r)
|
|
|
|
r.bufInUse = fBuf
|
|
|
|
case iBuf: // Already have interface samples. Add to the interface buf.
|
|
|
|
r.iBuf = addSample(s, r.iBuf, r)
|
|
|
|
default:
|
2022-12-08 15:44:48 -08:00
|
|
|
// Already have specialized samples that are not fSamples.
|
|
|
|
// Need to call the checked add method for conversion.
|
|
|
|
r.add(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addH is a version of the add method specialized for hSample.
|
|
|
|
func (r *sampleRing) addH(s hSample) {
|
2023-05-03 11:06:12 -07:00
|
|
|
switch r.bufInUse {
|
|
|
|
case hBuf: // Add to existing hSamples.
|
|
|
|
r.hBuf = addH(s, r.hBuf, r)
|
|
|
|
case noBuf: // Add first sample.
|
|
|
|
r.hBuf = addH(s, r.hBuf, r)
|
|
|
|
r.bufInUse = hBuf
|
|
|
|
case iBuf: // Already have interface samples. Add to the interface buf.
|
|
|
|
r.iBuf = addSample(s, r.iBuf, r)
|
|
|
|
default:
|
|
|
|
// Already have specialized samples that are not hSamples.
|
2022-12-08 15:44:48 -08:00
|
|
|
// Need to call the checked add method for conversion.
|
|
|
|
r.add(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addFH is a version of the add method specialized for fhSample.
|
|
|
|
func (r *sampleRing) addFH(s fhSample) {
|
2023-05-03 11:06:12 -07:00
|
|
|
switch r.bufInUse {
|
|
|
|
case fhBuf: // Add to existing fhSamples.
|
|
|
|
r.fhBuf = addFH(s, r.fhBuf, r)
|
|
|
|
case noBuf: // Add first sample.
|
|
|
|
r.fhBuf = addFH(s, r.fhBuf, r)
|
|
|
|
r.bufInUse = fhBuf
|
|
|
|
case iBuf: // Already have interface samples. Add to the interface buf.
|
|
|
|
r.iBuf = addSample(s, r.iBuf, r)
|
|
|
|
default:
|
|
|
|
// Already have specialized samples that are not fhSamples.
|
2022-12-08 15:44:48 -08:00
|
|
|
// Need to call the checked add method for conversion.
|
|
|
|
r.add(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 06:21:17 -07:00
|
|
|
// genericAdd is a generic implementation of adding a chunks.Sample
|
2023-04-13 08:42:40 -07:00
|
|
|
// implementation to a buffer of a sample ring. However, the Go compiler
|
|
|
|
// currently (go1.20) decides to not expand the code during compile time, but
|
|
|
|
// creates dynamic code to handle the different types. That has a significant
|
|
|
|
// overhead during runtime, noticeable in PromQL benchmarks. For example, the
|
|
|
|
// "RangeQuery/expr=rate(a_hundred[1d]),steps=.*" benchmarks show about 7%
|
|
|
|
// longer runtime, 9% higher allocation size, and 10% more allocations.
|
|
|
|
// Therefore, genericAdd has been manually implemented for all the types
|
|
|
|
// (addSample, addF, addH, addFH) below.
|
|
|
|
//
|
2023-08-24 06:21:17 -07:00
|
|
|
// func genericAdd[T chunks.Sample](s T, buf []T, r *sampleRing) []T {
|
2023-04-13 08:42:40 -07:00
|
|
|
// l := len(buf)
|
|
|
|
// // Grow the ring buffer if it fits no more elements.
|
|
|
|
// if l == 0 {
|
|
|
|
// buf = make([]T, 16)
|
|
|
|
// l = 16
|
|
|
|
// }
|
|
|
|
// if l == r.l {
|
|
|
|
// newBuf := make([]T, 2*l)
|
|
|
|
// copy(newBuf[l+r.f:], buf[r.f:])
|
|
|
|
// copy(newBuf, buf[:r.f])
|
|
|
|
//
|
|
|
|
// buf = newBuf
|
|
|
|
// r.i = r.f
|
|
|
|
// r.f += l
|
|
|
|
// l = 2 * l
|
|
|
|
// } else {
|
|
|
|
// r.i++
|
|
|
|
// if r.i >= l {
|
|
|
|
// r.i -= l
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// buf[r.i] = s
|
|
|
|
// r.l++
|
|
|
|
//
|
|
|
|
// // Free head of the buffer of samples that just fell out of the range.
|
|
|
|
// tmin := s.T() - r.delta
|
|
|
|
// for buf[r.f].T() < tmin {
|
|
|
|
// r.f++
|
|
|
|
// if r.f >= l {
|
|
|
|
// r.f -= l
|
|
|
|
// }
|
|
|
|
// r.l--
|
|
|
|
// }
|
|
|
|
// return buf
|
|
|
|
// }
|
|
|
|
|
|
|
|
// addSample is a handcoded specialization of genericAdd (see above).
|
2023-08-24 06:21:17 -07:00
|
|
|
func addSample(s chunks.Sample, buf []chunks.Sample, r *sampleRing) []chunks.Sample {
|
2023-04-13 08:42:40 -07:00
|
|
|
l := len(buf)
|
|
|
|
// Grow the ring buffer if it fits no more elements.
|
|
|
|
if l == 0 {
|
2023-08-24 06:21:17 -07:00
|
|
|
buf = make([]chunks.Sample, 16)
|
2023-04-13 08:42:40 -07:00
|
|
|
l = 16
|
|
|
|
}
|
|
|
|
if l == r.l {
|
2023-08-24 06:21:17 -07:00
|
|
|
newBuf := make([]chunks.Sample, 2*l)
|
2023-04-13 08:42:40 -07:00
|
|
|
copy(newBuf[l+r.f:], buf[r.f:])
|
|
|
|
copy(newBuf, buf[:r.f])
|
|
|
|
|
|
|
|
buf = newBuf
|
|
|
|
r.i = r.f
|
|
|
|
r.f += l
|
|
|
|
l = 2 * l
|
|
|
|
} else {
|
|
|
|
r.i++
|
|
|
|
if r.i >= l {
|
|
|
|
r.i -= l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[r.i] = s
|
|
|
|
r.l++
|
|
|
|
|
|
|
|
// Free head of the buffer of samples that just fell out of the range.
|
|
|
|
tmin := s.T() - r.delta
|
|
|
|
for buf[r.f].T() < tmin {
|
|
|
|
r.f++
|
|
|
|
if r.f >= l {
|
|
|
|
r.f -= l
|
|
|
|
}
|
|
|
|
r.l--
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
// addF is a handcoded specialization of genericAdd (see above).
|
|
|
|
func addF(s fSample, buf []fSample, r *sampleRing) []fSample {
|
|
|
|
l := len(buf)
|
|
|
|
// Grow the ring buffer if it fits no more elements.
|
|
|
|
if l == 0 {
|
|
|
|
buf = make([]fSample, 16)
|
|
|
|
l = 16
|
|
|
|
}
|
|
|
|
if l == r.l {
|
|
|
|
newBuf := make([]fSample, 2*l)
|
|
|
|
copy(newBuf[l+r.f:], buf[r.f:])
|
|
|
|
copy(newBuf, buf[:r.f])
|
|
|
|
|
|
|
|
buf = newBuf
|
|
|
|
r.i = r.f
|
|
|
|
r.f += l
|
|
|
|
l = 2 * l
|
|
|
|
} else {
|
|
|
|
r.i++
|
|
|
|
if r.i >= l {
|
|
|
|
r.i -= l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[r.i] = s
|
|
|
|
r.l++
|
|
|
|
|
|
|
|
// Free head of the buffer of samples that just fell out of the range.
|
|
|
|
tmin := s.T() - r.delta
|
|
|
|
for buf[r.f].T() < tmin {
|
|
|
|
r.f++
|
|
|
|
if r.f >= l {
|
|
|
|
r.f -= l
|
|
|
|
}
|
|
|
|
r.l--
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
// addH is a handcoded specialization of genericAdd (see above).
|
|
|
|
func addH(s hSample, buf []hSample, r *sampleRing) []hSample {
|
|
|
|
l := len(buf)
|
|
|
|
// Grow the ring buffer if it fits no more elements.
|
|
|
|
if l == 0 {
|
|
|
|
buf = make([]hSample, 16)
|
|
|
|
l = 16
|
|
|
|
}
|
|
|
|
if l == r.l {
|
|
|
|
newBuf := make([]hSample, 2*l)
|
|
|
|
copy(newBuf[l+r.f:], buf[r.f:])
|
|
|
|
copy(newBuf, buf[:r.f])
|
|
|
|
|
|
|
|
buf = newBuf
|
|
|
|
r.i = r.f
|
|
|
|
r.f += l
|
|
|
|
l = 2 * l
|
|
|
|
} else {
|
|
|
|
r.i++
|
|
|
|
if r.i >= l {
|
|
|
|
r.i -= l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 08:02:14 -08:00
|
|
|
buf[r.i].t = s.t
|
|
|
|
if buf[r.i].h == nil {
|
|
|
|
buf[r.i].h = s.h.Copy()
|
|
|
|
} else {
|
|
|
|
s.h.CopyTo(buf[r.i].h)
|
|
|
|
}
|
2023-04-13 08:42:40 -07:00
|
|
|
r.l++
|
|
|
|
|
|
|
|
// Free head of the buffer of samples that just fell out of the range.
|
|
|
|
tmin := s.T() - r.delta
|
|
|
|
for buf[r.f].T() < tmin {
|
|
|
|
r.f++
|
|
|
|
if r.f >= l {
|
|
|
|
r.f -= l
|
|
|
|
}
|
|
|
|
r.l--
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
// addFH is a handcoded specialization of genericAdd (see above).
|
|
|
|
func addFH(s fhSample, buf []fhSample, r *sampleRing) []fhSample {
|
2022-12-08 15:44:48 -08:00
|
|
|
l := len(buf)
|
2016-12-24 16:40:28 -08:00
|
|
|
// Grow the ring buffer if it fits no more elements.
|
2022-12-08 15:44:48 -08:00
|
|
|
if l == 0 {
|
2023-04-13 08:42:40 -07:00
|
|
|
buf = make([]fhSample, 16)
|
2022-12-08 15:44:48 -08:00
|
|
|
l = 16
|
|
|
|
}
|
2016-12-24 16:40:28 -08:00
|
|
|
if l == r.l {
|
2023-04-13 08:42:40 -07:00
|
|
|
newBuf := make([]fhSample, 2*l)
|
2022-12-08 15:44:48 -08:00
|
|
|
copy(newBuf[l+r.f:], buf[r.f:])
|
|
|
|
copy(newBuf, buf[:r.f])
|
2016-12-24 16:40:28 -08:00
|
|
|
|
2022-12-08 15:44:48 -08:00
|
|
|
buf = newBuf
|
2016-12-24 16:40:28 -08:00
|
|
|
r.i = r.f
|
|
|
|
r.f += l
|
2018-03-12 06:16:59 -07:00
|
|
|
l = 2 * l
|
2016-12-24 16:40:28 -08:00
|
|
|
} else {
|
|
|
|
r.i++
|
|
|
|
if r.i >= l {
|
|
|
|
r.i -= l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 08:02:14 -08:00
|
|
|
buf[r.i].t = s.t
|
|
|
|
if buf[r.i].fh == nil {
|
|
|
|
buf[r.i].fh = s.fh.Copy()
|
|
|
|
} else {
|
|
|
|
s.fh.CopyTo(buf[r.i].fh)
|
|
|
|
}
|
2016-12-24 16:40:28 -08:00
|
|
|
r.l++
|
|
|
|
|
|
|
|
// Free head of the buffer of samples that just fell out of the range.
|
2022-12-08 04:31:08 -08:00
|
|
|
tmin := s.T() - r.delta
|
2022-12-08 15:44:48 -08:00
|
|
|
for buf[r.f].T() < tmin {
|
2018-12-18 03:25:45 -08:00
|
|
|
r.f++
|
|
|
|
if r.f >= l {
|
|
|
|
r.f -= l
|
|
|
|
}
|
|
|
|
r.l--
|
|
|
|
}
|
2022-12-08 15:44:48 -08:00
|
|
|
return buf
|
2018-12-18 03:25:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// reduceDelta lowers the buffered time delta, dropping any samples that are
|
|
|
|
// out of the new delta range.
|
|
|
|
func (r *sampleRing) reduceDelta(delta int64) bool {
|
|
|
|
if delta > r.delta {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
r.delta = delta
|
|
|
|
|
|
|
|
if r.l == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-05-03 11:06:12 -07:00
|
|
|
switch r.bufInUse {
|
|
|
|
case fBuf:
|
2022-12-08 15:44:48 -08:00
|
|
|
genericReduceDelta(r.fBuf, r)
|
2023-05-03 11:06:12 -07:00
|
|
|
case hBuf:
|
2022-12-08 15:44:48 -08:00
|
|
|
genericReduceDelta(r.hBuf, r)
|
2023-05-03 11:06:12 -07:00
|
|
|
case fhBuf:
|
2022-12-08 15:44:48 -08:00
|
|
|
genericReduceDelta(r.fhBuf, r)
|
|
|
|
default:
|
2023-05-03 11:06:12 -07:00
|
|
|
genericReduceDelta(r.iBuf, r)
|
2022-12-08 15:44:48 -08:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-08-24 06:21:17 -07:00
|
|
|
func genericReduceDelta[T chunks.Sample](buf []T, r *sampleRing) {
|
2018-12-18 03:25:45 -08:00
|
|
|
// Free head of the buffer of samples that just fell out of the range.
|
2022-12-08 15:44:48 -08:00
|
|
|
l := len(buf)
|
|
|
|
tmin := buf[r.i].T() - r.delta
|
|
|
|
for buf[r.f].T() < tmin {
|
2016-12-24 16:40:28 -08:00
|
|
|
r.f++
|
|
|
|
if r.f >= l {
|
|
|
|
r.f -= l
|
|
|
|
}
|
|
|
|
r.l--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 09:01:54 -07:00
|
|
|
// nthLast returns the nth most recent element added to the ring.
|
2023-08-24 06:21:17 -07:00
|
|
|
func (r *sampleRing) nthLast(n int) (chunks.Sample, bool) {
|
2017-05-23 09:01:54 -07:00
|
|
|
if n > r.l {
|
2022-12-08 04:31:08 -08:00
|
|
|
return fSample{}, false
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
2022-12-08 15:44:48 -08:00
|
|
|
i := r.l - n
|
2023-05-03 11:06:12 -07:00
|
|
|
switch r.bufInUse {
|
|
|
|
case fBuf:
|
2022-12-08 15:44:48 -08:00
|
|
|
return r.atF(i), true
|
2023-05-03 11:06:12 -07:00
|
|
|
case hBuf:
|
2022-12-08 15:44:48 -08:00
|
|
|
return r.atH(i), true
|
2023-05-03 11:06:12 -07:00
|
|
|
case fhBuf:
|
2022-12-08 15:44:48 -08:00
|
|
|
return r.atFH(i), true
|
|
|
|
default:
|
|
|
|
return r.at(i), true
|
|
|
|
}
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
|
2023-08-24 06:21:17 -07:00
|
|
|
func (r *sampleRing) samples() []chunks.Sample {
|
|
|
|
res := make([]chunks.Sample, r.l)
|
2016-12-24 16:40:28 -08:00
|
|
|
|
2021-10-22 01:06:44 -07:00
|
|
|
k := r.f + r.l
|
2016-12-24 16:40:28 -08:00
|
|
|
var j int
|
|
|
|
|
2023-05-03 11:06:12 -07:00
|
|
|
switch r.bufInUse {
|
|
|
|
case iBuf:
|
|
|
|
if k > len(r.iBuf) {
|
|
|
|
k = len(r.iBuf)
|
2022-12-08 15:44:48 -08:00
|
|
|
j = r.l - k + r.f
|
|
|
|
}
|
2023-05-03 11:06:12 -07:00
|
|
|
n := copy(res, r.iBuf[r.f:k])
|
|
|
|
copy(res[n:], r.iBuf[:j])
|
|
|
|
case fBuf:
|
2022-12-08 15:44:48 -08:00
|
|
|
if k > len(r.fBuf) {
|
|
|
|
k = len(r.fBuf)
|
|
|
|
j = r.l - k + r.f
|
|
|
|
}
|
|
|
|
resF := make([]fSample, r.l)
|
|
|
|
n := copy(resF, r.fBuf[r.f:k])
|
|
|
|
copy(resF[n:], r.fBuf[:j])
|
|
|
|
for i, s := range resF {
|
|
|
|
res[i] = s
|
|
|
|
}
|
2023-05-03 11:06:12 -07:00
|
|
|
case hBuf:
|
2022-12-08 15:44:48 -08:00
|
|
|
if k > len(r.hBuf) {
|
|
|
|
k = len(r.hBuf)
|
|
|
|
j = r.l - k + r.f
|
|
|
|
}
|
|
|
|
resH := make([]hSample, r.l)
|
|
|
|
n := copy(resH, r.hBuf[r.f:k])
|
|
|
|
copy(resH[n:], r.hBuf[:j])
|
|
|
|
for i, s := range resH {
|
|
|
|
res[i] = s
|
|
|
|
}
|
2023-05-03 11:06:12 -07:00
|
|
|
case fhBuf:
|
2022-12-08 15:44:48 -08:00
|
|
|
if k > len(r.fhBuf) {
|
|
|
|
k = len(r.fhBuf)
|
|
|
|
j = r.l - k + r.f
|
|
|
|
}
|
|
|
|
resFH := make([]fhSample, r.l)
|
|
|
|
n := copy(resFH, r.fhBuf[r.f:k])
|
|
|
|
copy(resFH[n:], r.fhBuf[:j])
|
|
|
|
for i, s := range resFH {
|
|
|
|
res[i] = s
|
|
|
|
}
|
|
|
|
}
|
2016-12-24 16:40:28 -08:00
|
|
|
|
|
|
|
return res
|
|
|
|
}
|