Skip to content

Commit 2d1c267

Browse files
committed
db: move file number assignment to just before metadata creation
Previously we preallocated all table numbers upfront based on the ingest args. The comment on this being required in order to mark the files as pending seems outdated, so we now assign the file number just before ingesting the sst.
1 parent e2e0fe3 commit 2d1c267

File tree

4 files changed

+135
-133
lines changed

4 files changed

+135
-133
lines changed

flushable_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,11 @@ func TestIngestedSSTFlushableAPI(t *testing.T) {
5050
reset()
5151

5252
loadFileMeta := func(paths []string, exciseSpan KeyRange, seqNum base.SeqNum) []*manifest.TableMetadata {
53-
pendingOutputs := make([]base.TableNum, len(paths))
54-
for i := range paths {
55-
pendingOutputs[i] = d.mu.versions.getNextTableNum()
56-
}
5753
jobID := d.newJobID()
5854

5955
// We can reuse the ingestLoad function for this test even if we're
6056
// not actually ingesting a file.
61-
lr, err := ingestLoad(context.Background(), d.opts, d.FormatMajorVersion(), paths, nil, nil, d.cacheHandle, &d.compressionCounters, pendingOutputs)
57+
lr, err := ingestLoad(context.Background(), d.opts, d.FormatMajorVersion(), paths, nil, nil, d.cacheHandle, &d.compressionCounters, d.mu.versions.getNextDiskFileNum)
6258
if err != nil {
6359
t.Fatal(err)
6460
}

ingest.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ func ingestLoad1(
450450
}
451451

452452
if !meta.HasPointKeys && !meta.HasRangeKeys {
453+
// Elide ingesting empty sstables.
453454
return nil, keyspan.Span{}, base.BlockReadStats{}, nil
454455
}
455456

@@ -503,12 +504,8 @@ func ingestLoad(
503504
external []ExternalFile,
504505
cacheHandle *cache.Handle,
505506
compressionCounters *block.CompressionCounters,
506-
pending []base.TableNum,
507+
getNextFileNum func() base.DiskFileNum,
507508
) (ingestLoadResult, error) {
508-
localFileNums := pending[:len(paths)]
509-
sharedFileNums := pending[len(paths) : len(paths)+len(shared)]
510-
externalFileNums := pending[len(paths)+len(shared) : len(paths)+len(shared)+len(external)]
511-
512509
var result ingestLoadResult
513510
result.local = make([]ingestLocalMeta, 0, len(paths))
514511
var lastRangeKey keyspan.Span
@@ -536,7 +533,8 @@ func ingestLoad(
536533
if !shouldDisableRangeKeyChecks {
537534
rangeKeyValidator = validateSuffixedBoundaries(opts.Comparer, lastRangeKey)
538535
}
539-
m, lastRangeKey, blockReadStats, err = ingestLoad1(ctx, opts, fmv, readable, cacheHandle, compressionCounters, localFileNums[i], rangeKeyValidator)
536+
tableNum := base.TableNum(getNextFileNum())
537+
m, lastRangeKey, blockReadStats, err = ingestLoad1(ctx, opts, fmv, readable, cacheHandle, compressionCounters, tableNum, rangeKeyValidator)
540538
if err != nil {
541539
return ingestLoadResult{}, err
542540
}
@@ -561,7 +559,7 @@ func ingestLoad(
561559

562560
result.shared = make([]ingestSharedMeta, 0, len(shared))
563561
for i := range shared {
564-
m, err := ingestSynthesizeShared(opts, shared[i], sharedFileNums[i])
562+
m, err := ingestSynthesizeShared(opts, shared[i], base.PhysicalTableFileNum(getNextFileNum()))
565563
if err != nil {
566564
return ingestLoadResult{}, err
567565
}
@@ -575,7 +573,7 @@ func ingestLoad(
575573
}
576574
result.external = make([]ingestExternalMeta, 0, len(external))
577575
for i := range external {
578-
m, err := ingestLoad1External(opts, external[i], externalFileNums[i])
576+
m, err := ingestLoad1External(opts, external[i], base.PhysicalTableFileNum(getNextFileNum()))
579577
if err != nil {
580578
return ingestLoadResult{}, err
581579
}
@@ -1470,22 +1468,12 @@ func (d *DB) ingest(ctx context.Context, args ingestArgs) (IngestOperationStats,
14701468
}
14711469
}
14721470
}
1473-
// Allocate table numbers for all files being ingested and mark them as
1474-
// pending in order to prevent them from being deleted. Note that this causes
1475-
// the file number ordering to be out of alignment with sequence number
1476-
// ordering. The sorting of L0 tables by sequence number avoids relying on
1477-
// that (busted) invariant.
1478-
pendingOutputs := make([]base.TableNum, len(paths)+len(shared)+len(external))
1479-
for i := 0; i < len(paths)+len(shared)+len(external); i++ {
1480-
pendingOutputs[i] = d.mu.versions.getNextTableNum()
1481-
}
1482-
14831471
jobID := d.newJobID()
14841472

14851473
// Load the metadata for all the files being ingested. This step detects
14861474
// and elides empty sstables.
14871475
loadResult, err := ingestLoad(ctx, d.opts, d.FormatMajorVersion(), paths, shared, external,
1488-
d.cacheHandle, &d.compressionCounters, pendingOutputs)
1476+
d.cacheHandle, &d.compressionCounters, d.mu.versions.getNextDiskFileNum)
14891477
if err != nil {
14901478
return IngestOperationStats{}, err
14911479
}

ingest_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ func TestIngestLoad(t *testing.T) {
149149
FS: mem,
150150
}
151151
opts.WithFSDefaults()
152-
lr, err := ingestLoad(context.Background(), opts, dbVersion, []string{"ext"}, nil, nil, nil, nil, []base.TableNum{1})
152+
getNextFileNum := func() base.DiskFileNum { return 1 }
153+
lr, err := ingestLoad(context.Background(), opts, dbVersion, []string{"ext"}, nil, nil, nil, nil, getNextFileNum)
153154
if err != nil {
154155
return err.Error()
155156
}
@@ -184,14 +185,19 @@ func TestIngestLoadRand(t *testing.T) {
184185
}
185186

186187
paths := make([]string, 1+rng.IntN(10))
187-
pending := make([]base.TableNum, len(paths))
188188
expected := make([]ingestLocalMeta, len(paths))
189+
pending := make([]base.DiskFileNum, len(expected))
190+
for i := range expected {
191+
pending[i] = base.DiskFileNum(rng.Uint64())
192+
}
193+
fileNumAllocator := testFileNumAllocator{
194+
fileNums: pending,
195+
}
189196
for i := range paths {
190197
paths[i] = fmt.Sprint(i)
191-
pending[i] = base.TableNum(rng.Uint64())
192198
expected[i] = ingestLocalMeta{
193199
TableMetadata: &manifest.TableMetadata{
194-
TableNum: pending[i],
200+
TableNum: base.TableNum(pending[i]),
195201
},
196202
path: paths[i],
197203
}
@@ -248,7 +254,7 @@ func TestIngestLoadRand(t *testing.T) {
248254
}
249255
opts.WithFSDefaults()
250256
opts.EnsureDefaults()
251-
lr, err := ingestLoad(context.Background(), opts, version, paths, nil, nil, nil, nil, pending)
257+
lr, err := ingestLoad(context.Background(), opts, version, paths, nil, nil, nil, nil, fileNumAllocator.nextFileNum)
252258
require.NoError(t, err)
253259

254260
// Reset flaky stats.
@@ -273,7 +279,8 @@ func TestIngestLoadInvalid(t *testing.T) {
273279
FS: mem,
274280
}
275281
opts.WithFSDefaults()
276-
if _, err := ingestLoad(context.Background(), opts, internalFormatNewest, []string{"invalid"}, nil, nil, nil, nil, []base.TableNum{1}); err == nil {
282+
getNextFileNum := func() base.DiskFileNum { return 1 }
283+
if _, err := ingestLoad(context.Background(), opts, internalFormatNewest, []string{"invalid"}, nil, nil, nil, nil, getNextFileNum); err == nil {
277284
t.Fatalf("expected error, but found success")
278285
}
279286
}
@@ -3172,3 +3179,14 @@ func runBenchmarkManySSTablesInUseKeyRanges(b *testing.B, d *DB, count int) {
31723179
_ = v.CalculateInuseKeyRanges(d.mu.versions.latest.l0Organizer, 0, numLevels-1, smallest, largest)
31733180
}
31743181
}
3182+
3183+
type testFileNumAllocator struct {
3184+
fileNums []base.DiskFileNum
3185+
idx int
3186+
}
3187+
3188+
func (a *testFileNumAllocator) nextFileNum() base.DiskFileNum {
3189+
n := a.fileNums[a.idx]
3190+
a.idx++
3191+
return n
3192+
}

0 commit comments

Comments
 (0)