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