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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
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"
|
2021-06-30 07:48:13 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb/chunkenc"
|
2020-10-29 02:43:23 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-12-24 16:40:28 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSampleRing(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
input []int64
|
|
|
|
delta int64
|
|
|
|
size int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
|
|
delta: 2,
|
|
|
|
size: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
|
|
delta: 2,
|
|
|
|
size: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
|
|
|
|
delta: 7,
|
|
|
|
size: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: []int64{1, 2, 3, 4, 5, 16, 17, 18, 19, 20},
|
|
|
|
delta: 7,
|
|
|
|
size: 1,
|
|
|
|
},
|
2018-03-12 06:16:59 -07:00
|
|
|
{
|
|
|
|
input: []int64{1, 2, 3, 4, 6},
|
|
|
|
delta: 4,
|
|
|
|
size: 4,
|
|
|
|
},
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
for _, c := range cases {
|
2021-11-12 10:07:41 -08:00
|
|
|
r := newSampleRing(c.delta, c.size, chunkenc.EncNone)
|
2016-12-24 16:40:28 -08:00
|
|
|
|
|
|
|
input := []sample{}
|
|
|
|
for _, t := range c.input {
|
|
|
|
input = append(input, sample{
|
|
|
|
t: t,
|
|
|
|
v: float64(rand.Intn(100)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, s := range input {
|
2021-11-12 10:07:41 -08:00
|
|
|
r.add(s)
|
2016-12-24 16:40:28 -08:00
|
|
|
buffered := r.samples()
|
|
|
|
|
|
|
|
for _, sold := range input[:i] {
|
|
|
|
found := false
|
|
|
|
for _, bs := range buffered {
|
|
|
|
if bs.t == sold.t && bs.v == sold.v {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-10-01 22:28:08 -07:00
|
|
|
|
|
|
|
if found {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.GreaterOrEqual(t, sold.t, s.t-c.delta, "%d: unexpected sample %d in buffer; buffer %v", i, sold.t, buffered)
|
2019-10-01 22:28:08 -07:00
|
|
|
} else {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Less(t, sold.t, s.t-c.delta, "%d: expected sample %d to be in buffer but was not; buffer %v", i, sold.t, buffered)
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBufferedSeriesIterator(t *testing.T) {
|
|
|
|
var it *BufferedSeriesIterator
|
|
|
|
|
|
|
|
bufferEq := func(exp []sample) {
|
|
|
|
var b []sample
|
|
|
|
bit := it.Buffer()
|
|
|
|
for bit.Next() {
|
2017-01-02 04:33:37 -08:00
|
|
|
t, v := bit.At()
|
2016-12-24 16:40:28 -08:00
|
|
|
b = append(b, sample{t: t, v: v})
|
|
|
|
}
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, exp, b, "buffer mismatch")
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
|
|
|
sampleEq := func(ets int64, ev float64) {
|
|
|
|
ts, v := it.Values()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, ets, ts, "timestamp mismatch")
|
|
|
|
require.Equal(t, ev, v, "value mismatch")
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
2021-03-11 05:32:56 -08:00
|
|
|
prevSampleEq := func(ets int64, ev float64, eok bool) {
|
2021-11-12 10:07:41 -08:00
|
|
|
ts, v, _, ok := it.PeekBack(1)
|
2021-03-11 05:32:56 -08:00
|
|
|
require.Equal(t, eok, ok, "exist mismatch")
|
|
|
|
require.Equal(t, ets, ts, "timestamp mismatch")
|
|
|
|
require.Equal(t, ev, v, "value mismatch")
|
|
|
|
}
|
2016-12-24 16:40:28 -08:00
|
|
|
|
2020-06-24 06:41:52 -07:00
|
|
|
it = NewBufferIterator(NewListSeriesIterator(samples{
|
2020-03-24 13:15:47 -07:00
|
|
|
sample{t: 1, v: 2},
|
|
|
|
sample{t: 2, v: 3},
|
|
|
|
sample{t: 3, v: 4},
|
|
|
|
sample{t: 4, v: 5},
|
|
|
|
sample{t: 5, v: 6},
|
|
|
|
sample{t: 99, v: 8},
|
|
|
|
sample{t: 100, v: 9},
|
|
|
|
sample{t: 101, v: 10},
|
2016-12-24 16:40:28 -08:00
|
|
|
}), 2)
|
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
require.True(t, it.Seek(-123), "seek failed")
|
2016-12-24 16:40:28 -08:00
|
|
|
sampleEq(1, 2)
|
2021-03-11 05:32:56 -08:00
|
|
|
prevSampleEq(0, 0, false)
|
2016-12-24 16:40:28 -08:00
|
|
|
bufferEq(nil)
|
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
require.True(t, it.Next(), "next failed")
|
2016-12-24 16:40:28 -08:00
|
|
|
sampleEq(2, 3)
|
2021-03-11 05:32:56 -08:00
|
|
|
prevSampleEq(1, 2, true)
|
2016-12-24 16:40:28 -08:00
|
|
|
bufferEq([]sample{{t: 1, v: 2}})
|
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
require.True(t, it.Next(), "next failed")
|
|
|
|
require.True(t, it.Next(), "next failed")
|
|
|
|
require.True(t, it.Next(), "next failed")
|
2016-12-24 16:40:28 -08:00
|
|
|
sampleEq(5, 6)
|
2021-03-11 05:32:56 -08:00
|
|
|
prevSampleEq(4, 5, true)
|
2016-12-24 16:40:28 -08:00
|
|
|
bufferEq([]sample{{t: 2, v: 3}, {t: 3, v: 4}, {t: 4, v: 5}})
|
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
require.True(t, it.Seek(5), "seek failed")
|
2016-12-24 16:40:28 -08:00
|
|
|
sampleEq(5, 6)
|
2021-03-11 05:32:56 -08:00
|
|
|
prevSampleEq(4, 5, true)
|
2016-12-24 16:40:28 -08:00
|
|
|
bufferEq([]sample{{t: 2, v: 3}, {t: 3, v: 4}, {t: 4, v: 5}})
|
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
require.True(t, it.Seek(101), "seek failed")
|
2016-12-24 16:40:28 -08:00
|
|
|
sampleEq(101, 10)
|
2021-03-11 05:32:56 -08:00
|
|
|
prevSampleEq(100, 9, true)
|
2016-12-24 16:40:28 -08:00
|
|
|
bufferEq([]sample{{t: 99, v: 8}, {t: 100, v: 9}})
|
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
require.False(t, it.Next(), "next succeeded unexpectedly")
|
2016-12-24 16:40:28 -08:00
|
|
|
}
|
2016-12-30 01:45:56 -08:00
|
|
|
|
2017-06-12 22:22:27 -07:00
|
|
|
// At() should not be called once Next() returns false.
|
|
|
|
func TestBufferedSeriesIteratorNoBadAt(t *testing.T) {
|
|
|
|
done := false
|
|
|
|
|
|
|
|
m := &mockSeriesIterator{
|
|
|
|
seek: func(int64) bool { return false },
|
|
|
|
at: func() (int64, float64) {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.False(t, done, "unexpectedly done")
|
2017-06-12 22:22:27 -07:00
|
|
|
done = true
|
|
|
|
return 0, 0
|
|
|
|
},
|
|
|
|
next: func() bool { return !done },
|
|
|
|
err: func() error { return nil },
|
|
|
|
}
|
|
|
|
|
2018-07-17 21:10:28 -07:00
|
|
|
it := NewBufferIterator(m, 60)
|
2017-06-12 22:22:27 -07:00
|
|
|
it.Next()
|
|
|
|
it.Next()
|
|
|
|
}
|
|
|
|
|
2017-03-14 02:57:34 -07:00
|
|
|
func BenchmarkBufferedSeriesIterator(b *testing.B) {
|
|
|
|
// Simulate a 5 minute rate.
|
2018-12-18 03:22:33 -08:00
|
|
|
it := NewBufferIterator(newFakeSeriesIterator(int64(b.N), 30), 5*60)
|
2017-03-14 02:57:34 -07:00
|
|
|
|
|
|
|
b.SetBytes(int64(b.N * 16))
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for it.Next() {
|
|
|
|
// scan everything
|
|
|
|
}
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(b, it.Err())
|
2017-03-14 02:57:34 -07:00
|
|
|
}
|
|
|
|
|
2016-12-30 01:45:56 -08:00
|
|
|
type mockSeriesIterator struct {
|
2017-06-12 22:22:27 -07:00
|
|
|
seek func(int64) bool
|
|
|
|
at func() (int64, float64)
|
|
|
|
next func() bool
|
|
|
|
err func() error
|
2016-12-30 01:45:56 -08:00
|
|
|
}
|
|
|
|
|
2017-06-12 22:22:27 -07:00
|
|
|
func (m *mockSeriesIterator) Seek(t int64) bool { return m.seek(t) }
|
|
|
|
func (m *mockSeriesIterator) At() (int64, float64) { return m.at() }
|
2021-11-12 10:07:41 -08:00
|
|
|
func (m *mockSeriesIterator) AtHistogram() (int64, *histogram.Histogram) {
|
|
|
|
return 0, nil
|
2021-06-29 07:38:46 -07:00
|
|
|
}
|
2021-06-30 07:48:13 -07:00
|
|
|
func (m *mockSeriesIterator) ChunkEncoding() chunkenc.Encoding {
|
|
|
|
return chunkenc.EncXOR
|
|
|
|
}
|
2021-06-29 07:38:46 -07:00
|
|
|
func (m *mockSeriesIterator) Next() bool { return m.next() }
|
|
|
|
func (m *mockSeriesIterator) Err() error { return m.err() }
|
2016-12-30 01:45:56 -08:00
|
|
|
|
2018-12-18 03:22:33 -08:00
|
|
|
type fakeSeriesIterator struct {
|
|
|
|
nsamples int64
|
|
|
|
step int64
|
|
|
|
idx int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFakeSeriesIterator(nsamples, step int64) *fakeSeriesIterator {
|
|
|
|
return &fakeSeriesIterator{nsamples: nsamples, step: step, idx: -1}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *fakeSeriesIterator) At() (int64, float64) {
|
2021-11-07 08:12:04 -08:00
|
|
|
return it.idx * it.step, 123 // Value doesn't matter.
|
2018-12-18 03:22:33 -08:00
|
|
|
}
|
|
|
|
|
2021-11-12 10:07:41 -08:00
|
|
|
func (it *fakeSeriesIterator) AtHistogram() (int64, *histogram.Histogram) {
|
|
|
|
return it.idx * it.step, &histogram.Histogram{} // Value doesn't matter.
|
2021-06-29 07:38:46 -07:00
|
|
|
}
|
|
|
|
|
2021-06-30 07:48:13 -07:00
|
|
|
func (it *fakeSeriesIterator) ChunkEncoding() chunkenc.Encoding {
|
|
|
|
return chunkenc.EncXOR
|
|
|
|
}
|
|
|
|
|
2018-12-18 03:22:33 -08:00
|
|
|
func (it *fakeSeriesIterator) Next() bool {
|
|
|
|
it.idx++
|
|
|
|
return it.idx < it.nsamples
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *fakeSeriesIterator) Seek(t int64) bool {
|
|
|
|
it.idx = t / it.step
|
|
|
|
return it.idx < it.nsamples
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:15:47 -07:00
|
|
|
func (it *fakeSeriesIterator) Err() error { return nil }
|