Skip to content

Commit

Permalink
mvcc: hashKV example
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Fu <[email protected]>
  • Loading branch information
fuweid committed Jul 16, 2024
1 parent 4d9dceb commit a6454f2
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions server/storage/mvcc/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"

"go.etcd.io/etcd/pkg/v3/traceutil"
Expand Down Expand Up @@ -231,3 +232,103 @@ func TestHasherStoreFull(t *testing.T) {
t.Errorf("Didn't expect error for new revision, err: %v", err)
}
}

func TestHashKVImpactedByKeep(t *testing.T) {
// key: "foo"
// modified: 9
// generations:
// {{9, 0}[1]}
// {{5, 0}, {6, 0}, {7, 0}, {8, 0}(t)[4]}
// {{2, 0}, {3, 0}, {4, 0}(t)[3]}
s := newTestStoreForHashKV(t, false)

// After compacted, it will be like
//
// key: "foo"
// modified: 9
// generations:
// {{9, 0}[1]}
// {{5, 0}, {6, 0}, {7, 0}, {8, 0}(t)[4]}
//
doneCh, err := s.Compact(traceutil.TODO(), 5)
require.NoError(t, err)
<-doneCh

// (5,0), (6,0), (7,0) and (8,0),
s.HashStorage().HashByRev(8)

// (6,0), (7,0).
s.HashStorage().HashByRev(7)
}

func TestHashKVImpactedByKeep2(t *testing.T) {
// key: "foo"
// modified: 9
// generations:
// {{9, 0}[1]}
// {{5, 0}, {6, 0}, {7, 0}, {8, 0}(t)[4]}
// {{2, 0}, {3, 0}, {4, 0}(t)[3]}

// key: "foo2"
// modified: 2
// generations:
// {{2, 1}[1]}
s := newTestStoreForHashKV(t, true)

// After compacted, it will be like
//
// key: "foo"
// modified: 9
// generations:
// {{9, 0}[1]}
// {{5, 0}, {6, 0}, {7, 0}, {8, 0}(t)[4]}
//
// key: "foo2"
// modified: 2
// generations:
// {{2, 1}[1]}
doneCh, err := s.Compact(traceutil.TODO(), 5)
require.NoError(t, err)
<-doneCh

// (2,1), (6,0), (7,0) and (8,0)
s.HashStorage().HashByRev(8)

// (2,1), (6,0) and (7,0).
s.HashStorage().HashByRev(7)
}

func newTestStoreForHashKV(t *testing.T, twoKeys bool) *store {
b, _ := betesting.NewDefaultTmpBackend(t)
s := NewStore(zaptest.NewLogger(t), b, &lease.FakeLessor{}, StoreConfig{})
t.Cleanup(func() {
cleanup(s, b)
})

key := []byte("foo")
// key: "foo"
// modified: 9
// generations:
// {{9, 0}[1]}
// {{5, 0}, {6, 0}, {7, 0}, {8, 0}(t)[4]}
// {{2, 0}, {3, 0}, {4, 0}(t)[3]}

key2 := []byte("foo2")
// key: "foo2"
// modified: 2
// generations:
// {{2, 1}[1]}
for i := 2; i <= 9; i++ {
if i%4 == 0 {
s.DeleteRange(key, nil)
} else {
w := s.Write(traceutil.TODO())
w.Put(key, []byte(fmt.Sprintf("%d", i)), lease.NoLease)
if twoKeys && i == 2 {
w.Put(key2, []byte(fmt.Sprintf("%d", i)), lease.NoLease)
}
w.End()
}
}
return s
}

0 comments on commit a6454f2

Please sign in to comment.