Merge pull request #17325 from ahrtr/verify_key_20240125

Add verification on keys: should be always mononically increasing
This commit is contained in:
Benjamin Wang 2024-01-25 18:54:21 +00:00 committed by GitHub
commit 704c93c9ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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)
}