db: clear blobs when using --clear-db or --force-clear-db (#13605)

* Call Close() as part of ClearDB

* Add method to clear blob storage

* Clear blob storage when clearing DB
This commit is contained in:
Preston Van Loon 2024-02-09 21:52:30 -06:00 committed by GitHub
parent 5df8b83a05
commit 03068ba781
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 44 additions and 11 deletions

View File

@ -228,6 +228,20 @@ func (bs *BlobStorage) Indices(root [32]byte) ([fieldparams.MaxBlobsPerBlock]boo
return mask, nil
}
// Clear deletes all files on the filesystem.
func (bs *BlobStorage) Clear() error {
dirs, err := listDir(bs.fs, ".")
if err != nil {
return err
}
for _, dir := range dirs {
if err := bs.fs.RemoveAll(dir); err != nil {
return err
}
}
return nil
}
type blobNamer struct {
root [32]byte
index uint64

View File

@ -88,6 +88,19 @@ func TestBlobStorage_SaveBlobData(t *testing.T) {
_, err = bs.Get(expected.BlockRoot(), expected.Index)
require.ErrorContains(t, "file does not exist", err)
})
t.Run("clear", func(t *testing.T) {
blob := testSidecars[0]
b := NewEphemeralBlobStorage(t)
require.NoError(t, b.Save(blob))
res, err := b.Get(blob.BlockRoot(), blob.Index)
require.NoError(t, err)
require.NotNil(t, res)
require.NoError(t, b.Clear())
// After clearing, the blob should not exist in the db.
_, err = b.Get(blob.BlockRoot(), blob.Index)
require.ErrorIs(t, err, os.ErrNotExist)
})
}
// pollUntil polls a condition function until it returns true or a timeout is reached.

View File

@ -213,6 +213,9 @@ func NewKVStore(ctx context.Context, dirPath string, opts ...KVStoreOption) (*St
// ClearDB removes the previously stored database in the data directory.
func (s *Store) ClearDB() error {
if err := s.Close(); err != nil {
return fmt.Errorf("failed to close db: %w", err)
}
if _, err := os.Stat(s.databasePath); os.IsNotExist(err) {
return nil
}

View File

@ -4,6 +4,7 @@ package slasherkv
import (
"context"
"fmt"
"os"
"path"
"time"
@ -87,6 +88,9 @@ func NewKVStore(ctx context.Context, dirPath string) (*Store, error) {
// ClearDB removes the previously stored database in the data directory.
func (s *Store) ClearDB() error {
if err := s.Close(); err != nil {
return fmt.Errorf("failed to close db: %w", err)
}
if _, err := os.Stat(s.databasePath); os.IsNotExist(err) {
return nil
}

View File

@ -427,13 +427,12 @@ func (b *BeaconNode) startDB(cliCtx *cli.Context, depositAddress string) error {
}
if clearDBConfirmed || forceClearDB {
log.Warning("Removing database")
if err := d.Close(); err != nil {
return errors.Wrap(err, "could not close db prior to clearing")
}
if err := d.ClearDB(); err != nil {
return errors.Wrap(err, "could not clear database")
}
if err := b.BlobStorage.Clear(); err != nil {
return errors.Wrap(err, "could not clear blob storage")
}
d, err = kv.NewKVStore(b.ctx, dbPath)
if err != nil {
return errors.Wrap(err, "could not create new database")
@ -531,12 +530,12 @@ func (b *BeaconNode) startSlasherDB(cliCtx *cli.Context) error {
}
if clearDBConfirmed || forceClearDB {
log.Warning("Removing database")
if err := d.Close(); err != nil {
return errors.Wrap(err, "could not close db prior to clearing")
}
if err := d.ClearDB(); err != nil {
return errors.Wrap(err, "could not clear database")
}
if err := b.BlobStorage.Clear(); err != nil {
return errors.Wrap(err, "could not clear blob storage")
}
d, err = slasherkv.NewKVStore(b.ctx, dbPath)
if err != nil {
return errors.Wrap(err, "could not create new database")

View File

@ -3,6 +3,7 @@ package kv
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
@ -84,10 +85,12 @@ func (s *Store) view(fn func(*bolt.Tx) error) error {
// ClearDB removes any previously stored data at the configured data directory.
func (s *Store) ClearDB() error {
if err := s.Close(); err != nil {
return fmt.Errorf("failed to close db: %w", err)
}
if _, err := os.Stat(s.databasePath); os.IsNotExist(err) {
return nil
}
prometheus.Unregister(createBoltCollector(s.db))
return os.Remove(filepath.Join(s.databasePath, ProtectionDbFileName))
}

View File

@ -18,9 +18,6 @@ func SetupDB(t testing.TB, pubkeys [][fieldparams.BLSPubkeyLength]byte) iface.Va
t.Fatalf("Failed to instantiate DB: %v", err)
}
t.Cleanup(func() {
if err := db.Close(); err != nil {
t.Fatalf("Failed to close database: %v", err)
}
if err := db.ClearDB(); err != nil {
t.Fatalf("Failed to clear database: %v", err)
}