From e4bc3d0e9d6dec2e5982906edf4958c97a97530c Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 26 Oct 2022 09:04:21 +0300 Subject: [PATCH] [#1944] metabase: Recreate static buckets instead of resetting From the `Bucket.ForEach` doc: ``` The provided function must not modify the bucket; this will result in undefined behavior. ``` Signed-off-by: Evgenii Stratonikov --- pkg/local_object_storage/metabase/control.go | 16 +++++++++------- pkg/local_object_storage/metabase/util.go | 11 ----------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/pkg/local_object_storage/metabase/control.go b/pkg/local_object_storage/metabase/control.go index 750ecfdd5..adcd80943 100644 --- a/pkg/local_object_storage/metabase/control.go +++ b/pkg/local_object_storage/metabase/control.go @@ -106,15 +106,17 @@ func (db *DB) init(reset bool) error { } } for k := range mStaticBuckets { - b, err := tx.CreateBucketIfNotExists([]byte(k)) - if err != nil { - return fmt.Errorf("could not create static bucket %s: %w", k, err) + name := []byte(k) + if reset { + err := tx.DeleteBucket(name) + if err != nil && !errors.Is(err, bbolt.ErrBucketNotFound) { + return fmt.Errorf("could not delete static bucket %s: %w", k, err) + } } - if reset { - if err = resetBucket(b); err != nil { - return fmt.Errorf("could not reset static bucket %s: %w", k, err) - } + _, err := tx.CreateBucketIfNotExists(name) + if err != nil { + return fmt.Errorf("could not create static bucket %s: %w", k, err) } } diff --git a/pkg/local_object_storage/metabase/util.go b/pkg/local_object_storage/metabase/util.go index 132870d10..7102dd183 100644 --- a/pkg/local_object_storage/metabase/util.go +++ b/pkg/local_object_storage/metabase/util.go @@ -223,17 +223,6 @@ func objectKey(obj oid.ID, key []byte) []byte { return key[:objectKeySize] } -// removes all bucket elements. -func resetBucket(b *bbolt.Bucket) error { - return b.ForEach(func(k, v []byte) error { - if v != nil { - return b.Delete(k) - } - - return b.DeleteBucket(k) - }) -} - // if meets irregular object container in objs - returns its type, otherwise returns object.TypeRegular. // // firstIrregularObjectType(tx, cnr, obj) usage allows getting object type.