prometheus
142 строки · 4.3 Кб
1// Copyright 2021 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package storage
15
16import (
17"math"
18
19"github.com/prometheus/prometheus/model/histogram"
20"github.com/prometheus/prometheus/tsdb/chunkenc"
21)
22
23// MemoizedSeriesIterator wraps an iterator with a buffer to look back the previous element.
24//
25// This iterator regards integer histograms as float histograms; calls to Seek() will never return chunkenc.Histogram.
26// This iterator deliberately does not implement chunkenc.Iterator.
27type MemoizedSeriesIterator struct {
28it chunkenc.Iterator
29delta int64
30
31lastTime int64
32valueType chunkenc.ValueType
33
34// Keep track of the previously returned value.
35prevTime int64
36prevValue float64
37prevFloatHistogram *histogram.FloatHistogram
38}
39
40// NewMemoizedEmptyIterator is like NewMemoizedIterator but it's initialised with an empty iterator.
41func NewMemoizedEmptyIterator(delta int64) *MemoizedSeriesIterator {
42return NewMemoizedIterator(chunkenc.NewNopIterator(), delta)
43}
44
45// NewMemoizedIterator returns a new iterator that buffers the values within the
46// time range of the current element and the duration of delta before.
47func NewMemoizedIterator(it chunkenc.Iterator, delta int64) *MemoizedSeriesIterator {
48bit := &MemoizedSeriesIterator{
49delta: delta,
50prevTime: math.MinInt64,
51}
52bit.Reset(it)
53
54return bit
55}
56
57// Reset the internal state to reuse the wrapper with the provided iterator.
58func (b *MemoizedSeriesIterator) Reset(it chunkenc.Iterator) {
59b.it = it
60b.lastTime = math.MinInt64
61b.prevTime = math.MinInt64
62b.valueType = it.Next()
63}
64
65// PeekPrev returns the previous element of the iterator. If there is none buffered,
66// ok is false.
67func (b *MemoizedSeriesIterator) PeekPrev() (t int64, v float64, fh *histogram.FloatHistogram, ok bool) {
68if b.prevTime == math.MinInt64 {
69return 0, 0, nil, false
70}
71return b.prevTime, b.prevValue, b.prevFloatHistogram, true
72}
73
74// Seek advances the iterator to the element at time t or greater.
75func (b *MemoizedSeriesIterator) Seek(t int64) chunkenc.ValueType {
76t0 := t - b.delta
77
78if b.valueType != chunkenc.ValNone && t0 > b.lastTime {
79// Reset the previously stored element because the seek advanced
80// more than the delta.
81b.prevTime = math.MinInt64
82
83b.valueType = b.it.Seek(t0)
84switch b.valueType {
85case chunkenc.ValNone:
86return chunkenc.ValNone
87case chunkenc.ValHistogram:
88b.valueType = chunkenc.ValFloatHistogram
89}
90b.lastTime = b.it.AtT()
91}
92if b.lastTime >= t {
93return b.valueType
94}
95for b.Next() != chunkenc.ValNone {
96if b.lastTime >= t {
97return b.valueType
98}
99}
100
101return chunkenc.ValNone
102}
103
104// Next advances the iterator to the next element. Note that this does not check whether the element being buffered is
105// within the time range of the current element and the duration of delta before.
106func (b *MemoizedSeriesIterator) Next() chunkenc.ValueType {
107// Keep track of the previous element.
108switch b.valueType {
109case chunkenc.ValNone:
110return chunkenc.ValNone
111case chunkenc.ValFloat:
112b.prevTime, b.prevValue = b.it.At()
113b.prevFloatHistogram = nil
114case chunkenc.ValHistogram, chunkenc.ValFloatHistogram:
115b.prevValue = 0
116b.prevTime, b.prevFloatHistogram = b.it.AtFloatHistogram(nil)
117}
118
119b.valueType = b.it.Next()
120if b.valueType != chunkenc.ValNone {
121b.lastTime = b.it.AtT()
122}
123if b.valueType == chunkenc.ValHistogram {
124b.valueType = chunkenc.ValFloatHistogram
125}
126return b.valueType
127}
128
129// At returns the current float element of the iterator.
130func (b *MemoizedSeriesIterator) At() (int64, float64) {
131return b.it.At()
132}
133
134// AtFloatHistogram returns the current float-histogram element of the iterator.
135func (b *MemoizedSeriesIterator) AtFloatHistogram() (int64, *histogram.FloatHistogram) {
136return b.it.AtFloatHistogram(nil)
137}
138
139// Err returns the last encountered error.
140func (b *MemoizedSeriesIterator) Err() error {
141return b.it.Err()
142}
143