From 3ebee21407da8913f183a9e1354513ba8759521e Mon Sep 17 00:00:00 2001 From: Xiang Date: Wed, 7 Feb 2018 13:20:58 -0800 Subject: [PATCH] mvcc: allow large concurrent reads under light write workload --- internal/mvcc/backend/backend.go | 10 ++++------ internal/mvcc/backend/batch_tx.go | 7 +++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/internal/mvcc/backend/backend.go b/internal/mvcc/backend/backend.go index 5fc126167b9..bdc8a0d0f07 100644 --- a/internal/mvcc/backend/backend.go +++ b/internal/mvcc/backend/backend.go @@ -271,7 +271,9 @@ func (b *backend) run() { b.batchTx.CommitAndStop() return } - b.batchTx.Commit() + if b.batchTx.safePending() != 0 { + b.batchTx.Commit() + } t.Reset(b.batchInterval) } } @@ -302,11 +304,7 @@ func (b *backend) defrag() error { b.mu.Lock() defer b.mu.Unlock() - // block concurrent read requests while resetting tx - b.readTx.mu.Lock() - defer b.readTx.mu.Unlock() - - b.batchTx.unsafeCommit(true) + b.batchTx.commit(true) b.batchTx.tx = nil tmpdb, err := bolt.Open(b.db.Path()+".tmp", 0600, boltOpenOptions) diff --git a/internal/mvcc/backend/batch_tx.go b/internal/mvcc/backend/batch_tx.go index 7bb5f8d4a05..ceb0b1739df 100644 --- a/internal/mvcc/backend/batch_tx.go +++ b/internal/mvcc/backend/batch_tx.go @@ -98,6 +98,7 @@ func unsafeRange(c *bolt.Cursor, key, endKey []byte, limit int64) (keys [][]byte isMatch = func(b []byte) bool { return bytes.Equal(b, key) } limit = 1 } + for ck, cv := c.Seek(key); ck != nil && isMatch(ck); ck, cv = c.Next() { vs = append(vs, cv) keys = append(keys, ck) @@ -154,6 +155,12 @@ func (t *batchTx) Unlock() { t.Mutex.Unlock() } +func (t *batchTx) safePending() int { + t.Mutex.Lock() + defer t.Mutex.Unlock() + return t.pending +} + func (t *batchTx) commit(stop bool) { // commit the last tx if t.tx != nil {