2017-04-10 18:59:45 +00:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
2018-12-13 13:29:29 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2016-12-22 14:18:33 +00:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
2017-02-14 23:54:52 +00:00
|
|
|
"encoding/binary"
|
2018-03-21 20:39:43 +00:00
|
|
|
"io"
|
2016-12-22 14:18:33 +00:00
|
|
|
"io/ioutil"
|
2017-02-15 05:54:59 +00:00
|
|
|
"math/rand"
|
2016-12-22 14:18:33 +00:00
|
|
|
"os"
|
2018-05-27 17:05:11 +00:00
|
|
|
"path"
|
2018-10-25 09:32:57 +00:00
|
|
|
"path/filepath"
|
2016-12-22 14:18:33 +00:00
|
|
|
"testing"
|
2018-01-07 02:35:50 +00:00
|
|
|
"time"
|
2016-12-22 14:18:33 +00:00
|
|
|
|
2017-04-04 09:27:26 +00:00
|
|
|
"github.com/go-kit/kit/log"
|
2019-11-18 19:53:33 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2019-08-13 08:34:14 +00:00
|
|
|
"github.com/prometheus/prometheus/tsdb/fileutil"
|
2019-09-19 09:15:41 +00:00
|
|
|
"github.com/prometheus/prometheus/tsdb/record"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/tombstones"
|
2019-08-13 08:34:14 +00:00
|
|
|
"github.com/prometheus/prometheus/tsdb/wal"
|
2019-08-14 09:07:02 +00:00
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2016-12-22 14:18:33 +00:00
|
|
|
)
|
|
|
|
|
2017-05-13 15:09:26 +00:00
|
|
|
func TestSegmentWAL_cut(t *testing.T) {
|
2017-02-14 23:54:52 +00:00
|
|
|
tmpdir, err := ioutil.TempDir("", "test_wal_cut")
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2019-03-19 13:31:57 +00:00
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(tmpdir))
|
|
|
|
}()
|
2016-12-22 14:18:33 +00:00
|
|
|
|
2017-02-14 23:54:52 +00:00
|
|
|
// This calls cut() implicitly the first time without a previous tail.
|
2017-10-04 19:51:34 +00:00
|
|
|
w, err := OpenSegmentWAL(tmpdir, nil, 0, nil)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2016-12-22 14:18:33 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, w.write(WALEntrySeries, 1, []byte("Hello World!!")))
|
2016-12-22 14:18:33 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, w.cut())
|
2016-12-22 14:18:33 +00:00
|
|
|
|
2017-09-08 08:12:28 +00:00
|
|
|
// Cutting creates a new file.
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Equals(t, 2, len(w.files))
|
2016-12-22 14:18:33 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, w.write(WALEntrySeries, 1, []byte("Hello World!!")))
|
2016-12-22 14:18:33 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, w.Close())
|
2016-12-22 14:18:33 +00:00
|
|
|
|
2017-02-14 23:54:52 +00:00
|
|
|
for _, of := range w.files {
|
2017-09-06 12:59:25 +00:00
|
|
|
f, err := os.Open(of.Name())
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-02-14 23:54:52 +00:00
|
|
|
|
|
|
|
// Verify header data.
|
|
|
|
metab := make([]byte, 8)
|
|
|
|
_, err = f.Read(metab)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, WALMagic, binary.BigEndian.Uint32(metab[:4]))
|
|
|
|
testutil.Equals(t, WALFormatDefault, metab[4])
|
2016-12-22 14:18:33 +00:00
|
|
|
|
2017-02-14 23:54:52 +00:00
|
|
|
// We cannot actually check for correct pre-allocation as it is
|
|
|
|
// optional per filesystem and handled transparently.
|
2017-09-06 12:59:25 +00:00
|
|
|
et, flag, b, err := newWALReader(nil, nil).entry(f)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, WALEntrySeries, et)
|
2018-09-21 06:23:01 +00:00
|
|
|
testutil.Equals(t, byte(walSeriesSimple), flag)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Equals(t, []byte("Hello World!!"), b)
|
2017-02-14 23:54:52 +00:00
|
|
|
}
|
2016-12-22 14:18:33 +00:00
|
|
|
}
|
2017-02-15 05:54:59 +00:00
|
|
|
|
2017-09-06 12:59:25 +00:00
|
|
|
func TestSegmentWAL_Truncate(t *testing.T) {
|
|
|
|
const (
|
2017-09-07 11:04:02 +00:00
|
|
|
numMetrics = 20000
|
|
|
|
batch = 100
|
2017-09-06 12:59:25 +00:00
|
|
|
)
|
2018-10-25 09:32:57 +00:00
|
|
|
series, err := labels.ReadLabels(filepath.Join("testdata", "20kseries.json"), numMetrics)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-09-06 12:59:25 +00:00
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "test_wal_log_truncate")
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2019-03-19 13:31:57 +00:00
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2017-09-06 12:59:25 +00:00
|
|
|
|
2017-10-04 19:51:34 +00:00
|
|
|
w, err := OpenSegmentWAL(dir, nil, 0, nil)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-09-07 11:04:02 +00:00
|
|
|
w.segmentSize = 10000
|
2017-09-06 12:59:25 +00:00
|
|
|
|
|
|
|
for i := 0; i < numMetrics; i += batch {
|
2019-09-19 09:15:41 +00:00
|
|
|
var rs []record.RefSeries
|
2017-09-06 12:59:25 +00:00
|
|
|
|
|
|
|
for j, s := range series[i : i+batch] {
|
2019-09-19 09:15:41 +00:00
|
|
|
rs = append(rs, record.RefSeries{Labels: s, Ref: uint64(i+j) + 1})
|
2017-09-06 12:59:25 +00:00
|
|
|
}
|
|
|
|
err := w.LogSeries(rs)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-09-06 12:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We mark the 2nd half of the files with a min timestamp that should discard
|
|
|
|
// them from the selection of compactable files.
|
|
|
|
for i, f := range w.files[len(w.files)/2:] {
|
|
|
|
f.maxTime = int64(1000 + i)
|
|
|
|
}
|
|
|
|
// All series in those files must be preserved regarding of the provided postings list.
|
|
|
|
boundarySeries := w.files[len(w.files)/2].minSeries
|
|
|
|
|
|
|
|
// We truncate while keeping every 2nd series.
|
2017-09-21 09:02:30 +00:00
|
|
|
keep := map[uint64]struct{}{}
|
2017-09-06 12:59:25 +00:00
|
|
|
for i := 1; i <= numMetrics; i += 2 {
|
2017-09-21 09:02:30 +00:00
|
|
|
keep[uint64(i)] = struct{}{}
|
|
|
|
}
|
|
|
|
keepf := func(id uint64) bool {
|
|
|
|
_, ok := keep[id]
|
|
|
|
return ok
|
2017-09-06 12:59:25 +00:00
|
|
|
}
|
|
|
|
|
2017-09-21 09:02:30 +00:00
|
|
|
err = w.Truncate(1000, keepf)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-09-07 08:58:34 +00:00
|
|
|
|
2019-09-19 09:15:41 +00:00
|
|
|
var expected []record.RefSeries
|
2017-09-07 08:58:34 +00:00
|
|
|
|
|
|
|
for i := 1; i <= numMetrics; i++ {
|
|
|
|
if i%2 == 1 || uint64(i) >= boundarySeries {
|
2019-09-19 09:15:41 +00:00
|
|
|
expected = append(expected, record.RefSeries{Ref: uint64(i), Labels: series[i-1]})
|
2017-09-07 08:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call Truncate once again to see whether we can read the written file without
|
|
|
|
// creating a new WAL.
|
2017-09-21 09:02:30 +00:00
|
|
|
err = w.Truncate(1000, keepf)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Ok(t, w.Close())
|
2017-09-06 12:59:25 +00:00
|
|
|
|
2017-09-07 08:58:34 +00:00
|
|
|
// The same again with a new WAL.
|
2017-10-04 19:51:34 +00:00
|
|
|
w, err = OpenSegmentWAL(dir, nil, 0, nil)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-09-06 12:59:25 +00:00
|
|
|
|
2019-09-19 09:15:41 +00:00
|
|
|
var readSeries []record.RefSeries
|
2017-09-06 12:59:25 +00:00
|
|
|
r := w.Reader()
|
|
|
|
|
2019-09-19 09:15:41 +00:00
|
|
|
testutil.Ok(t, r.Read(func(s []record.RefSeries) {
|
2017-09-06 12:59:25 +00:00
|
|
|
readSeries = append(readSeries, s...)
|
2019-04-25 10:07:04 +00:00
|
|
|
}, nil, nil))
|
2017-09-06 12:59:25 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Equals(t, expected, readSeries)
|
2017-09-06 12:59:25 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 05:54:59 +00:00
|
|
|
// Symmetrical test of reading and writing to the WAL via its main interface.
|
2017-05-13 15:09:26 +00:00
|
|
|
func TestSegmentWAL_Log_Restore(t *testing.T) {
|
2017-03-08 19:52:03 +00:00
|
|
|
const (
|
2017-09-06 12:59:25 +00:00
|
|
|
numMetrics = 50
|
2017-03-08 19:52:03 +00:00
|
|
|
iterations = 5
|
2017-09-08 06:17:01 +00:00
|
|
|
stepSize = 5
|
2017-03-08 19:52:03 +00:00
|
|
|
)
|
2017-02-15 05:54:59 +00:00
|
|
|
// Generate testing data. It does not make semantical sense but
|
|
|
|
// for the purpose of this test.
|
2018-10-25 09:32:57 +00:00
|
|
|
series, err := labels.ReadLabels(filepath.Join("testdata", "20kseries.json"), numMetrics)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-02-15 05:54:59 +00:00
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "test_wal_log_restore")
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2019-03-19 13:31:57 +00:00
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2017-02-15 05:54:59 +00:00
|
|
|
|
2017-03-08 19:52:03 +00:00
|
|
|
var (
|
2019-09-19 09:15:41 +00:00
|
|
|
recordedSeries [][]record.RefSeries
|
|
|
|
recordedSamples [][]record.RefSample
|
|
|
|
recordedDeletes [][]tombstones.Stone
|
2017-03-08 19:52:03 +00:00
|
|
|
)
|
|
|
|
var totalSamples int
|
2017-02-15 05:54:59 +00:00
|
|
|
|
2017-03-08 19:52:03 +00:00
|
|
|
// Open WAL a bunch of times, validate all previous data can be read,
|
|
|
|
// write more data to it, close it.
|
|
|
|
for k := 0; k < numMetrics; k += numMetrics / iterations {
|
2017-10-04 19:51:34 +00:00
|
|
|
w, err := OpenSegmentWAL(dir, nil, 0, nil)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-02-15 05:54:59 +00:00
|
|
|
|
2017-03-08 19:52:03 +00:00
|
|
|
// Set smaller segment size so we can actually write several files.
|
|
|
|
w.segmentSize = 1000 * 1000
|
|
|
|
|
2017-09-06 12:59:25 +00:00
|
|
|
r := w.Reader()
|
2017-03-08 19:52:03 +00:00
|
|
|
|
|
|
|
var (
|
2019-09-19 09:15:41 +00:00
|
|
|
resultSeries [][]record.RefSeries
|
|
|
|
resultSamples [][]record.RefSample
|
|
|
|
resultDeletes [][]tombstones.Stone
|
2017-03-08 19:52:03 +00:00
|
|
|
)
|
|
|
|
|
2019-09-19 09:15:41 +00:00
|
|
|
serf := func(series []record.RefSeries) {
|
2017-08-31 09:39:22 +00:00
|
|
|
if len(series) > 0 {
|
2019-09-19 09:15:41 +00:00
|
|
|
clsets := make([]record.RefSeries, len(series))
|
2017-08-31 09:39:22 +00:00
|
|
|
copy(clsets, series)
|
2017-09-08 06:17:01 +00:00
|
|
|
resultSeries = append(resultSeries, clsets)
|
2017-03-08 19:52:03 +00:00
|
|
|
}
|
2017-05-23 10:45:16 +00:00
|
|
|
}
|
2019-09-19 09:15:41 +00:00
|
|
|
smplf := func(smpls []record.RefSample) {
|
2017-03-08 19:52:03 +00:00
|
|
|
if len(smpls) > 0 {
|
2019-09-19 09:15:41 +00:00
|
|
|
csmpls := make([]record.RefSample, len(smpls))
|
2017-03-08 19:52:03 +00:00
|
|
|
copy(csmpls, smpls)
|
|
|
|
resultSamples = append(resultSamples, csmpls)
|
|
|
|
}
|
|
|
|
}
|
2017-05-23 10:45:16 +00:00
|
|
|
|
2019-09-19 09:15:41 +00:00
|
|
|
delf := func(stones []tombstones.Stone) {
|
2017-05-23 10:45:16 +00:00
|
|
|
if len(stones) > 0 {
|
2019-09-19 09:15:41 +00:00
|
|
|
cst := make([]tombstones.Stone, len(stones))
|
2017-09-08 06:17:01 +00:00
|
|
|
copy(cst, stones)
|
|
|
|
resultDeletes = append(resultDeletes, cst)
|
2017-05-23 10:45:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, r.Read(serf, smplf, delf))
|
2017-02-15 05:54:59 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Equals(t, recordedSamples, resultSamples)
|
|
|
|
testutil.Equals(t, recordedSeries, resultSeries)
|
|
|
|
testutil.Equals(t, recordedDeletes, resultDeletes)
|
2017-02-15 05:54:59 +00:00
|
|
|
|
2017-03-08 19:52:03 +00:00
|
|
|
series := series[k : k+(numMetrics/iterations)]
|
2017-02-15 05:54:59 +00:00
|
|
|
|
2017-03-08 19:52:03 +00:00
|
|
|
// Insert in batches and generate different amounts of samples for each.
|
|
|
|
for i := 0; i < len(series); i += stepSize {
|
2019-09-19 09:15:41 +00:00
|
|
|
var samples []record.RefSample
|
|
|
|
var stones []tombstones.Stone
|
2017-02-15 05:54:59 +00:00
|
|
|
|
2017-03-08 19:52:03 +00:00
|
|
|
for j := 0; j < i*10; j++ {
|
2019-09-19 09:15:41 +00:00
|
|
|
samples = append(samples, record.RefSample{
|
2017-05-12 15:06:26 +00:00
|
|
|
Ref: uint64(j % 10000),
|
|
|
|
T: int64(j * 2),
|
|
|
|
V: rand.Float64(),
|
2017-03-08 19:52:03 +00:00
|
|
|
})
|
|
|
|
}
|
2017-02-15 05:54:59 +00:00
|
|
|
|
2017-05-23 10:45:16 +00:00
|
|
|
for j := 0; j < i*20; j++ {
|
|
|
|
ts := rand.Int63()
|
2019-09-19 09:15:41 +00:00
|
|
|
stones = append(stones, tombstones.Stone{Ref: rand.Uint64(), Intervals: tombstones.Intervals{{Mint: ts, Maxt: ts + rand.Int63n(10000)}}})
|
2017-05-23 10:45:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 19:52:03 +00:00
|
|
|
lbls := series[i : i+stepSize]
|
2019-09-19 09:15:41 +00:00
|
|
|
series := make([]record.RefSeries, 0, len(series))
|
2017-08-31 09:39:22 +00:00
|
|
|
for j, l := range lbls {
|
2019-09-19 09:15:41 +00:00
|
|
|
series = append(series, record.RefSeries{
|
2017-08-31 09:39:22 +00:00
|
|
|
Ref: uint64(i + j),
|
|
|
|
Labels: l,
|
|
|
|
})
|
|
|
|
}
|
2017-03-08 19:52:03 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, w.LogSeries(series))
|
|
|
|
testutil.Ok(t, w.LogSamples(samples))
|
|
|
|
testutil.Ok(t, w.LogDeletes(stones))
|
2017-03-08 19:52:03 +00:00
|
|
|
|
|
|
|
if len(lbls) > 0 {
|
2017-08-31 09:39:22 +00:00
|
|
|
recordedSeries = append(recordedSeries, series)
|
2017-03-08 19:52:03 +00:00
|
|
|
}
|
|
|
|
if len(samples) > 0 {
|
|
|
|
recordedSamples = append(recordedSamples, samples)
|
|
|
|
totalSamples += len(samples)
|
|
|
|
}
|
2017-05-23 10:45:16 +00:00
|
|
|
if len(stones) > 0 {
|
2017-05-26 15:56:31 +00:00
|
|
|
recordedDeletes = append(recordedDeletes, stones)
|
2017-05-23 10:45:16 +00:00
|
|
|
}
|
2017-02-15 05:54:59 +00:00
|
|
|
}
|
2017-03-08 19:52:03 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, w.Close())
|
2017-02-15 05:54:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-14 18:30:23 +00:00
|
|
|
|
2017-10-20 10:12:49 +00:00
|
|
|
func TestWALRestoreCorrupted_invalidSegment(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "test_wal_log_restore")
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2019-03-19 13:31:57 +00:00
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2017-10-20 10:12:49 +00:00
|
|
|
|
|
|
|
wal, err := OpenSegmentWAL(dir, nil, 0, nil)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-10-20 10:12:49 +00:00
|
|
|
|
2018-10-25 09:32:57 +00:00
|
|
|
_, err = wal.createSegmentFile(filepath.Join(dir, "000000"))
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2018-10-25 09:32:57 +00:00
|
|
|
f, err := wal.createSegmentFile(filepath.Join(dir, "000001"))
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2018-10-25 09:32:57 +00:00
|
|
|
f2, err := wal.createSegmentFile(filepath.Join(dir, "000002"))
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Ok(t, f2.Close())
|
2017-10-20 10:12:49 +00:00
|
|
|
|
|
|
|
// Make header of second segment invalid.
|
|
|
|
_, err = f.WriteAt([]byte{1, 2, 3, 4}, 0)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Ok(t, f.Close())
|
2017-10-20 10:12:49 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, wal.Close())
|
2017-10-20 10:12:49 +00:00
|
|
|
|
2019-01-02 16:48:42 +00:00
|
|
|
_, err = OpenSegmentWAL(dir, log.NewLogfmtLogger(os.Stderr), 0, nil)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-10-20 10:12:49 +00:00
|
|
|
|
|
|
|
fns, err := fileutil.ReadDir(dir)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, []string{"000000"}, fns)
|
2017-10-20 10:12:49 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 18:30:23 +00:00
|
|
|
// Test reading from a WAL that has been corrupted through various means.
|
|
|
|
func TestWALRestoreCorrupted(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
2017-05-13 15:09:26 +00:00
|
|
|
f func(*testing.T, *SegmentWAL)
|
2017-03-14 18:30:23 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "truncate_checksum",
|
2017-05-13 15:09:26 +00:00
|
|
|
f: func(t *testing.T, w *SegmentWAL) {
|
2017-09-06 12:59:25 +00:00
|
|
|
f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 18:30:23 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
2018-03-21 20:39:43 +00:00
|
|
|
off, err := f.Seek(0, io.SeekEnd)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 18:30:23 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, f.Truncate(off-1))
|
2017-03-14 18:30:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "truncate_body",
|
2017-05-13 15:09:26 +00:00
|
|
|
f: func(t *testing.T, w *SegmentWAL) {
|
2017-09-06 12:59:25 +00:00
|
|
|
f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 18:30:23 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
2018-03-21 20:39:43 +00:00
|
|
|
off, err := f.Seek(0, io.SeekEnd)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 18:30:23 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, f.Truncate(off-8))
|
2017-03-14 18:30:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "body_content",
|
2017-05-13 15:09:26 +00:00
|
|
|
f: func(t *testing.T, w *SegmentWAL) {
|
2017-09-06 12:59:25 +00:00
|
|
|
f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 18:30:23 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
2018-03-21 20:39:43 +00:00
|
|
|
off, err := f.Seek(0, io.SeekEnd)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 18:30:23 +00:00
|
|
|
|
|
|
|
// Write junk before checksum starts.
|
|
|
|
_, err = f.WriteAt([]byte{1, 2, 3, 4}, off-8)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 18:30:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "checksum",
|
2017-05-13 15:09:26 +00:00
|
|
|
f: func(t *testing.T, w *SegmentWAL) {
|
2017-09-06 12:59:25 +00:00
|
|
|
f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 18:30:23 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
2018-03-21 20:39:43 +00:00
|
|
|
off, err := f.Seek(0, io.SeekEnd)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 18:30:23 +00:00
|
|
|
|
|
|
|
// Write junk into checksum
|
|
|
|
_, err = f.WriteAt([]byte{1, 2, 3, 4}, off-4)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 18:30:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
// Generate testing data. It does not make semantical sense but
|
|
|
|
// for the purpose of this test.
|
2017-09-11 08:33:17 +00:00
|
|
|
dir, err := ioutil.TempDir("", "test_corrupted")
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2019-03-19 13:31:57 +00:00
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2017-03-14 18:30:23 +00:00
|
|
|
|
2017-10-04 19:51:34 +00:00
|
|
|
w, err := OpenSegmentWAL(dir, nil, 0, nil)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 18:30:23 +00:00
|
|
|
|
2019-09-19 09:15:41 +00:00
|
|
|
testutil.Ok(t, w.LogSamples([]record.RefSample{{T: 1, V: 2}}))
|
|
|
|
testutil.Ok(t, w.LogSamples([]record.RefSample{{T: 2, V: 3}}))
|
2017-03-14 18:30:23 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, w.cut())
|
2017-03-14 18:30:23 +00:00
|
|
|
|
2018-01-09 04:39:23 +00:00
|
|
|
// Sleep 2 seconds to avoid error where cut and test "cases" function may write or
|
|
|
|
// truncate the file out of orders as "cases" are not synchronized with cut.
|
|
|
|
// Hopefully cut will complete by 2 seconds.
|
2018-01-07 02:35:50 +00:00
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
2019-09-19 09:15:41 +00:00
|
|
|
testutil.Ok(t, w.LogSamples([]record.RefSample{{T: 3, V: 4}}))
|
|
|
|
testutil.Ok(t, w.LogSamples([]record.RefSample{{T: 5, V: 6}}))
|
2017-03-14 18:30:23 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, w.Close())
|
2017-03-14 18:30:23 +00:00
|
|
|
|
2017-09-11 08:34:35 +00:00
|
|
|
// cut() truncates and fsyncs the first segment async. If it happens after
|
|
|
|
// the corruption we apply below, the corruption will be overwritten again.
|
2019-09-30 15:54:55 +00:00
|
|
|
// Fire and forget a sync to avoid flakiness.
|
2017-09-11 08:33:17 +00:00
|
|
|
w.files[0].Sync()
|
2017-03-14 18:30:23 +00:00
|
|
|
// Corrupt the second entry in the first file.
|
|
|
|
// After re-opening we must be able to read the first entry
|
|
|
|
// and the rest, including the second file, must be truncated for clean further
|
|
|
|
// writes.
|
|
|
|
c.f(t, w)
|
|
|
|
|
|
|
|
logger := log.NewLogfmtLogger(os.Stderr)
|
|
|
|
|
2017-10-04 19:51:34 +00:00
|
|
|
w2, err := OpenSegmentWAL(dir, logger, 0, nil)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 18:30:23 +00:00
|
|
|
|
2017-09-06 12:59:25 +00:00
|
|
|
r := w2.Reader()
|
2017-08-31 09:39:22 +00:00
|
|
|
|
2019-09-19 09:15:41 +00:00
|
|
|
serf := func(l []record.RefSeries) {
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Equals(t, 0, len(l))
|
2017-05-23 10:45:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Weird hack to check order of reads.
|
|
|
|
i := 0
|
2019-09-19 09:15:41 +00:00
|
|
|
samplf := func(s []record.RefSample) {
|
2017-05-23 10:45:16 +00:00
|
|
|
if i == 0 {
|
2019-09-19 09:15:41 +00:00
|
|
|
testutil.Equals(t, []record.RefSample{{T: 1, V: 2}}, s)
|
2017-05-23 10:45:16 +00:00
|
|
|
i++
|
|
|
|
} else {
|
2019-09-19 09:15:41 +00:00
|
|
|
testutil.Equals(t, []record.RefSample{{T: 99, V: 100}}, s)
|
2017-05-23 10:45:16 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-14 18:30:23 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, r.Read(serf, samplf, nil))
|
2017-03-14 18:30:23 +00:00
|
|
|
|
2019-09-19 09:15:41 +00:00
|
|
|
testutil.Ok(t, w2.LogSamples([]record.RefSample{{T: 99, V: 100}}))
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, w2.Close())
|
2017-03-14 18:30:23 +00:00
|
|
|
|
|
|
|
// We should see the first valid entry and the new one, everything after
|
|
|
|
// is truncated.
|
2017-10-04 19:51:34 +00:00
|
|
|
w3, err := OpenSegmentWAL(dir, logger, 0, nil)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-03-14 18:30:23 +00:00
|
|
|
|
2017-09-06 12:59:25 +00:00
|
|
|
r = w3.Reader()
|
2017-03-14 18:30:23 +00:00
|
|
|
|
2017-05-23 10:45:16 +00:00
|
|
|
i = 0
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, r.Read(serf, samplf, nil))
|
2017-03-14 18:30:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-05-27 17:05:11 +00:00
|
|
|
|
2018-06-05 08:50:20 +00:00
|
|
|
func TestMigrateWAL_Empty(t *testing.T) {
|
|
|
|
// The migration proecedure must properly deal with a zero-length segment,
|
|
|
|
// which is valid in the new format.
|
|
|
|
dir, err := ioutil.TempDir("", "walmigrate")
|
|
|
|
testutil.Ok(t, err)
|
2019-03-19 13:31:57 +00:00
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2018-06-05 08:50:20 +00:00
|
|
|
|
|
|
|
wdir := path.Join(dir, "wal")
|
|
|
|
|
|
|
|
// Initialize empty WAL.
|
2019-06-19 13:46:24 +00:00
|
|
|
w, err := wal.New(nil, nil, wdir, false)
|
2018-06-05 08:50:20 +00:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Ok(t, w.Close())
|
|
|
|
|
|
|
|
testutil.Ok(t, MigrateWAL(nil, wdir))
|
|
|
|
}
|
|
|
|
|
2018-05-27 17:05:11 +00:00
|
|
|
func TestMigrateWAL_Fuzz(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "walmigrate")
|
|
|
|
testutil.Ok(t, err)
|
2019-03-19 13:31:57 +00:00
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2018-05-27 17:05:11 +00:00
|
|
|
|
|
|
|
wdir := path.Join(dir, "wal")
|
|
|
|
|
|
|
|
// Should pass if no WAL exists yet.
|
|
|
|
testutil.Ok(t, MigrateWAL(nil, wdir))
|
|
|
|
|
|
|
|
oldWAL, err := OpenSegmentWAL(wdir, nil, time.Minute, nil)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
// Write some data.
|
2019-09-19 09:15:41 +00:00
|
|
|
testutil.Ok(t, oldWAL.LogSeries([]record.RefSeries{
|
2018-05-27 17:05:11 +00:00
|
|
|
{Ref: 100, Labels: labels.FromStrings("abc", "def", "123", "456")},
|
|
|
|
{Ref: 1, Labels: labels.FromStrings("abc", "def2", "1234", "4567")},
|
|
|
|
}))
|
2019-09-19 09:15:41 +00:00
|
|
|
testutil.Ok(t, oldWAL.LogSamples([]record.RefSample{
|
2018-05-27 17:05:11 +00:00
|
|
|
{Ref: 1, T: 100, V: 200},
|
|
|
|
{Ref: 2, T: 300, V: 400},
|
|
|
|
}))
|
2019-09-19 09:15:41 +00:00
|
|
|
testutil.Ok(t, oldWAL.LogSeries([]record.RefSeries{
|
2018-05-27 17:05:11 +00:00
|
|
|
{Ref: 200, Labels: labels.FromStrings("xyz", "def", "foo", "bar")},
|
|
|
|
}))
|
2019-09-19 09:15:41 +00:00
|
|
|
testutil.Ok(t, oldWAL.LogSamples([]record.RefSample{
|
2018-05-27 17:05:11 +00:00
|
|
|
{Ref: 3, T: 100, V: 200},
|
|
|
|
{Ref: 4, T: 300, V: 400},
|
|
|
|
}))
|
2019-09-19 09:15:41 +00:00
|
|
|
testutil.Ok(t, oldWAL.LogDeletes([]tombstones.Stone{
|
|
|
|
{Ref: 1, Intervals: []tombstones.Interval{{Mint: 100, Maxt: 200}}},
|
2018-05-27 17:05:11 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
testutil.Ok(t, oldWAL.Close())
|
|
|
|
|
|
|
|
// Perform migration.
|
|
|
|
testutil.Ok(t, MigrateWAL(nil, wdir))
|
|
|
|
|
2019-06-19 13:46:24 +00:00
|
|
|
w, err := wal.New(nil, nil, wdir, false)
|
2018-05-27 17:05:11 +00:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
// We can properly write some new data after migration.
|
2019-09-19 09:15:41 +00:00
|
|
|
var enc record.Encoder
|
|
|
|
testutil.Ok(t, w.Log(enc.Samples([]record.RefSample{
|
2018-05-27 17:05:11 +00:00
|
|
|
{Ref: 500, T: 1, V: 1},
|
|
|
|
}, nil)))
|
|
|
|
|
|
|
|
testutil.Ok(t, w.Close())
|
|
|
|
|
|
|
|
// Read back all data.
|
|
|
|
sr, err := wal.NewSegmentsReader(wdir)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
r := wal.NewReader(sr)
|
|
|
|
var res []interface{}
|
2019-09-19 09:15:41 +00:00
|
|
|
var dec record.Decoder
|
2018-05-27 17:05:11 +00:00
|
|
|
|
|
|
|
for r.Next() {
|
|
|
|
rec := r.Record()
|
|
|
|
|
|
|
|
switch dec.Type(rec) {
|
2019-09-19 09:15:41 +00:00
|
|
|
case record.Series:
|
2018-05-27 17:05:11 +00:00
|
|
|
s, err := dec.Series(rec, nil)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
res = append(res, s)
|
2019-09-19 09:15:41 +00:00
|
|
|
case record.Samples:
|
2018-05-27 17:05:11 +00:00
|
|
|
s, err := dec.Samples(rec, nil)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
res = append(res, s)
|
2019-09-19 09:15:41 +00:00
|
|
|
case record.Tombstones:
|
2018-05-27 17:05:11 +00:00
|
|
|
s, err := dec.Tombstones(rec, nil)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
res = append(res, s)
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown record type %d", dec.Type(rec))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
testutil.Ok(t, r.Err())
|
|
|
|
|
|
|
|
testutil.Equals(t, []interface{}{
|
2019-09-19 09:15:41 +00:00
|
|
|
[]record.RefSeries{
|
2018-05-27 17:05:11 +00:00
|
|
|
{Ref: 100, Labels: labels.FromStrings("abc", "def", "123", "456")},
|
|
|
|
{Ref: 1, Labels: labels.FromStrings("abc", "def2", "1234", "4567")},
|
|
|
|
},
|
2019-09-19 09:15:41 +00:00
|
|
|
[]record.RefSample{{Ref: 1, T: 100, V: 200}, {Ref: 2, T: 300, V: 400}},
|
|
|
|
[]record.RefSeries{
|
2018-05-27 17:05:11 +00:00
|
|
|
{Ref: 200, Labels: labels.FromStrings("xyz", "def", "foo", "bar")},
|
|
|
|
},
|
2019-09-19 09:15:41 +00:00
|
|
|
[]record.RefSample{{Ref: 3, T: 100, V: 200}, {Ref: 4, T: 300, V: 400}},
|
|
|
|
[]tombstones.Stone{{Ref: 1, Intervals: []tombstones.Interval{{Mint: 100, Maxt: 200}}}},
|
|
|
|
[]record.RefSample{{Ref: 500, T: 1, V: 1}},
|
2018-05-27 17:05:11 +00:00
|
|
|
}, res)
|
|
|
|
|
|
|
|
// Migrating an already migrated WAL shouldn't do anything.
|
|
|
|
testutil.Ok(t, MigrateWAL(nil, wdir))
|
|
|
|
}
|