fix(defrag): handle defragdb failure

Signed-off-by: Thomas Gosteli <thomas.gosteli@protonmail.ch>
This commit is contained in:
Thomas Gosteli
2024-11-06 11:47:18 +01:00
parent 35cab80e1f
commit 04c042ceac
2 changed files with 48 additions and 18 deletions

View File

@ -490,8 +490,8 @@ func (b *backend) defrag() error {
options = *boltOpenOptions
}
options.OpenFile = func(_ string, _ int, _ os.FileMode) (file *os.File, err error) {
// gofail: var defragNoSpace string
// return nil, fmt.Errorf(defragNoSpace)
// gofail: var defragOpenFileError string
// return nil, fmt.Errorf(defragOpenFileError)
return temp, nil
}
// Don't load tmp db into memory regardless of opening options
@ -526,6 +526,11 @@ func (b *backend) defrag() error {
if rmErr := os.RemoveAll(tmpdb.Path()); rmErr != nil {
b.lg.Error("failed to remove db.tmp after defragmentation completed", zap.Error(rmErr))
}
// restore the bbolt transactions if defragmentation fails
b.batchTx.tx = b.unsafeBegin(true)
b.readTx.tx = b.unsafeBegin(false)
return err
}
@ -578,6 +583,9 @@ func (b *backend) defrag() error {
}
func defragdb(odb, tmpdb *bolt.DB, limit int) error {
// gofail: var defragdbFail string
// return fmt.Errorf(defragdbFail)
// open a tx on tmpdb for writes
tmptx, err := tmpdb.Begin(true)
if err != nil {