2022-07-06 14:09:50 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
2023-02-07 12:08:22 +00:00
|
|
|
"errors"
|
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
2023-10-11 07:33:42 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-07-06 14:09:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
|
|
|
"go.etcd.io/bbolt"
|
2023-04-12 14:01:29 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2022-07-06 14:09:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// StorageIDPrm groups the parameters of StorageID operation.
|
|
|
|
type StorageIDPrm struct {
|
|
|
|
addr oid.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageIDRes groups the resulting values of StorageID operation.
|
|
|
|
type StorageIDRes struct {
|
|
|
|
id []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress is a StorageID option to set the object address to check.
|
|
|
|
func (p *StorageIDPrm) SetAddress(addr oid.Address) {
|
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageID returns storage ID.
|
|
|
|
func (r StorageIDRes) StorageID() []byte {
|
|
|
|
return r.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageID returns storage descriptor for objects from the blobstor.
|
|
|
|
// It is put together with the object can makes get/delete operation faster.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (db *DB) StorageID(ctx context.Context, prm StorageIDPrm) (res StorageIDRes, err error) {
|
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "metabase.StorageID",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("address", prm.addr.EncodeToString()),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-11-15 12:46:32 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return res, ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:09:50 +00:00
|
|
|
err = db.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
res.id, err = db.storageID(tx, prm.addr)
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
return res, metaerr.Wrap(err)
|
2022-07-06 14:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) storageID(tx *bbolt.Tx, addr oid.Address) ([]byte, error) {
|
2022-09-08 11:54:21 +00:00
|
|
|
key := make([]byte, bucketKeySize)
|
|
|
|
smallBucket := tx.Bucket(smallBucketName(addr.Container(), key))
|
2022-07-06 14:09:50 +00:00
|
|
|
if smallBucket == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
storageID := smallBucket.Get(objectKey(addr.Object(), key))
|
2022-07-06 14:09:50 +00:00
|
|
|
if storageID == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return slice.Copy(storageID), nil
|
|
|
|
}
|
2022-10-20 12:09:05 +00:00
|
|
|
|
|
|
|
// UpdateStorageIDPrm groups the parameters of UpdateStorageID operation.
|
|
|
|
type UpdateStorageIDPrm struct {
|
|
|
|
addr oid.Address
|
|
|
|
id []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateStorageIDRes groups the resulting values of UpdateStorageID operation.
|
|
|
|
type UpdateStorageIDRes struct{}
|
|
|
|
|
|
|
|
// SetAddress is an UpdateStorageID option to set the object address to check.
|
|
|
|
func (p *UpdateStorageIDPrm) SetAddress(addr oid.Address) {
|
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetStorageID is an UpdateStorageID option to set the storage ID.
|
|
|
|
func (p *UpdateStorageIDPrm) SetStorageID(id []byte) {
|
|
|
|
p.id = id
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateStorageID updates storage descriptor for objects from the blobstor.
|
|
|
|
func (db *DB) UpdateStorageID(prm UpdateStorageIDPrm) (res UpdateStorageIDRes, err error) {
|
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
2022-11-15 12:46:32 +00:00
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return res, ErrDegradedMode
|
2022-11-15 14:53:23 +00:00
|
|
|
} else if db.mode.ReadOnly() {
|
|
|
|
return res, ErrReadOnlyMode
|
2022-11-15 12:46:32 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 12:09:05 +00:00
|
|
|
currEpoch := db.epochState.CurrentEpoch()
|
|
|
|
|
|
|
|
err = db.boltDB.Batch(func(tx *bbolt.Tx) error {
|
|
|
|
exists, err := db.exists(tx, prm.addr, currEpoch)
|
2023-10-11 07:33:42 +00:00
|
|
|
if err == nil && exists || errors.As(err, new(logicerr.Logical)) {
|
2023-02-07 12:08:22 +00:00
|
|
|
err = updateStorageID(tx, prm.addr, prm.id)
|
2022-10-20 12:09:05 +00:00
|
|
|
}
|
2023-02-07 12:08:22 +00:00
|
|
|
|
|
|
|
return err
|
2022-10-20 12:09:05 +00:00
|
|
|
})
|
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
return res, metaerr.Wrap(err)
|
2022-10-20 12:09:05 +00:00
|
|
|
}
|