From e190c7c78d1129819ad7a6d0179e3c1937708ee9 Mon Sep 17 00:00:00 2001 From: Bas Harenslak Date: Sun, 1 Oct 2017 22:18:50 +0200 Subject: [PATCH 1/3] Add list blocks command to CLI --- cmd/tsdb/main.go | 8 ++++++++ db.go | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/cmd/tsdb/main.go b/cmd/tsdb/main.go index 60f39e5d5..514a0bed1 100644 --- a/cmd/tsdb/main.go +++ b/cmd/tsdb/main.go @@ -43,6 +43,8 @@ func main() { benchWriteOutPath = benchWriteCmd.Flag("out", "set the output path").Default("benchout/").String() benchWriteNumMetrics = benchWriteCmd.Flag("metrics", "number of metrics to read").Default("10000").Int() benchSamplesFile = benchWriteCmd.Arg("file", "input file with samples data, default is (../../testdata/20k.series)").Default("../../testdata/20k.series").String() + listCmd = cli.Command("ls", "list db blocks") + listPath = listCmd.Arg("db path", "database path").Default("benchout/storage").String() ) switch kingpin.MustParse(cli.Parse(os.Args[1:])) { @@ -53,6 +55,12 @@ func main() { samplesFile: *benchSamplesFile, } wb.run() + case listCmd.FullCommand(): + db, err := tsdb.Open(*listPath, nil, nil, nil) + if err != nil { + exitWithError(err) + } + fmt.Println(db.PrintBlocks()) } flag.CommandLine.Set("log.level", "debug") } diff --git a/db.go b/db.go index 24437ada0..d353040ca 100644 --- a/db.go +++ b/db.go @@ -38,6 +38,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/tsdb/chunks" "github.com/prometheus/tsdb/labels" + "github.com/ryanuber/columnize" ) // DefaultOptions used for the DB. They are sane for setups using @@ -228,6 +229,25 @@ func (db *DB) Dir() string { return db.dir } +func (db *DB) PrintBlocks() string { + db.mtx.RLock() + defer db.mtx.RUnlock() + + var output []string + output = append(output, "BLOCK ULID | MIN TIME | MAX TIME | NUM SAMPLES | NUM CHUNKS | NUM SERIES") + for _, b := range db.blocks { + output = append(output, fmt.Sprintf("%v | %v | %v | %v | %v | %v", + b.Meta().ULID, + b.Meta().MinTime, + b.Meta().MaxTime, + b.Meta().Stats.NumSamples, + b.Meta().Stats.NumChunks, + b.Meta().Stats.NumSeries, + )) + } + return columnize.SimpleFormat(output) +} + func (db *DB) run() { defer close(db.donec) From 9945a67bfffb7fb3738b8d509c79fec693f6522e Mon Sep 17 00:00:00 2001 From: Bas Harenslak Date: Mon, 2 Oct 2017 22:25:51 +0200 Subject: [PATCH 2/3] Replace columnize by stdlib tabwriter --- cmd/tsdb/main.go | 4 ++-- db.go | 15 ++++++++------- tabwriter.go | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 tabwriter.go diff --git a/cmd/tsdb/main.go b/cmd/tsdb/main.go index 514a0bed1..e7e77d0d2 100644 --- a/cmd/tsdb/main.go +++ b/cmd/tsdb/main.go @@ -44,7 +44,7 @@ func main() { benchWriteNumMetrics = benchWriteCmd.Flag("metrics", "number of metrics to read").Default("10000").Int() benchSamplesFile = benchWriteCmd.Arg("file", "input file with samples data, default is (../../testdata/20k.series)").Default("../../testdata/20k.series").String() listCmd = cli.Command("ls", "list db blocks") - listPath = listCmd.Arg("db path", "database path").Default("benchout/storage").String() + listPath = listCmd.Arg("db path", "database path (default is benchout/storage)").Default("benchout/storage").String() ) switch kingpin.MustParse(cli.Parse(os.Args[1:])) { @@ -60,7 +60,7 @@ func main() { if err != nil { exitWithError(err) } - fmt.Println(db.PrintBlocks()) + db.PrintBlocks() } flag.CommandLine.Set("log.level", "debug") } diff --git a/db.go b/db.go index d353040ca..f72c9e7e3 100644 --- a/db.go +++ b/db.go @@ -38,7 +38,6 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/tsdb/chunks" "github.com/prometheus/tsdb/labels" - "github.com/ryanuber/columnize" ) // DefaultOptions used for the DB. They are sane for setups using @@ -229,23 +228,25 @@ func (db *DB) Dir() string { return db.dir } -func (db *DB) PrintBlocks() string { +func (db *DB) PrintBlocks() { db.mtx.RLock() defer db.mtx.RUnlock() - var output []string - output = append(output, "BLOCK ULID | MIN TIME | MAX TIME | NUM SAMPLES | NUM CHUNKS | NUM SERIES") + tw := GetNewTabWriter(os.Stdout) + defer tw.Flush() + + fmt.Fprintln(tw, "BLOCK ULID\tMIN TIME\tMAX TIME\tNUM SAMPLES\tNUM CHUNKS\tNUM SERIES") for _, b := range db.blocks { - output = append(output, fmt.Sprintf("%v | %v | %v | %v | %v | %v", + fmt.Fprintf(tw, + "%v\t%v\t%v\t%v\t%v\t%v\n", b.Meta().ULID, b.Meta().MinTime, b.Meta().MaxTime, b.Meta().Stats.NumSamples, b.Meta().Stats.NumChunks, b.Meta().Stats.NumSeries, - )) + ) } - return columnize.SimpleFormat(output) } func (db *DB) run() { diff --git a/tabwriter.go b/tabwriter.go new file mode 100644 index 000000000..8e84a9c67 --- /dev/null +++ b/tabwriter.go @@ -0,0 +1,18 @@ +package tsdb + +import ( + "io" + "text/tabwriter" +) + +const ( + minwidth = 0 + tabwidth = 0 + padding = 2 + padchar = ' ' + flags = 0 +) + +func GetNewTabWriter(output io.Writer) *tabwriter.Writer { + return tabwriter.NewWriter(output, minwidth, tabwidth, padding, padchar, flags) +} From a41dad30fba5a3a13479934c7d0613ef80662b0f Mon Sep 17 00:00:00 2001 From: Bas Harenslak Date: Mon, 2 Oct 2017 22:48:47 +0200 Subject: [PATCH 3/3] Move printing db.blocks to main.go --- cmd/tsdb/main.go | 20 +++++++++++++++++++- db.go | 21 --------------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/cmd/tsdb/main.go b/cmd/tsdb/main.go index e7e77d0d2..fe84340c0 100644 --- a/cmd/tsdb/main.go +++ b/cmd/tsdb/main.go @@ -60,7 +60,7 @@ func main() { if err != nil { exitWithError(err) } - db.PrintBlocks() + printBlocks(db.Blocks()) } flag.CommandLine.Set("log.level", "debug") } @@ -331,3 +331,21 @@ func exitWithError(err error) { fmt.Fprintln(os.Stderr, err) os.Exit(1) } + +func printBlocks(blocks []tsdb.DiskBlock) { + tw := tsdb.GetNewTabWriter(os.Stdout) + defer tw.Flush() + + fmt.Fprintln(tw, "BLOCK ULID\tMIN TIME\tMAX TIME\tNUM SAMPLES\tNUM CHUNKS\tNUM SERIES") + for _, b := range blocks { + fmt.Fprintf(tw, + "%v\t%v\t%v\t%v\t%v\t%v\n", + b.Meta().ULID, + b.Meta().MinTime, + b.Meta().MaxTime, + b.Meta().Stats.NumSamples, + b.Meta().Stats.NumChunks, + b.Meta().Stats.NumSeries, + ) + } +} diff --git a/db.go b/db.go index f72c9e7e3..24437ada0 100644 --- a/db.go +++ b/db.go @@ -228,27 +228,6 @@ func (db *DB) Dir() string { return db.dir } -func (db *DB) PrintBlocks() { - db.mtx.RLock() - defer db.mtx.RUnlock() - - tw := GetNewTabWriter(os.Stdout) - defer tw.Flush() - - fmt.Fprintln(tw, "BLOCK ULID\tMIN TIME\tMAX TIME\tNUM SAMPLES\tNUM CHUNKS\tNUM SERIES") - for _, b := range db.blocks { - fmt.Fprintf(tw, - "%v\t%v\t%v\t%v\t%v\t%v\n", - b.Meta().ULID, - b.Meta().MinTime, - b.Meta().MaxTime, - b.Meta().Stats.NumSamples, - b.Meta().Stats.NumChunks, - b.Meta().Stats.NumSeries, - ) - } -} - func (db *DB) run() { defer close(db.donec)