@@ -8,6 +8,7 @@ package backupsink
8
8
import (
9
9
"context"
10
10
"fmt"
11
+ "math/rand"
11
12
"reflect"
12
13
"strconv"
13
14
"strings"
@@ -122,6 +123,15 @@ func TestFileSSTSinkExtendOneFile(t *testing.T) {
122
123
require .Equal (t , 1 , len (progDetails .Files ))
123
124
}
124
125
126
+ func randomValue (n int64 ) []byte {
127
+ // Create random data so that it does not compress well.
128
+ b := make ([]byte , n )
129
+ for i := range b {
130
+ b [i ] = byte (rand .Int ())
131
+ }
132
+ return b
133
+ }
134
+
125
135
// TestFileSSTSinkWrite tests the contents of flushed files and the internal
126
136
// unflushed files of the FileSSTSink under different write scenarios. Each test
127
137
// writes a sequence of exportedSpans into a FileSSTSink. The test then verifies
@@ -133,6 +143,12 @@ func TestFileSSTSinkWrite(t *testing.T) {
133
143
defer log .Scope (t ).Close (t )
134
144
135
145
ctx := context .Background ()
146
+ testTargetFileSize := int64 (10 << 10 )
147
+
148
+ // Override the fileSpanByteLimit so we can test going over the limit without
149
+ // needing large buffers that may oom the test node.
150
+ defer func (oldLimit int64 ) { fileSpanByteLimit = oldLimit }(fileSpanByteLimit )
151
+ fileSpanByteLimit = testTargetFileSize / 2
136
152
137
153
type testCase struct {
138
154
name string
@@ -145,8 +161,7 @@ func TestFileSSTSinkWrite(t *testing.T) {
145
161
//
146
162
// TODO (msbutler): we currently don't test expected error handling. If this
147
163
// is non-empty, we just skip the test.
148
- errorExplanation string
149
- noSSTSizeOverride bool
164
+ errorExplanation string
150
165
}
151
166
152
167
for _ , tt := range []testCase {{name : "out-of-order-key-boundary" ,
@@ -278,7 +293,7 @@ func TestFileSSTSinkWrite(t *testing.T) {
278
293
{
279
294
name : "size-flush" ,
280
295
exportSpans : []ExportedSpan {
281
- newExportedSpanBuilder ("a" , "c" ).withKVs ([]kvAndTS {{key : "a" , timestamp : 10 , value : make ([] byte , 20 << 20 )}, {key : "b" , timestamp : 10 }}).build (),
296
+ newExportedSpanBuilder ("a" , "c" ).withKVs ([]kvAndTS {{key : "a" , timestamp : 10 , value : randomValue ( testTargetFileSize )}, {key : "b" , timestamp : 10 }}).build (),
282
297
newExportedSpanBuilder ("d" , "f" ).withKVs ([]kvAndTS {{key : "d" , timestamp : 10 }, {key : "e" , timestamp : 10 }}).build (),
283
298
},
284
299
flushedSpans : []roachpb.Spans {
@@ -292,7 +307,7 @@ func TestFileSSTSinkWrite(t *testing.T) {
292
307
// No flush can occur between two versions of the same key. Further, we must combine flushes which split a row.
293
308
name : "no-size-flush-if-mid-mvcc" ,
294
309
exportSpans : []ExportedSpan {
295
- newRawExportedSpanBuilder (s2k0 ("a" ), s2k0 ("c" ), s2k0 ("c" )).withKVs ([]kvAndTS {{key : "a" , timestamp : 10 , value : make ([] byte , 20 << 20 )}, {key : "c" , timestamp : 10 }}).build (),
310
+ newRawExportedSpanBuilder (s2k0 ("a" ), s2k0 ("c" ), s2k0 ("c" )).withKVs ([]kvAndTS {{key : "a" , timestamp : 10 , value : randomValue ( testTargetFileSize )}, {key : "c" , timestamp : 10 }}).build (),
296
311
newRawExportedSpanBuilder (s2k0 ("c" ), s2k0 ("f" ), s2k0 ("f" )).withKVs ([]kvAndTS {{key : "c" , timestamp : 8 }, {key : "f" , timestamp : 10 }}).build (),
297
312
},
298
313
flushedSpans : []roachpb.Spans {},
@@ -305,9 +320,9 @@ func TestFileSSTSinkWrite(t *testing.T) {
305
320
name : "no-size-flush-mid-col-family" ,
306
321
exportSpans : []ExportedSpan {
307
322
newRawExportedSpanBuilder (s2kWithColFamily ("c" , 0 ), s2kWithColFamily ("c" , 1 ), s2kWithColFamily ("c" , 1 )).withKVs ([]kvAndTS {
308
- {key : "c" , timestamp : 10 , value : make ([] byte , 20 << 20 )}}).build (),
323
+ {key : "c" , timestamp : 10 , value : randomValue ( testTargetFileSize )}}).build (),
309
324
newRawExportedSpanBuilder (s2kWithColFamily ("c" , 1 ), s2kWithColFamily ("c" , 2 ), s2kWithColFamily ("c" , 2 )).withKVs ([]kvAndTS {
310
- {key : "c" , timestamp : 10 , value : make ([] byte , 20 << 20 )}}).buildWithEncoding (func (stingedKey string ) roachpb.Key { return s2kWithColFamily (stingedKey , 1 ) }),
325
+ {key : "c" , timestamp : 10 , value : randomValue ( testTargetFileSize )}}).buildWithEncoding (func (stingedKey string ) roachpb.Key { return s2kWithColFamily (stingedKey , 1 ) }),
311
326
},
312
327
flushedSpans : []roachpb.Spans {},
313
328
unflushedSpans : []roachpb.Spans {
@@ -318,7 +333,7 @@ func TestFileSSTSinkWrite(t *testing.T) {
318
333
// It's safe to flush at the range boundary.
319
334
name : "size-flush-at-range-boundary" ,
320
335
exportSpans : []ExportedSpan {
321
- newRawExportedSpanBuilder (s2k ("a" ), s2k ("d" ), s2k ("d" )).withKVs ([]kvAndTS {{key : "a" , timestamp : 10 , value : make ([] byte , 20 << 20 )}, {key : "c" , timestamp : 10 }}).build (),
336
+ newRawExportedSpanBuilder (s2k ("a" ), s2k ("d" ), s2k ("d" )).withKVs ([]kvAndTS {{key : "a" , timestamp : 10 , value : randomValue ( testTargetFileSize )}, {key : "c" , timestamp : 10 }}).build (),
322
337
},
323
338
flushedSpans : []roachpb.Spans {
324
339
{{Key : s2k ("a" ), EndKey : s2k ("d" )}},
@@ -332,7 +347,7 @@ func TestFileSSTSinkWrite(t *testing.T) {
332
347
// row between two column families.
333
348
name : "trim-resume-key" ,
334
349
exportSpans : []ExportedSpan {
335
- newRawExportedSpanBuilder (s2k0 ("a" ), s2k0 ("c" ), s2k ("c" )).withKVs ([]kvAndTS {{key : "a" , timestamp : 10 , value : make ([] byte , 20 << 20 )}}).build (),
350
+ newRawExportedSpanBuilder (s2k0 ("a" ), s2k0 ("c" ), s2k ("c" )).withKVs ([]kvAndTS {{key : "a" , timestamp : 10 , value : randomValue ( testTargetFileSize )}}).build (),
336
351
},
337
352
flushedSpans : []roachpb.Spans {
338
353
{{Key : s2k0 ("a" ), EndKey : s2k ("c" )}},
@@ -344,24 +359,23 @@ func TestFileSSTSinkWrite(t *testing.T) {
344
359
// even if the next span's start key matches the file's end key.
345
360
name : "file-size-cut" ,
346
361
exportSpans : []ExportedSpan {
347
- newExportedSpanBuilder ("a" , "c" ).withKVs ([]kvAndTS {{key : "a" , timestamp : 10 , value : make ([] byte , 64 << 20 )}, {key : "b" , timestamp : 10 }}).build (),
362
+ newExportedSpanBuilder ("a" , "c" ).withKVs ([]kvAndTS {{key : "a" , timestamp : 10 , value : randomValue ( fileSpanByteLimit )}, {key : "b" , timestamp : 10 }}).build (),
348
363
newExportedSpanBuilder ("c" , "f" ).withKVs ([]kvAndTS {{key : "c" , timestamp : 10 }, {key : "e" , timestamp : 10 }}).build (),
349
364
},
350
365
flushedSpans : []roachpb.Spans {},
351
366
unflushedSpans : []roachpb.Spans {
352
367
{{Key : s2k0 ("a" ), EndKey : s2k0 ("c" )}, {Key : s2k0 ("c" ), EndKey : s2k0 ("f" )}},
353
368
},
354
- noSSTSizeOverride : true ,
355
369
},
356
370
{
357
371
// No file cut can occur between the two column families of the same row,
358
372
// even if the file is sufficiently large to get cut.
359
373
name : "no-file-cut-mid-col-family" ,
360
374
exportSpans : []ExportedSpan {
361
375
newRawExportedSpanBuilder (s2kWithColFamily ("c" , 0 ), s2kWithColFamily ("c" , 1 ), s2kWithColFamily ("c" , 1 )).withKVs ([]kvAndTS {
362
- {key : "c" , timestamp : 10 , value : make ([] byte , 65 << 20 )}}).build (),
376
+ {key : "c" , timestamp : 10 , value : randomValue ( testTargetFileSize )}}).build (),
363
377
newRawExportedSpanBuilder (s2kWithColFamily ("c" , 1 ), s2kWithColFamily ("c" , 2 ), s2kWithColFamily ("c" , 2 )).withKVs ([]kvAndTS {
364
- {key : "c" , timestamp : 10 , value : make ([] byte , 20 << 20 )}}).buildWithEncoding (func (stingedKey string ) roachpb.Key { return s2kWithColFamily (stingedKey , 1 ) }),
378
+ {key : "c" , timestamp : 10 , value : randomValue ( testTargetFileSize / 2 )}}).buildWithEncoding (func (stingedKey string ) roachpb.Key { return s2kWithColFamily (stingedKey , 1 ) }),
365
379
},
366
380
flushedSpans : []roachpb.Spans {},
367
381
unflushedSpans : []roachpb.Spans {
@@ -377,9 +391,7 @@ func TestFileSSTSinkWrite(t *testing.T) {
377
391
return
378
392
}
379
393
st := cluster .MakeTestingClusterSettings ()
380
- if ! tt .noSSTSizeOverride {
381
- targetFileSize .Override (ctx , & st .SV , 10 << 10 )
382
- }
394
+ targetFileSize .Override (ctx , & st .SV , testTargetFileSize )
383
395
384
396
sink , store := fileSSTSinkTestSetup (t , st , elide )
385
397
defer func () {
@@ -534,7 +546,7 @@ func TestFileSSTSinkStats(t *testing.T) {
534
546
sinkStats {hlc.Timestamp {WallTime : 10 }, 3 , 3 , 0 , 0 , 0 , 1 }},
535
547
{
536
548
// Write an exported span that comes after all spans so far. This span has enough data for a size flush.
537
- newExportedSpanBuilder ("g" , "h" ).withKVs ([]kvAndTS {{key : "g" , timestamp : 10 , value : make ([] byte , 20 << 20 )}}).build (),
549
+ newExportedSpanBuilder ("g" , "h" ).withKVs ([]kvAndTS {{key : "g" , timestamp : 10 , value : randomValue ( 10 << 10 )}}).build (),
538
550
sinkStats {hlc.Timestamp {WallTime : 0 }, 0 , 4 , 1 , 0 , 1 , 1 }},
539
551
{
540
552
// Write the first exported span after the flush.
0 commit comments