Skip to content

Commit ed3b219

Browse files
author
Ibrahim Jarif
authored
Fix(cleanup): Avoid truncating in value.Open on error (#1465)
* Fix(cleanup): Avoid truncating in value.Open on error 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 go routines 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 lenght 0 if open fails. This PR fixes this by using vlog.stopDiscardStatFlush() instead of vlog.close.
1 parent dc653b0 commit ed3b219

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
@@ -448,7 +448,10 @@ func (db *DB) cleanup() {
448448
}
449449

450450
db.orc.Stop()
451-
db.vlog.Close()
451+
452+
// Do not use vlog.Close() here. vlog.Close truncates the files. We don't
453+
// want to truncate files unless the user has specified the truncate flag.
454+
db.vlog.stopFlushDiscardStats()
452455
}
453456

454457
// 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
@@ -1047,6 +1047,8 @@ func (vlog *valueLog) replayLog(lf *logFile, offset uint32, replayFn logEntry) e
10471047
// End offset is different from file size. So, we should truncate the file
10481048
// to that size.
10491049
if !vlog.opt.Truncate {
1050+
vlog.db.opt.Warningf("Truncate Needed. File %s size: %d Endoffset: %d",
1051+
lf.fd.Name(), fi.Size(), endOffset)
10501052
return ErrTruncateNeeded
10511053
}
10521054

@@ -1063,6 +1065,7 @@ func (vlog *valueLog) replayLog(lf *logFile, offset uint32, replayFn logEntry) e
10631065
return lf.bootstrap()
10641066
}
10651067

1068+
vlog.db.opt.Infof("Truncating vlog file %s to offset: %d", lf.fd.Name(), endOffset)
10661069
if err := lf.fd.Truncate(int64(endOffset)); err != nil {
10671070
return errFile(err, lf.path, fmt.Sprintf(
10681071
"Truncation needed at offset %d. Can be done manually as well.", endOffset))
@@ -1228,6 +1231,12 @@ func (lf *logFile) init() error {
12281231
return nil
12291232
}
12301233

1234+
func (vlog *valueLog) stopFlushDiscardStats() {
1235+
if vlog.lfDiscardStats != nil {
1236+
vlog.lfDiscardStats.closer.Signal()
1237+
}
1238+
}
1239+
12311240
func (vlog *valueLog) Close() error {
12321241
if vlog == nil || vlog.db == nil || vlog.db.opt.InMemory {
12331242
return nil
@@ -1245,6 +1254,9 @@ func (vlog *valueLog) Close() error {
12451254
}
12461255

12471256
maxFid := vlog.maxFid
1257+
// TODO(ibrahim) - Do we need the following truncations on non-windows
1258+
// platforms? We expand the file only on windows and the vlog.woffset()
1259+
// should point to end of file on all other platforms.
12481260
if !vlog.opt.ReadOnly && id == maxFid {
12491261
// truncate writable log file to correct offset.
12501262
if truncErr := f.fd.Truncate(

0 commit comments

Comments
 (0)