From 7be3606841ce1eda54f1828ad42833bb96b7ca0b Mon Sep 17 00:00:00 2001 From: Siyuan Zhang Date: Mon, 25 Mar 2024 23:03:40 +0000 Subject: [PATCH] Add tx buffer test case of unordered key writes. Signed-off-by: Siyuan Zhang --- server/storage/backend/batch_tx_test.go | 104 ++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 5 deletions(-) diff --git a/server/storage/backend/batch_tx_test.go b/server/storage/backend/batch_tx_test.go index 4e1d98d09b8..9f83bc4fcf4 100644 --- a/server/storage/backend/batch_tx_test.go +++ b/server/storage/backend/batch_tx_test.go @@ -243,24 +243,118 @@ func TestRangeAfterDeleteMatch(t *testing.T) { tx.Unlock() tx.Commit() - checkRangeResponseMatch(t, b.BatchTx(), b.ReadTx(), []byte("foo"), nil, 0) + checkRangeResponseMatch(t, b.BatchTx(), b.ReadTx(), schema.Test, []byte("foo"), nil, 0) checkForEach(t, b.BatchTx(), b.ReadTx(), [][]byte{[]byte("foo")}, [][]byte{[]byte("bar")}) tx.Lock() tx.UnsafeDelete(schema.Test, []byte("foo")) tx.Unlock() - checkRangeResponseMatch(t, b.BatchTx(), b.ReadTx(), []byte("foo"), nil, 0) + checkRangeResponseMatch(t, b.BatchTx(), b.ReadTx(), schema.Test, []byte("foo"), nil, 0) checkForEach(t, b.BatchTx(), b.ReadTx(), nil, nil) } -func checkRangeResponseMatch(t *testing.T, tx backend.BatchTx, rtx backend.ReadTx, key, endKey []byte, limit int64) { +func TestRangeAfterUnorderedKeyWriteMatch(t *testing.T) { + b, _ := betesting.NewTmpBackend(t, time.Hour, 10000) + defer betesting.Close(t, b) + + tx := b.BatchTx() + + tx.Lock() + tx.UnsafeCreateBucket(schema.Test) + tx.UnsafePut(schema.Test, []byte("foo5"), []byte("bar5")) + tx.UnsafePut(schema.Test, []byte("foo2"), []byte("bar2")) + tx.UnsafePut(schema.Test, []byte("foo1"), []byte("bar1")) + tx.UnsafePut(schema.Test, []byte("foo3"), []byte("bar3")) + tx.UnsafePut(schema.Test, []byte("foo"), []byte("bar")) + tx.UnsafePut(schema.Test, []byte("foo4"), []byte("bar4")) + tx.Unlock() + + checkRangeResponseMatch(t, b.BatchTx(), b.ReadTx(), schema.Test, []byte("foo"), nil, 1) +} + +func TestRangeAfterAlternatingBucketWriteMatch(t *testing.T) { + b, _ := betesting.NewTmpBackend(t, time.Hour, 10000) + defer betesting.Close(t, b) + + tx := b.BatchTx() + + tx.Lock() + tx.UnsafeCreateBucket(schema.Key) + tx.UnsafeCreateBucket(schema.Test) + tx.UnsafeSeqPut(schema.Key, []byte("key1"), []byte("val1")) + tx.Unlock() + + tx.Lock() + tx.UnsafeSeqPut(schema.Key, []byte("key2"), []byte("val2")) + tx.Unlock() + tx.Commit() + // only in the 2nd commit the schema.Key key is removed from the readBuffer.buckets. + // This makes sure to test the case when an empty writeBuffer.bucket + // is used to replace the read buffer bucket. + tx.Commit() + + tx.Lock() + tx.UnsafePut(schema.Test, []byte("foo"), []byte("bar")) + tx.Unlock() + checkRangeResponseMatch(t, b.BatchTx(), b.ReadTx(), schema.Key, []byte("key"), []byte("key5"), 100) + checkRangeResponseMatch(t, b.BatchTx(), b.ReadTx(), schema.Test, []byte("foo"), []byte("foo3"), 1) +} + +func TestRangeAfterOverwriteMatch(t *testing.T) { + b, _ := betesting.NewTmpBackend(t, time.Hour, 10000) + defer betesting.Close(t, b) + + tx := b.BatchTx() + + tx.Lock() + tx.UnsafeCreateBucket(schema.Test) + tx.UnsafePut(schema.Test, []byte("foo"), []byte("bar2")) + tx.UnsafePut(schema.Test, []byte("foo"), []byte("bar0")) + tx.UnsafePut(schema.Test, []byte("foo1"), []byte("bar10")) + tx.UnsafePut(schema.Test, []byte("foo"), []byte("bar1")) + tx.UnsafePut(schema.Test, []byte("foo1"), []byte("bar11")) + tx.Unlock() + + checkRangeResponseMatch(t, b.BatchTx(), b.ReadTx(), schema.Test, []byte("foo"), []byte("foo3"), 1) + checkForEach(t, b.BatchTx(), b.ReadTx(), [][]byte{[]byte("foo"), []byte("foo1")}, [][]byte{[]byte("bar1"), []byte("bar11")}) +} + +func TestRangeAfterOverwriteAndDeleteMatch(t *testing.T) { + b, _ := betesting.NewTmpBackend(t, time.Hour, 10000) + defer betesting.Close(t, b) + + tx := b.BatchTx() + + tx.Lock() + tx.UnsafeCreateBucket(schema.Test) + tx.UnsafePut(schema.Test, []byte("foo"), []byte("bar2")) + tx.UnsafePut(schema.Test, []byte("foo"), []byte("bar0")) + tx.UnsafePut(schema.Test, []byte("foo1"), []byte("bar10")) + tx.UnsafePut(schema.Test, []byte("foo"), []byte("bar1")) + tx.UnsafePut(schema.Test, []byte("foo1"), []byte("bar11")) + tx.Unlock() + + checkRangeResponseMatch(t, b.BatchTx(), b.ReadTx(), schema.Test, []byte("foo"), nil, 0) + checkForEach(t, b.BatchTx(), b.ReadTx(), [][]byte{[]byte("foo"), []byte("foo1")}, [][]byte{[]byte("bar1"), []byte("bar11")}) + + tx.Lock() + tx.UnsafePut(schema.Test, []byte("foo"), []byte("bar3")) + tx.UnsafeDelete(schema.Test, []byte("foo1")) + tx.Unlock() + + checkRangeResponseMatch(t, b.BatchTx(), b.ReadTx(), schema.Test, []byte("foo"), nil, 0) + checkRangeResponseMatch(t, b.BatchTx(), b.ReadTx(), schema.Test, []byte("foo1"), nil, 0) + checkForEach(t, b.BatchTx(), b.ReadTx(), [][]byte{[]byte("foo")}, [][]byte{[]byte("bar3")}) +} + +func checkRangeResponseMatch(t *testing.T, tx backend.BatchTx, rtx backend.ReadTx, bucket backend.Bucket, key, endKey []byte, limit int64) { tx.Lock() - ks1, vs1 := tx.UnsafeRange(schema.Test, key, endKey, limit) + ks1, vs1 := tx.UnsafeRange(bucket, key, endKey, limit) tx.Unlock() rtx.RLock() - ks2, vs2 := rtx.UnsafeRange(schema.Test, key, endKey, limit) + ks2, vs2 := rtx.UnsafeRange(bucket, key, endKey, limit) rtx.RUnlock() if diff := cmp.Diff(ks1, ks2); diff != "" {