Skip to content

Commit

Permalink
kvdb+etcd: do not create transaction if the STM write set is empty
Browse files Browse the repository at this point in the history
This is a small optimization that will skip building of the STM
transaction upon commit when the write set is empty. As we're already
reading a snapshot, transaction formation is useless.
This also prevents retries when doing a kvdb.View and any of the keys
change.
  • Loading branch information
bhandras committed Jun 30, 2020
1 parent 033354e commit d403d8b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions channeldb/kvdb/etcd/stm.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,14 @@ func (s *stm) OnCommit(cb func()) {
// because the keys have changed return a CommitError, otherwise return a
// DatabaseError.
func (s *stm) commit() (CommitStats, error) {
// Do not create the transaction if the write set is empty.
if len(s.wset) == 0 {
return CommitStats{
Rset: len(s.rset),
Wset: 0,
}, nil
}

rset := s.rset.cmps(s.lset)
wset := s.wset.cmps(s.revision + 1)

Expand Down
4 changes: 4 additions & 0 deletions channeldb/kvdb/etcd/stm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ func TestManualTxError(t *testing.T) {
require.NoError(t, err)
require.Equal(t, []byte("xyz"), val)

// Rewrinting the key/val will make the commit fail due
// to the conflicting external put above.
stm.Put("123", "abc")

// Commit will fail with CommitError.
err = stm.Commit()
var e CommitError
Expand Down

0 comments on commit d403d8b

Please sign in to comment.