2017-07-13 03:05:38 -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
|
2017-10-16 18:26:38 -07:00
|
|
|
// limitations under the License.
|
2017-07-13 03:05:38 -07:00
|
|
|
|
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2018-01-26 03:01:59 -08:00
|
|
|
"fmt"
|
2018-06-18 09:34:08 -07:00
|
|
|
"math"
|
2020-03-24 13:15:47 -07:00
|
|
|
"sort"
|
2017-07-13 03:05:38 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2020-02-06 07:58:38 -08:00
|
|
|
"github.com/prometheus/prometheus/tsdb/chunkenc"
|
2020-03-24 13:15:47 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb/tsdbutil"
|
2019-08-08 18:36:42 -07:00
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2017-07-13 03:05:38 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMergeStringSlices(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
input [][]string
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
{},
|
|
|
|
{[][]string{{"foo"}}, []string{"foo"}},
|
|
|
|
{[][]string{{"foo"}, {"bar"}}, []string{"bar", "foo"}},
|
|
|
|
{[][]string{{"foo"}, {"bar"}, {"baz"}}, []string{"bar", "baz", "foo"}},
|
|
|
|
} {
|
2019-08-08 18:36:42 -07:00
|
|
|
testutil.Equals(t, tc.expected, mergeStringSlices(tc.input))
|
2017-07-13 03:05:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeTwoStringSlices(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
a, b, expected []string
|
|
|
|
}{
|
|
|
|
{[]string{}, []string{}, []string{}},
|
|
|
|
{[]string{"foo"}, nil, []string{"foo"}},
|
|
|
|
{nil, []string{"bar"}, []string{"bar"}},
|
|
|
|
{[]string{"foo"}, []string{"bar"}, []string{"bar", "foo"}},
|
|
|
|
{[]string{"foo"}, []string{"bar", "baz"}, []string{"bar", "baz", "foo"}},
|
|
|
|
{[]string{"foo"}, []string{"foo"}, []string{"foo"}},
|
|
|
|
} {
|
2019-08-08 18:36:42 -07:00
|
|
|
testutil.Equals(t, tc.expected, mergeTwoStringSlices(tc.a, tc.b))
|
2017-07-13 03:05:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:15:47 -07:00
|
|
|
func TestMergeQuerierWithChainMerger(t *testing.T) {
|
2017-07-13 03:05:38 -07:00
|
|
|
for _, tc := range []struct {
|
2020-03-24 13:15:47 -07:00
|
|
|
name string
|
|
|
|
querierSeries [][]Series
|
|
|
|
extraQueriers []Querier
|
|
|
|
|
2017-07-13 03:05:38 -07:00
|
|
|
expected SeriesSet
|
|
|
|
}{
|
|
|
|
{
|
2020-03-24 13:15:47 -07:00
|
|
|
name: "1 querier with no series",
|
|
|
|
querierSeries: [][]Series{{}},
|
|
|
|
expected: NewMockSeriesSet(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "many queriers with no series",
|
|
|
|
querierSeries: [][]Series{{}, {}, {}, {}, {}, {}, {}},
|
|
|
|
expected: NewMockSeriesSet(),
|
2017-07-13 03:05:38 -07:00
|
|
|
},
|
|
|
|
{
|
2020-03-24 13:15:47 -07:00
|
|
|
name: "1 querier, two series",
|
|
|
|
querierSeries: [][]Series{{
|
|
|
|
NewListSeries(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{1, 1}, sample{2, 2}, sample{3, 3}}),
|
|
|
|
NewListSeries(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, 0}, sample{1, 1}, sample{2, 2}}),
|
|
|
|
}},
|
|
|
|
expected: NewMockSeriesSet(
|
|
|
|
NewListSeries(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{1, 1}, sample{2, 2}, sample{3, 3}}),
|
|
|
|
NewListSeries(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, 0}, sample{1, 1}, sample{2, 2}}),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "2 queriers, 1 different series each",
|
|
|
|
querierSeries: [][]Series{{
|
|
|
|
NewListSeries(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{1, 1}, sample{2, 2}, sample{3, 3}}),
|
|
|
|
}, {
|
|
|
|
NewListSeries(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, 0}, sample{1, 1}, sample{2, 2}}),
|
|
|
|
}},
|
|
|
|
expected: NewMockSeriesSet(
|
|
|
|
NewListSeries(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{1, 1}, sample{2, 2}, sample{3, 3}}),
|
|
|
|
NewListSeries(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, 0}, sample{1, 1}, sample{2, 2}}),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "2 time unsorted queriers, 2 series each",
|
|
|
|
querierSeries: [][]Series{{
|
|
|
|
NewListSeries(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{5, 5}, sample{6, 6}}),
|
|
|
|
NewListSeries(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, 0}, sample{1, 1}, sample{2, 2}}),
|
|
|
|
}, {
|
|
|
|
NewListSeries(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{1, 1}, sample{2, 2}, sample{3, 3}}),
|
|
|
|
NewListSeries(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{3, 3}, sample{4, 4}}),
|
|
|
|
}},
|
|
|
|
expected: NewMockSeriesSet(
|
|
|
|
NewListSeries(
|
|
|
|
labels.FromStrings("bar", "baz"),
|
|
|
|
[]tsdbutil.Sample{sample{1, 1}, sample{2, 2}, sample{3, 3}, sample{5, 5}, sample{6, 6}},
|
|
|
|
),
|
|
|
|
NewListSeries(
|
|
|
|
labels.FromStrings("foo", "bar"),
|
|
|
|
[]tsdbutil.Sample{sample{0, 0}, sample{1, 1}, sample{2, 2}, sample{3, 3}, sample{4, 4}},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "5 queriers, only 2 queriers have 2 time unsorted series each",
|
|
|
|
querierSeries: [][]Series{{}, {}, {
|
|
|
|
NewListSeries(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{5, 5}, sample{6, 6}}),
|
|
|
|
NewListSeries(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, 0}, sample{1, 1}, sample{2, 2}}),
|
|
|
|
}, {
|
|
|
|
NewListSeries(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{1, 1}, sample{2, 2}, sample{3, 3}}),
|
|
|
|
NewListSeries(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{3, 3}, sample{4, 4}}),
|
|
|
|
}, {}},
|
|
|
|
expected: NewMockSeriesSet(
|
|
|
|
NewListSeries(
|
|
|
|
labels.FromStrings("bar", "baz"),
|
|
|
|
[]tsdbutil.Sample{sample{1, 1}, sample{2, 2}, sample{3, 3}, sample{5, 5}, sample{6, 6}},
|
|
|
|
),
|
|
|
|
NewListSeries(
|
|
|
|
labels.FromStrings("foo", "bar"),
|
|
|
|
[]tsdbutil.Sample{sample{0, 0}, sample{1, 1}, sample{2, 2}, sample{3, 3}, sample{4, 4}},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "2 queriers, only 2 queriers have 2 time unsorted series each, with 3 noop and one nil querier together",
|
|
|
|
querierSeries: [][]Series{{}, {}, {
|
|
|
|
NewListSeries(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{5, 5}, sample{6, 6}}),
|
|
|
|
NewListSeries(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, 0}, sample{1, 1}, sample{2, 2}}),
|
|
|
|
}, {
|
|
|
|
NewListSeries(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{1, 1}, sample{2, 2}, sample{3, 3}}),
|
|
|
|
NewListSeries(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{3, 3}, sample{4, 4}}),
|
|
|
|
}, {}},
|
|
|
|
extraQueriers: []Querier{NoopQuerier(), NoopQuerier(), nil, NoopQuerier()},
|
|
|
|
expected: NewMockSeriesSet(
|
|
|
|
NewListSeries(
|
|
|
|
labels.FromStrings("bar", "baz"),
|
|
|
|
[]tsdbutil.Sample{sample{1, 1}, sample{2, 2}, sample{3, 3}, sample{5, 5}, sample{6, 6}},
|
|
|
|
),
|
|
|
|
NewListSeries(
|
|
|
|
labels.FromStrings("foo", "bar"),
|
|
|
|
[]tsdbutil.Sample{sample{0, 0}, sample{1, 1}, sample{2, 2}, sample{3, 3}, sample{4, 4}},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "2 queriers, with 2 series, one is overlapping",
|
|
|
|
querierSeries: [][]Series{{}, {}, {
|
|
|
|
NewListSeries(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{2, 21}, sample{3, 31}, sample{5, 5}, sample{6, 6}}),
|
|
|
|
NewListSeries(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, 0}, sample{1, 1}, sample{2, 2}}),
|
|
|
|
}, {
|
|
|
|
NewListSeries(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{1, 1}, sample{2, 22}, sample{3, 32}}),
|
|
|
|
NewListSeries(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{3, 3}, sample{4, 4}}),
|
|
|
|
}, {}},
|
|
|
|
expected: NewMockSeriesSet(
|
|
|
|
NewListSeries(
|
|
|
|
labels.FromStrings("bar", "baz"),
|
|
|
|
[]tsdbutil.Sample{sample{1, 1}, sample{2, 21}, sample{3, 31}, sample{5, 5}, sample{6, 6}},
|
|
|
|
),
|
|
|
|
NewListSeries(
|
|
|
|
labels.FromStrings("foo", "bar"),
|
|
|
|
[]tsdbutil.Sample{sample{0, 0}, sample{1, 1}, sample{2, 2}, sample{3, 3}, sample{4, 4}},
|
|
|
|
),
|
2017-07-13 03:05:38 -07:00
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
2020-03-24 13:15:47 -07:00
|
|
|
name: "2 queries, one with NaN samples series",
|
|
|
|
querierSeries: [][]Series{{
|
|
|
|
NewListSeries(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, math.NaN()}}),
|
|
|
|
}, {
|
|
|
|
NewListSeries(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{1, 1}}),
|
|
|
|
}},
|
|
|
|
expected: NewMockSeriesSet(
|
|
|
|
NewListSeries(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, math.NaN()}, sample{1, 1}}),
|
2017-07-13 03:05:38 -07:00
|
|
|
),
|
|
|
|
},
|
2020-03-24 13:15:47 -07:00
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
var qs []Querier
|
|
|
|
for _, in := range tc.querierSeries {
|
|
|
|
qs = append(qs, &mockQuerier{toReturn: in})
|
|
|
|
}
|
|
|
|
qs = append(qs, tc.extraQueriers...)
|
|
|
|
|
2020-06-08 11:55:53 -07:00
|
|
|
mergedQuerier, _, _ := NewMergeQuerier(qs[0], qs, ChainedSeriesMerge).Select(false, nil)
|
|
|
|
|
|
|
|
// Get all merged series upfront to make sure there are no incorrectly retained shared
|
|
|
|
// buffers causing bugs.
|
|
|
|
var mergedSeries []Series
|
|
|
|
for mergedQuerier.Next() {
|
|
|
|
mergedSeries = append(mergedSeries, mergedQuerier.At())
|
|
|
|
}
|
|
|
|
testutil.Ok(t, mergedQuerier.Err())
|
|
|
|
|
|
|
|
for _, actualSeries := range mergedSeries {
|
2020-03-24 13:15:47 -07:00
|
|
|
testutil.Assert(t, tc.expected.Next(), "Expected Next() to be true")
|
|
|
|
expectedSeries := tc.expected.At()
|
|
|
|
testutil.Equals(t, expectedSeries.Labels(), actualSeries.Labels())
|
|
|
|
|
|
|
|
expSmpl, expErr := ExpandSamples(expectedSeries.Iterator())
|
|
|
|
actSmpl, actErr := ExpandSamples(actualSeries.Iterator())
|
|
|
|
testutil.Equals(t, expErr, actErr)
|
|
|
|
testutil.Equals(t, expSmpl, actSmpl)
|
|
|
|
}
|
|
|
|
testutil.Assert(t, !tc.expected.Next(), "Expected Next() to be false")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeChunkQuerierWithNoVerticalChunkSeriesMerger(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
chkQuerierSeries [][]ChunkSeries
|
|
|
|
extraQueriers []ChunkQuerier
|
2017-07-13 03:05:38 -07:00
|
|
|
|
2020-03-24 13:15:47 -07:00
|
|
|
expected ChunkSeriesSet
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "one querier with no series",
|
|
|
|
chkQuerierSeries: [][]ChunkSeries{{}},
|
|
|
|
expected: NewMockChunkSeriesSet(),
|
|
|
|
},
|
2017-07-13 03:05:38 -07:00
|
|
|
{
|
2020-03-24 13:15:47 -07:00
|
|
|
name: "many queriers with no series",
|
|
|
|
chkQuerierSeries: [][]ChunkSeries{{}, {}, {}, {}, {}, {}, {}},
|
|
|
|
expected: NewMockChunkSeriesSet(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "one querier, two series",
|
|
|
|
chkQuerierSeries: [][]ChunkSeries{{
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{1, 1}, sample{2, 2}}, []tsdbutil.Sample{sample{3, 3}}),
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, 0}, sample{1, 1}}, []tsdbutil.Sample{sample{2, 2}}),
|
|
|
|
}},
|
|
|
|
expected: NewMockChunkSeriesSet(
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{1, 1}, sample{2, 2}}, []tsdbutil.Sample{sample{3, 3}}),
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, 0}, sample{1, 1}}, []tsdbutil.Sample{sample{2, 2}}),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "two queriers, one different series each",
|
|
|
|
chkQuerierSeries: [][]ChunkSeries{{
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{1, 1}, sample{2, 2}}, []tsdbutil.Sample{sample{3, 3}}),
|
|
|
|
}, {
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, 0}, sample{1, 1}}, []tsdbutil.Sample{sample{2, 2}}),
|
|
|
|
}},
|
|
|
|
expected: NewMockChunkSeriesSet(
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{1, 1}, sample{2, 2}}, []tsdbutil.Sample{sample{3, 3}}),
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, 0}, sample{1, 1}}, []tsdbutil.Sample{sample{2, 2}}),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "two queriers, two not in time order series each",
|
|
|
|
chkQuerierSeries: [][]ChunkSeries{{
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{5, 5}}, []tsdbutil.Sample{sample{6, 6}}),
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, 0}, sample{1, 1}}, []tsdbutil.Sample{sample{2, 2}}),
|
|
|
|
}, {
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{1, 1}, sample{2, 2}}, []tsdbutil.Sample{sample{3, 3}}),
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{3, 3}}, []tsdbutil.Sample{sample{4, 4}}),
|
|
|
|
}},
|
|
|
|
expected: NewMockChunkSeriesSet(
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("bar", "baz"),
|
|
|
|
[]tsdbutil.Sample{sample{1, 1}, sample{2, 2}},
|
|
|
|
[]tsdbutil.Sample{sample{3, 3}},
|
|
|
|
[]tsdbutil.Sample{sample{5, 5}},
|
|
|
|
[]tsdbutil.Sample{sample{6, 6}},
|
|
|
|
),
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("foo", "bar"),
|
|
|
|
[]tsdbutil.Sample{sample{0, 0}, sample{1, 1}},
|
|
|
|
[]tsdbutil.Sample{sample{2, 2}},
|
|
|
|
[]tsdbutil.Sample{sample{3, 3}},
|
|
|
|
[]tsdbutil.Sample{sample{4, 4}},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "five queriers, only two have two not in time order series each",
|
|
|
|
chkQuerierSeries: [][]ChunkSeries{{}, {}, {
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{5, 5}}, []tsdbutil.Sample{sample{6, 6}}),
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, 0}, sample{1, 1}}, []tsdbutil.Sample{sample{2, 2}}),
|
|
|
|
}, {
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{1, 1}, sample{2, 2}}, []tsdbutil.Sample{sample{3, 3}}),
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{3, 3}}, []tsdbutil.Sample{sample{4, 4}}),
|
|
|
|
}, {}},
|
|
|
|
expected: NewMockChunkSeriesSet(
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("bar", "baz"),
|
|
|
|
[]tsdbutil.Sample{sample{1, 1}, sample{2, 2}},
|
|
|
|
[]tsdbutil.Sample{sample{3, 3}},
|
|
|
|
[]tsdbutil.Sample{sample{5, 5}},
|
|
|
|
[]tsdbutil.Sample{sample{6, 6}},
|
|
|
|
),
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("foo", "bar"),
|
|
|
|
[]tsdbutil.Sample{sample{0, 0}, sample{1, 1}},
|
|
|
|
[]tsdbutil.Sample{sample{2, 2}},
|
|
|
|
[]tsdbutil.Sample{sample{3, 3}},
|
|
|
|
[]tsdbutil.Sample{sample{4, 4}},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "two queriers, with two not in time order series each, with 3 noop queries and one nil together",
|
|
|
|
chkQuerierSeries: [][]ChunkSeries{{
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{5, 5}}, []tsdbutil.Sample{sample{6, 6}}),
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, 0}, sample{1, 1}}, []tsdbutil.Sample{sample{2, 2}}),
|
|
|
|
}, {
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("bar", "baz"), []tsdbutil.Sample{sample{1, 1}, sample{2, 2}}, []tsdbutil.Sample{sample{3, 3}}),
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{3, 3}}, []tsdbutil.Sample{sample{4, 4}}),
|
|
|
|
}},
|
|
|
|
extraQueriers: []ChunkQuerier{NoopChunkedQuerier(), NoopChunkedQuerier(), nil, NoopChunkedQuerier()},
|
|
|
|
expected: NewMockChunkSeriesSet(
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("bar", "baz"),
|
|
|
|
[]tsdbutil.Sample{sample{1, 1}, sample{2, 2}},
|
|
|
|
[]tsdbutil.Sample{sample{3, 3}},
|
|
|
|
[]tsdbutil.Sample{sample{5, 5}},
|
|
|
|
[]tsdbutil.Sample{sample{6, 6}},
|
|
|
|
),
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("foo", "bar"),
|
|
|
|
[]tsdbutil.Sample{sample{0, 0}, sample{1, 1}},
|
|
|
|
[]tsdbutil.Sample{sample{2, 2}},
|
|
|
|
[]tsdbutil.Sample{sample{3, 3}},
|
|
|
|
[]tsdbutil.Sample{sample{4, 4}},
|
|
|
|
),
|
2017-07-13 03:05:38 -07:00
|
|
|
),
|
|
|
|
},
|
2018-06-18 09:34:08 -07:00
|
|
|
{
|
2020-03-24 13:15:47 -07:00
|
|
|
name: "two queries, one with NaN samples series",
|
|
|
|
chkQuerierSeries: [][]ChunkSeries{{
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, math.NaN()}}),
|
|
|
|
}, {
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{1, 1}}),
|
|
|
|
}},
|
|
|
|
expected: NewMockChunkSeriesSet(
|
|
|
|
NewListChunkSeriesFromSamples(labels.FromStrings("foo", "bar"), []tsdbutil.Sample{sample{0, math.NaN()}}, []tsdbutil.Sample{sample{1, 1}}),
|
2018-06-18 09:34:08 -07:00
|
|
|
),
|
|
|
|
},
|
2017-07-13 03:05:38 -07:00
|
|
|
} {
|
2020-03-24 13:15:47 -07:00
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
var qs []ChunkQuerier
|
|
|
|
for _, in := range tc.chkQuerierSeries {
|
|
|
|
qs = append(qs, &mockChunkQurier{toReturn: in})
|
|
|
|
}
|
|
|
|
qs = append(qs, tc.extraQueriers...)
|
|
|
|
|
|
|
|
merged, _, _ := NewMergeChunkQuerier(qs[0], qs, NewVerticalChunkSeriesMerger(nil)).Select(false, nil)
|
|
|
|
for merged.Next() {
|
|
|
|
testutil.Assert(t, tc.expected.Next(), "Expected Next() to be true")
|
|
|
|
actualSeries := merged.At()
|
|
|
|
expectedSeries := tc.expected.At()
|
|
|
|
testutil.Equals(t, expectedSeries.Labels(), actualSeries.Labels())
|
|
|
|
|
|
|
|
expChks, expErr := ExpandChunks(expectedSeries.Iterator())
|
|
|
|
actChks, actErr := ExpandChunks(actualSeries.Iterator())
|
|
|
|
testutil.Equals(t, expErr, actErr)
|
|
|
|
testutil.Equals(t, expChks, actChks)
|
|
|
|
|
|
|
|
}
|
|
|
|
testutil.Ok(t, merged.Err())
|
|
|
|
testutil.Assert(t, !tc.expected.Next(), "Expected Next() to be false")
|
|
|
|
})
|
2017-07-13 03:05:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:15:47 -07:00
|
|
|
type mockQuerier struct {
|
|
|
|
baseQuerier
|
|
|
|
|
|
|
|
toReturn []Series
|
|
|
|
}
|
|
|
|
|
|
|
|
type seriesByLabel []Series
|
|
|
|
|
|
|
|
func (a seriesByLabel) Len() int { return len(a) }
|
|
|
|
func (a seriesByLabel) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a seriesByLabel) Less(i, j int) bool { return labels.Compare(a[i].Labels(), a[j].Labels()) < 0 }
|
|
|
|
|
|
|
|
func (m *mockQuerier) Select(sortSeries bool, _ *SelectHints, _ ...*labels.Matcher) (SeriesSet, Warnings, error) {
|
|
|
|
cpy := make([]Series, len(m.toReturn))
|
|
|
|
copy(cpy, m.toReturn)
|
|
|
|
if sortSeries {
|
|
|
|
sort.Sort(seriesByLabel(cpy))
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewMockSeriesSet(cpy...), nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockChunkQurier struct {
|
|
|
|
baseQuerier
|
|
|
|
|
|
|
|
toReturn []ChunkSeries
|
|
|
|
}
|
|
|
|
|
|
|
|
type chunkSeriesByLabel []ChunkSeries
|
|
|
|
|
|
|
|
func (a chunkSeriesByLabel) Len() int { return len(a) }
|
|
|
|
func (a chunkSeriesByLabel) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a chunkSeriesByLabel) Less(i, j int) bool {
|
|
|
|
return labels.Compare(a[i].Labels(), a[j].Labels()) < 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockChunkQurier) Select(sortSeries bool, _ *SelectHints, _ ...*labels.Matcher) (ChunkSeriesSet, Warnings, error) {
|
|
|
|
cpy := make([]ChunkSeries, len(m.toReturn))
|
|
|
|
copy(cpy, m.toReturn)
|
|
|
|
if sortSeries {
|
|
|
|
sort.Sort(chunkSeriesByLabel(cpy))
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewMockChunkSeriesSet(cpy...), nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockSeriesSet struct {
|
|
|
|
idx int
|
|
|
|
series []Series
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMockSeriesSet(series ...Series) SeriesSet {
|
|
|
|
return &mockSeriesSet{
|
|
|
|
idx: -1,
|
|
|
|
series: series,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockSeriesSet) Next() bool {
|
|
|
|
m.idx++
|
|
|
|
return m.idx < len(m.series)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockSeriesSet) At() Series { return m.series[m.idx] }
|
|
|
|
|
|
|
|
func (m *mockSeriesSet) Err() error { return nil }
|
|
|
|
|
|
|
|
type mockChunkSeriesSet struct {
|
|
|
|
idx int
|
|
|
|
series []ChunkSeries
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMockChunkSeriesSet(series ...ChunkSeries) ChunkSeriesSet {
|
|
|
|
return &mockChunkSeriesSet{
|
|
|
|
idx: -1,
|
|
|
|
series: series,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockChunkSeriesSet) Next() bool {
|
|
|
|
m.idx++
|
|
|
|
return m.idx < len(m.series)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockChunkSeriesSet) At() ChunkSeries { return m.series[m.idx] }
|
|
|
|
|
|
|
|
func (m *mockChunkSeriesSet) Err() error { return nil }
|
|
|
|
|
|
|
|
func TestChainSampleIterator(t *testing.T) {
|
2017-07-13 03:05:38 -07:00
|
|
|
for _, tc := range []struct {
|
2020-02-06 07:58:38 -08:00
|
|
|
input []chunkenc.Iterator
|
2020-03-24 13:15:47 -07:00
|
|
|
expected []tsdbutil.Sample
|
2017-07-13 03:05:38 -07:00
|
|
|
}{
|
|
|
|
{
|
2020-02-06 07:58:38 -08:00
|
|
|
input: []chunkenc.Iterator{
|
2020-03-24 13:15:47 -07:00
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{sample{0, 0}, sample{1, 1}}),
|
2017-07-13 03:05:38 -07:00
|
|
|
},
|
2020-03-24 13:15:47 -07:00
|
|
|
expected: []tsdbutil.Sample{sample{0, 0}, sample{1, 1}},
|
2017-07-13 03:05:38 -07:00
|
|
|
},
|
|
|
|
{
|
2020-02-06 07:58:38 -08:00
|
|
|
input: []chunkenc.Iterator{
|
2020-03-24 13:15:47 -07:00
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{sample{0, 0}, sample{1, 1}}),
|
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{sample{2, 2}, sample{3, 3}}),
|
2017-07-13 03:05:38 -07:00
|
|
|
},
|
2020-03-24 13:15:47 -07:00
|
|
|
expected: []tsdbutil.Sample{sample{0, 0}, sample{1, 1}, sample{2, 2}, sample{3, 3}},
|
2017-07-13 03:05:38 -07:00
|
|
|
},
|
|
|
|
{
|
2020-02-06 07:58:38 -08:00
|
|
|
input: []chunkenc.Iterator{
|
2020-03-24 13:15:47 -07:00
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{sample{0, 0}, sample{3, 3}}),
|
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{sample{1, 1}, sample{4, 4}}),
|
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{sample{2, 2}, sample{5, 5}}),
|
2017-07-13 03:05:38 -07:00
|
|
|
},
|
2020-03-24 13:15:47 -07:00
|
|
|
expected: []tsdbutil.Sample{
|
|
|
|
sample{0, 0}, sample{1, 1}, sample{2, 2}, sample{3, 3}, sample{4, 4}, sample{5, 5}},
|
2017-07-13 03:05:38 -07:00
|
|
|
},
|
2020-03-24 13:15:47 -07:00
|
|
|
// Overlap.
|
2018-05-23 04:15:47 -07:00
|
|
|
{
|
2020-02-06 07:58:38 -08:00
|
|
|
input: []chunkenc.Iterator{
|
2020-03-24 13:15:47 -07:00
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{sample{0, 0}, sample{1, 1}}),
|
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{sample{0, 0}, sample{2, 2}}),
|
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{sample{2, 2}, sample{3, 3}}),
|
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{}),
|
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{}),
|
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{}),
|
2018-05-23 04:15:47 -07:00
|
|
|
},
|
2020-03-24 13:15:47 -07:00
|
|
|
expected: []tsdbutil.Sample{sample{0, 0}, sample{1, 1}, sample{2, 2}, sample{3, 3}},
|
2018-05-23 04:15:47 -07:00
|
|
|
},
|
2017-07-13 03:05:38 -07:00
|
|
|
} {
|
2020-03-24 13:15:47 -07:00
|
|
|
merged := newChainSampleIterator(tc.input)
|
|
|
|
actual, err := ExpandSamples(merged)
|
|
|
|
testutil.Ok(t, err)
|
2019-08-08 18:36:42 -07:00
|
|
|
testutil.Equals(t, tc.expected, actual)
|
2017-07-13 03:05:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:15:47 -07:00
|
|
|
func TestChainSampleIteratorSeek(t *testing.T) {
|
2017-07-13 03:05:38 -07:00
|
|
|
for _, tc := range []struct {
|
2020-02-06 07:58:38 -08:00
|
|
|
input []chunkenc.Iterator
|
2017-07-13 03:05:38 -07:00
|
|
|
seek int64
|
2020-03-24 13:15:47 -07:00
|
|
|
expected []tsdbutil.Sample
|
2017-07-13 03:05:38 -07:00
|
|
|
}{
|
|
|
|
{
|
2020-02-06 07:58:38 -08:00
|
|
|
input: []chunkenc.Iterator{
|
2020-03-24 13:15:47 -07:00
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{sample{0, 0}, sample{1, 1}, sample{2, 2}}),
|
2017-07-13 03:05:38 -07:00
|
|
|
},
|
|
|
|
seek: 1,
|
2020-03-24 13:15:47 -07:00
|
|
|
expected: []tsdbutil.Sample{sample{1, 1}, sample{2, 2}},
|
2017-07-13 03:05:38 -07:00
|
|
|
},
|
|
|
|
{
|
2020-02-06 07:58:38 -08:00
|
|
|
input: []chunkenc.Iterator{
|
2020-03-24 13:15:47 -07:00
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{sample{0, 0}, sample{1, 1}}),
|
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{sample{2, 2}, sample{3, 3}}),
|
2017-07-13 03:05:38 -07:00
|
|
|
},
|
|
|
|
seek: 2,
|
2020-03-24 13:15:47 -07:00
|
|
|
expected: []tsdbutil.Sample{sample{2, 2}, sample{3, 3}},
|
2017-07-13 03:05:38 -07:00
|
|
|
},
|
|
|
|
{
|
2020-02-06 07:58:38 -08:00
|
|
|
input: []chunkenc.Iterator{
|
2020-03-24 13:15:47 -07:00
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{sample{0, 0}, sample{3, 3}}),
|
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{sample{1, 1}, sample{4, 4}}),
|
|
|
|
NewListSeriesIterator([]tsdbutil.Sample{sample{2, 2}, sample{5, 5}}),
|
2017-07-13 03:05:38 -07:00
|
|
|
},
|
|
|
|
seek: 2,
|
2020-03-24 13:15:47 -07:00
|
|
|
expected: []tsdbutil.Sample{sample{2, 2}, sample{3, 3}, sample{4, 4}, sample{5, 5}},
|
2017-07-13 03:05:38 -07:00
|
|
|
},
|
|
|
|
} {
|
2020-03-24 13:15:47 -07:00
|
|
|
merged := newChainSampleIterator(tc.input)
|
|
|
|
actual := []tsdbutil.Sample{}
|
2017-07-13 03:05:38 -07:00
|
|
|
if merged.Seek(tc.seek) {
|
|
|
|
t, v := merged.At()
|
|
|
|
actual = append(actual, sample{t, v})
|
|
|
|
}
|
2020-03-24 13:15:47 -07:00
|
|
|
s, err := ExpandSamples(merged)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
actual = append(actual, s...)
|
2019-08-08 18:36:42 -07:00
|
|
|
testutil.Equals(t, tc.expected, actual)
|
2017-07-13 03:05:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 13:15:47 -07:00
|
|
|
var result []tsdbutil.Sample
|
2018-01-26 03:01:59 -08:00
|
|
|
|
|
|
|
func makeSeriesSet(numSeries, numSamples int) SeriesSet {
|
|
|
|
series := []Series{}
|
|
|
|
for j := 0; j < numSeries; j++ {
|
|
|
|
labels := labels.Labels{{Name: "foo", Value: fmt.Sprintf("bar%d", j)}}
|
2020-03-24 13:15:47 -07:00
|
|
|
samples := []tsdbutil.Sample{}
|
2018-01-26 03:01:59 -08:00
|
|
|
for k := 0; k < numSamples; k++ {
|
|
|
|
samples = append(samples, sample{t: int64(k), v: float64(k)})
|
|
|
|
}
|
2020-03-24 13:15:47 -07:00
|
|
|
series = append(series, NewListSeries(labels, samples))
|
2018-01-26 03:01:59 -08:00
|
|
|
}
|
2020-03-24 13:15:47 -07:00
|
|
|
return NewMockSeriesSet(series...)
|
2018-01-26 03:01:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeMergeSeriesSet(numSeriesSets, numSeries, numSamples int) SeriesSet {
|
2020-03-24 13:15:47 -07:00
|
|
|
seriesSets := []genericSeriesSet{}
|
2018-01-26 03:01:59 -08:00
|
|
|
for i := 0; i < numSeriesSets; i++ {
|
2020-03-24 13:15:47 -07:00
|
|
|
seriesSets = append(seriesSets, &genericSeriesSetAdapter{makeSeriesSet(numSeries, numSamples)})
|
2018-01-26 03:01:59 -08:00
|
|
|
}
|
2020-03-24 13:15:47 -07:00
|
|
|
return &seriesSetAdapter{newGenericMergeSeriesSet(seriesSets, nil, (&seriesMergerAdapter{VerticalSeriesMergeFunc: ChainedSeriesMerge}).Merge)}
|
2018-01-26 03:01:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func benchmarkDrain(seriesSet SeriesSet, b *testing.B) {
|
2020-03-24 13:15:47 -07:00
|
|
|
var err error
|
2018-01-26 03:01:59 -08:00
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
for seriesSet.Next() {
|
2020-03-24 13:15:47 -07:00
|
|
|
result, err = ExpandSamples(seriesSet.At().Iterator())
|
|
|
|
testutil.Ok(b, err)
|
2018-01-26 03:01:59 -08:00
|
|
|
}
|
2017-07-13 03:05:38 -07:00
|
|
|
}
|
|
|
|
}
|
2018-01-26 03:01:59 -08:00
|
|
|
|
|
|
|
func BenchmarkNoMergeSeriesSet_100_100(b *testing.B) {
|
|
|
|
seriesSet := makeSeriesSet(100, 100)
|
|
|
|
benchmarkDrain(seriesSet, b)
|
|
|
|
}
|
|
|
|
|
2018-01-29 03:37:48 -08:00
|
|
|
func BenchmarkMergeSeriesSet(b *testing.B) {
|
|
|
|
for _, bm := range []struct {
|
|
|
|
numSeriesSets, numSeries, numSamples int
|
|
|
|
}{
|
|
|
|
{1, 100, 100},
|
|
|
|
{10, 100, 100},
|
|
|
|
{100, 100, 100},
|
|
|
|
} {
|
|
|
|
seriesSet := makeMergeSeriesSet(bm.numSeriesSets, bm.numSeries, bm.numSamples)
|
|
|
|
b.Run(fmt.Sprintf("%d_%d_%d", bm.numSeriesSets, bm.numSeries, bm.numSamples), func(b *testing.B) {
|
|
|
|
benchmarkDrain(seriesSet, b)
|
|
|
|
})
|
|
|
|
}
|
2018-01-26 03:01:59 -08:00
|
|
|
}
|