From 563713e128e1b3ccbbd8cd369a117da1f91ddd58 Mon Sep 17 00:00:00 2001 From: Benjamin Wang Date: Fri, 11 Nov 2022 17:35:26 +0800 Subject: [PATCH] etcdserver: call the OnPreCommitUnsafe in unsafeCommit `unsafeCommit` is called by both `(*batchTxBuffered) commit` and `(*backend) defrag`. When users perform the defragmentation operation, etcd doesn't update the consistent index. If etcd crashes(e.g. panicking) in the process for whatever reason, then etcd replays the WAL entries starting from the latest snapshot, accordingly it may re-apply entries which might have already been applied, eventually the revision isn't consistent with other members. Refer to discussion in https://github.com/etcd-io/etcd/pull/14685 Signed-off-by: Benjamin Wang --- server/mvcc/backend/batch_tx.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/server/mvcc/backend/batch_tx.go b/server/mvcc/backend/batch_tx.go index c8fa55954f6..9c025d79e1e 100644 --- a/server/mvcc/backend/batch_tx.go +++ b/server/mvcc/backend/batch_tx.go @@ -329,10 +329,6 @@ func (t *batchTxBuffered) CommitAndStop() { } func (t *batchTxBuffered) commit(stop bool) { - if t.backend.hooks != nil { - t.backend.hooks.OnPreCommitUnsafe(t) - } - // all read txs must be closed to acquire boltdb commit rwlock t.backend.readTx.Lock() t.unsafeCommit(stop) @@ -340,6 +336,9 @@ func (t *batchTxBuffered) commit(stop bool) { } func (t *batchTxBuffered) unsafeCommit(stop bool) { + if t.backend.hooks != nil { + t.backend.hooks.OnPreCommitUnsafe(t) + } if t.backend.readTx.tx != nil { // wait all store read transactions using the current boltdb tx to finish, // then close the boltdb tx