2020-11-30 13:10:05 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-02-02 13:28:08 +00:00
|
|
|
"path/filepath"
|
2020-11-30 13:10:05 +00:00
|
|
|
|
2021-07-28 16:19:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util"
|
2020-11-30 13:10:05 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Open boltDB instance for metabase.
|
2022-06-28 13:42:50 +00:00
|
|
|
func (db *DB) Open(readOnly bool) error {
|
2022-02-02 13:28:08 +00:00
|
|
|
err := util.MkdirAllX(filepath.Dir(db.info.Path), db.info.Permission)
|
2020-11-30 13:10:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't create dir %s for metabase: %w", db.info.Path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
db.log.Debug("created directory for Metabase", zap.String("path", db.info.Path))
|
|
|
|
|
2022-06-28 13:42:50 +00:00
|
|
|
if db.boltOptions == nil {
|
|
|
|
db.boltOptions = bbolt.DefaultOptions
|
|
|
|
}
|
|
|
|
db.boltOptions.ReadOnly = readOnly
|
|
|
|
|
2020-11-30 13:10:05 +00:00
|
|
|
db.boltDB, err = bbolt.Open(db.info.Path, db.info.Permission, db.boltOptions)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't open boltDB database: %w", err)
|
|
|
|
}
|
2022-03-21 13:34:31 +00:00
|
|
|
db.boltDB.MaxBatchDelay = db.boltBatchDelay
|
|
|
|
db.boltDB.MaxBatchSize = db.boltBatchSize
|
2020-11-30 13:10:05 +00:00
|
|
|
|
|
|
|
db.log.Debug("opened boltDB instance for Metabase")
|
|
|
|
|
2022-06-15 09:50:59 +00:00
|
|
|
db.log.Debug("checking metabase version")
|
|
|
|
return db.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
// The safest way to check if the metabase is fresh is to check if it has no buckets.
|
|
|
|
// However, shard info can be present. So here we check that the number of buckets is
|
|
|
|
// at most 1.
|
|
|
|
// Another thing to consider is that tests do not persist shard ID, we want to support
|
|
|
|
// this case too.
|
|
|
|
var n int
|
|
|
|
err := tx.ForEach(func([]byte, *bbolt.Bucket) error {
|
|
|
|
if n++; n >= 2 { // do not iterate a lot
|
|
|
|
return errBreakBucketForEach
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == errBreakBucketForEach {
|
|
|
|
db.initialized = true
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
2020-11-30 13:10:05 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 08:23:58 +00:00
|
|
|
// Init initializes metabase. It creates static (CID-independent) buckets in underlying BoltDB instance.
|
|
|
|
//
|
|
|
|
// Does nothing if metabase has already been initialized and filled. To roll back the database to its initial state,
|
|
|
|
// use Reset.
|
2020-11-30 13:10:05 +00:00
|
|
|
func (db *DB) Init() error {
|
2021-09-13 08:23:58 +00:00
|
|
|
return db.init(false)
|
|
|
|
}
|
2020-11-30 13:10:05 +00:00
|
|
|
|
2021-09-13 08:23:58 +00:00
|
|
|
// Reset resets metabase. Works similar to Init but cleans up all static buckets and
|
|
|
|
// removes all dynamic (CID-dependent) ones in non-blank BoltDB instances.
|
|
|
|
func (db *DB) Reset() error {
|
|
|
|
return db.init(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) init(reset bool) error {
|
2022-07-05 04:55:46 +00:00
|
|
|
if db.mode.NoMetabase() || db.mode.ReadOnly() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-13 08:23:58 +00:00
|
|
|
mStaticBuckets := map[string]struct{}{
|
|
|
|
string(containerVolumeBucketName): {},
|
|
|
|
string(graveyardBucketName): {},
|
|
|
|
string(toMoveItBucketName): {},
|
2021-04-08 14:19:31 +00:00
|
|
|
string(garbageBucketName): {},
|
2021-09-13 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return db.boltDB.Update(func(tx *bbolt.Tx) error {
|
2022-06-15 09:50:59 +00:00
|
|
|
var err error
|
|
|
|
if !reset {
|
|
|
|
// Normal open, check version and update if not initialized.
|
|
|
|
err := checkVersion(tx, db.initialized)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-09-13 08:23:58 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if reset {
|
|
|
|
if err = resetBucket(b); err != nil {
|
|
|
|
return fmt.Errorf("could not reset static bucket %s: %w", k, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-15 09:50:59 +00:00
|
|
|
err = tx.ForEach(func(name []byte, b *bbolt.Bucket) error {
|
2021-09-13 08:23:58 +00:00
|
|
|
if _, ok := mStaticBuckets[string(name)]; !ok {
|
|
|
|
return tx.DeleteBucket(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2022-06-15 09:50:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return updateVersion(tx, version)
|
2021-09-13 08:23:58 +00:00
|
|
|
})
|
2020-11-30 13:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes boltDB instance.
|
|
|
|
func (db *DB) Close() error {
|
|
|
|
return db.boltDB.Close()
|
|
|
|
}
|