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"
|
2017-07-13 03:05:38 -07:00
|
|
|
"testing"
|
|
|
|
|
2020-06-09 09:57:31 -07:00
|
|
|
"github.com/pkg/errors"
|
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"
|
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) {
|
|
|
|
querier, err := fanoutStorage.Querier(context.Background(), 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
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
seriesSet := querier.Select(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)
|
|
|
|
require.Equal(t, inputTotalSize, len(result))
|
2020-08-03 03:32:56 -07:00
|
|
|
})
|
|
|
|
t.Run("chunk querier", func(t *testing.T) {
|
|
|
|
querier, err := fanoutStorage.ChunkQuerier(ctx, 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
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
seriesSet := storage.NewSeriesSetFromChunkSeriesSet(querier.Select(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)
|
|
|
|
require.Equal(t, inputTotalSize, len(result))
|
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) {
|
|
|
|
querier, err := fanoutStorage.Querier(context.Background(), 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")
|
|
|
|
ss := querier.Select(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 {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Greater(t, len(ss.Warnings()), 0, "warnings expected")
|
|
|
|
require.Error(t, ss.Warnings()[0])
|
|
|
|
require.Equal(t, tc.warning.Error(), ss.Warnings()[0].Error())
|
2020-08-03 03:32:56 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("chunks", func(t *testing.T) {
|
|
|
|
t.Skip("enable once TestStorage and TSDB implements ChunkQuerier")
|
|
|
|
querier, err := fanoutStorage.ChunkQuerier(context.Background(), 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")
|
|
|
|
ss := querier.Select(true, nil, matcher)
|
|
|
|
|
|
|
|
// 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 {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Greater(t, len(ss.Warnings()), 0, "warnings expected")
|
|
|
|
require.Error(t, ss.Warnings()[0])
|
|
|
|
require.Equal(t, tc.warning.Error(), ss.Warnings()[0].Error())
|
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
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
func (errStorage) Querier(_ context.Context, _, _ int64) (storage.Querier, error) {
|
|
|
|
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
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
func (errStorage) ChunkQuerier(_ context.Context, _, _ int64) (storage.ChunkQuerier, error) {
|
|
|
|
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
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
func (errQuerier) Select(bool, *storage.SelectHints, ...*labels.Matcher) storage.SeriesSet {
|
|
|
|
return storage.ErrSeriesSet(errSelect)
|
2020-06-09 09:57:31 -07:00
|
|
|
}
|
|
|
|
|
2023-04-12 04:05:41 -07:00
|
|
|
func (errQuerier) LabelValues(string, ...*labels.Matcher) ([]string, storage.Warnings, 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
|
|
|
}
|
|
|
|
|
2021-07-20 05:38:08 -07:00
|
|
|
func (errQuerier) LabelNames(...*labels.Matcher) ([]string, storage.Warnings, 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
|
|
|
|
2020-08-03 03:32:56 -07:00
|
|
|
func (errChunkQuerier) Select(bool, *storage.SelectHints, ...*labels.Matcher) storage.ChunkSeriesSet {
|
|
|
|
return storage.ErrChunkSeriesSet(errSelect)
|
2020-06-09 09:57:31 -07:00
|
|
|
}
|