2017-05-26 08:56:31 -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.
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
package tombstones
|
2017-05-16 20:06:56 -07:00
|
|
|
|
|
|
|
import (
|
2020-06-29 09:00:22 -07:00
|
|
|
"math"
|
2017-05-16 20:06:56 -07:00
|
|
|
"math/rand"
|
2018-06-08 04:52:01 -07:00
|
|
|
"sync"
|
2017-05-16 20:06:56 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
2017-12-06 17:06:14 -08:00
|
|
|
|
2021-06-11 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
2020-10-29 02:43:23 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-11-06 03:10:04 -07:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/storage"
|
2023-04-17 06:20:58 -07:00
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2017-05-16 20:06:56 -07:00
|
|
|
)
|
|
|
|
|
2020-07-21 01:08:06 -07:00
|
|
|
func TestMain(m *testing.M) {
|
2023-04-17 06:20:58 -07:00
|
|
|
testutil.TolerantVerifyLeak(m)
|
2020-07-21 01:08:06 -07:00
|
|
|
}
|
|
|
|
|
2020-06-29 09:00:22 -07:00
|
|
|
func TestWriteAndReadbackTombstones(t *testing.T) {
|
2022-01-22 01:55:01 -08:00
|
|
|
tmpdir := t.TempDir()
|
2017-05-16 20:06:56 -07:00
|
|
|
|
2017-09-04 07:08:38 -07:00
|
|
|
ref := uint64(0)
|
2017-05-16 20:06:56 -07:00
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
stones := NewMemTombstones()
|
2017-05-16 20:06:56 -07:00
|
|
|
// Generate the tombstones.
|
|
|
|
for i := 0; i < 100; i++ {
|
2017-09-04 07:08:38 -07:00
|
|
|
ref += uint64(rand.Int31n(10)) + 1
|
2017-05-24 01:12:56 -07:00
|
|
|
numRanges := rand.Intn(5) + 1
|
2017-08-25 01:11:46 -07:00
|
|
|
dranges := make(Intervals, 0, numRanges)
|
2017-05-16 20:06:56 -07:00
|
|
|
mint := rand.Int63n(time.Now().UnixNano())
|
|
|
|
for j := 0; j < numRanges; j++ {
|
2019-09-19 02:15:41 -07:00
|
|
|
dranges = dranges.Add(Interval{mint, mint + rand.Int63n(1000)})
|
2017-05-16 20:06:56 -07:00
|
|
|
mint += rand.Int63n(1000) + 1
|
|
|
|
}
|
2021-11-06 03:10:04 -07:00
|
|
|
stones.AddInterval(storage.SeriesRef(ref), dranges...)
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
_, err := WriteFile(log.NewNopLogger(), tmpdir, stones)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2017-05-16 20:06:56 -07:00
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
restr, _, err := ReadTombstones(tmpdir)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2017-11-13 04:32:24 -08:00
|
|
|
|
2017-05-16 20:06:56 -07:00
|
|
|
// Compare the two readers.
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, stones, restr)
|
2017-05-16 20:06:56 -07:00
|
|
|
}
|
2017-05-17 02:19:42 -07:00
|
|
|
|
2021-09-15 23:50:03 -07:00
|
|
|
func TestDeletingTombstones(t *testing.T) {
|
|
|
|
stones := NewMemTombstones()
|
|
|
|
|
2021-11-06 03:10:04 -07:00
|
|
|
ref := storage.SeriesRef(42)
|
2021-09-15 23:50:03 -07:00
|
|
|
mint := rand.Int63n(time.Now().UnixNano())
|
|
|
|
dranges := make(Intervals, 0, 1)
|
|
|
|
dranges = dranges.Add(Interval{mint, mint + rand.Int63n(1000)})
|
|
|
|
stones.AddInterval(ref, dranges...)
|
2021-11-06 03:10:04 -07:00
|
|
|
stones.AddInterval(storage.SeriesRef(43), dranges...)
|
2021-09-15 23:50:03 -07:00
|
|
|
|
|
|
|
intervals, err := stones.Get(ref)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, intervals, dranges)
|
|
|
|
|
2021-11-06 03:10:04 -07:00
|
|
|
stones.DeleteTombstones(map[storage.SeriesRef]struct{}{ref: {}})
|
2021-09-15 23:50:03 -07:00
|
|
|
|
|
|
|
intervals, err = stones.Get(ref)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, intervals)
|
|
|
|
}
|
|
|
|
|
2023-05-17 06:15:12 -07:00
|
|
|
func TestTombstonesGetWithCopy(t *testing.T) {
|
|
|
|
stones := NewMemTombstones()
|
|
|
|
stones.AddInterval(1, Intervals{{Mint: 1, Maxt: 2}, {Mint: 7, Maxt: 8}, {Mint: 11, Maxt: 12}}...)
|
|
|
|
|
|
|
|
intervals0, err := stones.Get(1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, Intervals{{Mint: 1, Maxt: 2}, {Mint: 7, Maxt: 8}, {Mint: 11, Maxt: 12}}, intervals0)
|
|
|
|
intervals1 := intervals0.Add(Interval{Mint: 4, Maxt: 6})
|
|
|
|
require.Equal(t, Intervals{{Mint: 1, Maxt: 2}, {Mint: 4, Maxt: 8}, {Mint: 11, Maxt: 12}}, intervals0) // Original slice changed.
|
|
|
|
require.Equal(t, Intervals{{Mint: 1, Maxt: 2}, {Mint: 4, Maxt: 8}, {Mint: 11, Maxt: 12}}, intervals1)
|
|
|
|
|
|
|
|
intervals2, err := stones.Get(1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, Intervals{{Mint: 1, Maxt: 2}, {Mint: 7, Maxt: 8}, {Mint: 11, Maxt: 12}}, intervals2)
|
|
|
|
}
|
|
|
|
|
2021-09-15 23:50:03 -07:00
|
|
|
func TestTruncateBefore(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
before Intervals
|
|
|
|
beforeT int64
|
|
|
|
after Intervals
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
before: Intervals{{1, 2}, {4, 10}, {12, 100}},
|
|
|
|
beforeT: 3,
|
|
|
|
after: Intervals{{4, 10}, {12, 100}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
before: Intervals{{1, 2}, {4, 10}, {12, 100}, {200, 1000}},
|
|
|
|
beforeT: 900,
|
|
|
|
after: Intervals{{200, 1000}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
before: Intervals{{1, 2}, {4, 10}, {12, 100}, {200, 1000}},
|
|
|
|
beforeT: 2000,
|
|
|
|
after: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
before: Intervals{{1, 2}, {4, 10}, {12, 100}, {200, 1000}},
|
|
|
|
beforeT: 0,
|
|
|
|
after: Intervals{{1, 2}, {4, 10}, {12, 100}, {200, 1000}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
2021-11-06 03:10:04 -07:00
|
|
|
ref := storage.SeriesRef(42)
|
2021-09-15 23:50:03 -07:00
|
|
|
stones := NewMemTombstones()
|
|
|
|
stones.AddInterval(ref, c.before...)
|
|
|
|
|
|
|
|
stones.TruncateBefore(c.beforeT)
|
|
|
|
ts, err := stones.Get(ref)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, c.after, ts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 02:19:42 -07:00
|
|
|
func TestAddingNewIntervals(t *testing.T) {
|
|
|
|
cases := []struct {
|
2017-08-25 01:11:46 -07:00
|
|
|
exist Intervals
|
|
|
|
new Interval
|
2017-05-17 02:19:42 -07:00
|
|
|
|
2017-08-25 01:11:46 -07:00
|
|
|
exp Intervals
|
2017-05-17 02:19:42 -07:00
|
|
|
}{
|
|
|
|
{
|
2017-08-25 01:11:46 -07:00
|
|
|
new: Interval{1, 2},
|
|
|
|
exp: Intervals{{1, 2}},
|
2017-05-17 02:19:42 -07:00
|
|
|
},
|
|
|
|
{
|
2017-08-25 01:11:46 -07:00
|
|
|
exist: Intervals{{1, 2}},
|
|
|
|
new: Interval{1, 2},
|
|
|
|
exp: Intervals{{1, 2}},
|
2017-05-17 02:19:42 -07:00
|
|
|
},
|
2017-05-21 22:01:50 -07:00
|
|
|
{
|
2017-08-25 01:11:46 -07:00
|
|
|
exist: Intervals{{1, 4}, {6, 6}},
|
|
|
|
new: Interval{5, 6},
|
|
|
|
exp: Intervals{{1, 6}},
|
2017-05-21 22:01:50 -07:00
|
|
|
},
|
2017-05-17 02:19:42 -07:00
|
|
|
{
|
2017-08-25 01:11:46 -07:00
|
|
|
exist: Intervals{{1, 10}, {12, 20}, {25, 30}},
|
2020-06-29 09:00:22 -07:00
|
|
|
new: Interval{21, 25},
|
|
|
|
exp: Intervals{{1, 10}, {12, 30}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
exist: Intervals{{1, 10}, {12, 20}, {25, 30}},
|
|
|
|
new: Interval{22, 23},
|
|
|
|
exp: Intervals{{1, 10}, {12, 20}, {22, 23}, {25, 30}},
|
2017-05-17 02:19:42 -07:00
|
|
|
},
|
|
|
|
{
|
2017-08-25 01:11:46 -07:00
|
|
|
exist: Intervals{{1, 2}, {3, 5}, {7, 7}},
|
|
|
|
new: Interval{6, 7},
|
|
|
|
exp: Intervals{{1, 2}, {3, 7}},
|
2017-05-17 02:19:42 -07:00
|
|
|
},
|
|
|
|
{
|
2017-08-25 01:11:46 -07:00
|
|
|
exist: Intervals{{1, 10}, {12, 20}, {25, 30}},
|
|
|
|
new: Interval{18, 23},
|
|
|
|
exp: Intervals{{1, 10}, {12, 23}, {25, 30}},
|
2017-05-17 02:19:42 -07:00
|
|
|
},
|
|
|
|
{
|
2017-08-25 01:11:46 -07:00
|
|
|
exist: Intervals{{1, 10}, {12, 20}, {25, 30}},
|
|
|
|
new: Interval{9, 23},
|
|
|
|
exp: Intervals{{1, 23}, {25, 30}},
|
2017-05-22 04:12:36 -07:00
|
|
|
},
|
|
|
|
{
|
2017-08-25 01:11:46 -07:00
|
|
|
exist: Intervals{{1, 10}, {12, 20}, {25, 30}},
|
|
|
|
new: Interval{9, 230},
|
|
|
|
exp: Intervals{{1, 230}},
|
2017-05-22 04:12:36 -07:00
|
|
|
},
|
|
|
|
{
|
2017-08-25 01:11:46 -07:00
|
|
|
exist: Intervals{{5, 10}, {12, 20}, {25, 30}},
|
|
|
|
new: Interval{1, 4},
|
|
|
|
exp: Intervals{{1, 10}, {12, 20}, {25, 30}},
|
2017-05-22 04:12:36 -07:00
|
|
|
},
|
|
|
|
{
|
2017-08-25 01:11:46 -07:00
|
|
|
exist: Intervals{{5, 10}, {12, 20}, {25, 30}},
|
|
|
|
new: Interval{11, 14},
|
|
|
|
exp: Intervals{{5, 20}, {25, 30}},
|
2017-05-17 02:19:42 -07:00
|
|
|
},
|
2020-01-05 16:32:29 -08:00
|
|
|
{
|
|
|
|
exist: Intervals{{5, 10}, {12, 20}, {25, 30}},
|
|
|
|
new: Interval{1, 3},
|
|
|
|
exp: Intervals{{1, 3}, {5, 10}, {12, 20}, {25, 30}},
|
|
|
|
},
|
2020-06-29 09:00:22 -07:00
|
|
|
{
|
|
|
|
exist: Intervals{{5, 10}, {12, 20}, {25, 30}},
|
|
|
|
new: Interval{35, 40},
|
|
|
|
exp: Intervals{{5, 10}, {12, 20}, {25, 30}, {35, 40}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
new: Interval{math.MinInt64, 2},
|
|
|
|
exp: Intervals{{math.MinInt64, 2}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
exist: Intervals{{math.MinInt64, 2}},
|
|
|
|
new: Interval{9, math.MaxInt64},
|
|
|
|
exp: Intervals{{math.MinInt64, 2}, {9, math.MaxInt64}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
exist: Intervals{{9, math.MaxInt64}},
|
|
|
|
new: Interval{math.MinInt64, 2},
|
|
|
|
exp: Intervals{{math.MinInt64, 2}, {9, math.MaxInt64}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
exist: Intervals{{9, math.MaxInt64}},
|
|
|
|
new: Interval{math.MinInt64, 10},
|
|
|
|
exp: Intervals{{math.MinInt64, math.MaxInt64}},
|
|
|
|
},
|
2023-05-17 06:15:12 -07:00
|
|
|
{
|
|
|
|
exist: Intervals{{9, 10}},
|
|
|
|
new: Interval{math.MinInt64, 7},
|
|
|
|
exp: Intervals{{math.MinInt64, 7}, {9, 10}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
exist: Intervals{{9, 10}},
|
|
|
|
new: Interval{12, math.MaxInt64},
|
|
|
|
exp: Intervals{{9, 10}, {12, math.MaxInt64}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
exist: Intervals{{9, 10}},
|
|
|
|
new: Interval{math.MinInt64, 8},
|
|
|
|
exp: Intervals{{math.MinInt64, 10}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
exist: Intervals{{9, 10}},
|
|
|
|
new: Interval{11, math.MaxInt64},
|
|
|
|
exp: Intervals{{9, math.MaxInt64}},
|
|
|
|
},
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2020-06-29 09:00:22 -07:00
|
|
|
t.Run("", func(t *testing.T) {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, c.exp, c.exist.Add(c.new))
|
2020-06-29 09:00:22 -07:00
|
|
|
})
|
2017-05-17 02:19:42 -07:00
|
|
|
}
|
|
|
|
}
|
2018-06-08 04:52:01 -07:00
|
|
|
|
|
|
|
// TestMemTombstonesConcurrency to make sure they are safe to access from different goroutines.
|
|
|
|
func TestMemTombstonesConcurrency(t *testing.T) {
|
2019-09-19 02:15:41 -07:00
|
|
|
tomb := NewMemTombstones()
|
2018-06-08 04:52:01 -07:00
|
|
|
totalRuns := 100
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for x := 0; x < totalRuns; x++ {
|
2021-11-06 03:10:04 -07:00
|
|
|
tomb.AddInterval(storage.SeriesRef(x), Interval{int64(x), int64(x)})
|
2018-06-08 04:52:01 -07:00
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
for x := 0; x < totalRuns; x++ {
|
2021-11-06 03:10:04 -07:00
|
|
|
_, err := tomb.Get(storage.SeriesRef(x))
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2018-06-08 04:52:01 -07:00
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
}
|