Skip to content

Commit

Permalink
Add verification to verify there is no duplicated keys in the bucket …
Browse files Browse the repository at this point in the history
…buffer

Signed-off-by: Benjamin Wang <[email protected]>
  • Loading branch information
ahrtr committed Jan 19, 2024
1 parent e3c009e commit ddea824
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions server/storage/backend/tx_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package backend

import (
"bytes"
"fmt"
"sort"

"go.etcd.io/etcd/client/pkg/v3/verify"
Expand Down Expand Up @@ -81,6 +82,7 @@ func (txw *txWriteBuffer) writeback(txr *txReadBuffer) {
for k, wb := range txw.buckets {
rb, ok := txr.buckets[k]
if !ok {
verifyNoDuplicatedKeys(wb)
delete(txw.buckets, k)
txr.buckets[k] = wb
continue
Expand Down Expand Up @@ -206,15 +208,19 @@ func (bb *bucketBuffer) merge(bbsrc *bucketBuffer) {

sort.Stable(bb)

// remove duplicates, using only newest update
widx := 0
for ridx := 1; ridx < bb.used; ridx++ {
if !bytes.Equal(bb.buf[ridx].key, bb.buf[widx].key) {
widx++
verifyNoDuplicatedKeys(bb)
}

func verifyNoDuplicatedKeys(bb *bucketBuffer) {
verify.Verify(func() {
keyMaps := make(map[string]struct{})
for _, kv := range bb.buf {
if _, ok := keyMaps[string(kv.key)]; ok {
panic(fmt.Sprintf("found duplicated keys in the bucketBuffer: %s", string(kv.key)))
}
keyMaps[string(kv.key)] = struct{}{}
}
bb.buf[widx] = bb.buf[ridx]
}
bb.used = widx + 1
})
}

func (bb *bucketBuffer) Len() int { return bb.used }
Expand Down

0 comments on commit ddea824

Please sign in to comment.