2018-09-07 14:26:04 -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.
|
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"path"
|
2019-01-18 12:31:36 -08:00
|
|
|
"sync"
|
2018-09-07 14:26:04 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
|
|
|
"github.com/prometheus/tsdb"
|
|
|
|
"github.com/prometheus/tsdb/labels"
|
|
|
|
"github.com/prometheus/tsdb/wal"
|
|
|
|
)
|
|
|
|
|
2019-02-15 07:47:41 -08:00
|
|
|
var defaultRetryInterval = 100 * time.Millisecond
|
|
|
|
var defaultRetries = 100
|
|
|
|
|
|
|
|
// retry executes f() n times at each interval until it returns true.
|
|
|
|
func retry(t *testing.T, interval time.Duration, n int, f func() bool) {
|
|
|
|
t.Helper()
|
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
for i := 0; i <= n; i++ {
|
|
|
|
if f() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
<-ticker.C
|
|
|
|
}
|
|
|
|
ticker.Stop()
|
|
|
|
t.Logf("function returned false")
|
|
|
|
}
|
|
|
|
|
2018-09-07 14:26:04 -07:00
|
|
|
type writeToMock struct {
|
|
|
|
samplesAppended int
|
2019-01-18 12:31:36 -08:00
|
|
|
seriesLock sync.Mutex
|
2018-09-07 14:26:04 -07:00
|
|
|
seriesSegmentIndexes map[uint64]int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wtm *writeToMock) Append(s []tsdb.RefSample) bool {
|
|
|
|
wtm.samplesAppended += len(s)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wtm *writeToMock) StoreSeries(series []tsdb.RefSeries, index int) {
|
2019-01-18 12:31:36 -08:00
|
|
|
wtm.seriesLock.Lock()
|
|
|
|
defer wtm.seriesLock.Unlock()
|
2019-02-15 07:47:41 -08:00
|
|
|
for _, s := range series {
|
|
|
|
wtm.seriesSegmentIndexes[s.Ref] = index
|
2018-09-07 14:26:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wtm *writeToMock) SeriesReset(index int) {
|
|
|
|
// Check for series that are in segments older than the checkpoint
|
|
|
|
// that were not also present in the checkpoint.
|
2019-01-18 12:31:36 -08:00
|
|
|
wtm.seriesLock.Lock()
|
|
|
|
defer wtm.seriesLock.Unlock()
|
2018-09-07 14:26:04 -07:00
|
|
|
for k, v := range wtm.seriesSegmentIndexes {
|
|
|
|
if v < index {
|
|
|
|
delete(wtm.seriesSegmentIndexes, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 12:31:36 -08:00
|
|
|
func (wtm *writeToMock) checkNumLabels() int {
|
|
|
|
wtm.seriesLock.Lock()
|
|
|
|
defer wtm.seriesLock.Unlock()
|
2019-02-15 07:47:41 -08:00
|
|
|
return len(wtm.seriesSegmentIndexes)
|
2019-01-18 12:31:36 -08:00
|
|
|
}
|
|
|
|
|
2018-09-07 14:26:04 -07:00
|
|
|
func newWriteToMock() *writeToMock {
|
|
|
|
return &writeToMock{
|
|
|
|
seriesSegmentIndexes: make(map[uint64]int),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 02:52:44 -07:00
|
|
|
func TestTailSamples(t *testing.T) {
|
2019-02-19 20:03:41 -08:00
|
|
|
pageSize := 32 * 1024
|
|
|
|
const seriesCount = 10
|
|
|
|
const samplesCount = 250
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "readCheckpoint")
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
wdir := path.Join(dir, "wal")
|
|
|
|
err = os.Mkdir(wdir, 0777)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
enc := tsdb.RecordEncoder{}
|
2019-07-03 06:19:47 -07:00
|
|
|
w, err := wal.NewSize(nil, nil, wdir, 128*pageSize, false)
|
2019-02-19 20:03:41 -08:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
// Write to the initial segment then checkpoint.
|
|
|
|
for i := 0; i < seriesCount; i++ {
|
|
|
|
ref := i + 100
|
|
|
|
series := enc.Series([]tsdb.RefSeries{
|
|
|
|
tsdb.RefSeries{
|
|
|
|
Ref: uint64(ref),
|
2019-05-03 06:11:28 -07:00
|
|
|
Labels: labels.Labels{labels.Label{Name: "__name__", Value: fmt.Sprintf("metric_%d", i)}},
|
2019-02-19 20:03:41 -08:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
testutil.Ok(t, w.Log(series))
|
|
|
|
|
|
|
|
for j := 0; j < samplesCount; j++ {
|
|
|
|
inner := rand.Intn(ref + 1)
|
|
|
|
sample := enc.Samples([]tsdb.RefSample{
|
|
|
|
tsdb.RefSample{
|
|
|
|
Ref: uint64(inner),
|
|
|
|
T: int64(now.UnixNano()) + 1,
|
|
|
|
V: float64(i),
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
testutil.Ok(t, w.Log(sample))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start read after checkpoint, no more data written.
|
|
|
|
first, last, err := w.Segments()
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
wt := newWriteToMock()
|
|
|
|
watcher := NewWALWatcher(nil, "", wt, dir)
|
|
|
|
watcher.startTime = now.UnixNano()
|
2019-04-23 01:49:17 -07:00
|
|
|
|
|
|
|
// Set the Watcher's metrics so they're not nil pointers.
|
|
|
|
watcher.setMetrics()
|
2019-02-19 20:03:41 -08:00
|
|
|
for i := first; i <= last; i++ {
|
|
|
|
segment, err := wal.OpenReadSegment(wal.SegmentName(watcher.walDir, i))
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer segment.Close()
|
|
|
|
|
2019-07-03 06:19:47 -07:00
|
|
|
reader := wal.NewLiveReader(nil, liveReaderMetrics, segment)
|
2019-02-19 20:03:41 -08:00
|
|
|
// Use tail true so we can ensure we got the right number of samples.
|
|
|
|
watcher.readSegment(reader, i, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedSeries := seriesCount
|
|
|
|
expectedSamples := seriesCount * samplesCount
|
|
|
|
retry(t, defaultRetryInterval, defaultRetries, func() bool {
|
|
|
|
return wt.checkNumLabels() >= expectedSeries
|
|
|
|
})
|
|
|
|
testutil.Equals(t, expectedSeries, wt.checkNumLabels())
|
|
|
|
testutil.Equals(t, expectedSamples, wt.samplesAppended)
|
|
|
|
}
|
|
|
|
|
2019-04-09 02:52:44 -07:00
|
|
|
func TestReadToEndNoCheckpoint(t *testing.T) {
|
2018-09-07 14:26:04 -07:00
|
|
|
pageSize := 32 * 1024
|
|
|
|
const seriesCount = 10
|
|
|
|
const samplesCount = 250
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "readToEnd_noCheckpoint")
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
wdir := path.Join(dir, "wal")
|
|
|
|
err = os.Mkdir(wdir, 0777)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
2019-07-03 06:19:47 -07:00
|
|
|
w, err := wal.NewSize(nil, nil, wdir, 128*pageSize, false)
|
2018-09-07 14:26:04 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
var recs [][]byte
|
|
|
|
|
|
|
|
enc := tsdb.RecordEncoder{}
|
|
|
|
|
|
|
|
for i := 0; i < seriesCount; i++ {
|
|
|
|
series := enc.Series([]tsdb.RefSeries{
|
|
|
|
tsdb.RefSeries{
|
|
|
|
Ref: uint64(i),
|
2019-05-03 06:11:28 -07:00
|
|
|
Labels: labels.Labels{labels.Label{Name: "__name__", Value: fmt.Sprintf("metric_%d", i)}},
|
2018-09-07 14:26:04 -07:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
recs = append(recs, series)
|
|
|
|
for j := 0; j < samplesCount; j++ {
|
|
|
|
sample := enc.Samples([]tsdb.RefSample{
|
|
|
|
tsdb.RefSample{
|
|
|
|
Ref: uint64(j),
|
|
|
|
T: int64(i),
|
|
|
|
V: float64(i),
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
recs = append(recs, sample)
|
|
|
|
|
|
|
|
// Randomly batch up records.
|
|
|
|
if rand.Intn(4) < 3 {
|
|
|
|
testutil.Ok(t, w.Log(recs...))
|
|
|
|
recs = recs[:0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
testutil.Ok(t, w.Log(recs...))
|
|
|
|
|
2019-01-18 12:31:36 -08:00
|
|
|
_, _, err = w.Segments()
|
2018-09-07 14:26:04 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
wt := newWriteToMock()
|
2019-02-15 01:54:01 -08:00
|
|
|
watcher := NewWALWatcher(nil, "", wt, dir)
|
2019-01-18 12:31:36 -08:00
|
|
|
go watcher.Start()
|
|
|
|
|
2019-02-15 07:47:41 -08:00
|
|
|
expected := seriesCount
|
|
|
|
retry(t, defaultRetryInterval, defaultRetries, func() bool {
|
|
|
|
return wt.checkNumLabels() >= expected
|
|
|
|
})
|
2019-01-18 12:31:36 -08:00
|
|
|
watcher.Stop()
|
2019-02-15 07:47:41 -08:00
|
|
|
testutil.Equals(t, expected, wt.checkNumLabels())
|
2018-09-07 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
2019-04-09 02:52:44 -07:00
|
|
|
func TestReadToEndWithCheckpoint(t *testing.T) {
|
2019-02-19 20:03:41 -08:00
|
|
|
segmentSize := 32 * 1024
|
|
|
|
// We need something similar to this # of series and samples
|
|
|
|
// in order to get enough segments for us to checkpoint.
|
2018-09-07 14:26:04 -07:00
|
|
|
const seriesCount = 10
|
|
|
|
const samplesCount = 250
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "readToEnd_withCheckpoint")
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
wdir := path.Join(dir, "wal")
|
|
|
|
err = os.Mkdir(wdir, 0777)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
enc := tsdb.RecordEncoder{}
|
2019-07-03 06:19:47 -07:00
|
|
|
w, err := wal.NewSize(nil, nil, wdir, segmentSize, false)
|
2018-09-07 14:26:04 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
2019-01-18 12:31:36 -08:00
|
|
|
// Write to the initial segment then checkpoint.
|
2019-02-19 20:03:41 -08:00
|
|
|
for i := 0; i < seriesCount; i++ {
|
2018-09-07 14:26:04 -07:00
|
|
|
ref := i + 100
|
|
|
|
series := enc.Series([]tsdb.RefSeries{
|
|
|
|
tsdb.RefSeries{
|
|
|
|
Ref: uint64(ref),
|
2019-05-03 06:11:28 -07:00
|
|
|
Labels: labels.Labels{labels.Label{Name: "__name__", Value: fmt.Sprintf("metric_%d", i)}},
|
2018-09-07 14:26:04 -07:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
testutil.Ok(t, w.Log(series))
|
|
|
|
|
2019-02-19 20:03:41 -08:00
|
|
|
for j := 0; j < samplesCount; j++ {
|
2018-09-07 14:26:04 -07:00
|
|
|
inner := rand.Intn(ref + 1)
|
|
|
|
sample := enc.Samples([]tsdb.RefSample{
|
|
|
|
tsdb.RefSample{
|
|
|
|
Ref: uint64(inner),
|
|
|
|
T: int64(i),
|
|
|
|
V: float64(i),
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
testutil.Ok(t, w.Log(sample))
|
|
|
|
}
|
|
|
|
}
|
2019-02-19 20:03:41 -08:00
|
|
|
|
|
|
|
tsdb.Checkpoint(w, 0, 1, func(x uint64) bool { return true }, 0)
|
|
|
|
w.Truncate(1)
|
2018-09-07 14:26:04 -07:00
|
|
|
|
2019-01-18 12:31:36 -08:00
|
|
|
// Write more records after checkpointing.
|
2019-02-19 20:03:41 -08:00
|
|
|
for i := 0; i < seriesCount; i++ {
|
2018-09-07 14:26:04 -07:00
|
|
|
series := enc.Series([]tsdb.RefSeries{
|
|
|
|
tsdb.RefSeries{
|
|
|
|
Ref: uint64(i),
|
2019-05-03 06:11:28 -07:00
|
|
|
Labels: labels.Labels{labels.Label{Name: "__name__", Value: fmt.Sprintf("metric_%d", i)}},
|
2018-09-07 14:26:04 -07:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
testutil.Ok(t, w.Log(series))
|
|
|
|
|
2019-02-19 20:03:41 -08:00
|
|
|
for j := 0; j < samplesCount; j++ {
|
2018-09-07 14:26:04 -07:00
|
|
|
sample := enc.Samples([]tsdb.RefSample{
|
|
|
|
tsdb.RefSample{
|
|
|
|
Ref: uint64(j),
|
|
|
|
T: int64(i),
|
|
|
|
V: float64(i),
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
testutil.Ok(t, w.Log(sample))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 12:31:36 -08:00
|
|
|
_, _, err = w.Segments()
|
2018-09-07 14:26:04 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
wt := newWriteToMock()
|
2019-02-15 01:54:01 -08:00
|
|
|
watcher := NewWALWatcher(nil, "", wt, dir)
|
2019-01-18 12:31:36 -08:00
|
|
|
go watcher.Start()
|
|
|
|
|
2019-02-19 20:03:41 -08:00
|
|
|
expected := seriesCount * 2
|
2019-02-15 07:47:41 -08:00
|
|
|
retry(t, defaultRetryInterval, defaultRetries, func() bool {
|
|
|
|
return wt.checkNumLabels() >= expected
|
|
|
|
})
|
2019-01-18 12:31:36 -08:00
|
|
|
watcher.Stop()
|
2019-02-15 07:47:41 -08:00
|
|
|
testutil.Equals(t, expected, wt.checkNumLabels())
|
2018-09-07 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
2019-04-09 02:52:44 -07:00
|
|
|
func TestReadCheckpoint(t *testing.T) {
|
2018-09-07 14:26:04 -07:00
|
|
|
pageSize := 32 * 1024
|
|
|
|
const seriesCount = 10
|
|
|
|
const samplesCount = 250
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "readCheckpoint")
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
wdir := path.Join(dir, "wal")
|
|
|
|
err = os.Mkdir(wdir, 0777)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
os.Create(wal.SegmentName(wdir, 30))
|
|
|
|
|
|
|
|
enc := tsdb.RecordEncoder{}
|
2019-07-03 06:19:47 -07:00
|
|
|
w, err := wal.NewSize(nil, nil, wdir, 128*pageSize, false)
|
2018-09-07 14:26:04 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
2019-01-18 12:31:36 -08:00
|
|
|
// Write to the initial segment then checkpoint.
|
2019-02-19 20:03:41 -08:00
|
|
|
for i := 0; i < seriesCount; i++ {
|
2018-09-07 14:26:04 -07:00
|
|
|
ref := i + 100
|
|
|
|
series := enc.Series([]tsdb.RefSeries{
|
|
|
|
tsdb.RefSeries{
|
|
|
|
Ref: uint64(ref),
|
2019-05-03 06:11:28 -07:00
|
|
|
Labels: labels.Labels{labels.Label{Name: "__name__", Value: fmt.Sprintf("metric_%d", i)}},
|
2018-09-07 14:26:04 -07:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
testutil.Ok(t, w.Log(series))
|
|
|
|
|
2019-02-19 20:03:41 -08:00
|
|
|
for j := 0; j < samplesCount; j++ {
|
2018-09-07 14:26:04 -07:00
|
|
|
inner := rand.Intn(ref + 1)
|
|
|
|
sample := enc.Samples([]tsdb.RefSample{
|
|
|
|
tsdb.RefSample{
|
|
|
|
Ref: uint64(inner),
|
|
|
|
T: int64(i),
|
|
|
|
V: float64(i),
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
testutil.Ok(t, w.Log(sample))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tsdb.Checkpoint(w, 30, 31, func(x uint64) bool { return true }, 0)
|
|
|
|
w.Truncate(32)
|
|
|
|
|
2019-01-18 12:31:36 -08:00
|
|
|
// Start read after checkpoint, no more data written.
|
|
|
|
_, _, err = w.Segments()
|
2018-09-07 14:26:04 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
wt := newWriteToMock()
|
2019-02-15 01:54:01 -08:00
|
|
|
watcher := NewWALWatcher(nil, "", wt, dir)
|
2019-02-19 20:03:41 -08:00
|
|
|
// watcher.
|
2019-01-18 12:31:36 -08:00
|
|
|
go watcher.Start()
|
|
|
|
|
2019-02-19 20:03:41 -08:00
|
|
|
expectedSeries := seriesCount
|
2019-02-15 07:47:41 -08:00
|
|
|
retry(t, defaultRetryInterval, defaultRetries, func() bool {
|
2019-02-19 20:03:41 -08:00
|
|
|
return wt.checkNumLabels() >= expectedSeries
|
2019-02-15 07:47:41 -08:00
|
|
|
})
|
2019-01-18 12:31:36 -08:00
|
|
|
watcher.Stop()
|
2019-02-19 20:03:41 -08:00
|
|
|
testutil.Equals(t, expectedSeries, wt.checkNumLabels())
|
2018-09-07 14:26:04 -07:00
|
|
|
}
|
|
|
|
|
2019-04-09 02:52:44 -07:00
|
|
|
func TestReadCheckpointMultipleSegments(t *testing.T) {
|
|
|
|
pageSize := 32 * 1024
|
|
|
|
|
|
|
|
const segments = 1
|
|
|
|
const seriesCount = 20
|
|
|
|
const samplesCount = 300
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "readCheckpoint")
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
wdir := path.Join(dir, "wal")
|
|
|
|
err = os.Mkdir(wdir, 0777)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
enc := tsdb.RecordEncoder{}
|
2019-07-03 06:19:47 -07:00
|
|
|
w, err := wal.NewSize(nil, nil, wdir, pageSize, false)
|
2019-04-09 02:52:44 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
// Write a bunch of data.
|
|
|
|
for i := 0; i < segments; i++ {
|
|
|
|
for j := 0; j < seriesCount; j++ {
|
|
|
|
ref := j + (i * 100)
|
|
|
|
series := enc.Series([]tsdb.RefSeries{
|
|
|
|
tsdb.RefSeries{
|
|
|
|
Ref: uint64(ref),
|
2019-05-03 06:11:28 -07:00
|
|
|
Labels: labels.Labels{labels.Label{Name: "__name__", Value: fmt.Sprintf("metric_%d", j)}},
|
2019-04-09 02:52:44 -07:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
testutil.Ok(t, w.Log(series))
|
|
|
|
|
|
|
|
for k := 0; k < samplesCount; k++ {
|
|
|
|
inner := rand.Intn(ref + 1)
|
|
|
|
sample := enc.Samples([]tsdb.RefSample{
|
|
|
|
tsdb.RefSample{
|
|
|
|
Ref: uint64(inner),
|
|
|
|
T: int64(i),
|
|
|
|
V: float64(i),
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
testutil.Ok(t, w.Log(sample))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point we should have at least 6 segments, lets create a checkpoint dir of the first 5.
|
|
|
|
checkpointDir := dir + "/wal/checkpoint.000004"
|
|
|
|
err = os.Mkdir(checkpointDir, 0777)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
for i := 0; i <= 4; i++ {
|
|
|
|
err := os.Rename(wal.SegmentName(dir+"/wal", i), wal.SegmentName(checkpointDir, i))
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wt := newWriteToMock()
|
|
|
|
watcher := NewWALWatcher(nil, "", wt, dir)
|
|
|
|
watcher.maxSegment = -1
|
|
|
|
|
2019-04-23 01:49:17 -07:00
|
|
|
// Set the Watcher's metrics so they're not nil pointers.
|
|
|
|
watcher.setMetrics()
|
|
|
|
|
2019-04-09 02:52:44 -07:00
|
|
|
lastCheckpoint, _, err := tsdb.LastCheckpoint(watcher.walDir)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
err = watcher.readCheckpoint(lastCheckpoint)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckpointSeriesReset(t *testing.T) {
|
2019-02-19 20:03:41 -08:00
|
|
|
segmentSize := 32 * 1024
|
|
|
|
// We need something similar to this # of series and samples
|
|
|
|
// in order to get enough segments for us to checkpoint.
|
2019-04-09 02:52:44 -07:00
|
|
|
const seriesCount = 20
|
|
|
|
const samplesCount = 350
|
2018-09-07 14:26:04 -07:00
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "seriesReset")
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
wdir := path.Join(dir, "wal")
|
|
|
|
err = os.Mkdir(wdir, 0777)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
enc := tsdb.RecordEncoder{}
|
2019-07-03 06:19:47 -07:00
|
|
|
w, err := wal.NewSize(nil, nil, wdir, segmentSize, false)
|
2018-09-07 14:26:04 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
2019-01-18 12:31:36 -08:00
|
|
|
// Write to the initial segment, then checkpoint later.
|
2019-02-19 20:03:41 -08:00
|
|
|
for i := 0; i < seriesCount; i++ {
|
2018-09-07 14:26:04 -07:00
|
|
|
ref := i + 100
|
|
|
|
series := enc.Series([]tsdb.RefSeries{
|
|
|
|
tsdb.RefSeries{
|
|
|
|
Ref: uint64(ref),
|
2019-05-03 06:11:28 -07:00
|
|
|
Labels: labels.Labels{labels.Label{Name: "__name__", Value: fmt.Sprintf("metric_%d", i)}},
|
2018-09-07 14:26:04 -07:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
testutil.Ok(t, w.Log(series))
|
|
|
|
|
2019-02-19 20:03:41 -08:00
|
|
|
for j := 0; j < samplesCount; j++ {
|
2018-09-07 14:26:04 -07:00
|
|
|
inner := rand.Intn(ref + 1)
|
|
|
|
sample := enc.Samples([]tsdb.RefSample{
|
|
|
|
tsdb.RefSample{
|
|
|
|
Ref: uint64(inner),
|
|
|
|
T: int64(i),
|
|
|
|
V: float64(i),
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
testutil.Ok(t, w.Log(sample))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 12:31:36 -08:00
|
|
|
_, _, err = w.Segments()
|
2018-09-07 14:26:04 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
wt := newWriteToMock()
|
2019-02-15 01:54:01 -08:00
|
|
|
watcher := NewWALWatcher(nil, "", wt, dir)
|
2019-02-19 20:03:41 -08:00
|
|
|
watcher.maxSegment = -1
|
2019-01-18 12:31:36 -08:00
|
|
|
go watcher.Start()
|
|
|
|
|
2019-02-19 20:03:41 -08:00
|
|
|
expected := seriesCount
|
2019-02-15 07:47:41 -08:00
|
|
|
retry(t, defaultRetryInterval, defaultRetries, func() bool {
|
|
|
|
return wt.checkNumLabels() >= expected
|
|
|
|
})
|
2019-02-19 20:03:41 -08:00
|
|
|
testutil.Equals(t, seriesCount, wt.checkNumLabels())
|
2018-09-07 14:26:04 -07:00
|
|
|
|
2019-04-09 02:52:44 -07:00
|
|
|
_, err = tsdb.Checkpoint(w, 2, 4, func(x uint64) bool { return true }, 0)
|
2018-09-07 14:26:04 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
2019-04-09 02:52:44 -07:00
|
|
|
err = w.Truncate(5)
|
|
|
|
testutil.Ok(t, err)
|
2018-09-07 14:26:04 -07:00
|
|
|
|
2019-02-19 20:03:41 -08:00
|
|
|
_, cpi, err := tsdb.LastCheckpoint(path.Join(dir, "wal"))
|
2018-09-07 14:26:04 -07:00
|
|
|
testutil.Ok(t, err)
|
2019-02-19 20:03:41 -08:00
|
|
|
err = watcher.garbageCollectSeries(cpi + 1)
|
2019-01-18 12:31:36 -08:00
|
|
|
testutil.Ok(t, err)
|
2019-04-09 02:52:44 -07:00
|
|
|
|
|
|
|
watcher.Stop()
|
2019-02-19 20:03:41 -08:00
|
|
|
// If you modify the checkpoint and truncate segment #'s run the test to see how
|
|
|
|
// many series records you end up with and change the last Equals check accordingly
|
|
|
|
// or modify the Equals to Assert(len(wt.seriesLabels) < seriesCount*10)
|
2019-04-09 02:52:44 -07:00
|
|
|
testutil.Equals(t, 14, wt.checkNumLabels())
|
2019-01-18 12:31:36 -08:00
|
|
|
}
|