2021-03-11 13:32:56 +00:00
|
|
|
// Copyright 2021 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 (
|
|
|
|
"math"
|
|
|
|
|
2021-11-15 20:49:25 +00:00
|
|
|
"github.com/prometheus/prometheus/model/histogram"
|
2021-03-11 13:32:56 +00:00
|
|
|
"github.com/prometheus/prometheus/tsdb/chunkenc"
|
|
|
|
)
|
|
|
|
|
2021-11-15 20:49:25 +00:00
|
|
|
// ValueType defines the type of a value in the storage.
|
|
|
|
type ValueType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ValNone ValueType = iota
|
|
|
|
ValFloat
|
|
|
|
ValHistogram
|
|
|
|
)
|
|
|
|
|
2021-03-15 08:27:20 +00:00
|
|
|
// MemoizedSeriesIterator wraps an iterator with a buffer to look back the previous element.
|
2021-03-11 13:32:56 +00:00
|
|
|
type MemoizedSeriesIterator struct {
|
|
|
|
it chunkenc.Iterator
|
|
|
|
delta int64
|
|
|
|
|
2021-11-15 20:49:25 +00:00
|
|
|
lastTime int64
|
|
|
|
valueType ValueType
|
2021-03-11 13:32:56 +00:00
|
|
|
|
|
|
|
// Keep track of the previously returned value.
|
2021-11-15 20:49:25 +00:00
|
|
|
prevTime int64
|
|
|
|
prevValue float64
|
|
|
|
prevHistogram *histogram.Histogram
|
2021-03-11 13:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMemoizedEmptyIterator is like NewMemoizedIterator but it's initialised with an empty iterator.
|
|
|
|
func NewMemoizedEmptyIterator(delta int64) *MemoizedSeriesIterator {
|
|
|
|
return NewMemoizedIterator(chunkenc.NewNopIterator(), delta)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMemoizedIterator returns a new iterator that buffers the values within the
|
|
|
|
// time range of the current element and the duration of delta before.
|
|
|
|
func NewMemoizedIterator(it chunkenc.Iterator, delta int64) *MemoizedSeriesIterator {
|
|
|
|
bit := &MemoizedSeriesIterator{
|
|
|
|
delta: delta,
|
|
|
|
prevTime: math.MinInt64,
|
|
|
|
}
|
|
|
|
bit.Reset(it)
|
|
|
|
|
|
|
|
return bit
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the internal state to reuse the wrapper with the provided iterator.
|
|
|
|
func (b *MemoizedSeriesIterator) Reset(it chunkenc.Iterator) {
|
|
|
|
b.it = it
|
|
|
|
b.lastTime = math.MinInt64
|
|
|
|
b.prevTime = math.MinInt64
|
|
|
|
it.Next()
|
2021-11-15 20:49:25 +00:00
|
|
|
if it.ChunkEncoding() == chunkenc.EncHistogram {
|
|
|
|
b.valueType = ValHistogram
|
|
|
|
} else {
|
|
|
|
b.valueType = ValFloat
|
|
|
|
}
|
2021-03-11 13:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PeekPrev returns the previous element of the iterator. If there is none buffered,
|
|
|
|
// ok is false.
|
2021-11-15 20:49:25 +00:00
|
|
|
func (b *MemoizedSeriesIterator) PeekPrev() (t int64, v float64, h *histogram.Histogram, ok bool) {
|
2021-03-11 13:32:56 +00:00
|
|
|
if b.prevTime == math.MinInt64 {
|
2021-11-15 20:49:25 +00:00
|
|
|
return 0, 0, nil, false
|
2021-03-11 13:32:56 +00:00
|
|
|
}
|
2021-11-15 20:49:25 +00:00
|
|
|
return b.prevTime, b.prevValue, b.prevHistogram, true
|
2021-03-11 13:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Seek advances the iterator to the element at time t or greater.
|
2021-11-15 20:49:25 +00:00
|
|
|
func (b *MemoizedSeriesIterator) Seek(t int64) ValueType {
|
2021-03-11 13:32:56 +00:00
|
|
|
t0 := t - b.delta
|
|
|
|
|
|
|
|
if t0 > b.lastTime {
|
|
|
|
// Reset the previously stored element because the seek advanced
|
|
|
|
// more than the delta.
|
|
|
|
b.prevTime = math.MinInt64
|
|
|
|
|
2021-11-15 20:49:25 +00:00
|
|
|
ok := b.it.Seek(t0)
|
|
|
|
if !ok {
|
|
|
|
b.valueType = ValNone
|
|
|
|
return ValNone
|
2021-03-11 13:32:56 +00:00
|
|
|
}
|
2021-11-15 19:36:44 +00:00
|
|
|
if b.it.ChunkEncoding() == chunkenc.EncHistogram {
|
2021-11-15 20:49:25 +00:00
|
|
|
b.valueType = ValHistogram
|
2021-11-15 19:36:44 +00:00
|
|
|
b.lastTime, _ = b.it.AtHistogram()
|
|
|
|
} else {
|
2021-11-15 20:49:25 +00:00
|
|
|
b.valueType = ValFloat
|
2021-11-15 19:36:44 +00:00
|
|
|
b.lastTime, _ = b.it.At()
|
|
|
|
}
|
2021-03-11 13:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.lastTime >= t {
|
2021-11-15 20:49:25 +00:00
|
|
|
return b.valueType
|
2021-03-11 13:32:56 +00:00
|
|
|
}
|
2021-11-15 20:49:25 +00:00
|
|
|
for b.Next() != ValNone {
|
2021-03-11 13:32:56 +00:00
|
|
|
if b.lastTime >= t {
|
2021-11-15 20:49:25 +00:00
|
|
|
return b.valueType
|
2021-03-11 13:32:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 20:49:25 +00:00
|
|
|
return ValNone
|
2021-03-11 13:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next advances the iterator to the next element.
|
2021-11-15 20:49:25 +00:00
|
|
|
func (b *MemoizedSeriesIterator) Next() ValueType {
|
|
|
|
if b.valueType == ValNone {
|
|
|
|
return ValNone
|
2021-03-11 13:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of the previous element.
|
2021-11-15 19:36:44 +00:00
|
|
|
if b.it.ChunkEncoding() == chunkenc.EncHistogram {
|
2021-11-15 20:49:25 +00:00
|
|
|
b.prevTime, b.prevHistogram = b.it.AtHistogram()
|
|
|
|
b.prevValue = 0
|
2021-11-15 19:36:44 +00:00
|
|
|
} else {
|
|
|
|
b.prevTime, b.prevValue = b.it.At()
|
2021-11-15 20:49:25 +00:00
|
|
|
b.prevHistogram = nil
|
2021-11-15 19:36:44 +00:00
|
|
|
}
|
2021-03-11 13:32:56 +00:00
|
|
|
|
2021-11-15 20:49:25 +00:00
|
|
|
ok := b.it.Next()
|
|
|
|
if ok {
|
2021-11-15 19:36:44 +00:00
|
|
|
if b.it.ChunkEncoding() == chunkenc.EncHistogram {
|
|
|
|
b.lastTime, _ = b.it.AtHistogram()
|
2021-11-15 20:49:25 +00:00
|
|
|
b.valueType = ValHistogram
|
|
|
|
|
2021-11-15 19:36:44 +00:00
|
|
|
} else {
|
|
|
|
b.lastTime, _ = b.it.At()
|
2021-11-15 20:49:25 +00:00
|
|
|
b.valueType = ValFloat
|
2021-11-15 19:36:44 +00:00
|
|
|
}
|
2021-11-15 20:49:25 +00:00
|
|
|
} else {
|
|
|
|
b.valueType = ValNone
|
2021-03-11 13:32:56 +00:00
|
|
|
}
|
2021-11-15 20:49:25 +00:00
|
|
|
return b.valueType
|
2021-03-11 13:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Values returns the current element of the iterator.
|
|
|
|
func (b *MemoizedSeriesIterator) Values() (int64, float64) {
|
|
|
|
return b.it.At()
|
|
|
|
}
|
|
|
|
|
2021-11-15 20:49:25 +00:00
|
|
|
// Values returns the current element of the iterator.
|
|
|
|
func (b *MemoizedSeriesIterator) HistogramValues() (int64, *histogram.Histogram) {
|
|
|
|
return b.it.AtHistogram()
|
|
|
|
}
|
|
|
|
|
2021-03-11 13:32:56 +00:00
|
|
|
// Err returns the last encountered error.
|
|
|
|
func (b *MemoizedSeriesIterator) Err() error {
|
|
|
|
return b.it.Err()
|
|
|
|
}
|