From a3bd8107b44561a1a128db1613660c0549359cda Mon Sep 17 00:00:00 2001 From: Jan Kalina Date: Tue, 12 Dec 2023 12:05:31 +0100 Subject: [PATCH] Fix race on sizeEstimation in Flushable --- kvdb/flushable/flushable.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/kvdb/flushable/flushable.go b/kvdb/flushable/flushable.go index 024411fd..5645dae3 100644 --- a/kvdb/flushable/flushable.go +++ b/kvdb/flushable/flushable.go @@ -176,11 +176,17 @@ func (w *Flushable) Drop() { // NotFlushedPairs returns num of not flushed keys, including deleted keys. func (w *Flushable) NotFlushedPairs() int { + w.lock.RLock() + defer w.lock.RUnlock() + return w.modified.Size() } // NotFlushedSizeEst returns estimation of not flushed data, including deleted keys. func (w *Flushable) NotFlushedSizeEst() int { + w.lock.RLock() + defer w.lock.RUnlock() + return *w.sizeEstimation } @@ -228,12 +234,20 @@ func (w *Flushable) flush() error { // Stat returns a particular internal stat of the database. func (w *Flushable) Stat(property string) (string, error) { - return w.underlying.Stat(property) + w.lock.RLock() + underlying := w.underlying + w.lock.RUnlock() + + return underlying.Stat(property) } // Compact flattens the underlying data store for the given key range. func (w *Flushable) Compact(start []byte, limit []byte) error { - return w.underlying.Compact(start, limit) + w.lock.RLock() + underlying := w.underlying + w.lock.RUnlock() + + return underlying.Compact(start, limit) } /*