mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
337 lines
11 KiB
337 lines
11 KiB
7 years ago
|
// Copyright 2018 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
|
||
|
// limitations under the License.
|
||
|
|
||
2 years ago
|
package wlog
|
||
7 years ago
|
|
||
|
import (
|
||
6 years ago
|
"fmt"
|
||
7 years ago
|
"os"
|
||
|
"path/filepath"
|
||
2 years ago
|
"sort"
|
||
6 years ago
|
"strings"
|
||
7 years ago
|
"testing"
|
||
|
|
||
4 years ago
|
"github.com/go-kit/log"
|
||
6 years ago
|
"github.com/pkg/errors"
|
||
4 years ago
|
"github.com/stretchr/testify/require"
|
||
4 years ago
|
|
||
2 years ago
|
"github.com/prometheus/prometheus/model/histogram"
|
||
3 years ago
|
"github.com/prometheus/prometheus/model/labels"
|
||
3 years ago
|
"github.com/prometheus/prometheus/tsdb/chunks"
|
||
5 years ago
|
"github.com/prometheus/prometheus/tsdb/record"
|
||
7 years ago
|
)
|
||
|
|
||
|
func TestLastCheckpoint(t *testing.T) {
|
||
3 years ago
|
dir := t.TempDir()
|
||
7 years ago
|
|
||
3 years ago
|
_, _, err := LastCheckpoint(dir)
|
||
4 years ago
|
require.Equal(t, record.ErrNotFound, err)
|
||
7 years ago
|
|
||
3 years ago
|
require.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.0000"), 0o777))
|
||
6 years ago
|
s, k, err := LastCheckpoint(dir)
|
||
4 years ago
|
require.NoError(t, err)
|
||
|
require.Equal(t, filepath.Join(dir, "checkpoint.0000"), s)
|
||
|
require.Equal(t, 0, k)
|
||
7 years ago
|
|
||
3 years ago
|
require.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.xyz"), 0o777))
|
||
7 years ago
|
s, k, err = LastCheckpoint(dir)
|
||
4 years ago
|
require.NoError(t, err)
|
||
|
require.Equal(t, filepath.Join(dir, "checkpoint.0000"), s)
|
||
|
require.Equal(t, 0, k)
|
||
7 years ago
|
|
||
3 years ago
|
require.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.1"), 0o777))
|
||
7 years ago
|
s, k, err = LastCheckpoint(dir)
|
||
4 years ago
|
require.NoError(t, err)
|
||
|
require.Equal(t, filepath.Join(dir, "checkpoint.1"), s)
|
||
|
require.Equal(t, 1, k)
|
||
7 years ago
|
|
||
3 years ago
|
require.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.1000"), 0o777))
|
||
7 years ago
|
s, k, err = LastCheckpoint(dir)
|
||
4 years ago
|
require.NoError(t, err)
|
||
|
require.Equal(t, filepath.Join(dir, "checkpoint.1000"), s)
|
||
|
require.Equal(t, 1000, k)
|
||
5 years ago
|
|
||
3 years ago
|
require.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.99999999"), 0o777))
|
||
5 years ago
|
s, k, err = LastCheckpoint(dir)
|
||
4 years ago
|
require.NoError(t, err)
|
||
|
require.Equal(t, filepath.Join(dir, "checkpoint.99999999"), s)
|
||
|
require.Equal(t, 99999999, k)
|
||
5 years ago
|
|
||
3 years ago
|
require.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.100000000"), 0o777))
|
||
5 years ago
|
s, k, err = LastCheckpoint(dir)
|
||
4 years ago
|
require.NoError(t, err)
|
||
|
require.Equal(t, filepath.Join(dir, "checkpoint.100000000"), s)
|
||
|
require.Equal(t, 100000000, k)
|
||
7 years ago
|
}
|
||
|
|
||
|
func TestDeleteCheckpoints(t *testing.T) {
|
||
3 years ago
|
dir := t.TempDir()
|
||
7 years ago
|
|
||
4 years ago
|
require.NoError(t, DeleteCheckpoints(dir, 0))
|
||
7 years ago
|
|
||
3 years ago
|
require.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.00"), 0o777))
|
||
|
require.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.01"), 0o777))
|
||
|
require.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.02"), 0o777))
|
||
|
require.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.03"), 0o777))
|
||
7 years ago
|
|
||
4 years ago
|
require.NoError(t, DeleteCheckpoints(dir, 2))
|
||
7 years ago
|
|
||
3 years ago
|
files, err := os.ReadDir(dir)
|
||
4 years ago
|
require.NoError(t, err)
|
||
5 years ago
|
fns := []string{}
|
||
|
for _, f := range files {
|
||
|
fns = append(fns, f.Name())
|
||
|
}
|
||
4 years ago
|
require.Equal(t, []string{"checkpoint.02", "checkpoint.03"}, fns)
|
||
5 years ago
|
|
||
3 years ago
|
require.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.99999999"), 0o777))
|
||
|
require.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.100000000"), 0o777))
|
||
|
require.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.100000001"), 0o777))
|
||
5 years ago
|
|
||
4 years ago
|
require.NoError(t, DeleteCheckpoints(dir, 100000000))
|
||
5 years ago
|
|
||
3 years ago
|
files, err = os.ReadDir(dir)
|
||
4 years ago
|
require.NoError(t, err)
|
||
5 years ago
|
fns = []string{}
|
||
|
for _, f := range files {
|
||
|
fns = append(fns, f.Name())
|
||
|
}
|
||
4 years ago
|
require.Equal(t, []string{"checkpoint.100000000", "checkpoint.100000001"}, fns)
|
||
7 years ago
|
}
|
||
|
|
||
|
func TestCheckpoint(t *testing.T) {
|
||
2 years ago
|
makeHistogram := func(i int) *histogram.Histogram {
|
||
|
return &histogram.Histogram{
|
||
|
Count: 5 + uint64(i*4),
|
||
|
ZeroCount: 2 + uint64(i),
|
||
|
ZeroThreshold: 0.001,
|
||
|
Sum: 18.4 * float64(i+1),
|
||
|
Schema: 1,
|
||
|
PositiveSpans: []histogram.Span{
|
||
|
{Offset: 0, Length: 2},
|
||
|
{Offset: 1, Length: 2},
|
||
|
},
|
||
|
PositiveBuckets: []int64{int64(i + 1), 1, -1, 0},
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
for _, compress := range []bool{false, true} {
|
||
|
t.Run(fmt.Sprintf("compress=%t", compress), func(t *testing.T) {
|
||
3 years ago
|
dir := t.TempDir()
|
||
7 years ago
|
|
||
5 years ago
|
var enc record.Encoder
|
||
6 years ago
|
// Create a dummy segment to bump the initial number.
|
||
5 years ago
|
seg, err := CreateSegment(dir, 100)
|
||
4 years ago
|
require.NoError(t, err)
|
||
|
require.NoError(t, seg.Close())
|
||
7 years ago
|
|
||
6 years ago
|
// Manually create checkpoint for 99 and earlier.
|
||
5 years ago
|
w, err := New(nil, nil, filepath.Join(dir, "checkpoint.0099"), compress)
|
||
4 years ago
|
require.NoError(t, err)
|
||
7 years ago
|
|
||
6 years ago
|
// Add some data we expect to be around later.
|
||
5 years ago
|
err = w.Log(enc.Series([]record.RefSeries{
|
||
6 years ago
|
{Ref: 0, Labels: labels.FromStrings("a", "b", "c", "0")},
|
||
|
{Ref: 1, Labels: labels.FromStrings("a", "b", "c", "1")},
|
||
|
}, nil))
|
||
4 years ago
|
require.NoError(t, err)
|
||
4 years ago
|
// Log an unknown record, that might have come from a future Prometheus version.
|
||
4 years ago
|
require.NoError(t, w.Log([]byte{255}))
|
||
|
require.NoError(t, w.Close())
|
||
7 years ago
|
|
||
6 years ago
|
// Start a WAL and write records to it as usual.
|
||
5 years ago
|
w, err = NewSize(nil, nil, dir, 64*1024, compress)
|
||
4 years ago
|
require.NoError(t, err)
|
||
7 years ago
|
|
||
2 years ago
|
samplesInWAL, histogramsInWAL := 0, 0
|
||
6 years ago
|
var last int64
|
||
|
for i := 0; ; i++ {
|
||
4 years ago
|
_, n, err := Segments(w.Dir())
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
if n >= 106 {
|
||
|
break
|
||
|
}
|
||
|
// Write some series initially.
|
||
|
if i == 0 {
|
||
5 years ago
|
b := enc.Series([]record.RefSeries{
|
||
6 years ago
|
{Ref: 2, Labels: labels.FromStrings("a", "b", "c", "2")},
|
||
|
{Ref: 3, Labels: labels.FromStrings("a", "b", "c", "3")},
|
||
|
{Ref: 4, Labels: labels.FromStrings("a", "b", "c", "4")},
|
||
|
{Ref: 5, Labels: labels.FromStrings("a", "b", "c", "5")},
|
||
|
}, nil)
|
||
4 years ago
|
require.NoError(t, w.Log(b))
|
||
2 years ago
|
|
||
|
b = enc.Metadata([]record.RefMetadata{
|
||
|
{Ref: 2, Unit: "unit", Help: "help"},
|
||
|
{Ref: 3, Unit: "unit", Help: "help"},
|
||
|
{Ref: 4, Unit: "unit", Help: "help"},
|
||
|
{Ref: 5, Unit: "unit", Help: "help"},
|
||
|
}, nil)
|
||
|
require.NoError(t, w.Log(b))
|
||
6 years ago
|
}
|
||
|
// Write samples until the WAL has enough segments.
|
||
|
// Make them have drifting timestamps within a record to see that they
|
||
|
// get filtered properly.
|
||
5 years ago
|
b := enc.Samples([]record.RefSample{
|
||
6 years ago
|
{Ref: 0, T: last, V: float64(i)},
|
||
|
{Ref: 1, T: last + 10000, V: float64(i)},
|
||
|
{Ref: 2, T: last + 20000, V: float64(i)},
|
||
|
{Ref: 3, T: last + 30000, V: float64(i)},
|
||
|
}, nil)
|
||
4 years ago
|
require.NoError(t, w.Log(b))
|
||
2 years ago
|
samplesInWAL += 4
|
||
|
h := makeHistogram(i)
|
||
|
b = enc.HistogramSamples([]record.RefHistogramSample{
|
||
|
{Ref: 0, T: last, H: h},
|
||
|
{Ref: 1, T: last + 10000, H: h},
|
||
|
{Ref: 2, T: last + 20000, H: h},
|
||
|
{Ref: 3, T: last + 30000, H: h},
|
||
|
}, nil)
|
||
|
require.NoError(t, w.Log(b))
|
||
|
histogramsInWAL += 4
|
||
6 years ago
|
|
||
4 years ago
|
b = enc.Exemplars([]record.RefExemplar{
|
||
|
{Ref: 1, T: last, V: float64(i), Labels: labels.FromStrings("traceID", fmt.Sprintf("trace-%d", i))},
|
||
|
}, nil)
|
||
|
require.NoError(t, w.Log(b))
|
||
|
|
||
2 years ago
|
// Write changing metadata for each series. In the end, only the latest
|
||
|
// version should end up in the checkpoint.
|
||
|
b = enc.Metadata([]record.RefMetadata{
|
||
|
{Ref: 0, Unit: fmt.Sprintf("%d", last), Help: fmt.Sprintf("%d", last)},
|
||
|
{Ref: 1, Unit: fmt.Sprintf("%d", last), Help: fmt.Sprintf("%d", last)},
|
||
|
{Ref: 2, Unit: fmt.Sprintf("%d", last), Help: fmt.Sprintf("%d", last)},
|
||
|
{Ref: 3, Unit: fmt.Sprintf("%d", last), Help: fmt.Sprintf("%d", last)},
|
||
|
}, nil)
|
||
|
require.NoError(t, w.Log(b))
|
||
|
|
||
6 years ago
|
last += 100
|
||
|
}
|
||
4 years ago
|
require.NoError(t, w.Close())
|
||
7 years ago
|
|
||
3 years ago
|
_, err = Checkpoint(log.NewNopLogger(), w, 100, 106, func(x chunks.HeadSeriesRef) bool {
|
||
6 years ago
|
return x%2 == 0
|
||
|
}, last/2)
|
||
4 years ago
|
require.NoError(t, err)
|
||
|
require.NoError(t, w.Truncate(107))
|
||
|
require.NoError(t, DeleteCheckpoints(w.Dir(), 106))
|
||
7 years ago
|
|
||
6 years ago
|
// Only the new checkpoint should be left.
|
||
3 years ago
|
files, err := os.ReadDir(dir)
|
||
4 years ago
|
require.NoError(t, err)
|
||
|
require.Equal(t, 1, len(files))
|
||
|
require.Equal(t, "checkpoint.00000106", files[0].Name())
|
||
6 years ago
|
|
||
5 years ago
|
sr, err := NewSegmentsReader(filepath.Join(dir, "checkpoint.00000106"))
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
defer sr.Close()
|
||
|
|
||
5 years ago
|
var dec record.Decoder
|
||
|
var series []record.RefSeries
|
||
2 years ago
|
var metadata []record.RefMetadata
|
||
5 years ago
|
r := NewReader(sr)
|
||
6 years ago
|
|
||
2 years ago
|
samplesInCheckpoint, histogramsInCheckpoint := 0, 0
|
||
6 years ago
|
for r.Next() {
|
||
|
rec := r.Record()
|
||
|
|
||
|
switch dec.Type(rec) {
|
||
5 years ago
|
case record.Series:
|
||
6 years ago
|
series, err = dec.Series(rec, series)
|
||
4 years ago
|
require.NoError(t, err)
|
||
5 years ago
|
case record.Samples:
|
||
6 years ago
|
samples, err := dec.Samples(rec, nil)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
for _, s := range samples {
|
||
4 years ago
|
require.GreaterOrEqual(t, s.T, last/2, "sample with wrong timestamp")
|
||
6 years ago
|
}
|
||
2 years ago
|
samplesInCheckpoint += len(samples)
|
||
|
case record.HistogramSamples:
|
||
|
histograms, err := dec.HistogramSamples(rec, nil)
|
||
|
require.NoError(t, err)
|
||
|
for _, h := range histograms {
|
||
|
require.GreaterOrEqual(t, h.T, last/2, "histogram with wrong timestamp")
|
||
|
}
|
||
|
histogramsInCheckpoint += len(histograms)
|
||
4 years ago
|
case record.Exemplars:
|
||
|
exemplars, err := dec.Exemplars(rec, nil)
|
||
|
require.NoError(t, err)
|
||
|
for _, e := range exemplars {
|
||
|
require.GreaterOrEqual(t, e.T, last/2, "exemplar with wrong timestamp")
|
||
|
}
|
||
2 years ago
|
case record.Metadata:
|
||
|
metadata, err = dec.Metadata(rec, metadata)
|
||
|
require.NoError(t, err)
|
||
6 years ago
|
}
|
||
7 years ago
|
}
|
||
4 years ago
|
require.NoError(t, r.Err())
|
||
2 years ago
|
// Making sure we replayed some samples. We expect >50% samples to be still present.
|
||
|
require.Greater(t, float64(samplesInCheckpoint)/float64(samplesInWAL), 0.5)
|
||
|
require.Less(t, float64(samplesInCheckpoint)/float64(samplesInWAL), 0.8)
|
||
|
require.Greater(t, float64(histogramsInCheckpoint)/float64(histogramsInWAL), 0.5)
|
||
|
require.Less(t, float64(histogramsInCheckpoint)/float64(histogramsInWAL), 0.8)
|
||
2 years ago
|
|
||
|
expectedRefSeries := []record.RefSeries{
|
||
6 years ago
|
{Ref: 0, Labels: labels.FromStrings("a", "b", "c", "0")},
|
||
|
{Ref: 2, Labels: labels.FromStrings("a", "b", "c", "2")},
|
||
|
{Ref: 4, Labels: labels.FromStrings("a", "b", "c", "4")},
|
||
2 years ago
|
}
|
||
|
require.Equal(t, expectedRefSeries, series)
|
||
|
|
||
|
expectedRefMetadata := []record.RefMetadata{
|
||
|
{Ref: 0, Unit: fmt.Sprintf("%d", last-100), Help: fmt.Sprintf("%d", last-100)},
|
||
|
{Ref: 2, Unit: fmt.Sprintf("%d", last-100), Help: fmt.Sprintf("%d", last-100)},
|
||
|
{Ref: 4, Unit: "unit", Help: "help"},
|
||
|
}
|
||
|
sort.Slice(metadata, func(i, j int) bool { return metadata[i].Ref < metadata[j].Ref })
|
||
|
require.Equal(t, expectedRefMetadata, metadata)
|
||
6 years ago
|
})
|
||
7 years ago
|
}
|
||
|
}
|
||
6 years ago
|
|
||
|
func TestCheckpointNoTmpFolderAfterError(t *testing.T) {
|
||
2 years ago
|
// Create a new wlog with invalid data.
|
||
3 years ago
|
dir := t.TempDir()
|
||
5 years ago
|
w, err := NewSize(nil, nil, dir, 64*1024, false)
|
||
4 years ago
|
require.NoError(t, err)
|
||
4 years ago
|
var enc record.Encoder
|
||
4 years ago
|
require.NoError(t, w.Log(enc.Series([]record.RefSeries{
|
||
3 years ago
|
{Ref: 0, Labels: labels.FromStrings("a", "b", "c", "2")},
|
||
|
}, nil)))
|
||
4 years ago
|
require.NoError(t, w.Close())
|
||
6 years ago
|
|
||
4 years ago
|
// Corrupt data.
|
||
3 years ago
|
f, err := os.OpenFile(filepath.Join(w.Dir(), "00000000"), os.O_WRONLY, 0o666)
|
||
4 years ago
|
require.NoError(t, err)
|
||
4 years ago
|
_, err = f.WriteAt([]byte{42}, 1)
|
||
4 years ago
|
require.NoError(t, err)
|
||
|
require.NoError(t, f.Close())
|
||
4 years ago
|
|
||
2 years ago
|
// Run the checkpoint and since the wlog contains corrupt data this should return an error.
|
||
4 years ago
|
_, err = Checkpoint(log.NewNopLogger(), w, 0, 1, nil, 0)
|
||
4 years ago
|
require.Error(t, err)
|
||
6 years ago
|
|
||
2 years ago
|
// Walk the wlog dir to make sure there are no tmp folder left behind after the error.
|
||
6 years ago
|
err = filepath.Walk(w.Dir(), func(path string, info os.FileInfo, err error) error {
|
||
|
if err != nil {
|
||
3 years ago
|
return errors.Wrapf(err, "access err %q: %v", path, err)
|
||
6 years ago
|
}
|
||
|
if info.IsDir() && strings.HasSuffix(info.Name(), ".tmp") {
|
||
2 years ago
|
return fmt.Errorf("wlog dir contains temporary folder:%s", info.Name())
|
||
6 years ago
|
}
|
||
|
return nil
|
||
|
})
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
}
|