Dmitrii Stepanov
76cf7a051b
All checks were successful
DCO action / DCO (pull_request) Successful in 6m15s
Build / Build Components (1.22) (pull_request) Successful in 11m1s
Build / Build Components (1.21) (pull_request) Successful in 11m6s
Tests and linters / Staticcheck (pull_request) Successful in 16m15s
Tests and linters / gopls check (pull_request) Successful in 16m11s
Tests and linters / Lint (pull_request) Successful in 17m34s
Vulncheck / Vulncheck (pull_request) Successful in 7m47s
Pre-commit hooks / Pre-commit (pull_request) Successful in 21m25s
Tests and linters / Tests (1.21) (pull_request) Successful in 21m22s
Tests and linters / Tests with -race (pull_request) Successful in 21m51s
Tests and linters / Tests (1.22) (pull_request) Successful in 2m46s
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
96 lines
2.2 KiB
Go
96 lines
2.2 KiB
Go
package meta
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
|
|
"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 := os.Stat(db.info.Path); errors.Is(err, os.ErrNotExist) {
|
|
return nil, nil
|
|
}
|
|
|
|
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)
|
|
}))
|
|
}
|