From 97d1f935f67b084089d2d55c8812560b02d64250 Mon Sep 17 00:00:00 2001 From: horizonzy Date: Wed, 29 Dec 2021 11:23:38 +0800 Subject: [PATCH 1/2] avoid always update meta term. --- server/storage/schema/cindex.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/server/storage/schema/cindex.go b/server/storage/schema/cindex.go index 5fb4d5bf6ca..52d9ef126cd 100644 --- a/server/storage/schema/cindex.go +++ b/server/storage/schema/cindex.go @@ -70,6 +70,17 @@ func UnsafeUpdateConsistentIndex(tx backend.BatchTx, index uint64, term uint64, if term == oldTerm && index <= oldi { return } + bs1 := make([]byte, 8) + binary.BigEndian.PutUint64(bs1, index) + // put the index into the underlying backend + // tx has been locked in TxnBegin, so there is no need to lock it again + tx.UnsafePut(Meta, MetaConsistentIndexKeyName, bs1) + if term > 0 && term > oldTerm { + bs2 := make([]byte, 8) + binary.BigEndian.PutUint64(bs2, term) + tx.UnsafePut(Meta, MetaTermKeyName, bs2) + } + return } bs1 := make([]byte, 8) From 282e4e6e889e5e9969cf47a052c7f38e427d8f7e Mon Sep 17 00:00:00 2001 From: horizonzy Date: Wed, 19 Jan 2022 22:24:49 +0800 Subject: [PATCH 2/2] make code more readable. --- server/storage/schema/cindex.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/server/storage/schema/cindex.go b/server/storage/schema/cindex.go index 52d9ef126cd..d7b06b9cef7 100644 --- a/server/storage/schema/cindex.go +++ b/server/storage/schema/cindex.go @@ -58,7 +58,7 @@ func ReadConsistentIndex(tx backend.ReadTx) (uint64, uint64) { func UnsafeUpdateConsistentIndex(tx backend.BatchTx, index uint64, term uint64, onlyGrow bool) { if index == 0 { - // Never save 0 as it means that we didn't loaded the real index yet. + // Never save 0 as it means that we didn't load the real index yet. return } @@ -67,14 +67,13 @@ func UnsafeUpdateConsistentIndex(tx backend.BatchTx, index uint64, term uint64, if term < oldTerm { return } - if term == oldTerm && index <= oldi { - return + if index > oldi { + bs1 := make([]byte, 8) + binary.BigEndian.PutUint64(bs1, index) + // put the index into the underlying backend + // tx has been locked in TxnBegin, so there is no need to lock it again + tx.UnsafePut(Meta, MetaConsistentIndexKeyName, bs1) } - bs1 := make([]byte, 8) - binary.BigEndian.PutUint64(bs1, index) - // put the index into the underlying backend - // tx has been locked in TxnBegin, so there is no need to lock it again - tx.UnsafePut(Meta, MetaConsistentIndexKeyName, bs1) if term > 0 && term > oldTerm { bs2 := make([]byte, 8) binary.BigEndian.PutUint64(bs2, term)