Skip to content

Commit f50d5d9

Browse files
committed
backend: load defragJournal from batchTx instead of passing as parameter
defragCancelJournal and defragReplayAndSwap both hold the batchTx lock, so they can load the journal pointer directly rather than having it threaded through from defragPrepare. This simplifies the signatures and removes parameter passing through defragNonBlocking.
1 parent c6f92cf commit f50d5d9

1 file changed

Lines changed: 16 additions & 17 deletions

File tree

server/storage/backend/backend.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ func (b *backend) defragNonBlocking() error {
652652

653653
dbp, size1, sizeInUse1 := b.defragLogStart()
654654

655-
defragJournal, snapTx, err := b.defragPrepare()
655+
readTx, err := b.defragPrepare()
656656
if err != nil {
657657
tmpdb.Close()
658658
if rmErr := os.Remove(tdbp); rmErr != nil {
@@ -664,11 +664,11 @@ func (b *backend) defragNonBlocking() error {
664664
b.lg.Info("defrag: copying data (writes unlocked)")
665665

666666
// gofail: var defragBeforeCopy struct{}
667-
err = defragFromTx(snapTx, tmpdb, defragLimit)
668-
snapTx.Rollback()
667+
err = defragFromTx(readTx, tmpdb, defragLimit)
668+
readTx.Rollback()
669669

670670
if err != nil {
671-
b.defragCancelJournal(defragJournal)
671+
b.defragCancelJournal()
672672
tmpdb.Close()
673673
if rmErr := os.RemoveAll(tdbp); rmErr != nil {
674674
b.lg.Error("failed to remove tmp database", zap.String("path", tdbp), zap.Error(rmErr))
@@ -678,7 +678,7 @@ func (b *backend) defragNonBlocking() error {
678678

679679
// gofail: var defragBeforeReplay struct{}
680680
b.lg.Info("defrag: replaying journal")
681-
err = b.defragReplayAndSwap(defragJournal, tmpdb, dbp, tdbp)
681+
err = b.defragReplayAndSwap(tmpdb, dbp, tdbp)
682682
if err != nil {
683683
return err
684684
}
@@ -692,7 +692,7 @@ func (b *backend) defragNonBlocking() error {
692692
// defragPrepare commits pending writes, opens a read-only bbolt
693693
// transaction for the copy phase, and installs a journal to capture
694694
// writes that occur during the copy.
695-
func (b *backend) defragPrepare() (*defragJournal, *bolt.Tx, error) {
695+
func (b *backend) defragPrepare() (*bolt.Tx, error) {
696696
b.batchTx.LockOutsideApply()
697697
defer b.batchTx.Unlock()
698698

@@ -702,34 +702,33 @@ func (b *backend) defragPrepare() (*defragJournal, *bolt.Tx, error) {
702702
readTx, err := b.db.Begin(false)
703703
b.mu.RUnlock()
704704
if err != nil {
705-
return nil, nil, fmt.Errorf("failed to begin read tx for defrag: %w", err)
705+
return nil, fmt.Errorf("failed to begin read tx for defrag: %w", err)
706706
}
707707

708-
dj := newDefragJournal(b.defragJournalMaxOps)
709-
b.batchTx.defragJournal.Store(dj)
710-
return dj, readTx, nil
708+
b.batchTx.defragJournal.Store(newDefragJournal(b.defragJournalMaxOps))
709+
return readTx, nil
711710
}
712711

713712
// defragCancelJournal removes the journal from the batch transaction
714713
// and closes it. Called when the copy phase fails.
715-
func (b *backend) defragCancelJournal(dj *defragJournal) {
714+
func (b *backend) defragCancelJournal() {
716715
b.batchTx.LockOutsideApply()
717716
defer b.batchTx.Unlock()
718-
b.batchTx.defragJournal.Store(nil)
719-
dj.close()
717+
journal := b.batchTx.defragJournal.Swap(nil)
718+
journal.close()
720719
}
721720

722721
// defragReplayAndSwap drains the journal, replays it into the temp
723722
// database, then atomically swaps the old database for the new one.
724723
// batchTx is held for the entire operation to prevent writes between
725724
// journal drain and database swap.
726-
func (b *backend) defragReplayAndSwap(dj *defragJournal, tmpdb *bolt.DB, dbp, tdbp string) error {
725+
func (b *backend) defragReplayAndSwap(tmpdb *bolt.DB, dbp, tdbp string) error {
727726
b.batchTx.LockOutsideApply()
728727
defer b.batchTx.Unlock()
729728

730-
b.batchTx.defragJournal.Store(nil)
731-
dj.close()
732-
ops := dj.drain()
729+
journal := b.batchTx.defragJournal.Swap(nil)
730+
journal.close()
731+
ops := journal.drain()
733732

734733
if len(ops) > 0 {
735734
b.lg.Info("defrag: replaying journal ops", zap.Int("count", len(ops)))

0 commit comments

Comments
 (0)