From 4e4fb9ad529d2d3d1c4179e86c8dfd2d2d8b7f85 Mon Sep 17 00:00:00 2001 From: Justin Traglia <95511699+jtraglia@users.noreply.github.com> Date: Wed, 6 Dec 2023 17:39:02 -0600 Subject: [PATCH] Split blob pruning into two funcs (#13285) --- beacon-chain/db/filesystem/blob.go | 43 ++++++++++++++++++------------ 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/beacon-chain/db/filesystem/blob.go b/beacon-chain/db/filesystem/blob.go index 1e155f1ab..fe45eef45 100644 --- a/beacon-chain/db/filesystem/blob.go +++ b/beacon-chain/db/filesystem/blob.go @@ -6,6 +6,7 @@ import ( "io" "os" "path" + "path/filepath" "strconv" "strings" @@ -218,26 +219,34 @@ func (bs *BlobStorage) Prune(currentSlot primitives.Slot) error { } for _, folder := range folders { if folder.IsDir() { - f, err := bs.fs.Open(folder.Name() + "/0." + sszExt) - if err != nil { + if err := bs.processFolder(folder, currentSlot, retentionSlots); err != nil { return err } - defer func(f afero.File) { - err := f.Close() - if err != nil { - log.WithError(err).Errorf("Could not close blob file") - } - }(f) + } + } + return nil +} - slot, err := slotFromBlob(f) - if err != nil { - return err - } - if slot < (currentSlot - retentionSlots) { - if err = bs.fs.RemoveAll(folder.Name()); err != nil { - return errors.Wrapf(err, "failed to delete blob %s", f.Name()) - } - } +// processFolder will delete the folder of blobs if the blob slot is outside the +// retention period. We determine the slot by looking at the first blob in the folder. +func (bs *BlobStorage) processFolder(folder os.FileInfo, currentSlot, retentionSlots primitives.Slot) error { + f, err := bs.fs.Open(filepath.Join(folder.Name(), "0."+sszExt)) + if err != nil { + return err + } + defer func() { + if err := f.Close(); err != nil { + log.WithError(err).Errorf("Could not close blob file") + } + }() + + slot, err := slotFromBlob(f) + if err != nil { + return err + } + if slot < (currentSlot - retentionSlots) { + if err = bs.fs.RemoveAll(folder.Name()); err != nil { + return errors.Wrapf(err, "failed to delete blob %s", f.Name()) } } return nil