mirror of
https://github.com/prometheus/prometheus.git
synced 2025-01-11 05:47:27 -08:00
db: Tiny tuning of algo + added proper print.
Signed-off-by: Bartek Plotka <bwplotka@gmail.com>
This commit is contained in:
parent
1e60f02066
commit
f07d829946
45
db.go
45
db.go
|
@ -29,6 +29,8 @@ import (
|
||||||
|
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
|
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/go-kit/kit/log"
|
"github.com/go-kit/kit/log"
|
||||||
"github.com/go-kit/kit/log/level"
|
"github.com/go-kit/kit/log/level"
|
||||||
"github.com/nightlyone/lockfile"
|
"github.com/nightlyone/lockfile"
|
||||||
|
@ -567,11 +569,11 @@ func validateBlockSequence(bs []*Block) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
overlaps := OverlappingBlocks(metas)
|
overlaps := OverlappingBlocks(metas)
|
||||||
if len(overlaps) == 0 {
|
if len(overlaps) > 0 {
|
||||||
return nil
|
return errors.Errorf("block time ranges overlap: %s", PrintOverlappedBlocks(overlaps))
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.Errorf("block time ranges overlap (%v)", overlaps)
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// OverlappingBlocks returns all overlapping blocks from given meta files.
|
// OverlappingBlocks returns all overlapping blocks from given meta files.
|
||||||
|
@ -587,14 +589,17 @@ func OverlappingBlocks(bm []BlockMeta) (overlaps [][]BlockMeta) {
|
||||||
return bm[i].MinTime < bm[j].MinTime
|
return bm[i].MinTime < bm[j].MinTime
|
||||||
})
|
})
|
||||||
|
|
||||||
pending := []BlockMeta{bm[0]}
|
var (
|
||||||
|
// pending contains not ended blocks in regards to "current" timestamp.
|
||||||
|
pending = []BlockMeta{bm[0]}
|
||||||
|
// Same pending helps to aggregate same overlaps to single group.
|
||||||
|
samePendings = true
|
||||||
|
)
|
||||||
for _, b := range bm[1:] {
|
for _, b := range bm[1:] {
|
||||||
var (
|
var newPending []BlockMeta
|
||||||
newPending []BlockMeta
|
|
||||||
samePendings = true
|
|
||||||
)
|
|
||||||
|
|
||||||
for _, p := range pending {
|
for _, p := range pending {
|
||||||
|
// "b.MinTime" is our current time.
|
||||||
if b.MinTime >= p.MaxTime {
|
if b.MinTime >= p.MaxTime {
|
||||||
samePendings = false
|
samePendings = false
|
||||||
continue
|
continue
|
||||||
|
@ -603,10 +608,11 @@ func OverlappingBlocks(bm []BlockMeta) (overlaps [][]BlockMeta) {
|
||||||
// "p" overlaps with "b" and "p" is still pending.
|
// "p" overlaps with "b" and "p" is still pending.
|
||||||
newPending = append(newPending, p)
|
newPending = append(newPending, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Our block "b" is now pending.
|
// Our block "b" is now pending.
|
||||||
pending = append(newPending, b)
|
pending = append(newPending, b)
|
||||||
|
|
||||||
if len(newPending) == 0 {
|
if len(newPending) == 0 {
|
||||||
|
// No overlaps.
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -615,11 +621,30 @@ func OverlappingBlocks(bm []BlockMeta) (overlaps [][]BlockMeta) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
overlaps = append(overlaps, append(newPending, b))
|
overlaps = append(overlaps, append(newPending, b))
|
||||||
|
samePendings = true
|
||||||
}
|
}
|
||||||
return overlaps
|
return overlaps
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrintOverlappedBlocks returns human readable string form of overlapped blocks.
|
||||||
|
func PrintOverlappedBlocks(overlaps [][]BlockMeta) string {
|
||||||
|
var res []string
|
||||||
|
for _, o := range overlaps {
|
||||||
|
var groups []string
|
||||||
|
for _, m := range o {
|
||||||
|
groups = append(groups, fmt.Sprintf(
|
||||||
|
"[id: %s mint: %d maxt: %d range: %s]",
|
||||||
|
m.ULID.String(),
|
||||||
|
m.MinTime,
|
||||||
|
m.MaxTime,
|
||||||
|
(time.Duration((m.MaxTime-m.MinTime)/1000)*time.Second).String(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
res = append(res, fmt.Sprintf("<%s>", strings.Join(groups, "")))
|
||||||
|
}
|
||||||
|
return strings.Join(res, "")
|
||||||
|
}
|
||||||
|
|
||||||
func (db *DB) String() string {
|
func (db *DB) String() string {
|
||||||
return "HEAD"
|
return "HEAD"
|
||||||
}
|
}
|
||||||
|
|
10
db_test.go
10
db_test.go
|
@ -930,4 +930,14 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) {
|
||||||
o6a := BlockMeta{MinTime: 92, MaxTime: 105}
|
o6a := BlockMeta{MinTime: 92, MaxTime: 105}
|
||||||
o6b := BlockMeta{MinTime: 94, MaxTime: 99}
|
o6b := BlockMeta{MinTime: 94, MaxTime: 99}
|
||||||
testutil.Equals(t, [][]BlockMeta{{metas[9], o6a, o6b}, {o6a, metas[10]}}, OverlappingBlocks(append(metas, o6a, o6b)))
|
testutil.Equals(t, [][]BlockMeta{{metas[9], o6a, o6b}, {o6a, metas[10]}}, OverlappingBlocks(append(metas, o6a, o6b)))
|
||||||
|
|
||||||
|
// All together.
|
||||||
|
testutil.Equals(t, [][]BlockMeta{
|
||||||
|
{metas[1], o1},
|
||||||
|
{metas[2], o2}, {o2, metas[3]},
|
||||||
|
{metas[3], o3a, o3b},
|
||||||
|
{metas[5], o4},
|
||||||
|
{metas[6], o5}, {o5, metas[7]}, {o5, metas[8]},
|
||||||
|
{metas[9], o6a, o6b}, {o6a, metas[10]},
|
||||||
|
}, OverlappingBlocks(append(metas, o1, o2, o3a, o3b, o4, o5, o6a, o6b)))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue