From 4f45e201cc244c78352dd74d7b92f26387cca6db Mon Sep 17 00:00:00 2001 From: Arthur Silva Sens Date: Mon, 12 Oct 2020 18:15:40 -0300 Subject: [PATCH] Promtool tsdb list now prints block sizes (#7993) * promtool tsdb list now prints blocks' size Signed-off-by: arthursens --- cmd/promtool/tsdb.go | 13 +++++++++++-- tsdb/block.go | 3 +++ tsdb/compact_test.go | 1 + tsdb/db.go | 4 +--- tsdb/head.go | 12 ++++++++++++ tsdb/mocks_test.go | 1 + 6 files changed, 29 insertions(+), 5 deletions(-) diff --git a/cmd/promtool/tsdb.go b/cmd/promtool/tsdb.go index e3da413e4..cb692e9ef 100644 --- a/cmd/promtool/tsdb.go +++ b/cmd/promtool/tsdb.go @@ -30,6 +30,7 @@ import ( "text/tabwriter" "time" + "github.com/alecthomas/units" "github.com/go-kit/kit/log" "github.com/pkg/errors" "github.com/prometheus/prometheus/pkg/labels" @@ -363,12 +364,12 @@ func printBlocks(blocks []tsdb.BlockReader, humanReadable bool) { tw := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) defer tw.Flush() - fmt.Fprintln(tw, "BLOCK ULID\tMIN TIME\tMAX TIME\tDURATION\tNUM SAMPLES\tNUM CHUNKS\tNUM SERIES") + fmt.Fprintln(tw, "BLOCK ULID\tMIN TIME\tMAX TIME\tDURATION\tNUM SAMPLES\tNUM CHUNKS\tNUM SERIES\tSIZE") for _, b := range blocks { meta := b.Meta() fmt.Fprintf(tw, - "%v\t%v\t%v\t%v\t%v\t%v\t%v\n", + "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\n", meta.ULID, getFormatedTime(meta.MinTime, humanReadable), getFormatedTime(meta.MaxTime, humanReadable), @@ -376,6 +377,7 @@ func printBlocks(blocks []tsdb.BlockReader, humanReadable bool) { meta.Stats.NumSamples, meta.Stats.NumChunks, meta.Stats.NumSeries, + getFormatedBytes(b.Size(), humanReadable), ) } } @@ -387,6 +389,13 @@ func getFormatedTime(timestamp int64, humanReadable bool) string { return strconv.FormatInt(timestamp, 10) } +func getFormatedBytes(bytes int64, humanReadable bool) string { + if humanReadable { + return units.Base2Bytes(bytes).String() + } + return strconv.FormatInt(bytes, 10) +} + func openBlock(path, blockID string) (*tsdb.DBReadOnly, tsdb.BlockReader, error) { db, err := tsdb.OpenDBReadOnly(path, nil) if err != nil { diff --git a/tsdb/block.go b/tsdb/block.go index d708bc809..0df30e846 100644 --- a/tsdb/block.go +++ b/tsdb/block.go @@ -124,6 +124,9 @@ type BlockReader interface { // Meta provides meta information about the block reader. Meta() BlockMeta + + // Size returns the number of bytes that the block takes up on disk. + Size() int64 } // BlockMeta provides meta information about a block. diff --git a/tsdb/compact_test.go b/tsdb/compact_test.go index a1a50f5a6..160c25fff 100644 --- a/tsdb/compact_test.go +++ b/tsdb/compact_test.go @@ -461,6 +461,7 @@ func (erringBReader) Index() (IndexReader, error) { return nil, error func (erringBReader) Chunks() (ChunkReader, error) { return nil, errors.New("chunks") } func (erringBReader) Tombstones() (tombstones.Reader, error) { return nil, errors.New("tombstones") } func (erringBReader) Meta() BlockMeta { return BlockMeta{} } +func (erringBReader) Size() int64 { return 0 } type nopChunkWriter struct{} diff --git a/tsdb/db.go b/tsdb/db.go index b045fa155..3fe4c94d9 100644 --- a/tsdb/db.go +++ b/tsdb/db.go @@ -1103,11 +1103,9 @@ func BeyondSizeRetention(db *DB, blocks []*Block) (deletable map[ulid.ULID]struc deletable = make(map[ulid.ULID]struct{}) - walSize, _ := db.Head().wal.Size() - headChunksSize := db.Head().chunkDiskMapper.Size() // Initializing size counter with WAL size and Head chunks // written to disk, as that is part of the retention strategy. - blocksSize := walSize + headChunksSize + blocksSize := db.Head().Size() for i, block := range blocks { blocksSize += block.Size() if blocksSize > int64(db.opts.MaxBytes) { diff --git a/tsdb/head.go b/tsdb/head.go index 60c26a418..019991e0f 100644 --- a/tsdb/head.go +++ b/tsdb/head.go @@ -2340,3 +2340,15 @@ type noopSeriesLifecycleCallback struct{} func (noopSeriesLifecycleCallback) PreCreation(labels.Labels) error { return nil } func (noopSeriesLifecycleCallback) PostCreation(labels.Labels) {} func (noopSeriesLifecycleCallback) PostDeletion(...labels.Labels) {} + +func (h *Head) Size() int64 { + var walSize int64 + if h.wal != nil { + walSize, _ = h.wal.Size() + } + return walSize + h.chunkDiskMapper.Size() +} + +func (h *RangeHead) Size() int64 { + return h.head.Size() +} diff --git a/tsdb/mocks_test.go b/tsdb/mocks_test.go index 5294115d9..c186762a1 100644 --- a/tsdb/mocks_test.go +++ b/tsdb/mocks_test.go @@ -65,3 +65,4 @@ func (r *mockBReader) Tombstones() (tombstones.Reader, error) { return tombstones.NewMemTombstones(), nil } func (r *mockBReader) Meta() BlockMeta { return BlockMeta{MinTime: r.mint, MaxTime: r.maxt} } +func (r *mockBReader) Size() int64 { return 0 }