2018-05-17 06:02:47 -07:00
|
|
|
// 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.
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
package wal
|
2018-05-17 06:02:47 -07:00
|
|
|
|
|
|
|
import (
|
2019-01-07 00:43:33 -08:00
|
|
|
"fmt"
|
2018-05-17 06:02:47 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-01-07 00:43:33 -08:00
|
|
|
"strings"
|
2018-05-17 06:02:47 -07:00
|
|
|
"testing"
|
|
|
|
|
2019-01-07 00:43:33 -08:00
|
|
|
"github.com/pkg/errors"
|
2019-11-18 11:53:33 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2019-09-19 02:15:41 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb/record"
|
2019-08-14 02:07:02 -07:00
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2018-05-17 06:02:47 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLastCheckpoint(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "test_checkpoint")
|
|
|
|
testutil.Ok(t, err)
|
2019-03-19 06:31:57 -07:00
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2018-05-17 06:02:47 -07:00
|
|
|
|
2019-01-02 08:48:42 -08:00
|
|
|
_, _, err = LastCheckpoint(dir)
|
2019-09-19 02:15:41 -07:00
|
|
|
testutil.Equals(t, record.ErrNotFound, err)
|
2018-05-17 06:02:47 -07:00
|
|
|
|
|
|
|
testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.0000"), 0777))
|
2019-01-02 08:48:42 -08:00
|
|
|
s, k, err := LastCheckpoint(dir)
|
2018-05-17 06:02:47 -07:00
|
|
|
testutil.Ok(t, err)
|
2018-11-30 06:46:16 -08:00
|
|
|
testutil.Equals(t, filepath.Join(dir, "checkpoint.0000"), s)
|
2018-05-17 06:02:47 -07:00
|
|
|
testutil.Equals(t, 0, k)
|
|
|
|
|
|
|
|
testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.xyz"), 0777))
|
|
|
|
s, k, err = LastCheckpoint(dir)
|
|
|
|
testutil.Ok(t, err)
|
2018-11-30 06:46:16 -08:00
|
|
|
testutil.Equals(t, filepath.Join(dir, "checkpoint.0000"), s)
|
2018-05-17 06:02:47 -07:00
|
|
|
testutil.Equals(t, 0, k)
|
|
|
|
|
|
|
|
testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.1"), 0777))
|
|
|
|
s, k, err = LastCheckpoint(dir)
|
|
|
|
testutil.Ok(t, err)
|
2018-11-30 06:46:16 -08:00
|
|
|
testutil.Equals(t, filepath.Join(dir, "checkpoint.1"), s)
|
2018-05-17 06:02:47 -07:00
|
|
|
testutil.Equals(t, 1, k)
|
|
|
|
|
|
|
|
testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.1000"), 0777))
|
|
|
|
s, k, err = LastCheckpoint(dir)
|
|
|
|
testutil.Ok(t, err)
|
2018-11-30 06:46:16 -08:00
|
|
|
testutil.Equals(t, filepath.Join(dir, "checkpoint.1000"), s)
|
2018-05-17 06:02:47 -07:00
|
|
|
testutil.Equals(t, 1000, k)
|
2020-03-18 08:10:41 -07:00
|
|
|
|
|
|
|
testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.99999999"), 0777))
|
|
|
|
s, k, err = LastCheckpoint(dir)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, filepath.Join(dir, "checkpoint.99999999"), s)
|
|
|
|
testutil.Equals(t, 99999999, k)
|
|
|
|
|
|
|
|
testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.100000000"), 0777))
|
|
|
|
s, k, err = LastCheckpoint(dir)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, filepath.Join(dir, "checkpoint.100000000"), s)
|
|
|
|
testutil.Equals(t, 100000000, k)
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteCheckpoints(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "test_checkpoint")
|
|
|
|
testutil.Ok(t, err)
|
2019-03-19 06:31:57 -07:00
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2018-05-17 06:02:47 -07:00
|
|
|
|
|
|
|
testutil.Ok(t, DeleteCheckpoints(dir, 0))
|
|
|
|
|
|
|
|
testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.00"), 0777))
|
|
|
|
testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.01"), 0777))
|
|
|
|
testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.02"), 0777))
|
|
|
|
testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.03"), 0777))
|
|
|
|
|
|
|
|
testutil.Ok(t, DeleteCheckpoints(dir, 2))
|
|
|
|
|
2020-04-06 06:34:20 -07:00
|
|
|
files, err := ioutil.ReadDir(dir)
|
2018-05-17 06:02:47 -07:00
|
|
|
testutil.Ok(t, err)
|
2020-04-06 06:34:20 -07:00
|
|
|
fns := []string{}
|
|
|
|
for _, f := range files {
|
|
|
|
fns = append(fns, f.Name())
|
|
|
|
}
|
|
|
|
testutil.Equals(t, []string{"checkpoint.02", "checkpoint.03"}, fns)
|
2020-03-18 08:10:41 -07:00
|
|
|
|
|
|
|
testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.99999999"), 0777))
|
|
|
|
testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.100000000"), 0777))
|
|
|
|
testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.100000001"), 0777))
|
|
|
|
|
|
|
|
testutil.Ok(t, DeleteCheckpoints(dir, 100000000))
|
|
|
|
|
2020-04-06 06:34:20 -07:00
|
|
|
files, err = ioutil.ReadDir(dir)
|
2020-03-18 08:10:41 -07:00
|
|
|
testutil.Ok(t, err)
|
2020-04-06 06:34:20 -07:00
|
|
|
fns = []string{}
|
|
|
|
for _, f := range files {
|
|
|
|
fns = append(fns, f.Name())
|
|
|
|
}
|
|
|
|
testutil.Equals(t, []string{"checkpoint.100000000", "checkpoint.100000001"}, fns)
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckpoint(t *testing.T) {
|
2019-06-19 06:46:24 -07:00
|
|
|
for _, compress := range []bool{false, true} {
|
|
|
|
t.Run(fmt.Sprintf("compress=%t", compress), func(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "test_checkpoint")
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2018-05-17 06:02:47 -07:00
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
var enc record.Encoder
|
2019-06-19 06:46:24 -07:00
|
|
|
// Create a dummy segment to bump the initial number.
|
2019-09-19 02:15:41 -07:00
|
|
|
seg, err := CreateSegment(dir, 100)
|
2019-06-19 06:46:24 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Ok(t, seg.Close())
|
2018-05-17 06:02:47 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
// Manually create checkpoint for 99 and earlier.
|
2019-09-19 02:15:41 -07:00
|
|
|
w, err := New(nil, nil, filepath.Join(dir, "checkpoint.0099"), compress)
|
2019-06-19 06:46:24 -07:00
|
|
|
testutil.Ok(t, err)
|
2018-05-17 06:02:47 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
// Add some data we expect to be around later.
|
2019-09-19 02:15:41 -07:00
|
|
|
err = w.Log(enc.Series([]record.RefSeries{
|
2019-06-19 06:46:24 -07:00
|
|
|
{Ref: 0, Labels: labels.FromStrings("a", "b", "c", "0")},
|
|
|
|
{Ref: 1, Labels: labels.FromStrings("a", "b", "c", "1")},
|
|
|
|
}, nil))
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Ok(t, w.Close())
|
2018-05-17 06:02:47 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
// Start a WAL and write records to it as usual.
|
2019-09-19 02:15:41 -07:00
|
|
|
w, err = NewSize(nil, nil, dir, 64*1024, compress)
|
2019-06-19 06:46:24 -07:00
|
|
|
testutil.Ok(t, err)
|
2018-05-17 06:02:47 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
var last int64
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
_, n, err := w.Segments()
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
if n >= 106 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Write some series initially.
|
|
|
|
if i == 0 {
|
2019-09-19 02:15:41 -07:00
|
|
|
b := enc.Series([]record.RefSeries{
|
2019-06-19 06:46:24 -07:00
|
|
|
{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)
|
|
|
|
testutil.Ok(t, w.Log(b))
|
|
|
|
}
|
|
|
|
// Write samples until the WAL has enough segments.
|
|
|
|
// Make them have drifting timestamps within a record to see that they
|
|
|
|
// get filtered properly.
|
2019-09-19 02:15:41 -07:00
|
|
|
b := enc.Samples([]record.RefSample{
|
2019-06-19 06:46:24 -07:00
|
|
|
{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)
|
|
|
|
testutil.Ok(t, w.Log(b))
|
|
|
|
|
|
|
|
last += 100
|
|
|
|
}
|
|
|
|
testutil.Ok(t, w.Close())
|
2018-05-17 06:02:47 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
_, err = Checkpoint(w, 100, 106, func(x uint64) bool {
|
|
|
|
return x%2 == 0
|
|
|
|
}, last/2)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Ok(t, w.Truncate(107))
|
|
|
|
testutil.Ok(t, DeleteCheckpoints(w.Dir(), 106))
|
2018-05-17 06:02:47 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
// Only the new checkpoint should be left.
|
2020-04-06 06:34:20 -07:00
|
|
|
files, err := ioutil.ReadDir(dir)
|
2018-05-17 06:02:47 -07:00
|
|
|
testutil.Ok(t, err)
|
2019-06-19 06:46:24 -07:00
|
|
|
testutil.Equals(t, 1, len(files))
|
2020-04-06 06:34:20 -07:00
|
|
|
testutil.Equals(t, "checkpoint.00000106", files[0].Name())
|
2019-06-19 06:46:24 -07:00
|
|
|
|
2020-03-18 08:10:41 -07:00
|
|
|
sr, err := NewSegmentsReader(filepath.Join(dir, "checkpoint.00000106"))
|
2018-05-17 06:02:47 -07:00
|
|
|
testutil.Ok(t, err)
|
2019-06-19 06:46:24 -07:00
|
|
|
defer sr.Close()
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
var dec record.Decoder
|
|
|
|
var series []record.RefSeries
|
|
|
|
r := NewReader(sr)
|
2019-06-19 06:46:24 -07:00
|
|
|
|
|
|
|
for r.Next() {
|
|
|
|
rec := r.Record()
|
|
|
|
|
|
|
|
switch dec.Type(rec) {
|
2019-09-19 02:15:41 -07:00
|
|
|
case record.Series:
|
2019-06-19 06:46:24 -07:00
|
|
|
series, err = dec.Series(rec, series)
|
|
|
|
testutil.Ok(t, err)
|
2019-09-19 02:15:41 -07:00
|
|
|
case record.Samples:
|
2019-06-19 06:46:24 -07:00
|
|
|
samples, err := dec.Samples(rec, nil)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
for _, s := range samples {
|
|
|
|
testutil.Assert(t, s.T >= last/2, "sample with wrong timestamp")
|
|
|
|
}
|
|
|
|
}
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
2019-06-19 06:46:24 -07:00
|
|
|
testutil.Ok(t, r.Err())
|
2019-09-19 02:15:41 -07:00
|
|
|
testutil.Equals(t, []record.RefSeries{
|
2019-06-19 06:46:24 -07:00
|
|
|
{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")},
|
|
|
|
}, series)
|
|
|
|
})
|
2018-05-17 06:02:47 -07:00
|
|
|
}
|
|
|
|
}
|
2019-01-07 00:43:33 -08:00
|
|
|
|
|
|
|
func TestCheckpointNoTmpFolderAfterError(t *testing.T) {
|
|
|
|
// Create a new wal with an invalid records.
|
|
|
|
dir, err := ioutil.TempDir("", "test_checkpoint")
|
|
|
|
testutil.Ok(t, err)
|
2019-03-19 06:31:57 -07:00
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2019-09-19 02:15:41 -07:00
|
|
|
w, err := NewSize(nil, nil, dir, 64*1024, false)
|
2019-01-07 00:43:33 -08:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Ok(t, w.Log([]byte{99}))
|
|
|
|
w.Close()
|
|
|
|
|
|
|
|
// Run the checkpoint and since the wal contains an invalid records this should return an error.
|
|
|
|
_, err = Checkpoint(w, 0, 1, nil, 0)
|
|
|
|
testutil.NotOk(t, err)
|
|
|
|
|
|
|
|
// Walk the wal dir to make sure there are no tmp folder left behind after the error.
|
|
|
|
err = filepath.Walk(w.Dir(), func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "access err %q: %v\n", path, err)
|
|
|
|
}
|
|
|
|
if info.IsDir() && strings.HasSuffix(info.Name(), ".tmp") {
|
|
|
|
return fmt.Errorf("wal dir contains temporary folder:%s", info.Name())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
}
|