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 5d7d890
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 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,20 @@ 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 i := 0; i < bb.used; i++ {
data := bb.buf[i]
if _, ok := keyMaps[string(data.key)]; ok {
panic(fmt.Sprintf("found duplicated keys in the bucketBuffer: %s", string(data.key)))
}
keyMaps[string(data.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 5d7d890

Please sign in to comment.