2022-03-01 08:59:05 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2023-11-17 07:58:04 +00:00
|
|
|
"bytes"
|
2024-03-19 09:40:00 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-11-17 07:58:04 +00:00
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
2024-03-19 09:40:00 +00:00
|
|
|
metamode "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
2022-03-01 08:59:05 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-09-08 11:54:21 +00:00
|
|
|
shardInfoBucket = []byte{shardInfoPrefix}
|
2022-03-01 08:59:05 +00:00
|
|
|
shardIDKey = []byte("id")
|
|
|
|
)
|
|
|
|
|
2024-03-19 09:40:00 +00:00
|
|
|
// GetShardID sets metabase operation mode
|
|
|
|
// and reads shard id from db.
|
2022-03-01 08:59:05 +00:00
|
|
|
// If id is missing, returns nil, nil.
|
2024-03-19 09:40:00 +00:00
|
|
|
//
|
|
|
|
// GetShardID does not report any metrics.
|
|
|
|
func (db *DB) GetShardID(mode metamode.Mode) ([]byte, error) {
|
|
|
|
db.modeMtx.Lock()
|
|
|
|
defer db.modeMtx.Unlock()
|
|
|
|
db.mode = mode
|
2022-11-15 12:46:32 +00:00
|
|
|
|
2024-03-19 09:40:00 +00:00
|
|
|
if err := db.openDB(mode); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to open metabase: %w", err)
|
2022-11-15 12:46:32 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 09:40:00 +00:00
|
|
|
id, err := db.readShardID()
|
|
|
|
|
|
|
|
if cErr := db.close(); cErr != nil {
|
|
|
|
err = errors.Join(err, fmt.Errorf("failed to close metabase: %w", cErr))
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, metaerr.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadShardID reads shard id from db.
|
|
|
|
// If id is missing, returns nil, nil.
|
|
|
|
func (db *DB) readShardID() ([]byte, error) {
|
2022-03-01 08:59:05 +00:00
|
|
|
var id []byte
|
|
|
|
err := db.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(shardInfoBucket)
|
|
|
|
if b != nil {
|
2023-11-17 07:58:04 +00:00
|
|
|
id = bytes.Clone(b.Get(shardIDKey))
|
2022-03-01 08:59:05 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2023-06-15 10:19:36 +00:00
|
|
|
return id, metaerr.Wrap(err)
|
2022-03-01 08:59:05 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 09:40:00 +00:00
|
|
|
// SetShardID sets metabase operation mode
|
|
|
|
// and writes shard id to db.
|
|
|
|
func (db *DB) SetShardID(id []byte, mode metamode.Mode) error {
|
|
|
|
db.modeMtx.Lock()
|
|
|
|
defer db.modeMtx.Unlock()
|
|
|
|
db.mode = mode
|
2022-11-15 12:46:32 +00:00
|
|
|
|
2024-03-19 09:40:00 +00:00
|
|
|
if mode.ReadOnly() {
|
2022-11-15 14:53:23 +00:00
|
|
|
return ErrReadOnlyMode
|
2022-11-15 12:46:32 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 09:40:00 +00:00
|
|
|
if err := db.openDB(mode); err != nil {
|
|
|
|
return fmt.Errorf("failed to open metabase: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := db.writeShardID(id)
|
|
|
|
if err == nil {
|
2024-06-04 13:28:47 +00:00
|
|
|
db.metrics.SetMode(metamode.ConvertToComponentModeDegraded(mode))
|
2024-03-19 09:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cErr := db.close(); cErr != nil {
|
|
|
|
err = errors.Join(err, fmt.Errorf("failed to close metabase: %w", cErr))
|
|
|
|
}
|
|
|
|
|
|
|
|
return metaerr.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeShardID writes shard id to db.
|
|
|
|
func (db *DB) writeShardID(id []byte) error {
|
2023-06-15 10:19:36 +00:00
|
|
|
return metaerr.Wrap(db.boltDB.Update(func(tx *bbolt.Tx) error {
|
2022-03-01 08:59:05 +00:00
|
|
|
b, err := tx.CreateBucketIfNotExists(shardInfoBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.Put(shardIDKey, id)
|
2023-06-15 10:19:36 +00:00
|
|
|
}))
|
2022-03-01 08:59:05 +00:00
|
|
|
}
|