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")
|
|
|
|
|
2024-03-19 08:49:19 +00:00
|
|
|
var (
|
|
|
|
mStaticBuckets = map[string]struct{}{
|
|
|
|
string(containerVolumeBucketName): {},
|
|
|
|
string(containerCounterBucketName): {},
|
|
|
|
string(graveyardBucketName): {},
|
|
|
|
string(garbageBucketName): {},
|
|
|
|
string(shardInfoBucket): {},
|
|
|
|
string(bucketNameLocked): {},
|
2024-08-20 08:59:42 +00:00
|
|
|
string(expEpochToObjectBucketName): {},
|
2024-03-19 08:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// deprecatedBuckets buckets that are not used anymore.
|
|
|
|
deprecatedBuckets = [][]byte{
|
|
|
|
toMoveItBucketName,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-11-30 13:10:05 +00:00
|
|
|
// Open boltDB instance for metabase.
|
2024-10-21 08:56:38 +00:00
|
|
|
func (db *DB) Open(ctx context.Context, m mode.Mode) error {
|
2024-02-09 06:17:17 +00:00
|
|
|
db.modeMtx.Lock()
|
|
|
|
defer db.modeMtx.Unlock()
|
2024-06-04 13:28:47 +00:00
|
|
|
db.mode = m
|
|
|
|
db.metrics.SetMode(mode.ConvertToComponentModeDegraded(m))
|
2024-02-09 06:17:17 +00:00
|
|
|
|
2024-06-04 13:28:47 +00:00
|
|
|
if m.NoMetabase() {
|
2024-02-09 06:17:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-10-21 08:56:38 +00:00
|
|
|
return db.openDB(ctx, m)
|
2024-02-09 06:17:17 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 08:56:38 +00:00
|
|
|
func (db *DB) openDB(ctx context.Context, mode mode.Mode) 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)
|
|
|
|
}
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
db.log.Debug(context.Background(), 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
|
|
|
}
|
2024-02-09 06:17:17 +00:00
|
|
|
db.boltOptions.ReadOnly = mode.ReadOnly()
|
2022-06-28 13:42:50 +00:00
|
|
|
|
2024-10-21 08:56:38 +00:00
|
|
|
return metaerr.Wrap(db.openBolt(ctx))
|
2022-10-16 11:39:47 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 08:56:38 +00:00
|
|
|
func (db *DB) openBolt(ctx context.Context) error {
|
2022-10-16 11:39:47 +00:00
|
|
|
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
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
db.log.Debug(context.Background(), logs.MetabaseOpenedBoltDBInstanceForMetabase)
|
2020-11-30 13:10:05 +00:00
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
db.log.Debug(context.Background(), 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
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-02-20 12:55:18 +00:00
|
|
|
for _, b := range deprecatedBuckets {
|
|
|
|
err := tx.DeleteBucket(b)
|
|
|
|
if err != nil && !errors.Is(err, bbolt.ErrBucketNotFound) {
|
|
|
|
return fmt.Errorf("could not delete deprecated bucket %s: %w", string(b), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-01 17:53:24 +00:00
|
|
|
if !reset { // counters will be recalculated by refill metabase
|
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
|
|
|
|
}
|
|
|
|
|
2024-03-19 08:49:19 +00:00
|
|
|
bucketCursor := tx.Cursor()
|
|
|
|
name, _ := bucketCursor.First()
|
|
|
|
for name != nil {
|
2021-09-13 08:23:58 +00:00
|
|
|
if _, ok := mStaticBuckets[string(name)]; !ok {
|
2024-03-19 08:49:19 +00:00
|
|
|
if err := tx.DeleteBucket(name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name, _ = bucketCursor.Seek(name)
|
|
|
|
continue
|
2021-09-13 08:23:58 +00:00
|
|
|
}
|
2024-03-19 08:49:19 +00:00
|
|
|
name, _ = bucketCursor.Next()
|
2022-06-15 09:50:59 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-03-19 09:40:00 +00:00
|
|
|
// Close closes boltDB instance
|
|
|
|
// and reports metabase metric.
|
2020-11-30 13:10:05 +00:00
|
|
|
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 {
|
2024-03-19 09:40:00 +00:00
|
|
|
err = db.close()
|
2023-06-06 09:27:19 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2024-03-19 09:40:00 +00:00
|
|
|
func (db *DB) close() error {
|
|
|
|
return metaerr.Wrap(db.boltDB.Close())
|
|
|
|
}
|
|
|
|
|
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.
|
2024-10-21 08:56:38 +00:00
|
|
|
func (db *DB) Reload(ctx context.Context, opts ...Option) (bool, error) {
|
2022-10-16 11:39:47 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-06-04 13:28:47 +00:00
|
|
|
db.mode = mode.Disabled
|
|
|
|
db.metrics.SetMode(mode.ComponentDisabled)
|
2022-10-16 11:39:47 +00:00
|
|
|
db.info.Path = c.info.Path
|
2024-10-21 08:56:38 +00:00
|
|
|
if err := db.openBolt(ctx); 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
|
2024-06-04 13:28:47 +00:00
|
|
|
db.metrics.SetMode(mode.ComponentReadWrite)
|
2022-10-16 11:39:47 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|