2022-03-01 08:59:05 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2023-11-17 07:58:04 +00:00
|
|
|
"bytes"
|
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
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")
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReadShardID reads shard id from db.
|
|
|
|
// If id is missing, returns nil, nil.
|
|
|
|
func (db *DB) ReadShardID() ([]byte, error) {
|
2022-11-15 12:46:32 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return nil, ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// WriteShardID writes shard it to db.
|
|
|
|
func (db *DB) WriteShardID(id []byte) 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-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
|
|
|
}
|