2017-05-26 15:56:31 +00:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-05-17 03:06:56 +00:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
2018-06-08 11:52:01 +00:00
|
|
|
"sync"
|
2017-05-17 03:06:56 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2017-12-07 01:06:14 +00:00
|
|
|
|
2019-04-26 08:27:36 +00:00
|
|
|
"github.com/go-kit/kit/log"
|
2017-12-07 01:06:14 +00:00
|
|
|
"github.com/prometheus/tsdb/testutil"
|
2017-05-17 03:06:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWriteAndReadbackTombStones(t *testing.T) {
|
|
|
|
tmpdir, _ := ioutil.TempDir("", "test")
|
2019-03-19 13:31:57 +00:00
|
|
|
defer func() {
|
|
|
|
testutil.Ok(t, os.RemoveAll(tmpdir))
|
|
|
|
}()
|
2017-05-17 03:06:56 +00:00
|
|
|
|
2017-09-04 14:08:38 +00:00
|
|
|
ref := uint64(0)
|
2017-05-17 03:06:56 +00:00
|
|
|
|
2018-11-14 16:40:01 +00:00
|
|
|
stones := newMemTombstones()
|
2017-05-17 03:06:56 +00:00
|
|
|
// Generate the tombstones.
|
|
|
|
for i := 0; i < 100; i++ {
|
2017-09-04 14:08:38 +00:00
|
|
|
ref += uint64(rand.Int31n(10)) + 1
|
2017-05-24 08:12:56 +00:00
|
|
|
numRanges := rand.Intn(5) + 1
|
2017-08-25 08:11:46 +00:00
|
|
|
dranges := make(Intervals, 0, numRanges)
|
2017-05-17 03:06:56 +00:00
|
|
|
mint := rand.Int63n(time.Now().UnixNano())
|
|
|
|
for j := 0; j < numRanges; j++ {
|
2017-08-25 08:11:46 +00:00
|
|
|
dranges = dranges.add(Interval{mint, mint + rand.Int63n(1000)})
|
2017-05-17 03:06:56 +00:00
|
|
|
mint += rand.Int63n(1000) + 1
|
|
|
|
}
|
2018-06-25 13:52:11 +00:00
|
|
|
stones.addInterval(ref, dranges...)
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 08:27:36 +00:00
|
|
|
testutil.Ok(t, writeTombstoneFile(log.NewNopLogger(), tmpdir, stones))
|
2017-05-17 03:06:56 +00:00
|
|
|
|
2019-01-16 10:03:52 +00:00
|
|
|
restr, _, err := readTombstones(tmpdir)
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Ok(t, err)
|
2017-11-13 12:32:24 +00:00
|
|
|
|
2017-05-17 03:06:56 +00:00
|
|
|
// Compare the two readers.
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Equals(t, stones, restr)
|
2017-05-17 03:06:56 +00:00
|
|
|
}
|
2017-05-17 09:19:42 +00:00
|
|
|
|
|
|
|
func TestAddingNewIntervals(t *testing.T) {
|
|
|
|
cases := []struct {
|
2017-08-25 08:11:46 +00:00
|
|
|
exist Intervals
|
|
|
|
new Interval
|
2017-05-17 09:19:42 +00:00
|
|
|
|
2017-08-25 08:11:46 +00:00
|
|
|
exp Intervals
|
2017-05-17 09:19:42 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-08-25 08:11:46 +00:00
|
|
|
new: Interval{1, 2},
|
|
|
|
exp: Intervals{{1, 2}},
|
2017-05-17 09:19:42 +00:00
|
|
|
},
|
|
|
|
{
|
2017-08-25 08:11:46 +00:00
|
|
|
exist: Intervals{{1, 2}},
|
|
|
|
new: Interval{1, 2},
|
|
|
|
exp: Intervals{{1, 2}},
|
2017-05-17 09:19:42 +00:00
|
|
|
},
|
2017-05-22 05:01:50 +00:00
|
|
|
{
|
2017-08-25 08:11:46 +00:00
|
|
|
exist: Intervals{{1, 4}, {6, 6}},
|
|
|
|
new: Interval{5, 6},
|
|
|
|
exp: Intervals{{1, 6}},
|
2017-05-22 05:01:50 +00:00
|
|
|
},
|
2017-05-17 09:19:42 +00:00
|
|
|
{
|
2017-08-25 08:11:46 +00:00
|
|
|
exist: Intervals{{1, 10}, {12, 20}, {25, 30}},
|
|
|
|
new: Interval{21, 23},
|
|
|
|
exp: Intervals{{1, 10}, {12, 23}, {25, 30}},
|
2017-05-17 09:19:42 +00:00
|
|
|
},
|
|
|
|
{
|
2017-08-25 08:11:46 +00:00
|
|
|
exist: Intervals{{1, 2}, {3, 5}, {7, 7}},
|
|
|
|
new: Interval{6, 7},
|
|
|
|
exp: Intervals{{1, 2}, {3, 7}},
|
2017-05-17 09:19:42 +00:00
|
|
|
},
|
|
|
|
{
|
2017-08-25 08:11:46 +00:00
|
|
|
exist: Intervals{{1, 10}, {12, 20}, {25, 30}},
|
|
|
|
new: Interval{21, 25},
|
|
|
|
exp: Intervals{{1, 10}, {12, 30}},
|
2017-05-17 09:19:42 +00:00
|
|
|
},
|
|
|
|
{
|
2017-08-25 08:11:46 +00:00
|
|
|
exist: Intervals{{1, 10}, {12, 20}, {25, 30}},
|
|
|
|
new: Interval{18, 23},
|
|
|
|
exp: Intervals{{1, 10}, {12, 23}, {25, 30}},
|
2017-05-17 09:19:42 +00:00
|
|
|
},
|
|
|
|
{
|
2017-08-25 08:11:46 +00:00
|
|
|
exist: Intervals{{1, 10}, {12, 20}, {25, 30}},
|
|
|
|
new: Interval{9, 23},
|
|
|
|
exp: Intervals{{1, 23}, {25, 30}},
|
2017-05-22 11:12:36 +00:00
|
|
|
},
|
|
|
|
{
|
2017-08-25 08:11:46 +00:00
|
|
|
exist: Intervals{{1, 10}, {12, 20}, {25, 30}},
|
|
|
|
new: Interval{9, 230},
|
|
|
|
exp: Intervals{{1, 230}},
|
2017-05-22 11:12:36 +00:00
|
|
|
},
|
|
|
|
{
|
2017-08-25 08:11:46 +00:00
|
|
|
exist: Intervals{{5, 10}, {12, 20}, {25, 30}},
|
|
|
|
new: Interval{1, 4},
|
|
|
|
exp: Intervals{{1, 10}, {12, 20}, {25, 30}},
|
2017-05-22 11:12:36 +00:00
|
|
|
},
|
|
|
|
{
|
2017-08-25 08:11:46 +00:00
|
|
|
exist: Intervals{{5, 10}, {12, 20}, {25, 30}},
|
|
|
|
new: Interval{11, 14},
|
|
|
|
exp: Intervals{{5, 20}, {25, 30}},
|
2017-05-17 09:19:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2017-05-22 11:12:36 +00:00
|
|
|
|
2017-12-07 01:06:14 +00:00
|
|
|
testutil.Equals(t, c.exp, c.exist.add(c.new))
|
2017-05-17 09:19:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-08 11:52:01 +00:00
|
|
|
|
|
|
|
// TestMemTombstonesConcurrency to make sure they are safe to access from different goroutines.
|
|
|
|
func TestMemTombstonesConcurrency(t *testing.T) {
|
2018-11-14 16:40:01 +00:00
|
|
|
tomb := newMemTombstones()
|
2018-06-08 11:52:01 +00:00
|
|
|
totalRuns := 100
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for x := 0; x < totalRuns; x++ {
|
|
|
|
tomb.addInterval(uint64(x), Interval{int64(x), int64(x)})
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
for x := 0; x < totalRuns; x++ {
|
2019-04-25 10:07:04 +00:00
|
|
|
_, err := tomb.Get(uint64(x))
|
|
|
|
testutil.Ok(t, err)
|
2018-06-08 11:52:01 +00:00
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
}
|