Skip to content

Commit

Permalink
Add verification on keys: should be always mononically increasing
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Wang <benjamin.ahrtr@gmail.com>
  • Loading branch information
ahrtr committed Jan 25, 2024
1 parent b287378 commit c12e15d
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion server/storage/backend/tx_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package backend

import (
"bytes"
"encoding/hex"
"fmt"
"sort"

"go.etcd.io/etcd/client/pkg/v3/verify"
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit c12e15d

Please sign in to comment.