[#9999] metabase: Fix db engine to pebble in shard_id.go

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2024-07-02 15:13:28 +03:00
parent a5071be23e
commit 586a971b90
2 changed files with 16 additions and 15 deletions

View file

@ -8,13 +8,10 @@ import (
"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"
"github.com/cockroachdb/pebble"
)
var (
shardInfoBucket = []byte{shardInfoPrefix}
shardIDKey = []byte("id")
)
var shardIDKey = []byte("id")
// GetShardID sets metabase operation mode
// and reads shard id from db.
@ -47,11 +44,12 @@ func (db *DB) GetShardID(mode metamode.Mode) ([]byte, error) {
// If id is missing, returns nil, nil.
func (db *DB) readShardID() ([]byte, error) {
var id []byte
err := db.database.View(func(tx *bbolt.Tx) error {
b := tx.Bucket(shardInfoBucket)
if b != nil {
id = bytes.Clone(b.Get(shardIDKey))
err := db.snapshot(func(s *pebble.Snapshot) error {
v, err := valueSafe(s, shardInfoKey(shardIDKey))
if err != nil {
return err
}
id = bytes.Clone(v)
return nil
})
return id, metaerr.Wrap(err)
@ -86,11 +84,7 @@ func (db *DB) SetShardID(id []byte, mode metamode.Mode) error {
// writeShardID writes shard id to db.
func (db *DB) writeShardID(id []byte) error {
return metaerr.Wrap(db.database.Update(func(tx *bbolt.Tx) error {
b, err := tx.CreateBucketIfNotExists(shardInfoBucket)
if err != nil {
return err
}
return b.Put(shardIDKey, id)
return metaerr.Wrap(db.batch(func(b *pebble.Batch) error {
return b.Set(shardInfoKey(shardIDKey), id, pebble.Sync)
}))
}

View file

@ -669,3 +669,10 @@ func addressFromExpiredKey(key []byte) (oid.Address, error) {
result.SetContainer(cnr)
return result, nil
}
func shardInfoKey(key []byte) []byte {
result := make([]byte, len(key))
result[0] = shardInfoPrefix
copy(result[1:], key)
return result
}