Add Head.compactable method (#542)

* Add Head.compactable method

Signed-off-by: zhulongcheng <zhulongcheng.me@gmail.com>
This commit is contained in:
zhulongcheng 2019-04-01 16:19:06 +08:00 committed by Krasi Georgiev
parent fd188be052
commit aed16621c0
2 changed files with 10 additions and 5 deletions

8
db.go
View file

@ -391,7 +391,7 @@ func (a dbAppender) Commit() error {
// We could just run this check every few minutes practically. But for benchmarks // We could just run this check every few minutes practically. But for benchmarks
// and high frequency use cases this is the safer way. // 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 { select {
case a.db.compactc <- struct{}{}: case a.db.compactc <- struct{}{}:
default: default:
@ -418,13 +418,11 @@ func (db *DB) compact() (err error) {
return nil return nil
default: default:
} }
// The head has a compactable range if 1.5 level 0 ranges are between the oldest if !db.head.compactable() {
// 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 {
break break
} }
mint := db.head.MinTime() 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. // Wrap head into a range that bounds all reads to it.
head := &rangeHead{ head := &rangeHead{

View file

@ -1020,6 +1020,13 @@ func (h *Head) MaxTime() int64 {
return atomic.LoadInt64(&h.maxTime) 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. // Close flushes the WAL and closes the head.
func (h *Head) Close() error { func (h *Head) Close() error {
if h.wal == nil { if h.wal == nil {