From 01ce9b5c44fa531fd6e81e7c93ada3daaea64552 Mon Sep 17 00:00:00 2001 From: vyzo Date: Thu, 11 Mar 2021 11:45:05 +0200 Subject: [PATCH 1/3] add Compact to badger blockstore --- blockstore/badger/blockstore.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/blockstore/badger/blockstore.go b/blockstore/badger/blockstore.go index 2c00f424077..7c1615711f6 100644 --- a/blockstore/badger/blockstore.go +++ b/blockstore/badger/blockstore.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "runtime" "sync/atomic" "github.com/dgraph-io/badger/v2" @@ -150,6 +151,20 @@ func (b *Blockstore) CollectGarbage() error { return err } +// Compact runs a synchronous compaction +func (b *Blockstore) Compact() error { + if atomic.LoadInt64(&b.state) != stateOpen { + return ErrBlockstoreClosed + } + + nworkers := runtime.NumCPU() / 2 + if nworkers < 2 { + nworkers = 2 + } + + return b.DB.Flatten(nworkers) +} + // View implements blockstore.Viewer, which leverages zero-copy read-only // access to values. func (b *Blockstore) View(cid cid.Cid, fn func([]byte) error) error { From 353bb1881f7d23139167a803cdf8f28bf79050af Mon Sep 17 00:00:00 2001 From: vyzo Date: Thu, 11 Mar 2021 11:45:19 +0200 Subject: [PATCH 2/3] compact hotstore if it provides the method --- blockstore/splitstore/splitstore.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/blockstore/splitstore/splitstore.go b/blockstore/splitstore/splitstore.go index fb3e2880315..bec19284ac2 100644 --- a/blockstore/splitstore/splitstore.go +++ b/blockstore/splitstore/splitstore.go @@ -798,6 +798,18 @@ func (s *SplitStore) purgeTracking(cids []cid.Cid) error { } func (s *SplitStore) gcHotstore() { + if compact, ok := s.hot.(interface{ Compact() error }); ok { + log.Infof("compacting hotstore") + startCompact := time.Now() + err := compact.Compact() + if err != nil { + log.Warnf("error compacting hotstore: %s", err) + return + } else { + log.Infow("hotstore compaction done", "took", time.Since(startCompact)) + } + } + if gc, ok := s.hot.(interface{ CollectGarbage() error }); ok { log.Infof("garbage collecting hotstore") startGC := time.Now() From 1b1d3606cd67480e5250ff56080a65107d4fee33 Mon Sep 17 00:00:00 2001 From: vyzo Date: Thu, 11 Mar 2021 13:10:44 +0200 Subject: [PATCH 3/3] make linter happy --- blockstore/splitstore/splitstore.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/blockstore/splitstore/splitstore.go b/blockstore/splitstore/splitstore.go index bec19284ac2..23b2d342720 100644 --- a/blockstore/splitstore/splitstore.go +++ b/blockstore/splitstore/splitstore.go @@ -805,9 +805,8 @@ func (s *SplitStore) gcHotstore() { if err != nil { log.Warnf("error compacting hotstore: %s", err) return - } else { - log.Infow("hotstore compaction done", "took", time.Since(startCompact)) } + log.Infow("hotstore compaction done", "took", time.Since(startCompact)) } if gc, ok := s.hot.(interface{ CollectGarbage() error }); ok { @@ -816,9 +815,9 @@ func (s *SplitStore) gcHotstore() { err := gc.CollectGarbage() if err != nil { log.Warnf("error garbage collecting hotstore: %s", err) - } else { - log.Infow("garbage collection done", "took", time.Since(startGC)) + return } + log.Infow("hotstore garbage collection done", "took", time.Since(startGC)) } }