Dmitrii Stepanov
76ff26039c
All checks were successful
DCO action / DCO (pull_request) Successful in 2m13s
Build / Build Components (1.20) (pull_request) Successful in 5m48s
Build / Build Components (1.21) (pull_request) Successful in 5m47s
Vulncheck / Vulncheck (pull_request) Successful in 5m47s
Tests and linters / Tests (1.21) (pull_request) Successful in 7m33s
Tests and linters / Staticcheck (pull_request) Successful in 7m37s
Tests and linters / Lint (pull_request) Successful in 8m8s
Tests and linters / Tests (1.20) (pull_request) Successful in 10m38s
Tests and linters / Tests with -race (pull_request) Successful in 11m22s
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package meta
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
|
"go.etcd.io/bbolt"
|
|
)
|
|
|
|
var (
|
|
shardInfoBucket = []byte{shardInfoPrefix}
|
|
shardIDKey = []byte("id")
|
|
)
|
|
|
|
// ReadShardID reads shard id from db.
|
|
// If id is missing, returns nil, nil.
|
|
func (db *DB) ReadShardID() ([]byte, error) {
|
|
db.modeMtx.RLock()
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
if db.mode.NoMetabase() {
|
|
return nil, ErrDegradedMode
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
// WriteShardID writes shard it to db.
|
|
func (db *DB) WriteShardID(id []byte) error {
|
|
db.modeMtx.RLock()
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
if db.mode.NoMetabase() {
|
|
return ErrDegradedMode
|
|
} else if db.mode.ReadOnly() {
|
|
return ErrReadOnlyMode
|
|
}
|
|
|
|
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)
|
|
}))
|
|
}
|