@@ -8,6 +8,7 @@ package rangefeed
8
8
import (
9
9
"context"
10
10
"math/rand"
11
+ "slices"
11
12
"testing"
12
13
13
14
"github.com/cockroachdb/cockroach/pkg/keys"
@@ -27,31 +28,6 @@ import (
27
28
"github.com/stretchr/testify/require"
28
29
)
29
30
30
- type kvs = storageutils.KVs
31
-
32
- var (
33
- pointKV = storageutils .PointKV
34
- rangeKV = storageutils .RangeKV
35
- )
36
-
37
- var (
38
- testKey = roachpb .Key ("/db1" )
39
- testTxnID = uuid .MakeV4 ()
40
- testIsolationLevel = isolation .Serializable
41
- testSpan = roachpb.Span {Key : roachpb .Key ("a" ), EndKey : roachpb .Key ("z" )}
42
- testTs = hlc.Timestamp {WallTime : 1 }
43
- testStartKey = roachpb .Key ("a" )
44
- testEndKey = roachpb .Key ("z" )
45
- testValue = []byte ("1" )
46
- testPrevValue = []byte ("1234" )
47
- testSSTKVs = kvs {
48
- pointKV ("a" , 1 , "1" ),
49
- pointKV ("b" , 1 , "2" ),
50
- pointKV ("c" , 1 , "3" ),
51
- rangeKV ("d" , "e" , 1 , "" ),
52
- }
53
- )
54
-
55
31
type testData struct {
56
32
numOfLogicalOps int
57
33
kvs []interface {}
@@ -68,9 +44,34 @@ type testData struct {
68
44
omitInRangefeeds bool
69
45
}
70
46
71
- func generateStaticTestdata () testData {
47
+ // testSSTKVs returns a set of key-value pairs that can be used for testing.
48
+ // Note that the value output depends on storage.disableSimpleValueEncoding.
49
+ func testSSTKVs () storageutils.KVs {
50
+ return storageutils.KVs {
51
+ storageutils .PointKV ("a" , 1 , "1" ),
52
+ storageutils .PointKV ("b" , 1 , "2" ),
53
+ storageutils .PointKV ("c" , 1 , "3" ),
54
+ storageutils .RangeKV ("d" , "e" , 1 , "" ),
55
+ }
56
+ }
57
+
58
+ func generateStaticTestdata (t * testing.T ) testData {
59
+ storage .DisableMetamorphicSimpleValueEncoding (t )
60
+
61
+ var (
62
+ testKey = roachpb .Key ("/db1" )
63
+ testTxnID = uuid .MakeV4 ()
64
+ testIsolationLevel = isolation .Serializable
65
+ testSpan = roachpb.Span {Key : roachpb .Key ("a" ), EndKey : roachpb .Key ("z" )}
66
+ testTs = hlc.Timestamp {WallTime : 1 }
67
+ testStartKey = roachpb .Key ("a" )
68
+ testEndKey = roachpb .Key ("z" )
69
+ testValue = []byte ("1" )
70
+ testPrevValue = []byte ("1234" )
71
+ )
72
+
72
73
return testData {
73
- kvs : testSSTKVs ,
74
+ kvs : testSSTKVs () ,
74
75
span : testSpan ,
75
76
key : testKey ,
76
77
timestamp : testTs ,
@@ -94,9 +95,7 @@ func generateStaticTestdata() testData {
94
95
// be careful and account the memory usage of the additional underlying data
95
96
// structure. Otherwise, you can simply update the expected values in this test.
96
97
func TestEventSizeCalculation (t * testing.T ) {
97
- st := cluster .MakeTestingClusterSettings ()
98
- data := generateStaticTestdata ()
99
- storage .CompressionAlgorithmStorage .Override (context .Background (), & st .SV , storage .StoreCompressionSnappy )
98
+ data := generateStaticTestdata (t )
100
99
101
100
key := data .key
102
101
timestamp := data .timestamp
@@ -114,7 +113,16 @@ func TestEventSizeCalculation(t *testing.T) {
114
113
omitInRangefeeds := data .omitInRangefeeds
115
114
116
115
span := data .span
116
+
117
+ // Fix settings that can affect the size of the sstable.
118
+ st := cluster .MakeTestingClusterSettings ()
119
+ storage .CompressionAlgorithmStorage .Override (context .Background (), & st .SV , storage .StoreCompressionSnappy )
120
+ storage .IngestionValueBlocksEnabled .Override (context .Background (), & st .SV , true )
117
121
sst , _ , _ := storageutils .MakeSST (t , st , data .kvs )
122
+ // The test cares about cap(sst); this can hide bugs where the sst
123
+ // generation is non-deterministic and results in slightly different
124
+ // lengths. Clip the slice to its length half the time to detect this.
125
+ sst = slices .Clip (sst )
118
126
119
127
for _ , tc := range []struct {
120
128
name string
@@ -220,10 +228,10 @@ func TestEventSizeCalculation(t *testing.T) {
220
228
{
221
229
name : "sstEvent event" ,
222
230
ev : event {sst : & sstEvent {data : sst , span : span , ts : timestamp }},
223
- expectedCurrMemUsage : int64 (2218 ),
231
+ expectedCurrMemUsage : int64 (1186 ),
224
232
actualCurrMemUsage : eventOverhead + sstEventOverhead +
225
233
int64 (cap (sst )+ cap (span .Key )+ cap (span .EndKey )),
226
- expectedFutureMemUsage : int64 (2242 ),
234
+ expectedFutureMemUsage : int64 (1210 ),
227
235
actualFutureMemUsage : futureEventBaseOverhead + rangefeedSSTTableOverhead +
228
236
int64 (cap (sst )+ cap (span .Key )+ cap (span .EndKey )),
229
237
},
@@ -290,7 +298,7 @@ func generateRandomTestData(rand *rand.Rand) testData {
290
298
startKey , endkey := generateStartAndEndKey (rand )
291
299
return testData {
292
300
numOfLogicalOps : rand .Intn (100 ) + 1 , // Avoid 0 (empty event)
293
- kvs : testSSTKVs ,
301
+ kvs : testSSTKVs () ,
294
302
span : generateRandomizedSpan (rand ).AsRawSpanWithNoLocals (),
295
303
key : generateRandomizedBytes (rand ),
296
304
timestamp : GenerateRandomizedTs (rand , 100 /* maxTime */ ),
0 commit comments