Skip to content

Commit 8c6f425

Browse files
committed
db: remove obsolete WALs before proceeding with Init
Fixes: #5354
1 parent d97438a commit 8c6f425

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

open.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"github.com/cockroachdb/pebble/record"
3434
"github.com/cockroachdb/pebble/vfs"
3535
"github.com/cockroachdb/pebble/wal"
36-
"github.com/cockroachdb/redact"
3736
"github.com/prometheus/client_golang/prometheus"
3837
)
3938

@@ -413,11 +412,40 @@ func Open(dirname string, opts *Options) (db *DB, err error) {
413412
if err != nil {
414413
return nil, err
415414
}
416-
d.opts.Logger.Infof("Found %d WALs", redact.Safe(len(wals)))
417-
for i := range wals {
418-
d.opts.Logger.Infof(" - %s", wals[i])
415+
416+
// Remove obsolete WAL files now (as opposed to relying on asynchronous cleanup)
417+
// to prevent crash loops due to no disk space (ENOSPC).
418+
var retainedWALs wal.Logs
419+
for _, w := range wals {
420+
if base.DiskFileNum(w.Num) < d.mu.versions.minUnflushedLogNum {
421+
// Log obsolete WALs that will be removed.
422+
for i := range w.NumSegments() {
423+
fs, path := w.SegmentLocation(i)
424+
if err := fs.Remove(path); err != nil {
425+
// It's not a big deal if we can't delete the file now.
426+
// We'll try to remove it later in the cleanup process.
427+
d.opts.EventListener.WALDeleted(WALDeleteInfo{
428+
JobID: 0,
429+
Path: path,
430+
FileNum: base.DiskFileNum(w.Num),
431+
Err: err,
432+
})
433+
retainedWALs = append(retainedWALs, w)
434+
} else {
435+
d.opts.EventListener.WALDeleted(WALDeleteInfo{
436+
JobID: 0,
437+
Path: path,
438+
FileNum: base.DiskFileNum(w.Num),
439+
Err: nil,
440+
})
441+
}
442+
}
443+
} else {
444+
retainedWALs = append(retainedWALs, w)
445+
}
419446
}
420-
walManager, err := wal.Init(walOpts, wals)
447+
448+
walManager, err := wal.Init(walOpts, retainedWALs)
421449
if err != nil {
422450
return nil, err
423451
}

testdata/cleaner

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ open-dir: db1
244244
open: db1/MANIFEST-000001
245245
close: db1/MANIFEST-000001
246246
lock: db1_wal/LOCK
247+
remove: db1_wal/000002.log
247248
open-dir: db1_wal
248249
open: db1/OPTIONS-000003
249250
close: db1/OPTIONS-000003
@@ -255,7 +256,6 @@ create: db1/marker.manifest.000002.MANIFEST-000458
255256
close: db1/marker.manifest.000002.MANIFEST-000458
256257
remove: db1/marker.manifest.000001.MANIFEST-000001
257258
sync: db1
258-
remove: db1_wal/000002.log
259259
remove: db1_wal/000004.log
260260
create: db1_wal/000459.log
261261
sync: db1_wal

wal/wal.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ type Options struct {
156156
}
157157

158158
// Init constructs and initializes a WAL manager from the provided options and
159-
// the set of initial logs.
159+
// the set of initial logs. The initial parameter must contain all WAL files
160+
// that have not been deleted. It is only used to queue these up for deletion
161+
// at the appropriate time (i.e. when their numbers are less than minUnflushedNum)
162+
// and to ensure that they won't be recycled.
160163
func Init(o Options, initial Logs) (Manager, error) {
161164
var m Manager
162165
if o.Secondary == (Dir{}) {

0 commit comments

Comments
 (0)