From aed16621c0e5b3b14c11360e6432375df87dcae4 Mon Sep 17 00:00:00 2001 From: zhulongcheng Date: Mon, 1 Apr 2019 16:19:06 +0800 Subject: [PATCH] Add Head.compactable method (#542) * Add Head.compactable method Signed-off-by: zhulongcheng --- db.go | 8 +++----- head.go | 7 +++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/db.go b/db.go index 80f8debdd..2ed31ea5f 100644 --- a/db.go +++ b/db.go @@ -391,7 +391,7 @@ func (a dbAppender) Commit() error { // We could just run this check every few minutes practically. But for benchmarks // and high frequency use cases this is the safer way. - if a.db.head.MaxTime()-a.db.head.MinTime() > a.db.head.chunkRange/2*3 { + if a.db.head.compactable() { select { case a.db.compactc <- struct{}{}: default: @@ -418,13 +418,11 @@ func (db *DB) compact() (err error) { return nil default: } - // The head has a compactable range if 1.5 level 0 ranges are between the oldest - // and newest timestamp. The 0.5 acts as a buffer of the appendable window. - if db.head.MaxTime()-db.head.MinTime() <= db.opts.BlockRanges[0]/2*3 { + if !db.head.compactable() { break } mint := db.head.MinTime() - maxt := rangeForTimestamp(mint, db.opts.BlockRanges[0]) + maxt := rangeForTimestamp(mint, db.head.chunkRange) // Wrap head into a range that bounds all reads to it. head := &rangeHead{ diff --git a/head.go b/head.go index 093042702..26a42fb33 100644 --- a/head.go +++ b/head.go @@ -1020,6 +1020,13 @@ func (h *Head) MaxTime() int64 { return atomic.LoadInt64(&h.maxTime) } +// compactable returns whether the head has a compactable range. +// The head has a compactable range when the head time range is 1.5 times the chunk range. +// The 0.5 acts as a buffer of the appendable window. +func (h *Head) compactable() bool { + return h.MaxTime()-h.MinTime() > h.chunkRange/2*3 +} + // Close flushes the WAL and closes the head. func (h *Head) Close() error { if h.wal == nil {