frostfs-node/pkg/local_object_storage/metabase/shard_id.go
Alexander Chuprov 806236da78 [#1121] node: Change mode of shard components
Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
2024-06-05 05:55:24 +00:00

91 lines
2.1 KiB
Go

package meta
import (
"bytes"
"errors"
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
metamode "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
"go.etcd.io/bbolt"
)
var (
shardInfoBucket = []byte{shardInfoPrefix}
shardIDKey = []byte("id")
)
// GetShardID sets metabase operation mode
// and reads shard id from db.
// If id is missing, returns nil, nil.
//
// 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
if err := db.openDB(mode); err != nil {
return nil, fmt.Errorf("failed to open metabase: %w", err)
}
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) {
var id []byte
err := db.boltDB.View(func(tx *bbolt.Tx) error {
b := tx.Bucket(shardInfoBucket)
if b != nil {
id = bytes.Clone(b.Get(shardIDKey))
}
return nil
})
return id, metaerr.Wrap(err)
}
// 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
if mode.ReadOnly() {
return ErrReadOnlyMode
}
if err := db.openDB(mode); err != nil {
return fmt.Errorf("failed to open metabase: %w", err)
}
err := db.writeShardID(id)
if err == nil {
db.metrics.SetMode(metamode.ConvertToComponentModeDegraded(mode))
}
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 {
return metaerr.Wrap(db.boltDB.Update(func(tx *bbolt.Tx) error {
b, err := tx.CreateBucketIfNotExists(shardInfoBucket)
if err != nil {
return err
}
return b.Put(shardIDKey, id)
}))
}