Skip to content

Commit b41e77a

Browse files
author
Ibrahim Jarif
authored
Fix(cleanup): Avoid truncating in value.Open on error (#1465) (#1489)
The vlog.Open() function can return an error denoting that the open was unsuccessful but we have `db.cleanup` which will be called to stop all the running goroutines in case badger couldn't start. The db.cleanup function calls vlog.Close() which will truncate the maxFid vlog file based on the vlog.writableLogOffset. The vlog.writableLogOffset was not being updated in case open failed. As a result of this, we will end up truncating the vlog file to length 0 if open fails. This PR fixes this by using vlog.stopDiscardStatFlush() instead of vlog.close. (cherry picked from commit ed3b219)
1 parent a0d4903 commit b41e77a

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

db.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,10 @@ func (db *DB) cleanup() {
446446
}
447447

448448
db.orc.Stop()
449-
db.vlog.Close()
449+
450+
// Do not use vlog.Close() here. vlog.Close truncates the files. We don't
451+
// want to truncate files unless the user has specified the truncate flag.
452+
db.vlog.stopFlushDiscardStats()
450453
}
451454

452455
// BlockCacheMetrics returns the metrics for the underlying block cache.

value.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,8 @@ func (vlog *valueLog) replayLog(lf *logFile, offset uint32, replayFn logEntry) e
10581058
// End offset is different from file size. So, we should truncate the file
10591059
// to that size.
10601060
if !vlog.opt.Truncate {
1061+
vlog.db.opt.Warningf("Truncate Needed. File %s size: %d Endoffset: %d",
1062+
lf.fd.Name(), fi.Size(), endOffset)
10611063
return ErrTruncateNeeded
10621064
}
10631065

@@ -1074,6 +1076,7 @@ func (vlog *valueLog) replayLog(lf *logFile, offset uint32, replayFn logEntry) e
10741076
return lf.bootstrap()
10751077
}
10761078

1079+
vlog.db.opt.Infof("Truncating vlog file %s to offset: %d", lf.fd.Name(), endOffset)
10771080
if err := lf.fd.Truncate(int64(endOffset)); err != nil {
10781081
return errFile(err, lf.path, fmt.Sprintf(
10791082
"Truncation needed at offset %d. Can be done manually as well.", endOffset))
@@ -1239,6 +1242,12 @@ func (lf *logFile) init() error {
12391242
return nil
12401243
}
12411244

1245+
func (vlog *valueLog) stopFlushDiscardStats() {
1246+
if vlog.lfDiscardStats != nil {
1247+
vlog.lfDiscardStats.closer.Signal()
1248+
}
1249+
}
1250+
12421251
func (vlog *valueLog) Close() error {
12431252
if vlog == nil || vlog.db == nil || vlog.db.opt.InMemory {
12441253
return nil
@@ -1256,6 +1265,9 @@ func (vlog *valueLog) Close() error {
12561265
}
12571266

12581267
maxFid := vlog.maxFid
1268+
// TODO(ibrahim) - Do we need the following truncations on non-windows
1269+
// platforms? We expand the file only on windows and the vlog.woffset()
1270+
// should point to end of file on all other platforms.
12591271
if !vlog.opt.ReadOnly && id == maxFid {
12601272
// truncate writable log file to correct offset.
12611273
if truncErr := f.fd.Truncate(

0 commit comments

Comments
 (0)