Skip to content

Commit d928a2e

Browse files
committed
db: pull more recovery logic into recoverState
Additionally, remove the call to wal.Scan in Open, instead relying on the WALs scanned in recoverState. This was accidentally overlooked during the refactor that began scanning wals in recoverState.
1 parent eda4c4a commit d928a2e

File tree

5 files changed

+36
-46
lines changed

5 files changed

+36
-46
lines changed

open.go

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,6 @@ func Open(dirname string, opts *Options) (db *DB, err error) {
306306
Buckets: FsyncLatencyBuckets,
307307
})
308308
}
309-
walDirs := d.dirs.WALDirs()
310-
wals, err := wal.Scan(walDirs...)
311-
if err != nil {
312-
return nil, err
313-
}
314309

315310
walManager, err := wal.Init(walOpts, rs.walsReplay)
316311
if err != nil {
@@ -339,44 +334,17 @@ func Open(dirname string, opts *Options) (db *DB, err error) {
339334
d.blobFileDiskUsageAnnotator = d.makeBlobFileDiskSpaceUsageAnnotator()
340335

341336
d.mu.versions.markFileNumUsed(rs.maxFilenumUsed)
342-
if n := len(wals); n > 0 {
343-
// Don't reuse any obsolete file numbers to avoid modifying an
344-
// ingested sstable's original external file.
345-
d.mu.versions.markFileNumUsed(base.DiskFileNum(wals[n-1].Num))
346-
}
347-
348-
// Ratchet d.mu.versions.nextFileNum ahead of all known objects in the
349-
// objProvider. This avoids FileNum collisions with obsolete sstables.
350-
objects := d.objProvider.List()
351-
for _, obj := range objects {
352-
d.mu.versions.markFileNumUsed(obj.DiskFileNum)
353-
}
354-
355-
// Validate the most-recent OPTIONS file, if there is one.
356-
if rs.previousOptionsFilename != "" {
357-
path := opts.FS.PathJoin(dirname, rs.previousOptionsFilename)
358-
previousOptions, err := readOptionsFile(opts, path)
359-
if err != nil {
360-
return nil, err
361-
}
362-
if err := opts.CheckCompatibility(dirname, previousOptions); err != nil {
363-
return nil, err
364-
}
365-
}
366337

367338
// Replay any newer log files than the ones named in the manifest.
368339
var flushableIngests []*ingestedFlushable
369-
for i, w := range wals {
370-
if base.DiskFileNum(w.Num) < d.mu.versions.minUnflushedLogNum {
371-
continue
372-
}
340+
for i, w := range rs.walsReplay {
373341
// WALs other than the last one would have been closed cleanly.
374342
//
375343
// Note: we used to never require strict WAL tails when reading from older
376344
// versions: RocksDB 6.2.1 and the version of Pebble included in CockroachDB
377345
// 20.1 do not guarantee that closed WALs end cleanly. But the earliest
378346
// compatible Pebble format is newer and guarantees a clean EOF.
379-
strictWALTail := i < len(wals)-1
347+
strictWALTail := i < len(rs.walsReplay)-1
380348
fi, maxSeqNum, err := d.replayWAL(jobID, w, strictWALTail)
381349
if err != nil {
382350
return nil, err

recovery.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,34 @@ func (rs *recoveredState) init(opts *Options, dirname string) error {
108108
}
109109
}
110110

111+
// Validate the most-recent OPTIONS file, if there is one.
112+
if rs.previousOptionsFilename != "" {
113+
path := opts.FS.PathJoin(dirname, rs.previousOptionsFilename)
114+
previousOptions, err := readOptionsFile(opts, path)
115+
if err != nil {
116+
return err
117+
}
118+
if err := opts.CheckCompatibility(dirname, previousOptions); err != nil {
119+
return err
120+
}
121+
}
122+
123+
// Ratchet rs.maxFilenumUsed ahead of all known objects in the objProvider.
124+
// This avoids FileNum collisions with obsolete sstables.
125+
objects := rs.objProvider.List()
126+
for _, obj := range objects {
127+
rs.maxFilenumUsed = max(rs.maxFilenumUsed, obj.DiskFileNum)
128+
}
129+
111130
// Find all the WAL files across the various WAL directories.
112131
wals, err := wal.Scan(rs.dirs.WALDirs()...)
113132
if err != nil {
114133
return err
115134
}
116135
for _, w := range wals {
136+
// Don't reuse any obsolete file numbers to avoid modifying an ingested
137+
// sstable's original external file.
138+
rs.maxFilenumUsed = max(rs.maxFilenumUsed, base.DiskFileNum(w.Num))
117139
if rs.recoveredVersion == nil || base.DiskFileNum(w.Num) >= rs.recoveredVersion.minUnflushedLogNum {
118140
rs.walsReplay = append(rs.walsReplay, w)
119141
} else {

testdata/checkpoint

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,9 @@ open-dir: checkpoints/checkpoint1
358358
open-dir: checkpoints/checkpoint1
359359
open: checkpoints/checkpoint1/MANIFEST-000001
360360
close: checkpoints/checkpoint1/MANIFEST-000001
361-
open-dir: checkpoints/checkpoint1
362361
open: checkpoints/checkpoint1/OPTIONS-000002
363362
close: checkpoints/checkpoint1/OPTIONS-000002
363+
open-dir: checkpoints/checkpoint1
364364
open: checkpoints/checkpoint1/000006.log (options: *vfs.sequentialReadsOption)
365365
close: checkpoints/checkpoint1/000006.log
366366
get-disk-usage: checkpoints/checkpoint1
@@ -426,9 +426,9 @@ open-dir: checkpoints/checkpoint2
426426
open-dir: checkpoints/checkpoint2
427427
open: checkpoints/checkpoint2/MANIFEST-000001
428428
close: checkpoints/checkpoint2/MANIFEST-000001
429-
open-dir: checkpoints/checkpoint2
430429
open: checkpoints/checkpoint2/OPTIONS-000002
431430
close: checkpoints/checkpoint2/OPTIONS-000002
431+
open-dir: checkpoints/checkpoint2
432432
open: checkpoints/checkpoint2/000006.log (options: *vfs.sequentialReadsOption)
433433
close: checkpoints/checkpoint2/000006.log
434434
get-disk-usage: checkpoints/checkpoint2
@@ -469,9 +469,9 @@ open-dir: checkpoints/checkpoint3
469469
open-dir: checkpoints/checkpoint3
470470
open: checkpoints/checkpoint3/MANIFEST-000001
471471
close: checkpoints/checkpoint3/MANIFEST-000001
472-
open-dir: checkpoints/checkpoint3
473472
open: checkpoints/checkpoint3/OPTIONS-000002
474473
close: checkpoints/checkpoint3/OPTIONS-000002
474+
open-dir: checkpoints/checkpoint3
475475
open: checkpoints/checkpoint3/000006.log (options: *vfs.sequentialReadsOption)
476476
close: checkpoints/checkpoint3/000006.log
477477
get-disk-usage: checkpoints/checkpoint3
@@ -613,9 +613,9 @@ open-dir: checkpoints/checkpoint4
613613
open-dir: checkpoints/checkpoint4
614614
open: checkpoints/checkpoint4/MANIFEST-000001
615615
close: checkpoints/checkpoint4/MANIFEST-000001
616-
open-dir: checkpoints/checkpoint4
617616
open: checkpoints/checkpoint4/OPTIONS-000002
618617
close: checkpoints/checkpoint4/OPTIONS-000002
618+
open-dir: checkpoints/checkpoint4
619619
open: checkpoints/checkpoint4/000008.log (options: *vfs.sequentialReadsOption)
620620
close: checkpoints/checkpoint4/000008.log
621621
get-disk-usage: checkpoints/checkpoint4
@@ -729,9 +729,9 @@ open-dir: checkpoints/checkpoint5
729729
open-dir: checkpoints/checkpoint5
730730
open: checkpoints/checkpoint5/MANIFEST-000001
731731
close: checkpoints/checkpoint5/MANIFEST-000001
732-
open-dir: checkpoints/checkpoint5
733732
open: checkpoints/checkpoint5/OPTIONS-000002
734733
close: checkpoints/checkpoint5/OPTIONS-000002
734+
open-dir: checkpoints/checkpoint5
735735
open: checkpoints/checkpoint5/000008.log (options: *vfs.sequentialReadsOption)
736736
close: checkpoints/checkpoint5/000008.log
737737
create: checkpoints/checkpoint5/temporary.000018.dbtmp
@@ -833,9 +833,9 @@ open-dir: checkpoints/checkpoint6
833833
open-dir: checkpoints/checkpoint6
834834
open: checkpoints/checkpoint6/MANIFEST-000001
835835
close: checkpoints/checkpoint6/MANIFEST-000001
836-
open-dir: checkpoints/checkpoint6
837836
open: checkpoints/checkpoint6/OPTIONS-000002
838837
close: checkpoints/checkpoint6/OPTIONS-000002
838+
open-dir: checkpoints/checkpoint6
839839
open: checkpoints/checkpoint6/000008.log (options: *vfs.sequentialReadsOption)
840840
close: checkpoints/checkpoint6/000008.log
841841
create: checkpoints/checkpoint6/temporary.000018.dbtmp
@@ -1093,9 +1093,9 @@ open-dir: checkpoints/checkpoint8
10931093
open-dir: checkpoints/checkpoint8
10941094
open: checkpoints/checkpoint8/MANIFEST-000001
10951095
close: checkpoints/checkpoint8/MANIFEST-000001
1096-
open-dir: checkpoints/checkpoint8
10971096
open: checkpoints/checkpoint8/OPTIONS-000002
10981097
close: checkpoints/checkpoint8/OPTIONS-000002
1098+
open-dir: checkpoints/checkpoint8
10991099
open: checkpoints/checkpoint8/000004.log (options: *vfs.sequentialReadsOption)
11001100
close: checkpoints/checkpoint8/000004.log
11011101
get-disk-usage: checkpoints/checkpoint8
@@ -1207,9 +1207,9 @@ open-dir: checkpoints/checkpoint9
12071207
open-dir: checkpoints/checkpoint9
12081208
open: checkpoints/checkpoint9/MANIFEST-000001
12091209
close: checkpoints/checkpoint9/MANIFEST-000001
1210-
open-dir: checkpoints/checkpoint9
12111210
open: checkpoints/checkpoint9/OPTIONS-000002
12121211
close: checkpoints/checkpoint9/OPTIONS-000002
1212+
open-dir: checkpoints/checkpoint9
12131213
open: checkpoints/checkpoint9/000007.log (options: *vfs.sequentialReadsOption)
12141214
close: checkpoints/checkpoint9/000007.log
12151215
get-disk-usage: checkpoints/checkpoint9

testdata/checkpoint_shared

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,9 @@ close: checkpoints/checkpoint1/REMOTE-OBJ-CATALOG-000001
339339
open-dir: checkpoints/checkpoint1
340340
open: checkpoints/checkpoint1/MANIFEST-000001
341341
close: checkpoints/checkpoint1/MANIFEST-000001
342-
open-dir: checkpoints/checkpoint1
343342
open: checkpoints/checkpoint1/OPTIONS-000002
344343
close: checkpoints/checkpoint1/OPTIONS-000002
344+
open-dir: checkpoints/checkpoint1
345345
open: checkpoints/checkpoint1/000006.log (options: *vfs.sequentialReadsOption)
346346
close: checkpoints/checkpoint1/000006.log
347347
get-disk-usage: checkpoints/checkpoint1
@@ -392,9 +392,9 @@ close: checkpoints/checkpoint2/REMOTE-OBJ-CATALOG-000001
392392
open-dir: checkpoints/checkpoint2
393393
open: checkpoints/checkpoint2/MANIFEST-000001
394394
close: checkpoints/checkpoint2/MANIFEST-000001
395-
open-dir: checkpoints/checkpoint2
396395
open: checkpoints/checkpoint2/OPTIONS-000002
397396
close: checkpoints/checkpoint2/OPTIONS-000002
397+
open-dir: checkpoints/checkpoint2
398398
open: checkpoints/checkpoint2/000006.log (options: *vfs.sequentialReadsOption)
399399
close: checkpoints/checkpoint2/000006.log
400400
get-disk-usage: checkpoints/checkpoint2

testdata/cleaner

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,10 @@ open-dir: db1
254254
open-dir: db1
255255
open: db1/MANIFEST-000001
256256
close: db1/MANIFEST-000001
257-
remove: db1_wal/000003.log
258-
open-dir: db1_wal
259257
open: db1/OPTIONS-000002
260258
close: db1/OPTIONS-000002
259+
remove: db1_wal/000003.log
260+
open-dir: db1_wal
261261
open: db1_wal/000004.log (options: *vfs.sequentialReadsOption)
262262
close: db1_wal/000004.log
263263
create: db1/temporary.000458.dbtmp

0 commit comments

Comments
 (0)