From fb5c5535fc2dc76d224478c9390a34845d3b07c3 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Thu, 18 May 2017 16:31:02 +0200 Subject: [PATCH] Misc cleanup --- db.go | 30 ++++++------------------------ wal_test.go | 6 +----- 2 files changed, 7 insertions(+), 29 deletions(-) diff --git a/db.go b/db.go index 56a13e268b..34d1e884bb 100644 --- a/db.go +++ b/db.go @@ -482,26 +482,6 @@ func (db *DB) Close() error { func (db *DB) Appender() Appender { db.mtx.RLock() return &dbAppender{db: db} - - // // XXX(fabxc): turn off creating initial appender as it will happen on-demand - // // anyway. For now this, with combination of only having a single timestamp per batch, - // // prevents opening more than one appender and hitting an unresolved deadlock (#11). - // // - - // // Only instantiate appender after returning the headmtx to avoid - // // questionable locking order. - // db.headmtx.RLock() - // app := db.appendable() - // db.headmtx.RUnlock() - - // for _, b := range app { - // a.heads = append(a.heads, &metaAppender{ - // meta: b.Meta(), - // app: b.Appender(), - // }) - // } - - // return a } type dbAppender struct { @@ -612,14 +592,16 @@ func rangeForTimestamp(t int64, width int64) (mint, maxt int64) { func (db *DB) ensureHead(t int64) error { mint, maxt := rangeForTimestamp(t, int64(db.opts.MinBlockDuration)) - // Initial case with an empty database. t is the first timestamp we ever received. - // Create an additional buffering block in front. - if len(db.blocks) == 0 { + last := db.blocks[len(db.blocks)-1].Meta() + // Create another block of buffer in front if the DB is initialized or retrieving + // new data after a long gap. + // This ensures we always have a full block width if append window. + if len(db.blocks) == 0 || last.MaxTime <= mint-int64(db.opts.MinBlockDuration) { if _, err := db.createHeadBlock(mint-int64(db.opts.MinBlockDuration), mint); err != nil { return err } // If the previous block reaches into our new window, make it smaller. - } else if mt := db.blocks[len(db.blocks)-1].Meta().MaxTime; mt > mint { + } else if mt := last.MaxTime; mt > mint { mint = mt } if mint >= maxt { diff --git a/wal_test.go b/wal_test.go index cbd6c62477..c2988e7e04 100644 --- a/wal_test.go +++ b/wal_test.go @@ -320,17 +320,13 @@ func TestWALRestoreCorrupted(t *testing.T) { require.Equal(t, 0, len(l)) require.Equal(t, []RefSample{{T: 1, V: 2}}, s) - // Truncation should happen transparently and now cause an error. + // Truncation should happen transparently and not cause an error. require.False(t, r.Next()) require.Nil(t, r.Err()) require.NoError(t, w2.Log(nil, []RefSample{{T: 99, V: 100}})) require.NoError(t, w2.Close()) - files, err := fileutil.ReadDir(dir) - require.NoError(t, err) - require.Equal(t, 1, len(files)) - // We should see the first valid entry and the new one, everything after // is truncated. w3, err := OpenSegmentWAL(dir, logger, 0)