mirror of
https://github.com/prometheus/prometheus.git
synced 2025-01-11 13:57:36 -08:00
db: Simplified algorithm.
Signed-off-by: Bartek Plotka <bwplotka@gmail.com>
This commit is contained in:
parent
51ce1cc7ff
commit
c8b4a7b839
74
db.go
74
db.go
|
@ -575,6 +575,10 @@ func validateBlockSequence(bs []*Block) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// OverlappingBlocks returns all overlapping blocks from given meta files.
|
// OverlappingBlocks returns all overlapping blocks from given meta files.
|
||||||
|
// We sort blocks by minTime. Then we iterate over each block minTime and treat it as our "current" timestamp.
|
||||||
|
// We check all the pending blocks (blocks that we have seen their minTimes, but their maxTime was still ahead current
|
||||||
|
// timestamp) if they did not finish. If not, it means they overlap with our current b. In the same time b is assumed as
|
||||||
|
// pending.
|
||||||
func OverlappingBlocks(bm []BlockMeta) (overlaps [][]BlockMeta) {
|
func OverlappingBlocks(bm []BlockMeta) (overlaps [][]BlockMeta) {
|
||||||
if len(bm) <= 1 {
|
if len(bm) <= 1 {
|
||||||
return nil
|
return nil
|
||||||
|
@ -583,65 +587,37 @@ func OverlappingBlocks(bm []BlockMeta) (overlaps [][]BlockMeta) {
|
||||||
return bm[i].MinTime < bm[j].MinTime
|
return bm[i].MinTime < bm[j].MinTime
|
||||||
})
|
})
|
||||||
|
|
||||||
for i, b := range bm[1:] {
|
pending := []BlockMeta{bm[0]}
|
||||||
prev := bm[i]
|
for _, b := range bm[1:] {
|
||||||
if b.MinTime >= prev.MaxTime {
|
var (
|
||||||
|
newPending []BlockMeta
|
||||||
|
samePendings = true
|
||||||
|
)
|
||||||
|
|
||||||
|
for _, p := range pending {
|
||||||
|
if b.MinTime >= p.MaxTime {
|
||||||
|
samePendings = false
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// prev overlaps with b.
|
|
||||||
|
|
||||||
overlap := []BlockMeta{prev}
|
// "p" overlaps with "b" and "p" is still pending.
|
||||||
|
newPending = append(newPending, p)
|
||||||
|
}
|
||||||
|
// Our block "b" is now pending.
|
||||||
|
pending = append(newPending, b)
|
||||||
|
|
||||||
// Check if prev overlaps with something else.
|
if len(newPending) == 0 {
|
||||||
for j, fb := range bm[i+1:] {
|
continue
|
||||||
if fb.MinTime >= prev.MaxTime {
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if fb.MinTime >= bm[i+j].MaxTime {
|
if samePendings && len(overlaps) > 0 {
|
||||||
// fb overlaps with prev, but fb does not overlap with previous block. Pack in separate group.
|
overlaps[len(overlaps)-1] = append(overlaps[len(overlaps)-1], b)
|
||||||
overlaps = append(overlaps, overlap)
|
continue
|
||||||
overlap = []BlockMeta{prev}
|
|
||||||
}
|
}
|
||||||
|
overlaps = append(overlaps, append(newPending, b))
|
||||||
|
|
||||||
overlap = append(overlap, fb)
|
|
||||||
}
|
}
|
||||||
overlaps = append(overlaps, overlap)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(overlaps) < 2 {
|
|
||||||
return overlaps
|
return overlaps
|
||||||
}
|
|
||||||
|
|
||||||
// Deduplicate cases like {a, b, c} {b, c} into just {a, b, c}
|
|
||||||
newOverlaps := [][]BlockMeta{overlaps[0]}
|
|
||||||
for i, overlap := range overlaps[1:] {
|
|
||||||
prev := overlaps[i]
|
|
||||||
|
|
||||||
// Check if prev contains all overlap elements.
|
|
||||||
found := false
|
|
||||||
for _, o := range overlap {
|
|
||||||
found = false
|
|
||||||
for _, p := range prev {
|
|
||||||
if p.ULID.Compare(o.ULID) == 0 {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if found {
|
|
||||||
// We can ignore this overlap.
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
newOverlaps = append(newOverlaps, overlap)
|
|
||||||
}
|
|
||||||
|
|
||||||
return newOverlaps
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) String() string {
|
func (db *DB) String() string {
|
||||||
|
|
Loading…
Reference in a new issue