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.
|
|
|
|
|
2017-01-05 06:13:01 -08:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
2019-06-19 06:46:24 -07:00
|
|
|
"fmt"
|
2018-05-17 06:04:32 -07:00
|
|
|
"io/ioutil"
|
2018-12-04 02:30:49 -08:00
|
|
|
"math"
|
2017-09-04 06:07:30 -07:00
|
|
|
"math/rand"
|
2018-05-17 06:04:32 -07:00
|
|
|
"os"
|
2019-01-08 09:08:41 -08:00
|
|
|
"path"
|
2019-06-14 08:39:22 -07:00
|
|
|
"path/filepath"
|
2018-09-17 04:28:55 -07:00
|
|
|
"sort"
|
2019-10-02 23:19:55 -07:00
|
|
|
"strconv"
|
2017-01-05 06:13:01 -08:00
|
|
|
"testing"
|
|
|
|
|
2019-06-14 08:39:22 -07:00
|
|
|
"github.com/pkg/errors"
|
2018-12-18 02:24:56 -08:00
|
|
|
prom_testutil "github.com/prometheus/client_golang/prometheus/testutil"
|
2019-11-18 11:53:33 -08:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2019-08-13 01:34:14 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb/chunkenc"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/chunks"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/index"
|
2019-09-19 02:15:41 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb/record"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/tombstones"
|
2019-08-13 01:34:14 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb/tsdbutil"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/wal"
|
2019-08-14 02:07:02 -07:00
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2017-01-05 06:13:01 -08:00
|
|
|
)
|
|
|
|
|
2017-02-08 16:13:16 -08:00
|
|
|
func BenchmarkCreateSeries(b *testing.B) {
|
2019-06-14 05:30:49 -07:00
|
|
|
series := genSeries(b.N, 10, 0, 0)
|
2017-02-08 16:13:16 -08:00
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
h, err := NewHead(nil, nil, nil, 10000)
|
2018-09-20 23:17:59 -07:00
|
|
|
testutil.Ok(b, err)
|
2017-11-10 12:19:39 -08:00
|
|
|
defer h.Close()
|
2017-02-08 16:13:16 -08:00
|
|
|
|
2017-08-30 09:34:54 -07:00
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
2017-02-08 16:13:16 -08:00
|
|
|
|
2019-06-14 05:30:49 -07:00
|
|
|
for _, s := range series {
|
|
|
|
h.getOrCreate(s.Labels().Hash(), s.Labels())
|
2017-08-30 09:34:54 -07:00
|
|
|
}
|
2017-02-08 16:13:16 -08:00
|
|
|
}
|
2017-02-14 15:54:52 -08:00
|
|
|
|
2018-05-17 06:04:32 -07:00
|
|
|
func populateTestWAL(t testing.TB, w *wal.WAL, recs []interface{}) {
|
2019-09-19 02:15:41 -07:00
|
|
|
var enc record.Encoder
|
2018-05-17 06:04:32 -07:00
|
|
|
for _, r := range recs {
|
|
|
|
switch v := r.(type) {
|
2019-09-19 02:15:41 -07:00
|
|
|
case []record.RefSeries:
|
2018-05-17 06:04:32 -07:00
|
|
|
testutil.Ok(t, w.Log(enc.Series(v, nil)))
|
2019-09-19 02:15:41 -07:00
|
|
|
case []record.RefSample:
|
2018-05-17 06:04:32 -07:00
|
|
|
testutil.Ok(t, w.Log(enc.Samples(v, nil)))
|
2019-09-19 02:15:41 -07:00
|
|
|
case []tombstones.Stone:
|
2018-05-17 06:04:32 -07:00
|
|
|
testutil.Ok(t, w.Log(enc.Tombstones(v, nil)))
|
2017-09-19 01:20:19 -07:00
|
|
|
}
|
|
|
|
}
|
2018-05-17 06:04:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func readTestWAL(t testing.TB, dir string) (recs []interface{}) {
|
|
|
|
sr, err := wal.NewSegmentsReader(dir)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer sr.Close()
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
var dec record.Decoder
|
2018-05-17 06:04:32 -07:00
|
|
|
r := wal.NewReader(sr)
|
|
|
|
|
|
|
|
for r.Next() {
|
|
|
|
rec := r.Record()
|
|
|
|
|
|
|
|
switch dec.Type(rec) {
|
2019-09-19 02:15:41 -07:00
|
|
|
case record.Series:
|
2018-05-17 06:04:32 -07:00
|
|
|
series, err := dec.Series(rec, nil)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
recs = append(recs, series)
|
2019-09-19 02:15:41 -07:00
|
|
|
case record.Samples:
|
2018-05-17 06:04:32 -07:00
|
|
|
samples, err := dec.Samples(rec, nil)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
recs = append(recs, samples)
|
2019-09-19 02:15:41 -07:00
|
|
|
case record.Tombstones:
|
2018-05-17 06:04:32 -07:00
|
|
|
tstones, err := dec.Tombstones(rec, nil)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
recs = append(recs, tstones)
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown record type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
testutil.Ok(t, r.Err())
|
|
|
|
return recs
|
2017-09-19 01:20:19 -07:00
|
|
|
}
|
|
|
|
|
2019-10-02 23:19:55 -07:00
|
|
|
func BenchmarkLoadWAL(b *testing.B) {
|
|
|
|
cases := []struct {
|
|
|
|
// Total series is (batches*seriesPerBatch).
|
|
|
|
batches int
|
|
|
|
seriesPerBatch int
|
|
|
|
samplesPerSeries int
|
|
|
|
}{
|
2019-11-07 08:26:45 -08:00
|
|
|
{ // Less series and more samples. 2 hour WAL with 1 second scrape interval.
|
2019-10-02 23:19:55 -07:00
|
|
|
batches: 10,
|
|
|
|
seriesPerBatch: 100,
|
2019-11-07 08:26:45 -08:00
|
|
|
samplesPerSeries: 7200,
|
2019-10-02 23:19:55 -07:00
|
|
|
},
|
|
|
|
{ // More series and less samples.
|
|
|
|
batches: 10,
|
|
|
|
seriesPerBatch: 10000,
|
2019-11-07 08:26:45 -08:00
|
|
|
samplesPerSeries: 50,
|
2019-10-02 23:19:55 -07:00
|
|
|
},
|
|
|
|
{ // In between.
|
|
|
|
batches: 10,
|
|
|
|
seriesPerBatch: 1000,
|
2019-11-07 08:26:45 -08:00
|
|
|
samplesPerSeries: 480,
|
2019-10-02 23:19:55 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
labelsPerSeries := 5
|
|
|
|
for _, c := range cases {
|
|
|
|
b.Run(fmt.Sprintf("batches=%d,seriesPerBatch=%d,samplesPerSeries=%d", c.batches, c.seriesPerBatch, c.samplesPerSeries),
|
|
|
|
func(b *testing.B) {
|
|
|
|
dir, err := ioutil.TempDir("", "test_load_wal")
|
|
|
|
testutil.Ok(b, err)
|
|
|
|
defer func() {
|
|
|
|
testutil.Ok(b, os.RemoveAll(dir))
|
|
|
|
}()
|
|
|
|
|
|
|
|
w, err := wal.New(nil, nil, dir, false)
|
|
|
|
testutil.Ok(b, err)
|
|
|
|
|
|
|
|
// Write series.
|
|
|
|
refSeries := make([]record.RefSeries, 0, c.seriesPerBatch)
|
|
|
|
for k := 0; k < c.batches; k++ {
|
|
|
|
refSeries = refSeries[:0]
|
|
|
|
for i := k * c.seriesPerBatch; i < (k+1)*c.seriesPerBatch; i++ {
|
|
|
|
lbls := make(map[string]string, labelsPerSeries)
|
|
|
|
lbls[defaultLabelName] = strconv.Itoa(i)
|
|
|
|
for j := 1; len(lbls) < labelsPerSeries; j++ {
|
|
|
|
lbls[defaultLabelName+strconv.Itoa(j)] = defaultLabelValue + strconv.Itoa(j)
|
|
|
|
}
|
|
|
|
refSeries = append(refSeries, record.RefSeries{Ref: uint64(i) * 100, Labels: labels.FromMap(lbls)})
|
|
|
|
}
|
|
|
|
populateTestWAL(b, w, []interface{}{refSeries})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write samples.
|
|
|
|
refSamples := make([]record.RefSample, 0, c.seriesPerBatch)
|
|
|
|
for i := 0; i < c.samplesPerSeries; i++ {
|
|
|
|
for j := 0; j < c.batches; j++ {
|
|
|
|
refSamples = refSamples[:0]
|
|
|
|
for k := j * c.seriesPerBatch; k < (j+1)*c.seriesPerBatch; k++ {
|
|
|
|
refSamples = append(refSamples, record.RefSample{
|
|
|
|
Ref: uint64(k) * 100,
|
|
|
|
T: int64(i) * 10,
|
|
|
|
V: float64(i) * 100,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
populateTestWAL(b, w, []interface{}{refSamples})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
// Load the WAL.
|
2019-11-07 08:26:45 -08:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
h, err := NewHead(nil, nil, w, 1000)
|
|
|
|
testutil.Ok(b, err)
|
|
|
|
h.Init(0)
|
|
|
|
}
|
2019-10-02 23:19:55 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 01:20:19 -07:00
|
|
|
func TestHead_ReadWAL(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) {
|
|
|
|
entries := []interface{}{
|
2019-09-19 02:15:41 -07:00
|
|
|
[]record.RefSeries{
|
2019-06-19 06:46:24 -07:00
|
|
|
{Ref: 10, Labels: labels.FromStrings("a", "1")},
|
|
|
|
{Ref: 11, Labels: labels.FromStrings("a", "2")},
|
|
|
|
{Ref: 100, Labels: labels.FromStrings("a", "3")},
|
|
|
|
},
|
2019-09-19 02:15:41 -07:00
|
|
|
[]record.RefSample{
|
2019-06-19 06:46:24 -07:00
|
|
|
{Ref: 0, T: 99, V: 1},
|
|
|
|
{Ref: 10, T: 100, V: 2},
|
|
|
|
{Ref: 100, T: 100, V: 3},
|
|
|
|
},
|
2019-09-19 02:15:41 -07:00
|
|
|
[]record.RefSeries{
|
2019-06-19 06:46:24 -07:00
|
|
|
{Ref: 50, Labels: labels.FromStrings("a", "4")},
|
|
|
|
// This series has two refs pointing to it.
|
|
|
|
{Ref: 101, Labels: labels.FromStrings("a", "3")},
|
|
|
|
},
|
2019-09-19 02:15:41 -07:00
|
|
|
[]record.RefSample{
|
2019-06-19 06:46:24 -07:00
|
|
|
{Ref: 10, T: 101, V: 5},
|
|
|
|
{Ref: 50, T: 101, V: 6},
|
|
|
|
{Ref: 101, T: 101, V: 7},
|
|
|
|
},
|
2019-09-19 02:15:41 -07:00
|
|
|
[]tombstones.Stone{
|
|
|
|
{Ref: 0, Intervals: []tombstones.Interval{{Mint: 99, Maxt: 101}}},
|
2019-06-19 06:46:24 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
dir, err := ioutil.TempDir("", "test_read_wal")
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2018-05-17 06:04:32 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
w, err := wal.New(nil, nil, dir, compress)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer w.Close()
|
|
|
|
populateTestWAL(t, w, entries)
|
2017-09-19 01:20:19 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
head, err := NewHead(nil, nil, w, 1000)
|
|
|
|
testutil.Ok(t, err)
|
2017-09-19 01:20:19 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
testutil.Ok(t, head.Init(math.MinInt64))
|
|
|
|
testutil.Equals(t, uint64(101), head.lastSeriesID)
|
2017-09-19 01:20:19 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
s10 := head.series.getByID(10)
|
|
|
|
s11 := head.series.getByID(11)
|
|
|
|
s50 := head.series.getByID(50)
|
|
|
|
s100 := head.series.getByID(100)
|
2017-09-19 01:20:19 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
testutil.Equals(t, labels.FromStrings("a", "1"), s10.lset)
|
2019-09-30 08:54:55 -07:00
|
|
|
testutil.Equals(t, (*memSeries)(nil), s11) // Series without samples should be garbage collected at head.Init().
|
2019-06-19 06:46:24 -07:00
|
|
|
testutil.Equals(t, labels.FromStrings("a", "4"), s50.lset)
|
|
|
|
testutil.Equals(t, labels.FromStrings("a", "3"), s100.lset)
|
2017-09-19 01:20:19 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
expandChunk := func(c chunkenc.Iterator) (x []sample) {
|
|
|
|
for c.Next() {
|
|
|
|
t, v := c.At()
|
|
|
|
x = append(x, sample{t: t, v: v})
|
|
|
|
}
|
|
|
|
testutil.Ok(t, c.Err())
|
|
|
|
return x
|
|
|
|
}
|
2019-07-09 02:49:34 -07:00
|
|
|
testutil.Equals(t, []sample{{100, 2}, {101, 5}}, expandChunk(s10.iterator(0, nil)))
|
|
|
|
testutil.Equals(t, []sample{{101, 6}}, expandChunk(s50.iterator(0, nil)))
|
|
|
|
testutil.Equals(t, []sample{{100, 3}, {101, 7}}, expandChunk(s100.iterator(0, nil)))
|
2019-06-19 06:46:24 -07:00
|
|
|
})
|
2017-09-19 01:20:19 -07:00
|
|
|
}
|
2019-06-06 06:28:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHead_WALMultiRef(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "test_wal_multi_ref")
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
w, err := wal.New(nil, nil, dir, false)
|
2019-06-06 06:28:54 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
head, err := NewHead(nil, nil, w, 1000)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
testutil.Ok(t, head.Init(0))
|
|
|
|
app := head.Appender()
|
|
|
|
ref1, err := app.Add(labels.FromStrings("foo", "bar"), 100, 1)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Ok(t, app.Commit())
|
|
|
|
|
|
|
|
testutil.Ok(t, head.Truncate(200))
|
|
|
|
|
|
|
|
app = head.Appender()
|
|
|
|
ref2, err := app.Add(labels.FromStrings("foo", "bar"), 300, 2)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Ok(t, app.Commit())
|
|
|
|
|
|
|
|
if ref1 == ref2 {
|
|
|
|
t.Fatal("Refs are the same")
|
|
|
|
}
|
|
|
|
testutil.Ok(t, head.Close())
|
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
w, err = wal.New(nil, nil, dir, false)
|
2019-06-06 06:28:54 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
head, err = NewHead(nil, nil, w, 1000)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Ok(t, head.Init(0))
|
|
|
|
defer head.Close()
|
|
|
|
|
|
|
|
q, err := NewBlockQuerier(head, 0, 300)
|
|
|
|
testutil.Ok(t, err)
|
2019-11-18 11:53:33 -08:00
|
|
|
series := query(t, q, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar"))
|
2019-06-06 06:28:54 -07:00
|
|
|
testutil.Equals(t, map[string][]tsdbutil.Sample{`{foo="bar"}`: {sample{100, 1}, sample{300, 2}}}, series)
|
2017-09-19 01:20:19 -07:00
|
|
|
}
|
|
|
|
|
2017-09-01 05:38:49 -07:00
|
|
|
func TestHead_Truncate(t *testing.T) {
|
|
|
|
h, err := NewHead(nil, nil, nil, 1000)
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Ok(t, err)
|
2017-11-10 12:19:39 -08:00
|
|
|
defer h.Close()
|
2017-09-01 05:38:49 -07:00
|
|
|
|
|
|
|
h.initTime(0)
|
|
|
|
|
2017-09-18 03:28:56 -07:00
|
|
|
s1, _ := h.getOrCreate(1, labels.FromStrings("a", "1", "b", "1"))
|
|
|
|
s2, _ := h.getOrCreate(2, labels.FromStrings("a", "2", "b", "1"))
|
|
|
|
s3, _ := h.getOrCreate(3, labels.FromStrings("a", "1", "b", "2"))
|
|
|
|
s4, _ := h.getOrCreate(4, labels.FromStrings("a", "2", "b", "2", "c", "1"))
|
2017-09-01 05:38:49 -07:00
|
|
|
|
|
|
|
s1.chunks = []*memChunk{
|
|
|
|
{minTime: 0, maxTime: 999},
|
|
|
|
{minTime: 1000, maxTime: 1999},
|
|
|
|
{minTime: 2000, maxTime: 2999},
|
|
|
|
}
|
|
|
|
s2.chunks = []*memChunk{
|
|
|
|
{minTime: 1000, maxTime: 1999},
|
|
|
|
{minTime: 2000, maxTime: 2999},
|
|
|
|
{minTime: 3000, maxTime: 3999},
|
|
|
|
}
|
|
|
|
s3.chunks = []*memChunk{
|
|
|
|
{minTime: 0, maxTime: 999},
|
|
|
|
{minTime: 1000, maxTime: 1999},
|
|
|
|
}
|
|
|
|
s4.chunks = []*memChunk{}
|
|
|
|
|
2017-10-04 04:28:07 -07:00
|
|
|
// Truncation need not be aligned.
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Ok(t, h.Truncate(1))
|
2017-09-01 05:38:49 -07:00
|
|
|
|
2017-12-13 12:58:21 -08:00
|
|
|
testutil.Ok(t, h.Truncate(2000))
|
2017-09-01 05:38:49 -07:00
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, []*memChunk{
|
2017-09-01 05:38:49 -07:00
|
|
|
{minTime: 2000, maxTime: 2999},
|
2017-09-05 02:45:18 -07:00
|
|
|
}, h.series.getByID(s1.ref).chunks)
|
2017-09-01 05:38:49 -07:00
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, []*memChunk{
|
2017-09-01 05:38:49 -07:00
|
|
|
{minTime: 2000, maxTime: 2999},
|
|
|
|
{minTime: 3000, maxTime: 3999},
|
2017-09-05 02:45:18 -07:00
|
|
|
}, h.series.getByID(s2.ref).chunks)
|
2017-09-01 05:38:49 -07:00
|
|
|
|
2017-12-08 13:42:08 -08:00
|
|
|
testutil.Assert(t, h.series.getByID(s3.ref) == nil, "")
|
|
|
|
testutil.Assert(t, h.series.getByID(s4.ref) == nil, "")
|
2017-09-01 05:38:49 -07:00
|
|
|
|
2017-11-30 06:34:49 -08:00
|
|
|
postingsA1, _ := index.ExpandPostings(h.postings.Get("a", "1"))
|
|
|
|
postingsA2, _ := index.ExpandPostings(h.postings.Get("a", "2"))
|
|
|
|
postingsB1, _ := index.ExpandPostings(h.postings.Get("b", "1"))
|
|
|
|
postingsB2, _ := index.ExpandPostings(h.postings.Get("b", "2"))
|
|
|
|
postingsC1, _ := index.ExpandPostings(h.postings.Get("c", "1"))
|
|
|
|
postingsAll, _ := index.ExpandPostings(h.postings.Get("", ""))
|
2017-09-01 05:38:49 -07:00
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, []uint64{s1.ref}, postingsA1)
|
|
|
|
testutil.Equals(t, []uint64{s2.ref}, postingsA2)
|
|
|
|
testutil.Equals(t, []uint64{s1.ref, s2.ref}, postingsB1)
|
|
|
|
testutil.Equals(t, []uint64{s1.ref, s2.ref}, postingsAll)
|
2017-12-08 13:42:08 -08:00
|
|
|
testutil.Assert(t, postingsB2 == nil, "")
|
|
|
|
testutil.Assert(t, postingsC1 == nil, "")
|
2017-09-01 05:38:49 -07:00
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, map[string]struct{}{
|
2018-05-07 05:39:54 -07:00
|
|
|
"": {}, // from 'all' postings list
|
|
|
|
"a": {},
|
|
|
|
"b": {},
|
|
|
|
"1": {},
|
|
|
|
"2": {},
|
2017-09-01 05:38:49 -07:00
|
|
|
}, h.symbols)
|
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, map[string]stringset{
|
2018-05-07 05:39:54 -07:00
|
|
|
"a": {"1": struct{}{}, "2": struct{}{}},
|
|
|
|
"b": {"1": struct{}{}},
|
|
|
|
"": {"": struct{}{}},
|
2017-09-01 05:38:49 -07:00
|
|
|
}, h.values)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate various behaviors brought on by firstChunkID accounting for
|
|
|
|
// garbage collected chunks.
|
|
|
|
func TestMemSeries_truncateChunks(t *testing.T) {
|
|
|
|
s := newMemSeries(labels.FromStrings("a", "b"), 1, 2000)
|
|
|
|
|
|
|
|
for i := 0; i < 4000; i += 5 {
|
|
|
|
ok, _ := s.append(int64(i), float64(i))
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, ok == true, "sample append failed")
|
2017-09-01 05:38:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that truncate removes half of the chunks and afterwards
|
|
|
|
// that the ID of the last chunk still gives us the same chunk afterwards.
|
|
|
|
countBefore := len(s.chunks)
|
|
|
|
lastID := s.chunkID(countBefore - 1)
|
|
|
|
lastChunk := s.chunk(lastID)
|
|
|
|
|
2017-12-08 13:42:08 -08:00
|
|
|
testutil.Assert(t, s.chunk(0) != nil, "")
|
|
|
|
testutil.Assert(t, lastChunk != nil, "")
|
2017-09-01 05:38:49 -07:00
|
|
|
|
|
|
|
s.truncateChunksBefore(2000)
|
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, int64(2000), s.chunks[0].minTime)
|
2017-12-08 13:42:08 -08:00
|
|
|
testutil.Assert(t, s.chunk(0) == nil, "first chunks not gone")
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Equals(t, countBefore/2, len(s.chunks))
|
|
|
|
testutil.Equals(t, lastChunk, s.chunk(lastID))
|
2017-09-01 05:38:49 -07:00
|
|
|
|
|
|
|
// Validate that the series' sample buffer is applied correctly to the last chunk
|
|
|
|
// after truncation.
|
2019-07-09 02:49:34 -07:00
|
|
|
it1 := s.iterator(s.chunkID(len(s.chunks)-1), nil)
|
2017-09-01 05:38:49 -07:00
|
|
|
_, ok := it1.(*memSafeIterator)
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, ok == true, "")
|
2017-09-01 05:38:49 -07:00
|
|
|
|
2019-07-09 02:49:34 -07:00
|
|
|
it2 := s.iterator(s.chunkID(len(s.chunks)-2), nil)
|
2017-09-01 05:38:49 -07:00
|
|
|
_, ok = it2.(*memSafeIterator)
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, ok == false, "non-last chunk incorrectly wrapped with sample buffer")
|
2017-09-01 05:38:49 -07:00
|
|
|
}
|
|
|
|
|
2018-02-07 05:43:21 -08:00
|
|
|
func TestHeadDeleteSeriesWithoutSamples(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) {
|
|
|
|
entries := []interface{}{
|
2019-09-19 02:15:41 -07:00
|
|
|
[]record.RefSeries{
|
2019-06-19 06:46:24 -07:00
|
|
|
{Ref: 10, Labels: labels.FromStrings("a", "1")},
|
|
|
|
},
|
2019-09-19 02:15:41 -07:00
|
|
|
[]record.RefSample{},
|
|
|
|
[]record.RefSeries{
|
2019-06-19 06:46:24 -07:00
|
|
|
{Ref: 50, Labels: labels.FromStrings("a", "2")},
|
|
|
|
},
|
2019-09-19 02:15:41 -07:00
|
|
|
[]record.RefSample{
|
2019-06-19 06:46:24 -07:00
|
|
|
{Ref: 50, T: 80, V: 1},
|
|
|
|
{Ref: 50, T: 90, V: 1},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
dir, err := ioutil.TempDir("", "test_delete_series")
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2018-05-17 06:04:32 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
w, err := wal.New(nil, nil, dir, compress)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer w.Close()
|
|
|
|
populateTestWAL(t, w, entries)
|
2018-02-07 05:43:21 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
head, err := NewHead(nil, nil, w, 1000)
|
|
|
|
testutil.Ok(t, err)
|
2018-02-07 05:43:21 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
testutil.Ok(t, head.Init(math.MinInt64))
|
2018-02-07 05:43:21 -08:00
|
|
|
|
2019-11-18 11:53:33 -08:00
|
|
|
testutil.Ok(t, head.Delete(0, 100, labels.MustNewMatcher(labels.MatchEqual, "a", "1")))
|
2019-06-19 06:46:24 -07:00
|
|
|
})
|
|
|
|
}
|
2018-02-07 05:43:21 -08:00
|
|
|
}
|
|
|
|
|
2017-09-04 06:07:30 -07:00
|
|
|
func TestHeadDeleteSimple(t *testing.T) {
|
2019-01-11 08:34:09 -08:00
|
|
|
buildSmpls := func(s []int64) []sample {
|
|
|
|
ss := make([]sample, 0, len(s))
|
|
|
|
for _, t := range s {
|
|
|
|
ss = append(ss, sample{t: t, v: float64(t)})
|
|
|
|
}
|
|
|
|
return ss
|
2017-09-04 06:07:30 -07:00
|
|
|
}
|
2019-01-11 08:34:09 -08:00
|
|
|
smplsAll := buildSmpls([]int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
|
2019-08-13 01:34:14 -07:00
|
|
|
lblDefault := labels.Label{Name: "a", Value: "b"}
|
2017-08-28 15:39:17 -07:00
|
|
|
|
2017-09-04 06:07:30 -07:00
|
|
|
cases := []struct {
|
2020-01-20 07:38:00 -08:00
|
|
|
dranges tombstones.Intervals
|
|
|
|
addSamples []sample // Samples to add after delete.
|
|
|
|
smplsExp []sample
|
2017-09-04 06:07:30 -07:00
|
|
|
}{
|
|
|
|
{
|
2019-09-19 02:15:41 -07:00
|
|
|
dranges: tombstones.Intervals{{Mint: 0, Maxt: 3}},
|
2019-01-11 08:34:09 -08:00
|
|
|
smplsExp: buildSmpls([]int64{4, 5, 6, 7, 8, 9}),
|
2017-09-04 06:07:30 -07:00
|
|
|
},
|
|
|
|
{
|
2019-09-19 02:15:41 -07:00
|
|
|
dranges: tombstones.Intervals{{Mint: 1, Maxt: 3}},
|
2019-01-11 08:34:09 -08:00
|
|
|
smplsExp: buildSmpls([]int64{0, 4, 5, 6, 7, 8, 9}),
|
2017-09-04 06:07:30 -07:00
|
|
|
},
|
|
|
|
{
|
2019-09-19 02:15:41 -07:00
|
|
|
dranges: tombstones.Intervals{{Mint: 1, Maxt: 3}, {Mint: 4, Maxt: 7}},
|
2019-01-11 08:34:09 -08:00
|
|
|
smplsExp: buildSmpls([]int64{0, 8, 9}),
|
2017-09-04 06:07:30 -07:00
|
|
|
},
|
|
|
|
{
|
2019-09-19 02:15:41 -07:00
|
|
|
dranges: tombstones.Intervals{{Mint: 1, Maxt: 3}, {Mint: 4, Maxt: 700}},
|
2019-01-11 08:34:09 -08:00
|
|
|
smplsExp: buildSmpls([]int64{0}),
|
2017-09-04 06:07:30 -07:00
|
|
|
},
|
2019-01-08 09:08:41 -08:00
|
|
|
{ // This case is to ensure that labels and symbols are deleted.
|
2019-09-19 02:15:41 -07:00
|
|
|
dranges: tombstones.Intervals{{Mint: 0, Maxt: 9}},
|
2019-01-11 08:34:09 -08:00
|
|
|
smplsExp: buildSmpls([]int64{}),
|
2017-09-04 06:07:30 -07:00
|
|
|
},
|
2020-01-20 07:38:00 -08:00
|
|
|
{
|
|
|
|
dranges: tombstones.Intervals{{Mint: 1, Maxt: 3}},
|
|
|
|
addSamples: buildSmpls([]int64{11, 13, 15}),
|
|
|
|
smplsExp: buildSmpls([]int64{0, 4, 5, 6, 7, 8, 9, 11, 13, 15}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// After delete, the appended samples in the deleted range should be visible
|
|
|
|
// as the tombstones are clamped to head min/max time.
|
|
|
|
dranges: tombstones.Intervals{{Mint: 7, Maxt: 20}},
|
|
|
|
addSamples: buildSmpls([]int64{11, 13, 15}),
|
|
|
|
smplsExp: buildSmpls([]int64{0, 1, 2, 3, 4, 5, 6, 11, 13, 15}),
|
|
|
|
},
|
2017-09-04 06:07:30 -07:00
|
|
|
}
|
2017-08-28 15:39:17 -07:00
|
|
|
|
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) {
|
|
|
|
Outer:
|
|
|
|
for _, c := range cases {
|
|
|
|
dir, err := ioutil.TempDir("", "test_wal_reload")
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2019-01-08 09:08:41 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
w, err := wal.New(nil, nil, path.Join(dir, "wal"), compress)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer w.Close()
|
2019-01-11 08:34:09 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
head, err := NewHead(nil, nil, w, 1000)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer head.Close()
|
2017-08-28 15:39:17 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
app := head.Appender()
|
|
|
|
for _, smpl := range smplsAll {
|
|
|
|
_, err = app.Add(labels.Labels{lblDefault}, smpl.t, smpl.v)
|
|
|
|
testutil.Ok(t, err)
|
2017-08-28 15:39:17 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
}
|
|
|
|
testutil.Ok(t, app.Commit())
|
2017-08-28 15:39:17 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
// Delete the ranges.
|
|
|
|
for _, r := range c.dranges {
|
2019-11-18 11:53:33 -08:00
|
|
|
testutil.Ok(t, head.Delete(r.Mint, r.Maxt, labels.MustNewMatcher(labels.MatchEqual, lblDefault.Name, lblDefault.Value)))
|
2019-06-19 06:46:24 -07:00
|
|
|
}
|
2019-01-08 09:08:41 -08:00
|
|
|
|
2020-01-20 07:38:00 -08:00
|
|
|
// Add more samples.
|
|
|
|
app = head.Appender()
|
|
|
|
for _, smpl := range c.addSamples {
|
|
|
|
_, err = app.Add(labels.Labels{lblDefault}, smpl.t, smpl.v)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
testutil.Ok(t, app.Commit())
|
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
// Compare the samples for both heads - before and after the reload.
|
|
|
|
reloadedW, err := wal.New(nil, nil, w.Dir(), compress) // Use a new wal to ensure deleted samples are gone even after a reload.
|
2019-01-08 09:08:41 -08:00
|
|
|
testutil.Ok(t, err)
|
2019-06-19 06:46:24 -07:00
|
|
|
defer reloadedW.Close()
|
|
|
|
reloadedHead, err := NewHead(nil, nil, reloadedW, 1000)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer reloadedHead.Close()
|
|
|
|
testutil.Ok(t, reloadedHead.Init(0))
|
2017-08-28 15:39:17 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
// Compare the query results for both heads - before and after the reload.
|
|
|
|
expSeriesSet := newMockSeriesSet([]Series{
|
|
|
|
newSeries(map[string]string{lblDefault.Name: lblDefault.Value}, func() []tsdbutil.Sample {
|
|
|
|
ss := make([]tsdbutil.Sample, 0, len(c.smplsExp))
|
|
|
|
for _, s := range c.smplsExp {
|
|
|
|
ss = append(ss, s)
|
|
|
|
}
|
|
|
|
return ss
|
|
|
|
}(),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
for _, h := range []*Head{head, reloadedHead} {
|
|
|
|
q, err := NewBlockQuerier(h, h.MinTime(), h.MaxTime())
|
|
|
|
testutil.Ok(t, err)
|
2019-11-18 11:53:33 -08:00
|
|
|
actSeriesSet, err := q.Select(labels.MustNewMatcher(labels.MatchEqual, lblDefault.Name, lblDefault.Value))
|
2019-06-19 06:46:24 -07:00
|
|
|
testutil.Ok(t, err)
|
2017-08-28 15:39:17 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
for {
|
|
|
|
eok, rok := expSeriesSet.Next(), actSeriesSet.Next()
|
|
|
|
testutil.Equals(t, eok, rok)
|
2017-08-28 15:39:17 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
if !eok {
|
|
|
|
testutil.Ok(t, h.Close())
|
|
|
|
continue Outer
|
|
|
|
}
|
|
|
|
expSeries := expSeriesSet.At()
|
|
|
|
actSeries := actSeriesSet.At()
|
2017-08-28 15:39:17 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
testutil.Equals(t, expSeries.Labels(), actSeries.Labels())
|
2017-08-28 15:39:17 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
smplExp, errExp := expandSeriesIterator(expSeries.Iterator())
|
|
|
|
smplRes, errRes := expandSeriesIterator(actSeries.Iterator())
|
|
|
|
|
|
|
|
testutil.Equals(t, errExp, errRes)
|
|
|
|
testutil.Equals(t, smplExp, smplRes)
|
|
|
|
}
|
|
|
|
}
|
2019-01-08 09:08:41 -08:00
|
|
|
}
|
2019-06-19 06:46:24 -07:00
|
|
|
})
|
2017-09-04 06:07:30 -07:00
|
|
|
}
|
|
|
|
}
|
2017-08-28 15:39:17 -07:00
|
|
|
|
2018-09-17 04:28:55 -07:00
|
|
|
func TestDeleteUntilCurMax(t *testing.T) {
|
|
|
|
numSamples := int64(10)
|
|
|
|
hb, err := NewHead(nil, nil, nil, 1000000)
|
|
|
|
testutil.Ok(t, err)
|
2019-01-02 08:48:42 -08:00
|
|
|
defer hb.Close()
|
2018-09-17 04:28:55 -07:00
|
|
|
app := hb.Appender()
|
|
|
|
smpls := make([]float64, numSamples)
|
|
|
|
for i := int64(0); i < numSamples; i++ {
|
|
|
|
smpls[i] = rand.Float64()
|
2019-08-13 01:34:14 -07:00
|
|
|
_, err := app.Add(labels.Labels{{Name: "a", Value: "b"}}, i, smpls[i])
|
2018-09-17 04:28:55 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
}
|
|
|
|
testutil.Ok(t, app.Commit())
|
2019-11-18 11:53:33 -08:00
|
|
|
testutil.Ok(t, hb.Delete(0, 10000, labels.MustNewMatcher(labels.MatchEqual, "a", "b")))
|
2018-09-17 04:28:55 -07:00
|
|
|
|
2020-01-20 07:38:00 -08:00
|
|
|
// Test the series returns no samples. The series is cleared only after compaction.
|
2018-09-17 04:28:55 -07:00
|
|
|
q, err := NewBlockQuerier(hb, 0, 100000)
|
|
|
|
testutil.Ok(t, err)
|
2019-11-18 11:53:33 -08:00
|
|
|
res, err := q.Select(labels.MustNewMatcher(labels.MatchEqual, "a", "b"))
|
2018-09-17 04:28:55 -07:00
|
|
|
testutil.Ok(t, err)
|
2020-01-20 07:38:00 -08:00
|
|
|
testutil.Assert(t, res.Next(), "series is not present")
|
|
|
|
s := res.At()
|
|
|
|
it := s.Iterator()
|
|
|
|
testutil.Assert(t, !it.Next(), "expected no samples")
|
2018-09-17 04:28:55 -07:00
|
|
|
|
|
|
|
// Add again and test for presence.
|
|
|
|
app = hb.Appender()
|
2019-08-13 01:34:14 -07:00
|
|
|
_, err = app.Add(labels.Labels{{Name: "a", Value: "b"}}, 11, 1)
|
2018-09-17 04:28:55 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Ok(t, app.Commit())
|
|
|
|
q, err = NewBlockQuerier(hb, 0, 100000)
|
|
|
|
testutil.Ok(t, err)
|
2019-11-18 11:53:33 -08:00
|
|
|
res, err = q.Select(labels.MustNewMatcher(labels.MatchEqual, "a", "b"))
|
2018-09-17 04:28:55 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Assert(t, res.Next(), "series don't exist")
|
|
|
|
exps := res.At()
|
2020-01-20 07:38:00 -08:00
|
|
|
it = exps.Iterator()
|
2020-01-02 06:54:09 -08:00
|
|
|
resSamples, err := expandSeriesIterator(it)
|
2018-09-17 04:28:55 -07:00
|
|
|
testutil.Ok(t, err)
|
2020-01-02 06:54:09 -08:00
|
|
|
testutil.Equals(t, []tsdbutil.Sample{sample{11, 1}}, resSamples)
|
2018-09-17 04:28:55 -07:00
|
|
|
}
|
2019-04-09 06:16:24 -07:00
|
|
|
|
|
|
|
func TestDeletedSamplesAndSeriesStillInWALAfterCheckpoint(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "test_delete_wal")
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2019-06-19 06:46:24 -07:00
|
|
|
wlog, err := wal.NewSize(nil, nil, dir, 32768, false)
|
2019-04-09 06:16:24 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
// Enough samples to cause a checkpoint.
|
|
|
|
numSamples := 10000
|
|
|
|
hb, err := NewHead(nil, nil, wlog, int64(numSamples)*10)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer hb.Close()
|
|
|
|
for i := 0; i < numSamples; i++ {
|
|
|
|
app := hb.Appender()
|
2019-08-13 01:34:14 -07:00
|
|
|
_, err := app.Add(labels.Labels{{Name: "a", Value: "b"}}, int64(i), 0)
|
2019-04-09 06:16:24 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Ok(t, app.Commit())
|
|
|
|
}
|
2019-11-18 11:53:33 -08:00
|
|
|
testutil.Ok(t, hb.Delete(0, int64(numSamples), labels.MustNewMatcher(labels.MatchEqual, "a", "b")))
|
2019-04-09 06:16:24 -07:00
|
|
|
testutil.Ok(t, hb.Truncate(1))
|
|
|
|
testutil.Ok(t, hb.Close())
|
|
|
|
|
|
|
|
// Confirm there's been a checkpoint.
|
2019-09-19 02:15:41 -07:00
|
|
|
cdir, _, err := wal.LastCheckpoint(dir)
|
2019-04-09 06:16:24 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
// Read in checkpoint and WAL.
|
|
|
|
recs := readTestWAL(t, cdir)
|
|
|
|
recs = append(recs, readTestWAL(t, dir)...)
|
|
|
|
|
|
|
|
var series, samples, stones int
|
|
|
|
for _, rec := range recs {
|
|
|
|
switch rec.(type) {
|
2019-09-19 02:15:41 -07:00
|
|
|
case []record.RefSeries:
|
2019-04-09 06:16:24 -07:00
|
|
|
series++
|
2019-09-19 02:15:41 -07:00
|
|
|
case []record.RefSample:
|
2019-04-09 06:16:24 -07:00
|
|
|
samples++
|
2019-09-19 02:15:41 -07:00
|
|
|
case []tombstones.Stone:
|
2019-04-09 06:16:24 -07:00
|
|
|
stones++
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown record type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
testutil.Equals(t, 1, series)
|
|
|
|
testutil.Equals(t, 9999, samples)
|
|
|
|
testutil.Equals(t, 1, stones)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-17 04:28:55 -07:00
|
|
|
func TestDelete_e2e(t *testing.T) {
|
|
|
|
numDatapoints := 1000
|
|
|
|
numRanges := 1000
|
|
|
|
timeInterval := int64(2)
|
|
|
|
// Create 8 series with 1000 data-points of different ranges, delete and run queries.
|
|
|
|
lbls := [][]labels.Label{
|
|
|
|
{
|
2019-08-13 01:34:14 -07:00
|
|
|
{Name: "a", Value: "b"},
|
|
|
|
{Name: "instance", Value: "localhost:9090"},
|
|
|
|
{Name: "job", Value: "prometheus"},
|
2018-09-17 04:28:55 -07:00
|
|
|
},
|
|
|
|
{
|
2019-08-13 01:34:14 -07:00
|
|
|
{Name: "a", Value: "b"},
|
|
|
|
{Name: "instance", Value: "127.0.0.1:9090"},
|
|
|
|
{Name: "job", Value: "prometheus"},
|
2018-09-17 04:28:55 -07:00
|
|
|
},
|
|
|
|
{
|
2019-08-13 01:34:14 -07:00
|
|
|
{Name: "a", Value: "b"},
|
|
|
|
{Name: "instance", Value: "127.0.0.1:9090"},
|
|
|
|
{Name: "job", Value: "prom-k8s"},
|
2018-09-17 04:28:55 -07:00
|
|
|
},
|
|
|
|
{
|
2019-08-13 01:34:14 -07:00
|
|
|
{Name: "a", Value: "b"},
|
|
|
|
{Name: "instance", Value: "localhost:9090"},
|
|
|
|
{Name: "job", Value: "prom-k8s"},
|
2018-09-17 04:28:55 -07:00
|
|
|
},
|
|
|
|
{
|
2019-08-13 01:34:14 -07:00
|
|
|
{Name: "a", Value: "c"},
|
|
|
|
{Name: "instance", Value: "localhost:9090"},
|
|
|
|
{Name: "job", Value: "prometheus"},
|
2018-09-17 04:28:55 -07:00
|
|
|
},
|
|
|
|
{
|
2019-08-13 01:34:14 -07:00
|
|
|
{Name: "a", Value: "c"},
|
|
|
|
{Name: "instance", Value: "127.0.0.1:9090"},
|
|
|
|
{Name: "job", Value: "prometheus"},
|
2018-09-17 04:28:55 -07:00
|
|
|
},
|
|
|
|
{
|
2019-08-13 01:34:14 -07:00
|
|
|
{Name: "a", Value: "c"},
|
|
|
|
{Name: "instance", Value: "127.0.0.1:9090"},
|
|
|
|
{Name: "job", Value: "prom-k8s"},
|
2018-09-17 04:28:55 -07:00
|
|
|
},
|
|
|
|
{
|
2019-08-13 01:34:14 -07:00
|
|
|
{Name: "a", Value: "c"},
|
|
|
|
{Name: "instance", Value: "localhost:9090"},
|
|
|
|
{Name: "job", Value: "prom-k8s"},
|
2018-09-17 04:28:55 -07:00
|
|
|
},
|
|
|
|
}
|
2019-01-28 03:24:49 -08:00
|
|
|
seriesMap := map[string][]tsdbutil.Sample{}
|
2018-09-17 04:28:55 -07:00
|
|
|
for _, l := range lbls {
|
2019-01-28 03:24:49 -08:00
|
|
|
seriesMap[labels.New(l...).String()] = []tsdbutil.Sample{}
|
2018-09-17 04:28:55 -07:00
|
|
|
}
|
|
|
|
dir, _ := ioutil.TempDir("", "test")
|
2019-03-19 06:31:57 -07:00
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2018-09-17 04:28:55 -07:00
|
|
|
hb, err := NewHead(nil, nil, nil, 100000)
|
|
|
|
testutil.Ok(t, err)
|
2018-12-13 05:29:29 -08:00
|
|
|
defer hb.Close()
|
2018-09-17 04:28:55 -07:00
|
|
|
app := hb.Appender()
|
|
|
|
for _, l := range lbls {
|
|
|
|
ls := labels.New(l...)
|
2019-01-28 03:24:49 -08:00
|
|
|
series := []tsdbutil.Sample{}
|
2018-09-17 04:28:55 -07:00
|
|
|
ts := rand.Int63n(300)
|
|
|
|
for i := 0; i < numDatapoints; i++ {
|
|
|
|
v := rand.Float64()
|
|
|
|
_, err := app.Add(ls, ts, v)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
series = append(series, sample{ts, v})
|
|
|
|
ts += rand.Int63n(timeInterval) + 1
|
|
|
|
}
|
|
|
|
seriesMap[labels.New(l...).String()] = series
|
|
|
|
}
|
|
|
|
testutil.Ok(t, app.Commit())
|
|
|
|
// Delete a time-range from each-selector.
|
|
|
|
dels := []struct {
|
2019-11-18 11:53:33 -08:00
|
|
|
ms []*labels.Matcher
|
2019-09-19 02:15:41 -07:00
|
|
|
drange tombstones.Intervals
|
2018-09-17 04:28:55 -07:00
|
|
|
}{
|
|
|
|
{
|
2019-11-18 11:53:33 -08:00
|
|
|
ms: []*labels.Matcher{labels.MustNewMatcher(labels.MatchEqual, "a", "b")},
|
2019-09-19 02:15:41 -07:00
|
|
|
drange: tombstones.Intervals{{Mint: 300, Maxt: 500}, {Mint: 600, Maxt: 670}},
|
2018-09-17 04:28:55 -07:00
|
|
|
},
|
|
|
|
{
|
2019-11-18 11:53:33 -08:00
|
|
|
ms: []*labels.Matcher{
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "a", "b"),
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "job", "prom-k8s"),
|
2018-09-17 04:28:55 -07:00
|
|
|
},
|
2019-09-19 02:15:41 -07:00
|
|
|
drange: tombstones.Intervals{{Mint: 300, Maxt: 500}, {Mint: 100, Maxt: 670}},
|
2018-09-17 04:28:55 -07:00
|
|
|
},
|
|
|
|
{
|
2019-11-18 11:53:33 -08:00
|
|
|
ms: []*labels.Matcher{
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "a", "c"),
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "instance", "localhost:9090"),
|
|
|
|
labels.MustNewMatcher(labels.MatchEqual, "job", "prometheus"),
|
2018-09-17 04:28:55 -07:00
|
|
|
},
|
2019-09-19 02:15:41 -07:00
|
|
|
drange: tombstones.Intervals{{Mint: 300, Maxt: 400}, {Mint: 100, Maxt: 6700}},
|
2018-09-17 04:28:55 -07:00
|
|
|
},
|
|
|
|
// TODO: Add Regexp Matchers.
|
|
|
|
}
|
|
|
|
for _, del := range dels {
|
|
|
|
for _, r := range del.drange {
|
|
|
|
testutil.Ok(t, hb.Delete(r.Mint, r.Maxt, del.ms...))
|
|
|
|
}
|
|
|
|
matched := labels.Slice{}
|
|
|
|
for _, ls := range lbls {
|
|
|
|
s := labels.Selector(del.ms)
|
|
|
|
if s.Matches(ls) {
|
|
|
|
matched = append(matched, ls)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Sort(matched)
|
|
|
|
for i := 0; i < numRanges; i++ {
|
|
|
|
q, err := NewBlockQuerier(hb, 0, 100000)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer q.Close()
|
2020-01-17 03:21:44 -08:00
|
|
|
ss, err := q.SelectSorted(del.ms...)
|
2018-09-17 04:28:55 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
// Build the mockSeriesSet.
|
|
|
|
matchedSeries := make([]Series, 0, len(matched))
|
|
|
|
for _, m := range matched {
|
|
|
|
smpls := seriesMap[m.String()]
|
|
|
|
smpls = deletedSamples(smpls, del.drange)
|
|
|
|
// Only append those series for which samples exist as mockSeriesSet
|
|
|
|
// doesn't skip series with no samples.
|
|
|
|
// TODO: But sometimes SeriesSet returns an empty SeriesIterator
|
|
|
|
if len(smpls) > 0 {
|
|
|
|
matchedSeries = append(matchedSeries, newSeries(
|
|
|
|
m.Map(),
|
|
|
|
smpls,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2018-09-21 01:07:35 -07:00
|
|
|
expSs := newMockSeriesSet(matchedSeries)
|
2018-09-17 04:28:55 -07:00
|
|
|
// Compare both SeriesSets.
|
|
|
|
for {
|
|
|
|
eok, rok := expSs.Next(), ss.Next()
|
|
|
|
// Skip a series if iterator is empty.
|
|
|
|
if rok {
|
|
|
|
for !ss.At().Iterator().Next() {
|
|
|
|
rok = ss.Next()
|
|
|
|
if !rok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
testutil.Equals(t, eok, rok)
|
|
|
|
if !eok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
sexp := expSs.At()
|
|
|
|
sres := ss.At()
|
|
|
|
testutil.Equals(t, sexp.Labels(), sres.Labels())
|
|
|
|
smplExp, errExp := expandSeriesIterator(sexp.Iterator())
|
|
|
|
smplRes, errRes := expandSeriesIterator(sres.Iterator())
|
|
|
|
testutil.Equals(t, errExp, errRes)
|
|
|
|
testutil.Equals(t, smplExp, smplRes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-19 10:24:29 -07:00
|
|
|
|
2019-02-14 05:29:41 -08:00
|
|
|
func boundedSamples(full []tsdbutil.Sample, mint, maxt int64) []tsdbutil.Sample {
|
2017-05-05 06:52:07 -07:00
|
|
|
for len(full) > 0 {
|
2019-02-14 05:29:41 -08:00
|
|
|
if full[0].T() >= mint {
|
2017-05-03 10:20:34 -07:00
|
|
|
break
|
|
|
|
}
|
2017-05-05 06:52:07 -07:00
|
|
|
full = full[1:]
|
2017-05-03 10:20:34 -07:00
|
|
|
}
|
2017-05-05 06:52:07 -07:00
|
|
|
for i, s := range full {
|
2017-09-05 02:45:18 -07:00
|
|
|
// labels.Labelinate on the first sample larger than maxt.
|
2019-02-14 05:29:41 -08:00
|
|
|
if s.T() > maxt {
|
2017-05-05 06:52:07 -07:00
|
|
|
return full[:i]
|
2017-05-03 10:20:34 -07:00
|
|
|
}
|
|
|
|
}
|
2017-05-05 06:52:07 -07:00
|
|
|
// maxt is after highest sample.
|
|
|
|
return full
|
2017-05-03 10:20:34 -07:00
|
|
|
}
|
2017-05-19 10:24:29 -07:00
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
func deletedSamples(full []tsdbutil.Sample, dranges tombstones.Intervals) []tsdbutil.Sample {
|
2019-01-28 03:24:49 -08:00
|
|
|
ds := make([]tsdbutil.Sample, 0, len(full))
|
2017-05-19 10:24:29 -07:00
|
|
|
Outer:
|
|
|
|
for _, s := range full {
|
|
|
|
for _, r := range dranges {
|
2019-09-19 02:15:41 -07:00
|
|
|
if r.InBounds(s.T()) {
|
2017-05-19 10:24:29 -07:00
|
|
|
continue Outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ds = append(ds, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ds
|
|
|
|
}
|
2017-06-07 04:42:53 -07:00
|
|
|
|
|
|
|
func TestComputeChunkEndTime(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
start, cur, max int64
|
|
|
|
res int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
start: 0,
|
|
|
|
cur: 250,
|
|
|
|
max: 1000,
|
|
|
|
res: 1000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
start: 100,
|
|
|
|
cur: 200,
|
|
|
|
max: 1000,
|
|
|
|
res: 550,
|
|
|
|
},
|
|
|
|
// Case where we fit floored 0 chunks. Must catch division by 0
|
|
|
|
// and default to maximum time.
|
|
|
|
{
|
|
|
|
start: 0,
|
|
|
|
cur: 500,
|
|
|
|
max: 1000,
|
|
|
|
res: 1000,
|
|
|
|
},
|
2018-04-08 02:28:30 -07:00
|
|
|
// Catch division by zero for cur == start. Strictly not a possible case.
|
2017-06-07 04:42:53 -07:00
|
|
|
{
|
|
|
|
start: 100,
|
|
|
|
cur: 100,
|
|
|
|
max: 1000,
|
|
|
|
res: 104,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
got := computeChunkEndTime(c.start, c.cur, c.max)
|
|
|
|
if got != c.res {
|
|
|
|
t.Errorf("expected %d for (start: %d, cur: %d, max: %d), got %d", c.res, c.start, c.cur, c.max, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 00:32:06 -07:00
|
|
|
|
|
|
|
func TestMemSeries_append(t *testing.T) {
|
|
|
|
s := newMemSeries(labels.Labels{}, 1, 500)
|
|
|
|
|
|
|
|
// Add first two samples at the very end of a chunk range and the next two
|
|
|
|
// on and after it.
|
|
|
|
// New chunk must correctly be cut at 1000.
|
|
|
|
ok, chunkCreated := s.append(998, 1)
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, ok, "append failed")
|
|
|
|
testutil.Assert(t, chunkCreated, "first sample created chunk")
|
2017-10-25 00:32:06 -07:00
|
|
|
|
|
|
|
ok, chunkCreated = s.append(999, 2)
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, ok, "append failed")
|
|
|
|
testutil.Assert(t, !chunkCreated, "second sample should use same chunk")
|
2017-10-25 00:32:06 -07:00
|
|
|
|
|
|
|
ok, chunkCreated = s.append(1000, 3)
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, ok, "append failed")
|
2019-01-02 08:48:42 -08:00
|
|
|
testutil.Assert(t, chunkCreated, "expected new chunk on boundary")
|
2017-10-25 00:32:06 -07:00
|
|
|
|
|
|
|
ok, chunkCreated = s.append(1001, 4)
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, ok, "append failed")
|
|
|
|
testutil.Assert(t, !chunkCreated, "second sample should use same chunk")
|
2017-10-25 00:32:06 -07:00
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, s.chunks[0].minTime == 998 && s.chunks[0].maxTime == 999, "wrong chunk range")
|
|
|
|
testutil.Assert(t, s.chunks[1].minTime == 1000 && s.chunks[1].maxTime == 1001, "wrong chunk range")
|
2017-10-25 00:32:06 -07:00
|
|
|
|
|
|
|
// Fill the range [1000,2000) with many samples. Intermediate chunks should be cut
|
|
|
|
// at approximately 120 samples per chunk.
|
|
|
|
for i := 1; i < 1000; i++ {
|
|
|
|
ok, _ := s.append(1001+int64(i), float64(i))
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, ok, "append failed")
|
2017-10-25 00:32:06 -07:00
|
|
|
}
|
|
|
|
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, len(s.chunks) > 7, "expected intermediate chunks")
|
2017-10-25 00:32:06 -07:00
|
|
|
|
|
|
|
// All chunks but the first and last should now be moderately full.
|
|
|
|
for i, c := range s.chunks[1 : len(s.chunks)-1] {
|
2017-12-06 17:06:14 -08:00
|
|
|
testutil.Assert(t, c.chunk.NumSamples() > 100, "unexpected small chunk %d of length %d", i, c.chunk.NumSamples())
|
2017-10-25 00:32:06 -07:00
|
|
|
}
|
|
|
|
}
|
2017-12-13 12:58:21 -08:00
|
|
|
|
|
|
|
func TestGCChunkAccess(t *testing.T) {
|
|
|
|
// Put a chunk, select it. GC it and then access it.
|
2018-05-17 06:04:32 -07:00
|
|
|
h, err := NewHead(nil, nil, nil, 1000)
|
2017-12-13 12:58:21 -08:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer h.Close()
|
|
|
|
|
|
|
|
h.initTime(0)
|
|
|
|
|
|
|
|
s, _ := h.getOrCreate(1, labels.FromStrings("a", "1"))
|
|
|
|
s.chunks = []*memChunk{
|
|
|
|
{minTime: 0, maxTime: 999},
|
|
|
|
{minTime: 1000, maxTime: 1999},
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := h.indexRange(0, 1500)
|
|
|
|
var (
|
|
|
|
lset labels.Labels
|
2017-12-21 02:55:58 -08:00
|
|
|
chunks []chunks.Meta
|
2017-12-13 12:58:21 -08:00
|
|
|
)
|
|
|
|
testutil.Ok(t, idx.Series(1, &lset, &chunks))
|
|
|
|
|
|
|
|
testutil.Equals(t, labels.Labels{{
|
|
|
|
Name: "a", Value: "1",
|
|
|
|
}}, lset)
|
|
|
|
testutil.Equals(t, 2, len(chunks))
|
|
|
|
|
|
|
|
cr := h.chunksRange(0, 1500)
|
|
|
|
_, err = cr.Chunk(chunks[0].Ref)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
_, err = cr.Chunk(chunks[1].Ref)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
2019-04-25 03:07:04 -07:00
|
|
|
testutil.Ok(t, h.Truncate(1500)) // Remove a chunk.
|
2017-12-13 12:58:21 -08:00
|
|
|
|
|
|
|
_, err = cr.Chunk(chunks[0].Ref)
|
|
|
|
testutil.Equals(t, ErrNotFound, err)
|
|
|
|
_, err = cr.Chunk(chunks[1].Ref)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGCSeriesAccess(t *testing.T) {
|
|
|
|
// Put a series, select it. GC it and then access it.
|
2018-05-17 06:04:32 -07:00
|
|
|
h, err := NewHead(nil, nil, nil, 1000)
|
2017-12-13 12:58:21 -08:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer h.Close()
|
|
|
|
|
|
|
|
h.initTime(0)
|
|
|
|
|
|
|
|
s, _ := h.getOrCreate(1, labels.FromStrings("a", "1"))
|
|
|
|
s.chunks = []*memChunk{
|
|
|
|
{minTime: 0, maxTime: 999},
|
|
|
|
{minTime: 1000, maxTime: 1999},
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := h.indexRange(0, 2000)
|
|
|
|
var (
|
|
|
|
lset labels.Labels
|
2017-12-21 02:55:58 -08:00
|
|
|
chunks []chunks.Meta
|
2017-12-13 12:58:21 -08:00
|
|
|
)
|
|
|
|
testutil.Ok(t, idx.Series(1, &lset, &chunks))
|
|
|
|
|
|
|
|
testutil.Equals(t, labels.Labels{{
|
|
|
|
Name: "a", Value: "1",
|
|
|
|
}}, lset)
|
|
|
|
testutil.Equals(t, 2, len(chunks))
|
|
|
|
|
|
|
|
cr := h.chunksRange(0, 2000)
|
|
|
|
_, err = cr.Chunk(chunks[0].Ref)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
_, err = cr.Chunk(chunks[1].Ref)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
2019-04-25 03:07:04 -07:00
|
|
|
testutil.Ok(t, h.Truncate(2000)) // Remove the series.
|
2017-12-13 12:58:21 -08:00
|
|
|
|
|
|
|
testutil.Equals(t, (*memSeries)(nil), h.series.getByID(1))
|
|
|
|
|
|
|
|
_, err = cr.Chunk(chunks[0].Ref)
|
|
|
|
testutil.Equals(t, ErrNotFound, err)
|
|
|
|
_, err = cr.Chunk(chunks[1].Ref)
|
|
|
|
testutil.Equals(t, ErrNotFound, err)
|
|
|
|
}
|
2018-06-28 06:04:07 -07:00
|
|
|
|
2018-09-17 09:58:42 -07:00
|
|
|
func TestUncommittedSamplesNotLostOnTruncate(t *testing.T) {
|
|
|
|
h, err := NewHead(nil, nil, nil, 1000)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer h.Close()
|
|
|
|
|
|
|
|
h.initTime(0)
|
|
|
|
|
|
|
|
app := h.appender()
|
|
|
|
lset := labels.FromStrings("a", "1")
|
|
|
|
_, err = app.Add(lset, 2100, 1)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
testutil.Ok(t, h.Truncate(2000))
|
|
|
|
testutil.Assert(t, nil != h.series.getByHash(lset.Hash(), lset), "series should not have been garbage collected")
|
|
|
|
|
|
|
|
testutil.Ok(t, app.Commit())
|
|
|
|
|
|
|
|
q, err := NewBlockQuerier(h, 1500, 2500)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer q.Close()
|
|
|
|
|
2019-11-18 11:53:33 -08:00
|
|
|
ss, err := q.Select(labels.MustNewMatcher(labels.MatchEqual, "a", "1"))
|
2018-09-17 09:58:42 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
testutil.Equals(t, true, ss.Next())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveSeriesAfterRollbackAndTruncate(t *testing.T) {
|
|
|
|
h, err := NewHead(nil, nil, nil, 1000)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer h.Close()
|
|
|
|
|
|
|
|
h.initTime(0)
|
|
|
|
|
|
|
|
app := h.appender()
|
|
|
|
lset := labels.FromStrings("a", "1")
|
|
|
|
_, err = app.Add(lset, 2100, 1)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
testutil.Ok(t, h.Truncate(2000))
|
|
|
|
testutil.Assert(t, nil != h.series.getByHash(lset.Hash(), lset), "series should not have been garbage collected")
|
|
|
|
|
|
|
|
testutil.Ok(t, app.Rollback())
|
|
|
|
|
|
|
|
q, err := NewBlockQuerier(h, 1500, 2500)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer q.Close()
|
|
|
|
|
2019-11-18 11:53:33 -08:00
|
|
|
ss, err := q.Select(labels.MustNewMatcher(labels.MatchEqual, "a", "1"))
|
2018-09-17 09:58:42 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
testutil.Equals(t, false, ss.Next())
|
|
|
|
|
|
|
|
// Truncate again, this time the series should be deleted
|
|
|
|
testutil.Ok(t, h.Truncate(2050))
|
|
|
|
testutil.Equals(t, (*memSeries)(nil), h.series.getByHash(lset.Hash(), lset))
|
|
|
|
}
|
|
|
|
|
2018-06-28 06:04:07 -07:00
|
|
|
func TestHead_LogRollback(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("", "wal_rollback")
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2018-05-17 06:04:32 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
w, err := wal.New(nil, nil, dir, compress)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer w.Close()
|
|
|
|
h, err := NewHead(nil, nil, w, 1000)
|
|
|
|
testutil.Ok(t, err)
|
2018-06-28 06:04:07 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
app := h.Appender()
|
|
|
|
_, err = app.Add(labels.FromStrings("a", "b"), 1, 2)
|
|
|
|
testutil.Ok(t, err)
|
2018-06-28 06:04:07 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
testutil.Ok(t, app.Rollback())
|
|
|
|
recs := readTestWAL(t, w.Dir())
|
2018-05-17 06:04:32 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
testutil.Equals(t, 1, len(recs))
|
2018-06-28 06:04:07 -07:00
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
series, ok := recs[0].([]record.RefSeries)
|
2019-06-19 06:46:24 -07:00
|
|
|
testutil.Assert(t, ok, "expected series record but got %+v", recs[0])
|
2019-09-19 02:15:41 -07:00
|
|
|
testutil.Equals(t, []record.RefSeries{{Ref: 1, Labels: labels.FromStrings("a", "b")}}, series)
|
2019-06-19 06:46:24 -07:00
|
|
|
})
|
|
|
|
}
|
2018-06-28 06:04:07 -07:00
|
|
|
}
|
2018-11-30 03:37:04 -08:00
|
|
|
|
2019-06-12 07:10:37 -07:00
|
|
|
// TestWalRepair_DecodingError ensures that a repair is run for an error
|
|
|
|
// when decoding a record.
|
|
|
|
func TestWalRepair_DecodingError(t *testing.T) {
|
2019-09-19 02:15:41 -07:00
|
|
|
var enc record.Encoder
|
2018-11-30 03:37:04 -08:00
|
|
|
for name, test := range map[string]struct {
|
|
|
|
corrFunc func(rec []byte) []byte // Func that applies the corruption to a record.
|
|
|
|
rec []byte
|
|
|
|
totalRecs int
|
|
|
|
expRecs int
|
|
|
|
}{
|
|
|
|
"invalid_record": {
|
|
|
|
func(rec []byte) []byte {
|
2019-06-19 06:46:24 -07:00
|
|
|
// Do not modify the base record because it is Logged multiple times.
|
|
|
|
res := make([]byte, len(rec))
|
|
|
|
copy(res, rec)
|
2019-09-19 02:15:41 -07:00
|
|
|
res[0] = byte(record.Invalid)
|
2019-06-19 06:46:24 -07:00
|
|
|
return res
|
2018-11-30 03:37:04 -08:00
|
|
|
},
|
2019-09-19 02:15:41 -07:00
|
|
|
enc.Series([]record.RefSeries{{Ref: 1, Labels: labels.FromStrings("a", "b")}}, []byte{}),
|
2018-11-30 03:37:04 -08:00
|
|
|
9,
|
|
|
|
5,
|
|
|
|
},
|
|
|
|
"decode_series": {
|
|
|
|
func(rec []byte) []byte {
|
|
|
|
return rec[:3]
|
|
|
|
},
|
2019-09-19 02:15:41 -07:00
|
|
|
enc.Series([]record.RefSeries{{Ref: 1, Labels: labels.FromStrings("a", "b")}}, []byte{}),
|
2018-11-30 03:37:04 -08:00
|
|
|
9,
|
|
|
|
5,
|
|
|
|
},
|
|
|
|
"decode_samples": {
|
|
|
|
func(rec []byte) []byte {
|
|
|
|
return rec[:3]
|
|
|
|
},
|
2019-09-19 02:15:41 -07:00
|
|
|
enc.Samples([]record.RefSample{{Ref: 0, T: 99, V: 1}}, []byte{}),
|
2018-11-30 03:37:04 -08:00
|
|
|
9,
|
|
|
|
5,
|
|
|
|
},
|
|
|
|
"decode_tombstone": {
|
|
|
|
func(rec []byte) []byte {
|
|
|
|
return rec[:3]
|
|
|
|
},
|
2019-09-19 02:15:41 -07:00
|
|
|
enc.Tombstones([]tombstones.Stone{{Ref: 1, Intervals: tombstones.Intervals{}}}, []byte{}),
|
2018-11-30 03:37:04 -08:00
|
|
|
9,
|
|
|
|
5,
|
|
|
|
},
|
|
|
|
} {
|
2019-06-19 06:46:24 -07:00
|
|
|
for _, compress := range []bool{false, true} {
|
|
|
|
t.Run(fmt.Sprintf("%s,compress=%t", name, compress), func(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "wal_repair")
|
2019-06-14 08:39:22 -07:00
|
|
|
testutil.Ok(t, err)
|
2019-06-19 06:46:24 -07:00
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2018-11-30 03:37:04 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
// Fill the wal and corrupt it.
|
|
|
|
{
|
|
|
|
w, err := wal.New(nil, nil, filepath.Join(dir, "wal"), compress)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
for i := 1; i <= test.totalRecs; i++ {
|
|
|
|
// At this point insert a corrupted record.
|
|
|
|
if i-1 == test.expRecs {
|
|
|
|
testutil.Ok(t, w.Log(test.corrFunc(test.rec)))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
testutil.Ok(t, w.Log(test.rec))
|
2019-06-14 08:39:22 -07:00
|
|
|
}
|
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
h, err := NewHead(nil, nil, w, 1)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, 0.0, prom_testutil.ToFloat64(h.metrics.walCorruptionsTotal))
|
|
|
|
initErr := h.Init(math.MinInt64)
|
2019-06-14 08:39:22 -07:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
err = errors.Cause(initErr) // So that we can pick up errors even if wrapped.
|
|
|
|
_, corrErr := err.(*wal.CorruptionErr)
|
|
|
|
testutil.Assert(t, corrErr, "reading the wal didn't return corruption error")
|
|
|
|
testutil.Ok(t, w.Close())
|
|
|
|
}
|
2018-11-30 03:37:04 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
// Open the db to trigger a repair.
|
|
|
|
{
|
|
|
|
db, err := Open(dir, nil, nil, DefaultOptions)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, db.Close())
|
|
|
|
}()
|
|
|
|
testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.walCorruptionsTotal))
|
|
|
|
}
|
2018-11-30 03:37:04 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
// Read the wal content after the repair.
|
|
|
|
{
|
|
|
|
sr, err := wal.NewSegmentsReader(filepath.Join(dir, "wal"))
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer sr.Close()
|
|
|
|
r := wal.NewReader(sr)
|
2018-11-30 03:37:04 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
var actRec int
|
|
|
|
for r.Next() {
|
|
|
|
actRec++
|
|
|
|
}
|
|
|
|
testutil.Ok(t, r.Err())
|
|
|
|
testutil.Equals(t, test.expRecs, actRec, "Wrong number of intact records")
|
2019-06-14 08:39:22 -07:00
|
|
|
}
|
2019-06-19 06:46:24 -07:00
|
|
|
})
|
|
|
|
}
|
2018-11-30 03:37:04 -08:00
|
|
|
}
|
|
|
|
}
|
2019-06-07 03:35:02 -07:00
|
|
|
|
|
|
|
func TestNewWalSegmentOnTruncate(t *testing.T) {
|
2020-01-20 03:05:27 -08:00
|
|
|
dir, err := ioutil.TempDir("", "test_wal_segments")
|
2019-06-07 03:35:02 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
2019-06-19 06:46:24 -07:00
|
|
|
wlog, err := wal.NewSize(nil, nil, dir, 32768, false)
|
2019-06-07 03:35:02 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
h, err := NewHead(nil, nil, wlog, 1000)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer h.Close()
|
|
|
|
add := func(ts int64) {
|
|
|
|
app := h.Appender()
|
2019-08-13 01:34:14 -07:00
|
|
|
_, err := app.Add(labels.Labels{{Name: "a", Value: "b"}}, ts, 0)
|
2019-06-07 03:35:02 -07:00
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Ok(t, app.Commit())
|
|
|
|
}
|
|
|
|
|
|
|
|
add(0)
|
|
|
|
_, last, err := wlog.Segments()
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, 0, last)
|
|
|
|
|
|
|
|
add(1)
|
|
|
|
testutil.Ok(t, h.Truncate(1))
|
|
|
|
_, last, err = wlog.Segments()
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, 1, last)
|
|
|
|
|
|
|
|
add(2)
|
|
|
|
testutil.Ok(t, h.Truncate(2))
|
|
|
|
_, last, err = wlog.Segments()
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, 2, last)
|
|
|
|
}
|
2020-01-20 03:05:27 -08:00
|
|
|
|
|
|
|
func TestAddDuplicateLabelName(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "test_duplicate_label_name")
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(dir))
|
|
|
|
}()
|
|
|
|
wlog, err := wal.NewSize(nil, nil, dir, 32768, false)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
h, err := NewHead(nil, nil, wlog, 1000)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
defer h.Close()
|
|
|
|
|
|
|
|
add := func(labels labels.Labels, labelName string) {
|
|
|
|
app := h.Appender()
|
|
|
|
_, err = app.Add(labels, 0, 0)
|
|
|
|
testutil.NotOk(t, err)
|
|
|
|
testutil.Equals(t, fmt.Sprintf(`label name "%s" is not unique: invalid sample`, labelName), err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
add(labels.Labels{{Name: "a", Value: "c"}, {Name: "a", Value: "b"}}, "a")
|
|
|
|
add(labels.Labels{{Name: "a", Value: "c"}, {Name: "a", Value: "c"}}, "a")
|
|
|
|
add(labels.Labels{{Name: "__name__", Value: "up"}, {Name: "job", Value: "prometheus"}, {Name: "le", Value: "500"}, {Name: "le", Value: "400"}, {Name: "unit", Value: "s"}}, "le")
|
|
|
|
}
|