mirror of
https://github.com/prometheus/prometheus
synced 2024-12-27 00:53:12 +00:00
Ensure blocks are time-ordered in memory
We assume in multiple places that the block list held by DB has blocks sequential by time. A regression caused us to hold them ordered by ULID, i.e. by creation time instead. Signed-off-by: Fabian Reinartz <freinartz@google.com>
This commit is contained in:
parent
def912ce0e
commit
7841d417b3
5
db.go
5
db.go
@ -502,7 +502,6 @@ func (db *DB) reload() (err error) {
|
||||
sort.Slice(blocks, func(i, j int) bool {
|
||||
return blocks[i].Meta().MinTime < blocks[j].Meta().MinTime
|
||||
})
|
||||
|
||||
if err := validateBlockSequence(blocks); err != nil {
|
||||
return errors.Wrap(err, "invalid block sequence")
|
||||
}
|
||||
@ -597,10 +596,6 @@ func OverlappingBlocks(bm []BlockMeta) Overlaps {
|
||||
if len(bm) <= 1 {
|
||||
return nil
|
||||
}
|
||||
sort.Slice(bm, func(i, j int) bool {
|
||||
return bm[i].MinTime < bm[j].MinTime
|
||||
})
|
||||
|
||||
var (
|
||||
overlaps [][]BlockMeta
|
||||
|
||||
|
24
db_test.go
24
db_test.go
@ -1025,33 +1025,41 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) {
|
||||
|
||||
testutil.Assert(t, len(OverlappingBlocks(metas)) == 0, "we found unexpected overlaps")
|
||||
|
||||
// Add overlapping blocks.
|
||||
// Add overlapping blocks. We've to establish order again since we aren't interested
|
||||
// in trivial overlaps caused by unorderedness.
|
||||
add := func(ms ...BlockMeta) []BlockMeta {
|
||||
repl := append(append([]BlockMeta{}, metas...), ms...)
|
||||
sort.Slice(repl, func(i, j int) bool {
|
||||
return repl[i].MinTime < repl[j].MinTime
|
||||
})
|
||||
return repl
|
||||
}
|
||||
|
||||
// o1 overlaps with 10-20.
|
||||
o1 := BlockMeta{MinTime: 15, MaxTime: 17}
|
||||
testutil.Equals(t, Overlaps{
|
||||
{Min: 15, Max: 17}: {metas[1], o1},
|
||||
}, OverlappingBlocks(append(metas, o1)))
|
||||
}, OverlappingBlocks(add(o1)))
|
||||
|
||||
// o2 overlaps with 20-30 and 30-40.
|
||||
o2 := BlockMeta{MinTime: 21, MaxTime: 31}
|
||||
testutil.Equals(t, Overlaps{
|
||||
{Min: 21, Max: 30}: {metas[2], o2},
|
||||
{Min: 30, Max: 31}: {o2, metas[3]},
|
||||
}, OverlappingBlocks(append(metas, o2)))
|
||||
}, OverlappingBlocks(add(o2)))
|
||||
|
||||
// o3a and o3b overlaps with 30-40 and each other.
|
||||
o3a := BlockMeta{MinTime: 33, MaxTime: 39}
|
||||
o3b := BlockMeta{MinTime: 34, MaxTime: 36}
|
||||
testutil.Equals(t, Overlaps{
|
||||
{Min: 34, Max: 36}: {metas[3], o3a, o3b},
|
||||
}, OverlappingBlocks(append(metas, o3a, o3b)))
|
||||
}, OverlappingBlocks(add(o3a, o3b)))
|
||||
|
||||
// o4 is 1:1 overlap with 50-60.
|
||||
o4 := BlockMeta{MinTime: 50, MaxTime: 60}
|
||||
testutil.Equals(t, Overlaps{
|
||||
{Min: 50, Max: 60}: {metas[5], o4},
|
||||
}, OverlappingBlocks(append(metas, o4)))
|
||||
}, OverlappingBlocks(add(o4)))
|
||||
|
||||
// o5 overlaps with 60-70, 70-80 and 80-90.
|
||||
o5 := BlockMeta{MinTime: 61, MaxTime: 85}
|
||||
@ -1059,7 +1067,7 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) {
|
||||
{Min: 61, Max: 70}: {metas[6], o5},
|
||||
{Min: 70, Max: 80}: {o5, metas[7]},
|
||||
{Min: 80, Max: 85}: {o5, metas[8]},
|
||||
}, OverlappingBlocks(append(metas, o5)))
|
||||
}, OverlappingBlocks(add(o5)))
|
||||
|
||||
// o6a overlaps with 90-100, 100-110 and o6b, o6b overlaps with 90-100 and o6a.
|
||||
o6a := BlockMeta{MinTime: 92, MaxTime: 105}
|
||||
@ -1067,7 +1075,7 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) {
|
||||
testutil.Equals(t, Overlaps{
|
||||
{Min: 94, Max: 99}: {metas[9], o6a, o6b},
|
||||
{Min: 100, Max: 105}: {o6a, metas[10]},
|
||||
}, OverlappingBlocks(append(metas, o6a, o6b)))
|
||||
}, OverlappingBlocks(add(o6a, o6b)))
|
||||
|
||||
// All together.
|
||||
testutil.Equals(t, Overlaps{
|
||||
@ -1077,7 +1085,7 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) {
|
||||
{Min: 50, Max: 60}: {metas[5], o4},
|
||||
{Min: 61, Max: 70}: {metas[6], o5}, {Min: 70, Max: 80}: {o5, metas[7]}, {Min: 80, Max: 85}: {o5, metas[8]},
|
||||
{Min: 94, Max: 99}: {metas[9], o6a, o6b}, {Min: 100, Max: 105}: {o6a, metas[10]},
|
||||
}, OverlappingBlocks(append(metas, o1, o2, o3a, o3b, o4, o5, o6a, o6b)))
|
||||
}, OverlappingBlocks(add(o1, o2, o3a, o3b, o4, o5, o6a, o6b)))
|
||||
|
||||
// Additional case.
|
||||
var nc1 []BlockMeta
|
||||
|
Loading…
Reference in New Issue
Block a user