tsdb: Avoid potential overflow in SortFunc

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Arve Knudsen 2023-10-10 09:49:10 +02:00
parent 27d0e12f32
commit 26d07ee8d3

View file

@ -631,7 +631,14 @@ func (db *DBReadOnly) Blocks() ([]BlockReader, error) {
}
slices.SortFunc(loadable, func(a, b *Block) int {
return int(a.Meta().MinTime - b.Meta().MinTime)
switch {
case a.Meta().MinTime < b.Meta().MinTime:
return -1
case a.Meta().MinTime > b.Meta().MinTime:
return 1
default:
return 0
}
})
blockMetas := make([]BlockMeta, 0, len(loadable))
@ -1507,7 +1514,14 @@ func (db *DB) reloadBlocks() (err error) {
db.metrics.blocksBytes.Set(float64(blocksSize))
slices.SortFunc(toLoad, func(a, b *Block) int {
return int(a.Meta().MinTime - b.Meta().MinTime)
switch {
case a.Meta().MinTime < b.Meta().MinTime:
return -1
case a.Meta().MinTime > b.Meta().MinTime:
return 1
default:
return 0
}
})
// Swap new blocks first for subsequently created readers to be seen.
@ -1582,7 +1596,14 @@ func deletableBlocks(db *DB, blocks []*Block) map[ulid.ULID]struct{} {
// Sort the blocks by time - newest to oldest (largest to smallest timestamp).
// This ensures that the retentions will remove the oldest blocks.
slices.SortFunc(blocks, func(a, b *Block) int {
return int(b.Meta().MaxTime - a.Meta().MaxTime)
switch {
case b.Meta().MaxTime < a.Meta().MaxTime:
return -1
case b.Meta().MaxTime > a.Meta().MaxTime:
return 1
default:
return 0
}
})
for _, block := range blocks {