Skip to content

Commit

Permalink
mvcc: allow large concurrent reads under light write workload
Browse files Browse the repository at this point in the history
  • Loading branch information
xiang90 committed Feb 8, 2018
1 parent b83244b commit 84909fb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
6 changes: 1 addition & 5 deletions internal/mvcc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,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)
Expand Down
8 changes: 8 additions & 0 deletions internal/mvcc/backend/batch_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -231,6 +232,13 @@ func (t *batchTxBuffered) CommitAndStop() {
}

func (t *batchTxBuffered) commit(stop bool) {
// no need to reset read tx if there is no write.
// call commit to update stats.
if t.batchTx.pending == 0 && !stop {
t.batchTx.commit(stop)
return
}

// all read txs must be closed to acquire boltdb commit rwlock
t.backend.readTx.mu.Lock()
defer t.backend.readTx.mu.Unlock()
Expand Down
2 changes: 2 additions & 0 deletions internal/mvcc/kvstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,8 @@ func TestTxnBlockBackendForceCommit(t *testing.T) {
s := NewStore(b, &lease.FakeLessor{}, nil)
defer os.Remove(tmpPath)

// Put a key into the store so that force commit can take effect.
s.Put([]byte("foo"), []byte("bar"), lease.NoLease)
txn := s.Read()

done := make(chan struct{})
Expand Down

0 comments on commit 84909fb

Please sign in to comment.