@@ -395,35 +395,41 @@ func Open(dirname string, opts *Options) (db *DB, err error) {
395395 return nil , err
396396 }
397397
398- // Remove obsolete WAL files now (as opposed to relying on asynchronous cleanup)
399- // to prevent crash loops due to no disk space (ENOSPC).
398+ // Remove obsolete WAL files now (as opposed to relying on asynchronous
399+ // cleanup) to prevent crash loops due to no disk space (ENOSPC).
400400 var retainedWALs wal.Logs
401401 for _ , w := range wals {
402- if base .DiskFileNum (w .Num ) < d .mu .versions .minUnflushedLogNum {
403- // Log obsolete WALs that will be removed.
404- for i := range w .NumSegments () {
405- fs , path := w .SegmentLocation (i )
406- if err := fs .Remove (path ); err != nil {
407- // It's not a big deal if we can't delete the file now.
408- // We'll try to remove it later in the cleanup process.
409- d .opts .EventListener .WALDeleted (WALDeleteInfo {
410- JobID : 0 ,
411- Path : path ,
412- FileNum : base .DiskFileNum (w .Num ),
413- Err : err ,
414- })
415- retainedWALs = append (retainedWALs , w )
416- } else {
417- d .opts .EventListener .WALDeleted (WALDeleteInfo {
418- JobID : 0 ,
419- Path : path ,
420- FileNum : base .DiskFileNum (w .Num ),
421- Err : nil ,
422- })
423- }
424- }
425- } else {
402+ // Any WALs with file numbers ≥ minUnflushedLogNum must be replayed to
403+ // recover the state.
404+ if base .DiskFileNum (w .Num ) >= d .mu .versions .minUnflushedLogNum {
426405 retainedWALs = append (retainedWALs , w )
406+ continue
407+ }
408+ // Skip removal of obsolete WALs in read-only mode.
409+ if opts .ReadOnly {
410+ continue
411+ }
412+ // Remove obsolete WALs, logging each removal.
413+ for i := range w .NumSegments () {
414+ fs , path := w .SegmentLocation (i )
415+ if err := fs .Remove (path ); err != nil {
416+ // It's not a big deal if we can't delete the file now.
417+ // We'll try to remove it later in the cleanup process.
418+ d .opts .EventListener .WALDeleted (WALDeleteInfo {
419+ JobID : 0 ,
420+ Path : path ,
421+ FileNum : base .DiskFileNum (w .Num ),
422+ Err : err ,
423+ })
424+ retainedWALs = append (retainedWALs , w )
425+ } else {
426+ d .opts .EventListener .WALDeleted (WALDeleteInfo {
427+ JobID : 0 ,
428+ Path : path ,
429+ FileNum : base .DiskFileNum (w .Num ),
430+ Err : nil ,
431+ })
432+ }
427433 }
428434 }
429435
0 commit comments