From f1435f2e2c401bb1d3b7e077fe3f5e2f4e95c38a Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Wed, 8 Feb 2017 16:13:16 -0800 Subject: [PATCH] Track appended samples properly in metric --- db.go | 25 ++++++++++++++++++------- head.go | 1 + head_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/db.go b/db.go index 4b69601e7..72b82c778 100644 --- a/db.go +++ b/db.go @@ -29,8 +29,8 @@ import ( // millisecond precision timestampdb. var DefaultOptions = &Options{ WALFlushInterval: 5 * time.Second, - MinBlockDuration: 2 * 60 * 60 * 1000, // 2 hours in milliseconds - MaxBlockDuration: 48 * 60 * 60 * 1000, // 2 days in milliseconds + MinBlockDuration: 3 * 60 * 60 * 1000, // 2 hours in milliseconds + MaxBlockDuration: 24 * 60 * 60 * 1000, // 1 days in milliseconds AppendableBlocks: 2, } @@ -131,8 +131,8 @@ func Open(dir string, logger log.Logger, opts *Options) (db *DB, err error) { return nil, err } } - var r prometheus.Registerer - // r := prometheus.DefaultRegisterer + // var r prometheus.Registerer + r := prometheus.DefaultRegisterer if opts == nil { opts = DefaultOptions @@ -354,8 +354,9 @@ func (db *DB) Appender() Appender { } type dbAppender struct { - db *DB - heads []*headAppender + db *DB + heads []*headAppender + samples int } func (a *dbAppender) Add(lset labels.Labels, t int64, v float64) (uint64, error) { @@ -367,6 +368,7 @@ func (a *dbAppender) Add(lset labels.Labels, t int64, v float64) (uint64, error) if err != nil { return 0, err } + a.samples++ return ref | (uint64(h.generation) << 40), nil } @@ -379,6 +381,7 @@ func (a *dbAppender) hashedAdd(hash uint64, lset labels.Labels, t int64, v float if err != nil { return 0, err } + a.samples++ return ref | (uint64(h.generation) << 40), nil } @@ -396,7 +399,12 @@ func (a *dbAppender) AddFast(ref uint64, t int64, v float64) error { if h.generation != gen { return ErrNotFound } - return h.AddFast(ref, t, v) + if err := h.AddFast(ref, t, v); err != nil { + return err + } + + a.samples++ + return nil } // appenderFor gets the appender for the head containing timestamp t. @@ -469,6 +477,9 @@ func (a *dbAppender) Commit() error { } a.db.mtx.RUnlock() + if merr.Err() == nil { + a.db.metrics.samplesAppended.Add(float64(a.samples)) + } return merr.Err() } diff --git a/head.go b/head.go index 1c69ac3bb..db9e97ec7 100644 --- a/head.go +++ b/head.go @@ -43,6 +43,7 @@ type headBlock struct { activeWriters uint64 + symbols map[string]struct{} // descs holds all chunk descs for the head block. Each chunk implicitly // is assigned the index as its ID. series []*memSeries diff --git a/head_test.go b/head_test.go index 7cecb235c..b707d63c3 100644 --- a/head_test.go +++ b/head_test.go @@ -1,6 +1,8 @@ package tsdb import ( + "io/ioutil" + "os" "sort" "testing" @@ -33,3 +35,28 @@ func TestPositionMapper(t *testing.T) { require.Equal(t, c.res, m.fw) } } + +func BenchmarkCreateSeries(b *testing.B) { + f, err := os.Open("cmd/tsdb/testdata.1m") + require.NoError(b, err) + defer f.Close() + + lbls, err := readPrometheusLabels(f, 1e6) + require.NoError(b, err) + + b.Run("", func(b *testing.B) { + dir, err := ioutil.TempDir("", "create_series_bench") + require.NoError(b, err) + defer os.RemoveAll(dir) + + h, err := createHeadBlock(dir, 0, nil, 0, 1) + require.NoError(b, err) + + b.ReportAllocs() + b.ResetTimer() + + for _, l := range lbls[:b.N] { + h.create(l.Hash(), l) + } + }) +}