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:
Fabian Reinartz 2018-05-22 08:51:20 -04:00
parent def912ce0e
commit 7841d417b3
2 changed files with 16 additions and 13 deletions

5
db.go
View file

@ -502,7 +502,6 @@ func (db *DB) reload() (err error) {
sort.Slice(blocks, func(i, j int) bool { sort.Slice(blocks, func(i, j int) bool {
return blocks[i].Meta().MinTime < blocks[j].Meta().MinTime return blocks[i].Meta().MinTime < blocks[j].Meta().MinTime
}) })
if err := validateBlockSequence(blocks); err != nil { if err := validateBlockSequence(blocks); err != nil {
return errors.Wrap(err, "invalid block sequence") return errors.Wrap(err, "invalid block sequence")
} }
@ -597,10 +596,6 @@ func OverlappingBlocks(bm []BlockMeta) Overlaps {
if len(bm) <= 1 { if len(bm) <= 1 {
return nil return nil
} }
sort.Slice(bm, func(i, j int) bool {
return bm[i].MinTime < bm[j].MinTime
})
var ( var (
overlaps [][]BlockMeta overlaps [][]BlockMeta

View file

@ -1025,33 +1025,41 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) {
testutil.Assert(t, len(OverlappingBlocks(metas)) == 0, "we found unexpected overlaps") 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 overlaps with 10-20.
o1 := BlockMeta{MinTime: 15, MaxTime: 17} o1 := BlockMeta{MinTime: 15, MaxTime: 17}
testutil.Equals(t, Overlaps{ testutil.Equals(t, Overlaps{
{Min: 15, Max: 17}: {metas[1], o1}, {Min: 15, Max: 17}: {metas[1], o1},
}, OverlappingBlocks(append(metas, o1))) }, OverlappingBlocks(add(o1)))
// o2 overlaps with 20-30 and 30-40. // o2 overlaps with 20-30 and 30-40.
o2 := BlockMeta{MinTime: 21, MaxTime: 31} o2 := BlockMeta{MinTime: 21, MaxTime: 31}
testutil.Equals(t, Overlaps{ testutil.Equals(t, Overlaps{
{Min: 21, Max: 30}: {metas[2], o2}, {Min: 21, Max: 30}: {metas[2], o2},
{Min: 30, Max: 31}: {o2, metas[3]}, {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 and o3b overlaps with 30-40 and each other.
o3a := BlockMeta{MinTime: 33, MaxTime: 39} o3a := BlockMeta{MinTime: 33, MaxTime: 39}
o3b := BlockMeta{MinTime: 34, MaxTime: 36} o3b := BlockMeta{MinTime: 34, MaxTime: 36}
testutil.Equals(t, Overlaps{ testutil.Equals(t, Overlaps{
{Min: 34, Max: 36}: {metas[3], o3a, o3b}, {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 is 1:1 overlap with 50-60.
o4 := BlockMeta{MinTime: 50, MaxTime: 60} o4 := BlockMeta{MinTime: 50, MaxTime: 60}
testutil.Equals(t, Overlaps{ testutil.Equals(t, Overlaps{
{Min: 50, Max: 60}: {metas[5], o4}, {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 overlaps with 60-70, 70-80 and 80-90.
o5 := BlockMeta{MinTime: 61, MaxTime: 85} o5 := BlockMeta{MinTime: 61, MaxTime: 85}
@ -1059,7 +1067,7 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) {
{Min: 61, Max: 70}: {metas[6], o5}, {Min: 61, Max: 70}: {metas[6], o5},
{Min: 70, Max: 80}: {o5, metas[7]}, {Min: 70, Max: 80}: {o5, metas[7]},
{Min: 80, Max: 85}: {o5, metas[8]}, {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 overlaps with 90-100, 100-110 and o6b, o6b overlaps with 90-100 and o6a.
o6a := BlockMeta{MinTime: 92, MaxTime: 105} o6a := BlockMeta{MinTime: 92, MaxTime: 105}
@ -1067,7 +1075,7 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) {
testutil.Equals(t, Overlaps{ testutil.Equals(t, Overlaps{
{Min: 94, Max: 99}: {metas[9], o6a, o6b}, {Min: 94, Max: 99}: {metas[9], o6a, o6b},
{Min: 100, Max: 105}: {o6a, metas[10]}, {Min: 100, Max: 105}: {o6a, metas[10]},
}, OverlappingBlocks(append(metas, o6a, o6b))) }, OverlappingBlocks(add(o6a, o6b)))
// All together. // All together.
testutil.Equals(t, Overlaps{ testutil.Equals(t, Overlaps{
@ -1077,7 +1085,7 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) {
{Min: 50, Max: 60}: {metas[5], o4}, {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: 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]}, {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. // Additional case.
var nc1 []BlockMeta var nc1 []BlockMeta