Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the data inconsistency by moving the SetConsistentIndex into the transaction lock #13844

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions server/etcdserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,7 @@ func (s *EtcdServer) apply(

// set the consistent index of current executing entry
if e.Index > s.consistIndex.ConsistentIndex() {
s.consistIndex.SetConsistentIndex(e.Index, e.Term)
s.setUnlockCallback(e.Index, e.Term)
shouldApplyV3 = membership.ApplyBoth
}

Expand All @@ -1795,13 +1795,21 @@ func (s *EtcdServer) apply(
return appliedt, appliedi, shouldStop
}

func (s *EtcdServer) setUnlockCallback(index, term uint64) {
if s.be != nil {
s.be.SetOnPreUnlockFunc(func() {
s.consistIndex.SetConsistentIndex(index, term)
})
}
}

// applyEntryNormal applies an EntryNormal type raftpb request to the EtcdServer
func (s *EtcdServer) applyEntryNormal(e *raftpb.Entry) {
shouldApplyV3 := membership.ApplyV2storeOnly
index := s.consistIndex.ConsistentIndex()
if e.Index > index {
// set the consistent index of current executing entry
s.consistIndex.SetConsistentIndex(e.Index, e.Term)
s.setUnlockCallback(e.Index, e.Term)
shouldApplyV3 = membership.ApplyBoth
}
s.lg.Debug("apply entry normal",
Expand Down
8 changes: 3 additions & 5 deletions server/etcdserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,10 +686,8 @@ func TestApplyConfigChangeUpdatesConsistIndex(t *testing.T) {
}}

_, appliedi, _ := srv.apply(ents, &raftpb.ConfState{})
assert.Equal(t, uint64(2), appliedi)
consistIndex := srv.consistIndex.ConsistentIndex()
if consistIndex != appliedi {
t.Fatalf("consistIndex = %v, want %v", consistIndex, appliedi)
}

t.Run("verify-backend", func(t *testing.T) {
tx := be.BatchTx()
Expand All @@ -698,9 +696,9 @@ func TestApplyConfigChangeUpdatesConsistIndex(t *testing.T) {
srv.beHooks.OnPreCommitUnsafe(tx)
assert.Equal(t, raftpb.ConfState{Voters: []uint64{2}}, *schema.UnsafeConfStateFromBackend(lg, tx))
})
rindex, rterm := schema.ReadConsistentIndex(be.BatchTx())
rindex, _ := schema.ReadConsistentIndex(be.BatchTx())
assert.Equal(t, consistIndex, rindex)
assert.Equal(t, uint64(4), rterm)

}

func realisticRaftNode(lg *zap.Logger) *raftNode {
Expand Down
22 changes: 22 additions & 0 deletions server/storage/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ type Backend interface {
Defrag() error
ForceCommit()
Close() error

// SetOnPreUnlockFunc sets a callback function which is called before unlocking the batchTx.
SetOnPreUnlockFunc(func())
OnPreUnlock()
}

type Snapshot interface {
Expand Down Expand Up @@ -119,9 +123,27 @@ type backend struct {

hooks Hooks

muUnlock sync.Mutex
onUnlock func()

lg *zap.Logger
}

func (b *backend) SetOnPreUnlockFunc(f func()) {
b.muUnlock.Lock()
defer b.muUnlock.Unlock()
b.onUnlock = f
}

func (b *backend) OnPreUnlock() {
b.muUnlock.Lock()
defer b.muUnlock.Unlock()
if b.onUnlock != nil {
b.onUnlock()
b.onUnlock = nil
}
}

type BackendConfig struct {
// Path is the file path to the backend file.
Path string
Expand Down
21 changes: 19 additions & 2 deletions server/storage/backend/batch_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func (t *batchTx) Lock() {
}

func (t *batchTx) Unlock() {
t.backend.OnPreUnlock()
t.SimpleUnlock()
}

func (t *batchTx) SimpleUnlock() {
if t.pending >= t.backend.batchLimit {
t.commit(false)
}
Expand Down Expand Up @@ -290,16 +295,28 @@ func (t *batchTxBuffered) Unlock() {
t.batchTx.Unlock()
}

func (t *batchTxBuffered) SimpleUnlock() {
if t.pending != 0 {
t.backend.readTx.Lock() // blocks txReadBuffer for writing.
t.buf.writeback(&t.backend.readTx.buf)
t.backend.readTx.Unlock()
if t.pending >= t.backend.batchLimit {
t.commit(false)
}
}
t.batchTx.SimpleUnlock()
}

func (t *batchTxBuffered) Commit() {
t.Lock()
t.commit(false)
t.Unlock()
t.SimpleUnlock()
}

func (t *batchTxBuffered) CommitAndStop() {
t.Lock()
t.commit(true)
t.Unlock()
t.SimpleUnlock()
}

func (t *batchTxBuffered) commit(stop bool) {
Expand Down
2 changes: 2 additions & 0 deletions server/storage/mvcc/kvstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,8 @@ func (b *fakeBackend) Snapshot() backend.Snapshot
func (b *fakeBackend) ForceCommit() {}
func (b *fakeBackend) Defrag() error { return nil }
func (b *fakeBackend) Close() error { return nil }
func (b *fakeBackend) SetOnPreUnlockFunc(func()) {}
func (b *fakeBackend) OnPreUnlock() {}

type indexGetResp struct {
rev revision
Expand Down