From ff08c9690e6be8161032fa94a174646ef211dfb9 Mon Sep 17 00:00:00 2001 From: Roman Atachiants Date: Mon, 13 Jun 2022 00:50:02 +0400 Subject: [PATCH] Fix snapshot race (#69) --- snapshot.go | 12 ++++++++++++ txn_lock.go | 17 ----------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/snapshot.go b/snapshot.go index 18d2505..ce4b4a0 100644 --- a/snapshot.go +++ b/snapshot.go @@ -218,3 +218,15 @@ func (c *Collection) chunks() int { max, _ := c.fill.Max() return int(commit.ChunkAt(max) + 1) } + +// readChunk acquires appropriate locks for a chunk and executes a read callback. +// This is used for snapshotting purposes only. +func (c *Collection) readChunk(chunk commit.Chunk, fn func(uint64, commit.Chunk, bitmap.Bitmap) error) error { + + // Lock both the chunk and the fill list + c.slock.RLock(uint(chunk)) + c.lock.RLock() + defer c.slock.RUnlock(uint(chunk)) + defer c.lock.RUnlock() + return fn(c.commits[chunk], chunk, chunk.OfBitmap(c.fill)) +} diff --git a/txn_lock.go b/txn_lock.go index 2ca206b..d2db367 100644 --- a/txn_lock.go +++ b/txn_lock.go @@ -93,20 +93,3 @@ func (txn *Txn) rangeWrite(fn func(commitID uint64, chunk commit.Chunk, fill bit lock.Unlock(uint(chunk)) }) } - -// readChunk acquires appropriate locks for a chunk and executes a read callback -func (c *Collection) readChunk(chunk commit.Chunk, fn func(uint64, commit.Chunk, bitmap.Bitmap) error) (err error) { - lock := c.slock - lock.RLock(uint(chunk)) - - // Compute the fill - c.lock.RLock() - fill := chunk.OfBitmap(c.fill) - commitID := c.commits[chunk] - c.lock.RUnlock() - - // Call the delegate - err = fn(commitID, chunk, fill) - lock.RUnlock(uint(chunk)) - return -}