2020-08-03 03:32:56 -07:00
|
|
|
// Copyright 2020 The Prometheus Authors
|
2017-07-13 03:05:38 -07:00
|
|
|
// 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
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
package storage_test
|
2017-07-13 03:05:38 -07:00
|
|
|
|
|
|
|
import (
|
2020-08-03 03:32:56 -07:00
|
|
|
"context"
|
2023-11-02 13:45:07 -07:00
|
|
|
"errors"
|
2017-07-13 03:05:38 -07:00
|
|
|
"testing"
|
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2020-10-29 02:43:23 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-06-09 09:57:31 -07:00
|
|
|
|
2021-11-08 06:23:17 -08:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
2020-08-03 03:32:56 -07:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2021-11-28 23:54:23 -08:00
|
|
|
"github.com/prometheus/prometheus/tsdb/chunkenc"
|
2023-09-14 09:57:31 -07:00
|
|
|
"github.com/prometheus/prometheus/util/annotations"
|
2020-08-03 03:32:56 -07:00
|
|
|
"github.com/prometheus/prometheus/util/teststorage"
|
2017-07-13 03:05:38 -07:00
|
|
|
)
|
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
func TestFanout_SelectSorted(t *testing.T) {
|
|
|
|
inputLabel := labels.FromStrings(model.MetricNameLabel, "a")
|
|
|
|
outputLabel := labels.FromStrings(model.MetricNameLabel, "a")
|
|
|
|
|
|
|
|
inputTotalSize := 0
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
priStorage := teststorage.New(t)
|
|
|
|
defer priStorage.Close()
|
|
|
|
app1 := priStorage.Appender(ctx)
|
2021-02-18 04:07:00 -08:00
|
|
|
app1.Append(0, inputLabel, 0, 0)
|
2020-08-03 03:32:56 -07:00
|
|
|
inputTotalSize++
|
2021-02-18 04:07:00 -08:00
|
|
|
app1.Append(0, inputLabel, 1000, 1)
|
2020-08-03 03:32:56 -07:00
|
|
|
inputTotalSize++
|
2021-02-18 04:07:00 -08:00
|
|
|
app1.Append(0, inputLabel, 2000, 2)
|
2020-08-03 03:32:56 -07:00
|
|
|
inputTotalSize++
|
|
|
|
err := app1.Commit()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-08-03 03:32:56 -07:00
|
|
|
|
|
|
|
remoteStorage1 := teststorage.New(t)
|
|
|
|
defer remoteStorage1.Close()
|
|
|
|
app2 := remoteStorage1.Appender(ctx)
|
2021-02-18 04:07:00 -08:00
|
|
|
app2.Append(0, inputLabel, 3000, 3)
|
2020-08-03 03:32:56 -07:00
|
|
|
inputTotalSize++
|
2021-02-18 04:07:00 -08:00
|
|
|
app2.Append(0, inputLabel, 4000, 4)
|
2020-08-03 03:32:56 -07:00
|
|
|
inputTotalSize++
|
2021-02-18 04:07:00 -08:00
|
|
|
app2.Append(0, inputLabel, 5000, 5)
|
2020-08-03 03:32:56 -07:00
|
|
|
inputTotalSize++
|
|
|
|
err = app2.Commit()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-08-03 03:32:56 -07:00
|
|
|
|
|
|
|
remoteStorage2 := teststorage.New(t)
|
|
|
|
defer remoteStorage2.Close()
|
|
|
|
|
|
|
|
app3 := remoteStorage2.Appender(ctx)
|
2021-02-18 04:07:00 -08:00
|
|
|
app3.Append(0, inputLabel, 6000, 6)
|
2020-08-03 03:32:56 -07:00
|
|
|
inputTotalSize++
|
2021-02-18 04:07:00 -08:00
|
|
|
app3.Append(0, inputLabel, 7000, 7)
|
2020-08-03 03:32:56 -07:00
|
|
|
inputTotalSize++
|
2021-02-18 04:07:00 -08:00
|
|
|
app3.Append(0, inputLabel, 8000, 8)
|
2020-08-03 03:32:56 -07:00
|
|
|
inputTotalSize++
|
|
|
|
|
|
|
|
err = app3.Commit()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-08-03 03:32:56 -07:00
|
|
|
|
|
|
|
fanoutStorage := storage.NewFanout(nil, priStorage, remoteStorage1, remoteStorage2)
|
|
|
|
|
|
|
|
t.Run("querier", func(t *testing.T) {
|
2023-09-12 03:37:38 -07:00
|
|
|
querier, err := fanoutStorage.Querier(0, 8000)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-08-03 03:32:56 -07:00
|
|
|
defer querier.Close()
|
2020-03-24 13:15:47 -07:00
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
matcher, err := labels.NewMatcher(labels.MatchEqual, model.MetricNameLabel, "a")
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-03-24 13:15:47 -07:00
|
|
|
|
2023-09-12 03:37:38 -07:00
|
|
|
seriesSet := querier.Select(ctx, true, nil, matcher)
|
2020-06-08 11:55:53 -07:00
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
result := make(map[int64]float64)
|
|
|
|
var labelsResult labels.Labels
|
2022-09-20 10:16:45 -07:00
|
|
|
var iterator chunkenc.Iterator
|
2020-08-03 03:32:56 -07:00
|
|
|
for seriesSet.Next() {
|
|
|
|
series := seriesSet.At()
|
|
|
|
seriesLabels := series.Labels()
|
|
|
|
labelsResult = seriesLabels
|
2022-09-20 10:16:45 -07:00
|
|
|
iterator := series.Iterator(iterator)
|
2021-11-28 23:54:23 -08:00
|
|
|
for iterator.Next() == chunkenc.ValFloat {
|
2020-08-03 03:32:56 -07:00
|
|
|
timestamp, value := iterator.At()
|
|
|
|
result[timestamp] = value
|
2020-06-08 11:55:53 -07:00
|
|
|
}
|
2020-08-03 03:32:56 -07:00
|
|
|
}
|
2020-06-08 11:55:53 -07:00
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, labelsResult, outputLabel)
|
2023-12-07 03:35:01 -08:00
|
|
|
require.Len(t, result, inputTotalSize)
|
2020-08-03 03:32:56 -07:00
|
|
|
})
|
|
|
|
t.Run("chunk querier", func(t *testing.T) {
|
2023-09-12 03:37:38 -07:00
|
|
|
querier, err := fanoutStorage.ChunkQuerier(0, 8000)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-08-03 03:32:56 -07:00
|
|
|
defer querier.Close()
|
2020-03-24 13:15:47 -07:00
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
matcher, err := labels.NewMatcher(labels.MatchEqual, model.MetricNameLabel, "a")
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-03-24 13:15:47 -07:00
|
|
|
|
2023-09-12 03:37:38 -07:00
|
|
|
seriesSet := storage.NewSeriesSetFromChunkSeriesSet(querier.Select(ctx, true, nil, matcher))
|
2017-07-13 03:05:38 -07:00
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
result := make(map[int64]float64)
|
|
|
|
var labelsResult labels.Labels
|
2022-09-20 10:16:45 -07:00
|
|
|
var iterator chunkenc.Iterator
|
2020-08-03 03:32:56 -07:00
|
|
|
for seriesSet.Next() {
|
|
|
|
series := seriesSet.At()
|
|
|
|
seriesLabels := series.Labels()
|
|
|
|
labelsResult = seriesLabels
|
2022-09-20 10:16:45 -07:00
|
|
|
iterator := series.Iterator(iterator)
|
2021-11-28 23:54:23 -08:00
|
|
|
for iterator.Next() == chunkenc.ValFloat {
|
2020-08-03 03:32:56 -07:00
|
|
|
timestamp, value := iterator.At()
|
|
|
|
result[timestamp] = value
|
2020-03-24 13:15:47 -07:00
|
|
|
}
|
2020-08-03 03:32:56 -07:00
|
|
|
}
|
2020-03-24 13:15:47 -07:00
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, seriesSet.Err())
|
|
|
|
require.Equal(t, labelsResult, outputLabel)
|
2023-12-07 03:35:01 -08:00
|
|
|
require.Len(t, result, inputTotalSize)
|
2020-08-03 03:32:56 -07:00
|
|
|
})
|
2017-07-13 03:05:38 -07:00
|
|
|
}
|
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
func TestFanoutErrors(t *testing.T) {
|
|
|
|
workingStorage := teststorage.New(t)
|
|
|
|
defer workingStorage.Close()
|
2020-07-31 08:03:02 -07:00
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
cases := []struct {
|
|
|
|
primary storage.Storage
|
|
|
|
secondary storage.Storage
|
|
|
|
warning error
|
|
|
|
err error
|
2020-07-31 08:03:02 -07:00
|
|
|
}{
|
|
|
|
{
|
2020-08-03 03:32:56 -07:00
|
|
|
primary: workingStorage,
|
|
|
|
secondary: errStorage{},
|
|
|
|
warning: errSelect,
|
|
|
|
err: nil,
|
2020-07-31 08:03:02 -07:00
|
|
|
},
|
|
|
|
{
|
2020-08-03 03:32:56 -07:00
|
|
|
primary: errStorage{},
|
|
|
|
secondary: workingStorage,
|
|
|
|
warning: nil,
|
|
|
|
err: errSelect,
|
2020-07-31 08:03:02 -07:00
|
|
|
},
|
2020-03-24 13:15:47 -07:00
|
|
|
}
|
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
for _, tc := range cases {
|
|
|
|
fanoutStorage := storage.NewFanout(nil, tc.primary, tc.secondary)
|
2020-03-24 13:15:47 -07:00
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
t.Run("samples", func(t *testing.T) {
|
2023-09-12 03:37:38 -07:00
|
|
|
querier, err := fanoutStorage.Querier(0, 8000)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-08-03 03:32:56 -07:00
|
|
|
defer querier.Close()
|
2020-03-24 13:15:47 -07:00
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
matcher := labels.MustNewMatcher(labels.MatchEqual, "a", "b")
|
2023-09-12 03:37:38 -07:00
|
|
|
ss := querier.Select(context.Background(), true, nil, matcher)
|
2020-03-24 13:15:47 -07:00
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
// Exhaust.
|
|
|
|
for ss.Next() {
|
|
|
|
ss.At()
|
|
|
|
}
|
2018-01-26 03:01:59 -08:00
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
if tc.err != nil {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Error(t, ss.Err())
|
|
|
|
require.Equal(t, tc.err.Error(), ss.Err().Error())
|
2020-08-03 03:32:56 -07:00
|
|
|
}
|
2018-01-26 03:01:59 -08:00
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
if tc.warning != nil {
|
2023-12-07 03:35:01 -08:00
|
|
|
require.NotEmpty(t, ss.Warnings(), "warnings expected")
|
2023-09-14 09:57:31 -07:00
|
|
|
w := ss.Warnings()
|
|
|
|
require.Error(t, w.AsErrors()[0])
|
|
|
|
require.Equal(t, tc.warning.Error(), w.AsStrings("", 0)[0])
|
2020-08-03 03:32:56 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("chunks", func(t *testing.T) {
|
|
|
|
t.Skip("enable once TestStorage and TSDB implements ChunkQuerier")
|
2023-09-12 03:37:38 -07:00
|
|
|
querier, err := fanoutStorage.ChunkQuerier(0, 8000)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2020-08-03 03:32:56 -07:00
|
|
|
defer querier.Close()
|
|
|
|
|
|
|
|
matcher := labels.MustNewMatcher(labels.MatchEqual, "a", "b")
|
2023-09-12 03:37:38 -07:00
|
|
|
ss := querier.Select(context.Background(), true, nil, matcher)
|
2020-08-03 03:32:56 -07:00
|
|
|
|
|
|
|
// Exhaust.
|
|
|
|
for ss.Next() {
|
|
|
|
ss.At()
|
|
|
|
}
|
2018-01-26 03:01:59 -08:00
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
if tc.err != nil {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Error(t, ss.Err())
|
|
|
|
require.Equal(t, tc.err.Error(), ss.Err().Error())
|
2020-08-03 03:32:56 -07:00
|
|
|
}
|
2018-01-26 03:01:59 -08:00
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
if tc.warning != nil {
|
2023-12-07 03:35:01 -08:00
|
|
|
require.NotEmpty(t, ss.Warnings(), "warnings expected")
|
2023-09-14 09:57:31 -07:00
|
|
|
w := ss.Warnings()
|
|
|
|
require.Error(t, w.AsErrors()[0])
|
|
|
|
require.Equal(t, tc.warning.Error(), w.AsStrings("", 0)[0])
|
2020-08-03 03:32:56 -07:00
|
|
|
}
|
2018-01-29 03:37:48 -08:00
|
|
|
})
|
|
|
|
}
|
2018-01-26 03:01:59 -08:00
|
|
|
}
|
2020-06-09 09:57:31 -07:00
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
var errSelect = errors.New("select error")
|
2020-06-09 09:57:31 -07:00
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
type errStorage struct{}
|
2020-06-09 09:57:31 -07:00
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
type errQuerier struct{}
|
2020-06-09 09:57:31 -07:00
|
|
|
|
2023-09-12 03:37:38 -07:00
|
|
|
func (errStorage) Querier(_, _ int64) (storage.Querier, error) {
|
2020-08-03 03:32:56 -07:00
|
|
|
return errQuerier{}, nil
|
2020-06-09 09:57:31 -07:00
|
|
|
}
|
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
type errChunkQuerier struct{ errQuerier }
|
2020-06-09 09:57:31 -07:00
|
|
|
|
2023-09-12 03:37:38 -07:00
|
|
|
func (errStorage) ChunkQuerier(_, _ int64) (storage.ChunkQuerier, error) {
|
2020-08-03 03:32:56 -07:00
|
|
|
return errChunkQuerier{}, nil
|
2020-06-09 09:57:31 -07:00
|
|
|
}
|
2020-08-03 03:32:56 -07:00
|
|
|
func (errStorage) Appender(_ context.Context) storage.Appender { return nil }
|
|
|
|
func (errStorage) StartTime() (int64, error) { return 0, nil }
|
|
|
|
func (errStorage) Close() error { return nil }
|
2020-06-09 09:57:31 -07:00
|
|
|
|
2023-09-12 03:37:38 -07:00
|
|
|
func (errQuerier) Select(context.Context, bool, *storage.SelectHints, ...*labels.Matcher) storage.SeriesSet {
|
2020-08-03 03:32:56 -07:00
|
|
|
return storage.ErrSeriesSet(errSelect)
|
2020-06-09 09:57:31 -07:00
|
|
|
}
|
|
|
|
|
2023-09-14 09:57:31 -07:00
|
|
|
func (errQuerier) LabelValues(context.Context, string, ...*labels.Matcher) ([]string, annotations.Annotations, error) {
|
2020-08-03 03:32:56 -07:00
|
|
|
return nil, nil, errors.New("label values error")
|
2020-06-09 09:57:31 -07:00
|
|
|
}
|
|
|
|
|
2023-09-14 09:57:31 -07:00
|
|
|
func (errQuerier) LabelNames(context.Context, ...*labels.Matcher) ([]string, annotations.Annotations, error) {
|
2020-08-03 03:32:56 -07:00
|
|
|
return nil, nil, errors.New("label names error")
|
2020-06-09 09:57:31 -07:00
|
|
|
}
|
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
func (errQuerier) Close() error { return nil }
|
2020-06-09 09:57:31 -07:00
|
|
|
|
2023-09-12 03:37:38 -07:00
|
|
|
func (errChunkQuerier) Select(context.Context, bool, *storage.SelectHints, ...*labels.Matcher) storage.ChunkSeriesSet {
|
2020-08-03 03:32:56 -07:00
|
|
|
return storage.ErrChunkSeriesSet(errSelect)
|
2020-06-09 09:57:31 -07:00
|
|
|
}
|