|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package bulksst |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "errors" |
| 11 | + "fmt" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/cockroachdb/cockroach/pkg/roachpb" |
| 15 | + "github.com/cockroachdb/cockroach/pkg/settings/cluster" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/storage" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/util/hlc" |
| 18 | + "github.com/cockroachdb/cockroach/pkg/util/leaktest" |
| 19 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
| 20 | + "github.com/cockroachdb/pebble/objstorage" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | +) |
| 23 | + |
| 24 | +func TestRowSampleReservoirCapsSamples(t *testing.T) { |
| 25 | + defer leaktest.AfterTest(t)() |
| 26 | + defer log.Scope(t).Close(t) |
| 27 | + |
| 28 | + var base fileAllocatorBase |
| 29 | + expected := make(map[string]struct{}) |
| 30 | + total := maxRowSamples*2 + 17 |
| 31 | + for i := 0; i < total; i++ { |
| 32 | + key := roachpb.Key(fmt.Sprintf("key-%04d", i)) |
| 33 | + expected[string(key)] = struct{}{} |
| 34 | + span := roachpb.Span{Key: key, EndKey: key.Next()} |
| 35 | + base.addFile(fmt.Sprintf("uri-%d", i), span, key, 1) |
| 36 | + } |
| 37 | + |
| 38 | + require.Equal(t, total, base.totalRowSamples) |
| 39 | + require.Len(t, base.fileInfo.RowSamples, maxRowSamples) |
| 40 | + |
| 41 | + for _, sample := range base.fileInfo.RowSamples { |
| 42 | + _, ok := expected[sample] |
| 43 | + require.True(t, ok, "sample %q must correspond to ingested keys", sample) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestRowSampleSkipsEmptyKeys(t *testing.T) { |
| 48 | + defer leaktest.AfterTest(t)() |
| 49 | + defer log.Scope(t).Close(t) |
| 50 | + |
| 51 | + var base fileAllocatorBase |
| 52 | + key := roachpb.Key("some-key") |
| 53 | + span := roachpb.Span{Key: key, EndKey: key.Next()} |
| 54 | + |
| 55 | + base.addFile("uri-1", span, key, 1) |
| 56 | + base.addFile("uri-2", span, nil, 1) |
| 57 | + |
| 58 | + require.Len(t, base.fileInfo.RowSamples, 1) |
| 59 | + require.Equal(t, string(key), base.fileInfo.RowSamples[0]) |
| 60 | +} |
| 61 | + |
| 62 | +func TestWriterFlushFailureDoesNotPersistMetadata(t *testing.T) { |
| 63 | + defer leaktest.AfterTest(t)() |
| 64 | + defer log.Scope(t).Close(t) |
| 65 | + |
| 66 | + ctx := context.Background() |
| 67 | + st := cluster.MakeTestingClusterSettings() |
| 68 | + failErr := errors.New("finish failed") |
| 69 | + allocator := &mockFailingAllocator{finishErr: failErr} |
| 70 | + |
| 71 | + writer := NewUnsortedSSTBatcher(st, allocator) |
| 72 | + ts := hlc.Timestamp{WallTime: 1} |
| 73 | + require.NoError(t, writer.AddMVCCKey(ctx, storage.MVCCKey{Key: roachpb.Key("k"), Timestamp: ts}, []byte("v"))) |
| 74 | + |
| 75 | + err := writer.Flush(ctx) |
| 76 | + require.ErrorIs(t, err, failErr) |
| 77 | + |
| 78 | + fileList := allocator.GetFileList() |
| 79 | + require.Empty(t, fileList.SST, "no files should be tracked after a failed flush") |
| 80 | + require.Zero(t, fileList.TotalSize) |
| 81 | + require.Empty(t, fileList.RowSamples) |
| 82 | +} |
| 83 | + |
| 84 | +type mockFailingAllocator struct { |
| 85 | + fileAllocatorBase |
| 86 | + finishErr error |
| 87 | + nextID int |
| 88 | +} |
| 89 | + |
| 90 | +func (m *mockFailingAllocator) AddFile(ctx context.Context) (objstorage.Writable, string, error) { |
| 91 | + uri := fmt.Sprintf("mock://%d", m.nextID) |
| 92 | + m.nextID++ |
| 93 | + w := &failingWritable{finishErr: m.finishErr} |
| 94 | + return w, uri, nil |
| 95 | +} |
| 96 | + |
| 97 | +func (m *mockFailingAllocator) CommitFile( |
| 98 | + uri string, span roachpb.Span, rowSample roachpb.Key, fileSize uint64, |
| 99 | +) { |
| 100 | + m.fileAllocatorBase.addFile(uri, span, rowSample, fileSize) |
| 101 | +} |
| 102 | + |
| 103 | +type failingWritable struct { |
| 104 | + finishErr error |
| 105 | +} |
| 106 | + |
| 107 | +func (f *failingWritable) Write(p []byte) error { |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +func (f *failingWritable) Finish() error { |
| 112 | + return f.finishErr |
| 113 | +} |
| 114 | + |
| 115 | +func (f *failingWritable) Abort() {} |
0 commit comments