diff --git a/pkg/query/querier_test.go b/pkg/query/querier_test.go index 22e9f79cbd1..ad764cddfc5 100644 --- a/pkg/query/querier_test.go +++ b/pkg/query/querier_test.go @@ -602,68 +602,6 @@ func TestQuerier_Select_After_promql(t *testing.T) { expectedWarning string }{ - { - name: "A exhausted first", - storeAPI: &storeServer{ - resps: []*storepb.SeriesResponse{ - storeSeriesResponse(t, labels.FromStrings("a", "a"), []sample{{0, 0}, {5000, 5000}, {10000, 10000}, {15000, 15000}}), - storeSeriesResponse(t, labels.FromStrings("a", "b"), []sample{{0, 0}, {5000, 5000}, {10000, 10000}, {15000, 15000}, {25000, 5}}), // Last sample should be at least 2 * the delta. - }, - }, - hints: &storage.SelectHints{ - Start: 0, - End: 25000, - Step: 5000, - }, - replicaLabels: []string{"a"}, - equivalentQuery: `{a=~"a|b"}`, - expected: []series{ - { - lset: labels.FromStrings("a", "a"), - samples: []sample{{0, 0}, {5000, 5000}, {10000, 10000}, {15000, 15000}, {20000, 15000}, {25000, 15000}}, - }, - { - lset: labels.FromStrings("a", "b"), - samples: []sample{{0, 0}, {5000, 5000}, {10000, 10000}, {15000, 15000}, {20000, 15000}, {25000, 5}}, - }, - }, - expectedAfterDedup: series{ - lset: labels.Labels{}, - samples: []sample{{0, 0}, {5000, 5000}, {10000, 10000}, {15000, 15000}, {20000, 15000}, {25000, 5}}, - }, - }, - - { - name: "B exhausted first", - storeAPI: &storeServer{ - resps: []*storepb.SeriesResponse{ - storeSeriesResponse(t, labels.FromStrings("a", "a"), []sample{{0, 0}, {5000, 5000}, {10000, 10000}, {15000, 15000}, {25000, 5}}), // Last sample should be at least 2 * the delta. - storeSeriesResponse(t, labels.FromStrings("a", "b"), []sample{{0, 0}, {5000, 5000}, {10000, 10000}, {15000, 15000}}), - }, - }, - hints: &storage.SelectHints{ - Start: 0, - End: 25000, - Step: 5000, - }, - replicaLabels: []string{"a"}, - equivalentQuery: `{a=~"a|b"}`, - expected: []series{ - { - lset: labels.FromStrings("a", "a"), - samples: []sample{{0, 0}, {5000, 5000}, {10000, 10000}, {15000, 15000}, {20000, 15000}, {25000, 5}}, - }, - { - lset: labels.FromStrings("a", "b"), - samples: []sample{{0, 0}, {5000, 5000}, {10000, 10000}, {15000, 15000}, {20000, 15000}, {25000, 15000}}, - }, - }, - expectedAfterDedup: series{ - lset: labels.Labels{}, - samples: []sample{{0, 0}, {5000, 5000}, {10000, 10000}, {15000, 15000}, {20000, 15000}, {25000, 5}}, - }, - }, - { // Simulate Prom with 1m scrape interval scraping 30s apart. // This should start with replica-1 until a brief outage, @@ -739,13 +677,15 @@ func TestQuerier_Select_After_promql(t *testing.T) { // Boostrap a local store and pass the data through promql. { g := gate.New(2) - qCreator := &mockedQueryable{ + mq := &mockedQueryable{ Creator: func(mint, maxt int64) storage.Querier { return newQuerier(context.Background(), nil, nil, mint, maxt, tcase.replicaLabels, nil, tcase.storeAPI, sc.dedup, 0, true, false, g, timeout) }, } - t.Cleanup(func() { testutil.Ok(t, qCreator.Close()) }) - q, err := e.NewRangeQuery(qCreator, tcase.equivalentQuery, timestamp.Time(tcase.hints.Start), timestamp.Time(tcase.hints.End), resolution) + t.Cleanup(func() { + testutil.Ok(t, mq.Close()) + }) + q, err := e.NewRangeQuery(mq, tcase.equivalentQuery, timestamp.Time(tcase.hints.Start), timestamp.Time(tcase.hints.End), resolution) testutil.Ok(t, err) t.Cleanup(q.Close) res := q.Exec(context.Background()) @@ -773,10 +713,9 @@ func jsonToSeries(t *testing.T, filename string) []series { testutil.Ok(t, err) data := Response{} - err = json.Unmarshal(file, &data) - testutil.Ok(t, err) + testutil.Ok(t, json.Unmarshal(file, &data)) - var sers []series + var ss []series for _, ser := range data.Data.Results { var lbls labels.Labels for n, v := range ser.Metric { @@ -785,7 +724,7 @@ func jsonToSeries(t *testing.T, filename string) []series { Value: string(v), }) } - // Label names need to be sorted + // Label names need to be sorted. sort.Sort(lbls) var smpls []sample @@ -796,18 +735,18 @@ func jsonToSeries(t *testing.T, filename string) []series { }) } - sers = append(sers, series{ + ss = append(ss, series{ lset: lbls, samples: smpls, }) } // Sort the series by their labels. - sort.Slice(sers, func(i, j int) bool { - return labels.Compare(sers[i].lset, sers[j].lset) <= 0 + sort.Slice(ss, func(i, j int) bool { + return labels.Compare(ss[i].lset, ss[j].lset) <= 0 }) - return sers + return ss } type Response struct { @@ -860,7 +799,7 @@ type mockedQueryable struct { } // Querier creates a queirier with the provided min and maxt. -// The promq engine set mint calculated based on the default lookback delta. +// The promq engine sets mint and it is calculated based on the default lookback delta. func (q *mockedQueryable) Querier(_ context.Context, mint int64, maxt int64) (storage.Querier, error) { if q.Creator == nil { return q.querier, nil @@ -1468,27 +1407,36 @@ func TestDedupSeriesSet(t *testing.T) { } func TestDedupSeriesIterator(t *testing.T) { + // The deltas between timestamps should be at least 10000 to not be affected + // by the initial penalty of 5000, that will cause the second iterator to seek + // ahead this far at least once. cases := []struct { a, b, exp []sample - step int64 }{ { // Generally prefer the first series. - step: 10000, - a: []sample{{10000, 10}, {20000, 11}, {30000, 12}, {40000, 13}}, - b: []sample{{10000, 20}, {20000, 21}, {30000, 22}, {40000, 23}}, - exp: []sample{{10000, 10}, {20000, 11}, {30000, 12}, {40000, 13}}, + a: []sample{{10000, 10}, {20000, 11}, {30000, 12}, {40000, 13}}, + b: []sample{{10000, 20}, {20000, 21}, {30000, 22}, {40000, 23}}, + exp: []sample{{10000, 10}, {20000, 11}, {30000, 12}, {40000, 13}}, }, { // Prefer b if it starts earlier. - step: 10000, - a: []sample{{10100, 1}, {20100, 1}, {30100, 1}, {40100, 1}}, - b: []sample{{10000, 2}, {20000, 2}, {30000, 2}, {40000, 2}}, - exp: []sample{{10000, 2}, {20000, 2}, {30000, 2}, {40000, 2}}, + a: []sample{{10100, 1}, {20100, 1}, {30100, 1}, {40100, 1}}, + b: []sample{{10000, 2}, {20000, 2}, {30000, 2}, {40000, 2}}, + exp: []sample{{10000, 2}, {20000, 2}, {30000, 2}, {40000, 2}}, + }, + { // Don't switch series on a single delta sized gap. + a: []sample{{10000, 1}, {20000, 1}, {40000, 1}}, + b: []sample{{10000, 2}, {20000, 2}, {30000, 2}, {40000, 2}}, + exp: []sample{{10000, 1}, {20000, 1}, {40000, 1}}, + }, + { + a: []sample{{10000, 1}, {20000, 1}, {40000, 1}}, + b: []sample{{15000, 2}, {25000, 2}, {35000, 2}, {45000, 2}}, + exp: []sample{{10000, 1}, {20000, 1}, {40000, 1}}, }, - { // Don't skip samples within the step when switching replicas. - step: 10000, - a: []sample{{10000, 1}, {20000, 1}, {40000, 1}}, - b: []sample{{10000, 2}, {20000, 2}, {40000, 2}, {50000, 2}, {60000, 2}}, - exp: []sample{{10000, 1}, {20000, 1}, {40000, 1}, {50000, 2}, {60000, 2}}, + { // Once the gap gets bigger than 2 deltas, switch and stay with the new series. + a: []sample{{10000, 1}, {20000, 1}, {30000, 1}, {60000, 1}, {70000, 1}}, + b: []sample{{10100, 2}, {20100, 2}, {30100, 2}, {40100, 2}, {50100, 2}, {60100, 2}}, + exp: []sample{{10000, 1}, {20000, 1}, {30000, 1}, {50100, 2}, {60100, 2}}, }, } for i, c := range cases {