2017-04-10 11:59:45 -07: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.
|
|
|
|
|
2016-12-22 06:18:33 -08:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
2017-02-14 15:54:52 -08:00
|
|
|
"encoding/binary"
|
2016-12-22 06:18:33 -08:00
|
|
|
"io/ioutil"
|
2017-02-14 21:54:59 -08:00
|
|
|
"math/rand"
|
2016-12-22 06:18:33 -08:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2017-04-04 02:27:26 -07:00
|
|
|
"github.com/go-kit/kit/log"
|
2017-10-20 03:12:49 -07:00
|
|
|
"github.com/prometheus/tsdb/fileutil"
|
2016-12-22 06:18:33 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-05-13 08:09:26 -07:00
|
|
|
func TestSegmentWAL_cut(t *testing.T) {
|
2017-02-14 15:54:52 -08:00
|
|
|
tmpdir, err := ioutil.TempDir("", "test_wal_cut")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
// This calls cut() implicitly the first time without a previous tail.
|
2017-10-04 12:51:34 -07:00
|
|
|
w, err := OpenSegmentWAL(tmpdir, nil, 0, nil)
|
2017-02-14 15:54:52 -08:00
|
|
|
require.NoError(t, err)
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
require.NoError(t, w.write(WALEntrySeries, 1, []byte("Hello World!!")))
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
require.NoError(t, w.cut(), "cut failed")
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-09-08 01:12:28 -07:00
|
|
|
// Cutting creates a new file.
|
2017-02-14 15:54:52 -08:00
|
|
|
require.Equal(t, 2, len(w.files))
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
require.NoError(t, w.write(WALEntrySeries, 1, []byte("Hello World!!")))
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
require.NoError(t, w.Close())
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
for _, of := range w.files {
|
2017-09-06 05:59:25 -07:00
|
|
|
f, err := os.Open(of.Name())
|
2017-02-14 15:54:52 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Verify header data.
|
|
|
|
metab := make([]byte, 8)
|
|
|
|
_, err = f.Read(metab)
|
|
|
|
require.NoError(t, err, "read meta data %s", f.Name())
|
|
|
|
require.Equal(t, WALMagic, binary.BigEndian.Uint32(metab[:4]), "verify magic")
|
|
|
|
require.Equal(t, WALFormatDefault, metab[4], "verify format flag")
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
// We cannot actually check for correct pre-allocation as it is
|
|
|
|
// optional per filesystem and handled transparently.
|
2017-09-06 05:59:25 -07:00
|
|
|
et, flag, b, err := newWALReader(nil, nil).entry(f)
|
2017-02-14 15:54:52 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, WALEntrySeries, et)
|
|
|
|
require.Equal(t, flag, byte(walSeriesSimple))
|
|
|
|
require.Equal(t, []byte("Hello World!!"), b)
|
|
|
|
}
|
2016-12-22 06:18:33 -08:00
|
|
|
}
|
2017-02-14 21:54:59 -08:00
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
func TestSegmentWAL_Truncate(t *testing.T) {
|
|
|
|
const (
|
2017-09-07 04:04:02 -07:00
|
|
|
numMetrics = 20000
|
|
|
|
batch = 100
|
2017-09-06 05:59:25 -07:00
|
|
|
)
|
|
|
|
series, err := readPrometheusLabels("testdata/20k.series", numMetrics)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "test_wal_log_truncate")
|
|
|
|
require.NoError(t, err)
|
|
|
|
// defer os.RemoveAll(dir)
|
|
|
|
|
2017-10-04 12:51:34 -07:00
|
|
|
w, err := OpenSegmentWAL(dir, nil, 0, nil)
|
2017-09-06 05:59:25 -07:00
|
|
|
require.NoError(t, err)
|
2017-09-07 04:04:02 -07:00
|
|
|
w.segmentSize = 10000
|
2017-09-06 05:59:25 -07:00
|
|
|
|
|
|
|
for i := 0; i < numMetrics; i += batch {
|
|
|
|
var rs []RefSeries
|
|
|
|
|
|
|
|
for j, s := range series[i : i+batch] {
|
|
|
|
rs = append(rs, RefSeries{Labels: s, Ref: uint64(i+j) + 1})
|
|
|
|
}
|
|
|
|
err := w.LogSeries(rs)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 02:02:30 -07:00
|
|
|
keep := map[uint64]struct{}{}
|
2017-09-06 05:59:25 -07:00
|
|
|
for i := 1; i <= numMetrics; i += 2 {
|
2017-09-21 02:02:30 -07:00
|
|
|
keep[uint64(i)] = struct{}{}
|
|
|
|
}
|
|
|
|
keepf := func(id uint64) bool {
|
|
|
|
_, ok := keep[id]
|
|
|
|
return ok
|
2017-09-06 05:59:25 -07:00
|
|
|
}
|
|
|
|
|
2017-09-21 02:02:30 -07:00
|
|
|
err = w.Truncate(1000, keepf)
|
2017-09-07 01:58:34 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var expected []RefSeries
|
|
|
|
|
|
|
|
for i := 1; i <= numMetrics; i++ {
|
|
|
|
if i%2 == 1 || uint64(i) >= boundarySeries {
|
|
|
|
expected = append(expected, RefSeries{Ref: uint64(i), Labels: series[i-1]})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call Truncate once again to see whether we can read the written file without
|
|
|
|
// creating a new WAL.
|
2017-09-21 02:02:30 -07:00
|
|
|
err = w.Truncate(1000, keepf)
|
2017-09-06 05:59:25 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, w.Close())
|
|
|
|
|
2017-09-07 01:58:34 -07:00
|
|
|
// The same again with a new WAL.
|
2017-10-04 12:51:34 -07:00
|
|
|
w, err = OpenSegmentWAL(dir, nil, 0, nil)
|
2017-09-06 05:59:25 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var readSeries []RefSeries
|
|
|
|
r := w.Reader()
|
|
|
|
|
2017-10-06 05:06:39 -07:00
|
|
|
r.Read(func(s []RefSeries) {
|
2017-09-06 05:59:25 -07:00
|
|
|
readSeries = append(readSeries, s...)
|
|
|
|
}, nil, nil)
|
|
|
|
|
2017-09-07 01:58:34 -07:00
|
|
|
require.Equal(t, expected, readSeries)
|
2017-09-06 05:59:25 -07:00
|
|
|
}
|
|
|
|
|
2017-02-14 21:54:59 -08:00
|
|
|
// Symmetrical test of reading and writing to the WAL via its main interface.
|
2017-05-13 08:09:26 -07:00
|
|
|
func TestSegmentWAL_Log_Restore(t *testing.T) {
|
2017-03-08 11:52:03 -08:00
|
|
|
const (
|
2017-09-06 05:59:25 -07:00
|
|
|
numMetrics = 50
|
2017-03-08 11:52:03 -08:00
|
|
|
iterations = 5
|
2017-09-07 23:17:01 -07:00
|
|
|
stepSize = 5
|
2017-03-08 11:52:03 -08:00
|
|
|
)
|
2017-02-14 21:54:59 -08:00
|
|
|
// Generate testing data. It does not make semantical sense but
|
|
|
|
// for the purpose of this test.
|
2017-03-08 11:52:03 -08:00
|
|
|
series, err := readPrometheusLabels("testdata/20k.series", numMetrics)
|
2017-02-14 21:54:59 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "test_wal_log_restore")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2017-03-08 11:52:03 -08:00
|
|
|
var (
|
2017-08-31 02:39:22 -07:00
|
|
|
recordedSeries [][]RefSeries
|
2017-05-12 08:06:26 -07:00
|
|
|
recordedSamples [][]RefSample
|
2017-05-26 08:56:31 -07:00
|
|
|
recordedDeletes [][]Stone
|
2017-03-08 11:52:03 -08:00
|
|
|
)
|
|
|
|
var totalSamples int
|
2017-02-14 21:54:59 -08:00
|
|
|
|
2017-03-08 11:52:03 -08: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 12:51:34 -07:00
|
|
|
w, err := OpenSegmentWAL(dir, nil, 0, nil)
|
2017-03-08 11:52:03 -08:00
|
|
|
require.NoError(t, err)
|
2017-02-14 21:54:59 -08:00
|
|
|
|
2017-03-08 11:52:03 -08:00
|
|
|
// Set smaller segment size so we can actually write several files.
|
|
|
|
w.segmentSize = 1000 * 1000
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
r := w.Reader()
|
2017-03-08 11:52:03 -08:00
|
|
|
|
|
|
|
var (
|
2017-08-31 02:39:22 -07:00
|
|
|
resultSeries [][]RefSeries
|
2017-05-12 08:06:26 -07:00
|
|
|
resultSamples [][]RefSample
|
2017-05-26 08:56:31 -07:00
|
|
|
resultDeletes [][]Stone
|
2017-03-08 11:52:03 -08:00
|
|
|
)
|
|
|
|
|
2017-10-06 05:06:39 -07:00
|
|
|
serf := func(series []RefSeries) {
|
2017-08-31 02:39:22 -07:00
|
|
|
if len(series) > 0 {
|
|
|
|
clsets := make([]RefSeries, len(series))
|
|
|
|
copy(clsets, series)
|
2017-09-07 23:17:01 -07:00
|
|
|
resultSeries = append(resultSeries, clsets)
|
2017-03-08 11:52:03 -08:00
|
|
|
}
|
2017-05-23 03:45:16 -07:00
|
|
|
}
|
2017-10-06 05:06:39 -07:00
|
|
|
smplf := func(smpls []RefSample) {
|
2017-03-08 11:52:03 -08:00
|
|
|
if len(smpls) > 0 {
|
2017-05-12 08:06:26 -07:00
|
|
|
csmpls := make([]RefSample, len(smpls))
|
2017-03-08 11:52:03 -08:00
|
|
|
copy(csmpls, smpls)
|
|
|
|
resultSamples = append(resultSamples, csmpls)
|
|
|
|
}
|
|
|
|
}
|
2017-05-23 03:45:16 -07:00
|
|
|
|
2017-10-06 05:06:39 -07:00
|
|
|
delf := func(stones []Stone) {
|
2017-05-23 03:45:16 -07:00
|
|
|
if len(stones) > 0 {
|
2017-09-07 23:17:01 -07:00
|
|
|
cst := make([]Stone, len(stones))
|
|
|
|
copy(cst, stones)
|
|
|
|
resultDeletes = append(resultDeletes, cst)
|
2017-05-23 03:45:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, r.Read(serf, smplf, delf))
|
2017-02-14 21:54:59 -08:00
|
|
|
|
2017-03-08 11:52:03 -08:00
|
|
|
require.Equal(t, recordedSamples, resultSamples)
|
|
|
|
require.Equal(t, recordedSeries, resultSeries)
|
2017-05-23 03:45:16 -07:00
|
|
|
require.Equal(t, recordedDeletes, resultDeletes)
|
2017-02-14 21:54:59 -08:00
|
|
|
|
2017-03-08 11:52:03 -08:00
|
|
|
series := series[k : k+(numMetrics/iterations)]
|
2017-02-14 21:54:59 -08:00
|
|
|
|
2017-03-08 11:52:03 -08:00
|
|
|
// Insert in batches and generate different amounts of samples for each.
|
|
|
|
for i := 0; i < len(series); i += stepSize {
|
2017-05-12 08:06:26 -07:00
|
|
|
var samples []RefSample
|
2017-05-26 08:56:31 -07:00
|
|
|
var stones []Stone
|
2017-02-14 21:54:59 -08:00
|
|
|
|
2017-03-08 11:52:03 -08:00
|
|
|
for j := 0; j < i*10; j++ {
|
2017-05-12 08:06:26 -07:00
|
|
|
samples = append(samples, RefSample{
|
|
|
|
Ref: uint64(j % 10000),
|
|
|
|
T: int64(j * 2),
|
|
|
|
V: rand.Float64(),
|
2017-03-08 11:52:03 -08:00
|
|
|
})
|
|
|
|
}
|
2017-02-14 21:54:59 -08:00
|
|
|
|
2017-05-23 03:45:16 -07:00
|
|
|
for j := 0; j < i*20; j++ {
|
|
|
|
ts := rand.Int63()
|
2017-09-04 07:08:38 -07:00
|
|
|
stones = append(stones, Stone{rand.Uint64(), Intervals{{ts, ts + rand.Int63n(10000)}}})
|
2017-05-23 03:45:16 -07:00
|
|
|
}
|
|
|
|
|
2017-03-08 11:52:03 -08:00
|
|
|
lbls := series[i : i+stepSize]
|
2017-08-31 02:39:22 -07:00
|
|
|
series := make([]RefSeries, 0, len(series))
|
|
|
|
for j, l := range lbls {
|
|
|
|
series = append(series, RefSeries{
|
|
|
|
Ref: uint64(i + j),
|
|
|
|
Labels: l,
|
|
|
|
})
|
|
|
|
}
|
2017-03-08 11:52:03 -08:00
|
|
|
|
2017-08-31 02:39:22 -07:00
|
|
|
require.NoError(t, w.LogSeries(series))
|
2017-05-23 03:45:16 -07:00
|
|
|
require.NoError(t, w.LogSamples(samples))
|
2017-05-26 08:56:31 -07:00
|
|
|
require.NoError(t, w.LogDeletes(stones))
|
2017-03-08 11:52:03 -08:00
|
|
|
|
|
|
|
if len(lbls) > 0 {
|
2017-08-31 02:39:22 -07:00
|
|
|
recordedSeries = append(recordedSeries, series)
|
2017-03-08 11:52:03 -08:00
|
|
|
}
|
|
|
|
if len(samples) > 0 {
|
|
|
|
recordedSamples = append(recordedSamples, samples)
|
|
|
|
totalSamples += len(samples)
|
|
|
|
}
|
2017-05-23 03:45:16 -07:00
|
|
|
if len(stones) > 0 {
|
2017-05-26 08:56:31 -07:00
|
|
|
recordedDeletes = append(recordedDeletes, stones)
|
2017-05-23 03:45:16 -07:00
|
|
|
}
|
2017-02-14 21:54:59 -08:00
|
|
|
}
|
2017-03-08 11:52:03 -08:00
|
|
|
|
|
|
|
require.NoError(t, w.Close())
|
2017-02-14 21:54:59 -08:00
|
|
|
}
|
|
|
|
}
|
2017-03-14 11:30:23 -07:00
|
|
|
|
2017-10-20 03:12:49 -07:00
|
|
|
func TestWALRestoreCorrupted_invalidSegment(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "test_wal_log_restore")
|
|
|
|
Ok(t, err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
wal, err := OpenSegmentWAL(dir, nil, 0, nil)
|
|
|
|
Ok(t, err)
|
|
|
|
|
|
|
|
_, err = wal.createSegmentFile(dir + "/000000")
|
|
|
|
Ok(t, err)
|
|
|
|
f, err := wal.createSegmentFile(dir + "/000001")
|
|
|
|
Ok(t, err)
|
2017-10-31 05:07:29 -07:00
|
|
|
f2, err := wal.createSegmentFile(dir + "/000002")
|
2017-10-20 03:12:49 -07:00
|
|
|
Ok(t, err)
|
2017-10-31 05:07:29 -07:00
|
|
|
Ok(t, f2.Close()) // close for windows!
|
2017-10-20 03:12:49 -07:00
|
|
|
|
|
|
|
// Make header of second segment invalid.
|
|
|
|
_, err = f.WriteAt([]byte{1, 2, 3, 4}, 0)
|
|
|
|
Ok(t, err)
|
|
|
|
Ok(t, f.Close())
|
|
|
|
|
|
|
|
Ok(t, wal.Close())
|
|
|
|
|
|
|
|
wal, err = OpenSegmentWAL(dir, log.NewLogfmtLogger(os.Stderr), 0, nil)
|
|
|
|
Ok(t, err)
|
|
|
|
|
|
|
|
fns, err := fileutil.ReadDir(dir)
|
|
|
|
Ok(t, err)
|
|
|
|
Equals(t, []string{"000000"}, fns)
|
|
|
|
}
|
|
|
|
|
2017-03-14 11:30:23 -07: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 08:09:26 -07:00
|
|
|
f func(*testing.T, *SegmentWAL)
|
2017-03-14 11:30:23 -07:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "truncate_checksum",
|
2017-05-13 08:09:26 -07:00
|
|
|
f: func(t *testing.T, w *SegmentWAL) {
|
2017-09-06 05:59:25 -07:00
|
|
|
f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666)
|
2017-03-14 11:30:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
off, err := f.Seek(0, os.SEEK_END)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NoError(t, f.Truncate(off-1))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "truncate_body",
|
2017-05-13 08:09:26 -07:00
|
|
|
f: func(t *testing.T, w *SegmentWAL) {
|
2017-09-06 05:59:25 -07:00
|
|
|
f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666)
|
2017-03-14 11:30:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
off, err := f.Seek(0, os.SEEK_END)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NoError(t, f.Truncate(off-8))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "body_content",
|
2017-05-13 08:09:26 -07:00
|
|
|
f: func(t *testing.T, w *SegmentWAL) {
|
2017-09-06 05:59:25 -07:00
|
|
|
f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666)
|
2017-03-14 11:30:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
off, err := f.Seek(0, os.SEEK_END)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Write junk before checksum starts.
|
|
|
|
_, err = f.WriteAt([]byte{1, 2, 3, 4}, off-8)
|
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "checksum",
|
2017-05-13 08:09:26 -07:00
|
|
|
f: func(t *testing.T, w *SegmentWAL) {
|
2017-09-06 05:59:25 -07:00
|
|
|
f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666)
|
2017-03-14 11:30:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
off, err := f.Seek(0, os.SEEK_END)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Write junk into checksum
|
|
|
|
_, err = f.WriteAt([]byte{1, 2, 3, 4}, off-4)
|
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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 01:33:17 -07:00
|
|
|
dir, err := ioutil.TempDir("", "test_corrupted")
|
2017-03-14 11:30:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
2017-10-04 12:51:34 -07:00
|
|
|
w, err := OpenSegmentWAL(dir, nil, 0, nil)
|
2017-03-14 11:30:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-05-23 03:45:16 -07:00
|
|
|
require.NoError(t, w.LogSamples([]RefSample{{T: 1, V: 2}}))
|
|
|
|
require.NoError(t, w.LogSamples([]RefSample{{T: 2, V: 3}}))
|
2017-03-14 11:30:23 -07:00
|
|
|
|
|
|
|
require.NoError(t, w.cut())
|
|
|
|
|
2017-05-23 03:45:16 -07:00
|
|
|
require.NoError(t, w.LogSamples([]RefSample{{T: 3, V: 4}}))
|
|
|
|
require.NoError(t, w.LogSamples([]RefSample{{T: 5, V: 6}}))
|
2017-03-14 11:30:23 -07:00
|
|
|
|
|
|
|
require.NoError(t, w.Close())
|
|
|
|
|
2017-09-11 01:34:35 -07:00
|
|
|
// cut() truncates and fsyncs the first segment async. If it happens after
|
|
|
|
// the corruption we apply below, the corruption will be overwritten again.
|
|
|
|
// Fire and forget a sync to avoid flakyness.
|
2017-09-11 01:33:17 -07:00
|
|
|
w.files[0].Sync()
|
2017-03-14 11:30:23 -07: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 12:51:34 -07:00
|
|
|
w2, err := OpenSegmentWAL(dir, logger, 0, nil)
|
2017-03-14 11:30:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
r := w2.Reader()
|
2017-08-31 02:39:22 -07:00
|
|
|
|
2017-10-06 05:06:39 -07:00
|
|
|
serf := func(l []RefSeries) {
|
2017-05-23 03:45:16 -07:00
|
|
|
require.Equal(t, 0, len(l))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Weird hack to check order of reads.
|
|
|
|
i := 0
|
2017-10-06 05:06:39 -07:00
|
|
|
samplf := func(s []RefSample) {
|
2017-05-23 03:45:16 -07:00
|
|
|
if i == 0 {
|
|
|
|
require.Equal(t, []RefSample{{T: 1, V: 2}}, s)
|
|
|
|
i++
|
|
|
|
} else {
|
|
|
|
require.Equal(t, []RefSample{{T: 99, V: 100}}, s)
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 11:30:23 -07:00
|
|
|
|
2017-10-06 05:06:39 -07:00
|
|
|
require.NoError(t, r.Read(serf, samplf, nil))
|
2017-03-14 11:30:23 -07:00
|
|
|
|
2017-05-23 03:45:16 -07:00
|
|
|
require.NoError(t, w2.LogSamples([]RefSample{{T: 99, V: 100}}))
|
2017-03-14 11:30:23 -07:00
|
|
|
require.NoError(t, w2.Close())
|
|
|
|
|
|
|
|
// We should see the first valid entry and the new one, everything after
|
|
|
|
// is truncated.
|
2017-10-04 12:51:34 -07:00
|
|
|
w3, err := OpenSegmentWAL(dir, logger, 0, nil)
|
2017-03-14 11:30:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-09-06 05:59:25 -07:00
|
|
|
r = w3.Reader()
|
2017-03-14 11:30:23 -07:00
|
|
|
|
2017-05-23 03:45:16 -07:00
|
|
|
i = 0
|
2017-10-06 05:06:39 -07:00
|
|
|
require.NoError(t, r.Read(serf, samplf, nil))
|
2017-03-14 11:30:23 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|