From c12e15d91af62aae6363c837c4f23efa8301207a Mon Sep 17 00:00:00 2001 From: Benjamin Wang Date: Thu, 25 Jan 2024 18:00:26 +0000 Subject: [PATCH] Add verification on keys: should be always mononically increasing Signed-off-by: Benjamin Wang --- server/storage/backend/tx_buffer.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/server/storage/backend/tx_buffer.go b/server/storage/backend/tx_buffer.go index 7c2f9d63ac4..c6907e7e6b5 100644 --- a/server/storage/backend/tx_buffer.go +++ b/server/storage/backend/tx_buffer.go @@ -16,6 +16,8 @@ package backend import ( "bytes" + "encoding/hex" + "fmt" "sort" "go.etcd.io/etcd/client/pkg/v3/verify" @@ -52,7 +54,20 @@ func (txw *txWriteBuffer) put(bucket Bucket, k, v []byte) { } func (txw *txWriteBuffer) putSeq(bucket Bucket, k, v []byte) { - // TODO: Add (in tests?) verification whether k>b[len(b)] + // putSeq is only be called for the data in the Key bucket. The keys + // in the Key bucket should be monotonically increasing revisions. + verify.Verify(func() { + b, ok := txw.buckets[bucket.ID()] + if !ok || b.used == 0 { + return + } + + existingMaxKey := b.buf[b.used-1].key + if bytes.Compare(k, existingMaxKey) <= 0 { + panic(fmt.Sprintf("Broke the rule of monotonically increasing, existingMaxKey: %s, currentKey: %s", + hex.EncodeToString(existingMaxKey), hex.EncodeToString(k))) + } + }) txw.putInternal(bucket, k, v) }