Fix compacting disable/enable
Enabling and disabling compaction no longer blocks are potentially
causes panics.
@@ -100,7 +100,6 @@ type DB struct {
opts *Options
// Mutex for that must be held when modifying the general block layout.
- // cmtx must be held before acquiring it.
mtx sync.RWMutex
blocks []Block
@@ -118,7 +117,7 @@ type DB struct {
// cmtx is used to control compactions and deletions.
cmtx sync.Mutex
- compacting bool
+ compactionsEnabled bool
}
type dbMetrics struct {
@@ -203,7 +202,7 @@ func Open(dir string, l log.Logger, r prometheus.Registerer, opts *Options) (db
compactc: make(chan struct{}, 1),
donec: make(chan struct{}),
stopc: make(chan struct{}),
- compacting: true,
+ compactionsEnabled: true,
}
db.metrics = newDBMetrics(db, r)
@@ -371,6 +370,10 @@ func (db *DB) compact() (changes bool, err error) {
db.cmtx.Lock()
defer db.cmtx.Unlock()
+ if !db.compactionsEnabled {
+ return false, nil
+ }
+
// Check whether we have pending head blocks that are ready to be persisted.
// They have the highest priority.
for _, h := range db.completedHeads() {
@@ -579,21 +582,21 @@ func (db *DB) Close() error {
// DisableCompactions disables compactions.
func (db *DB) DisableCompactions() {
- if db.compacting {
db.cmtx.Lock()
- db.compacting = false
+ defer db.cmtx.Unlock()
+
+ db.compactionsEnabled = false
db.logger.Log("msg", "compactions disabled")
}
-}
// EnableCompactions enables compactions.
func (db *DB) EnableCompactions() {
- if !db.compacting {
- db.cmtx.Unlock()
- db.compacting = true
+ db.cmtx.Lock()
+ defer db.cmtx.Unlock()
+
+ db.compactionsEnabled = true
db.logger.Log("msg", "compactions enabled")
}
-}
// Snapshot writes the current data to the directory.
func (db *DB) Snapshot(dir string) error {
