2020-11-30 13:10:05 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2023-08-31 16:26:47 +00:00
|
|
|
"context"
|
2022-10-16 11:39:47 +00:00
|
|
|
"errors"
|
2020-11-30 13:10:05 +00:00
|
|
|
"fmt"
|
2022-02-02 13:28:08 +00:00
|
|
|
"path/filepath"
|
2020-11-30 13:10:05 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util"
|
2020-11-30 13:10:05 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-10-16 11:39:47 +00:00
|
|
|
// ErrDegradedMode is returned when metabase is in a degraded mode.
|
2022-10-31 07:02:30 +00:00
|
|
|
var ErrDegradedMode = logicerr.New("metabase is in a degraded mode")
|
2022-10-16 11:39:47 +00:00
|
|
|
|
2022-11-15 14:53:23 +00:00
|
|
|
// ErrReadOnlyMode is returned when metabase is in a read-only mode.
|
|
|
|
var ErrReadOnlyMode = logicerr.New("metabase is in a read-only mode")
|
|
|
|
|
2020-11-30 13:10:05 +00:00
|
|
|
// Open boltDB instance for metabase.
|
2023-08-31 16:26:47 +00:00
|
|
|
func (db *DB) Open(_ context.Context, 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)
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
db.log.Debug(logs.MetabaseCreatedDirectoryForMetabase, zap.String("path", db.info.Path))
|
2020-11-30 13:10:05 +00:00
|
|
|
|
2022-06-28 13:42:50 +00:00
|
|
|
if db.boltOptions == nil {
|
2022-07-21 13:26:25 +00:00
|
|
|
opts := *bbolt.DefaultOptions
|
|
|
|
db.boltOptions = &opts
|
2022-06-28 13:42:50 +00:00
|
|
|
}
|
|
|
|
db.boltOptions.ReadOnly = readOnly
|
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
return metaerr.Wrap(db.openBolt())
|
2022-10-16 11:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) openBolt() error {
|
|
|
|
var err error
|
|
|
|
|
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
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
db.log.Debug(logs.MetabaseOpenedBoltDBInstanceForMetabase)
|
2020-11-30 13:10:05 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
db.log.Debug(logs.MetabaseCheckingMetabaseVersion)
|
2022-06-15 09:50:59 +00:00
|
|
|
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.
|
|
|
|
//
|
2022-09-21 20:57:46 +00:00
|
|
|
// Returns ErrOutdatedVersion if a database at the provided path is outdated.
|
|
|
|
//
|
2021-09-13 08:23:58 +00:00
|
|
|
// 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 {
|
2023-06-15 10:19:36 +00:00
|
|
|
return metaerr.Wrap(db.init(false))
|
2021-09-13 08:23:58 +00:00
|
|
|
}
|
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 {
|
2022-11-15 12:46:32 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
return metaerr.Wrap(db.init(true))
|
2021-09-13 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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): {},
|
2022-08-19 12:36:37 +00:00
|
|
|
string(shardInfoBucket): {},
|
2022-10-26 06:10:18 +00:00
|
|
|
string(bucketNameLocked): {},
|
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 {
|
2022-10-26 06:04:21 +00:00
|
|
|
name := []byte(k)
|
2021-09-13 08:23:58 +00:00
|
|
|
if reset {
|
2022-10-26 06:04:21 +00:00
|
|
|
err := tx.DeleteBucket(name)
|
|
|
|
if err != nil && !errors.Is(err, bbolt.ErrBucketNotFound) {
|
|
|
|
return fmt.Errorf("could not delete static bucket %s: %w", k, err)
|
2021-09-13 08:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-26 06:04:21 +00:00
|
|
|
|
|
|
|
_, err := tx.CreateBucketIfNotExists(name)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create static bucket %s: %w", k, err)
|
|
|
|
}
|
2021-09-13 08:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !reset {
|
2022-09-12 17:35:44 +00:00
|
|
|
err = syncCounter(tx, false)
|
2022-08-25 15:58:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not sync object counter: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-09-13 08:23:58 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-08 17:32:02 +00:00
|
|
|
// SyncCounters forces to synchronize the object counters.
|
|
|
|
func (db *DB) SyncCounters() error {
|
2022-11-15 12:46:32 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return ErrDegradedMode
|
2022-11-15 14:53:23 +00:00
|
|
|
} else if db.mode.ReadOnly() {
|
|
|
|
return ErrReadOnlyMode
|
2022-11-15 12:46:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
return metaerr.Wrap(db.boltDB.Update(func(tx *bbolt.Tx) error {
|
2022-09-12 17:35:44 +00:00
|
|
|
return syncCounter(tx, true)
|
2023-06-15 10:19:36 +00:00
|
|
|
}))
|
2022-09-08 17:32:02 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 13:10:05 +00:00
|
|
|
// Close closes boltDB instance.
|
|
|
|
func (db *DB) Close() error {
|
2023-06-06 09:27:19 +00:00
|
|
|
var err error
|
2022-06-27 08:06:01 +00:00
|
|
|
if db.boltDB != nil {
|
2023-06-06 09:27:19 +00:00
|
|
|
err = metaerr.Wrap(db.boltDB.Close())
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
db.metrics.Close()
|
2022-06-27 08:06:01 +00:00
|
|
|
}
|
2023-06-06 09:27:19 +00:00
|
|
|
return err
|
2020-11-30 13:10:05 +00:00
|
|
|
}
|
2022-10-16 11:39:47 +00:00
|
|
|
|
|
|
|
// Reload reloads part of the configuration.
|
|
|
|
// It returns true iff database was reopened.
|
|
|
|
// If a config option is invalid, it logs an error and returns nil.
|
|
|
|
// If there was a problem with applying new configuration, an error is returned.
|
|
|
|
//
|
|
|
|
// If a metabase was couldn't be reopened because of an error, ErrDegradedMode is returned.
|
|
|
|
func (db *DB) Reload(opts ...Option) (bool, error) {
|
|
|
|
var c cfg
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](&c)
|
|
|
|
}
|
|
|
|
|
|
|
|
db.modeMtx.Lock()
|
|
|
|
defer db.modeMtx.Unlock()
|
|
|
|
|
2022-10-16 13:11:29 +00:00
|
|
|
if db.mode.NoMetabase() || c.info.Path != "" && filepath.Clean(db.info.Path) != filepath.Clean(c.info.Path) {
|
2022-10-16 11:39:47 +00:00
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
db.mode = mode.Degraded
|
2023-06-06 09:27:19 +00:00
|
|
|
db.metrics.SetMode(mode.Degraded)
|
2022-10-16 11:39:47 +00:00
|
|
|
db.info.Path = c.info.Path
|
|
|
|
if err := db.openBolt(); err != nil {
|
2023-06-15 10:19:36 +00:00
|
|
|
return false, metaerr.Wrap(fmt.Errorf("%w: %v", ErrDegradedMode, err))
|
2022-10-16 11:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
db.mode = mode.ReadWrite
|
2023-06-06 09:27:19 +00:00
|
|
|
db.metrics.SetMode(mode.ReadWrite)
|
2022-10-16 11:39:47 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|