@@ -33,7 +33,6 @@ import (
33
33
"github.com/cockroachdb/pebble/record"
34
34
"github.com/cockroachdb/pebble/vfs"
35
35
"github.com/cockroachdb/pebble/wal"
36
- "github.com/cockroachdb/redact"
37
36
"github.com/prometheus/client_golang/prometheus"
38
37
)
39
38
@@ -413,11 +412,40 @@ func Open(dirname string, opts *Options) (db *DB, err error) {
413
412
if err != nil {
414
413
return nil , err
415
414
}
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
+ }
419
446
}
420
- walManager , err := wal .Init (walOpts , wals )
447
+
448
+ walManager , err := wal .Init (walOpts , retainedWALs )
421
449
if err != nil {
422
450
return nil , err
423
451
}
0 commit comments