2022-02-15 22:21:44 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2022-02-15 22:21:44 +00:00
|
|
|
"errors"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard"
|
|
|
|
"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
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2023-04-12 14:01:29 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2024-08-05 10:24:48 +00:00
|
|
|
"go.uber.org/zap"
|
2022-02-15 22:21:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var errLockFailed = errors.New("lock operation failed")
|
|
|
|
|
|
|
|
// Lock marks objects as locked with another object. All objects from the
|
|
|
|
// specified container.
|
|
|
|
//
|
2022-03-05 08:46:02 +00:00
|
|
|
// Allows locking regular objects only (otherwise returns apistatus.LockNonRegularObject).
|
2022-02-15 22:21:44 +00:00
|
|
|
//
|
|
|
|
// Locked list should be unique. Panics if it is empty.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (e *StorageEngine) Lock(ctx context.Context, idCnr cid.ID, locker oid.ID, locked []oid.ID) error {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "StorageEngine.Lock",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("container_id", idCnr.EncodeToString()),
|
|
|
|
attribute.String("locker", locker.EncodeToString()),
|
|
|
|
attribute.Int("locked_count", len(locked)),
|
|
|
|
))
|
|
|
|
defer span.End()
|
2024-11-11 09:46:55 +00:00
|
|
|
defer elapsed("Lock", e.metrics.AddMethodDuration)()
|
2023-04-12 14:01:29 +00:00
|
|
|
|
2022-02-15 22:21:44 +00:00
|
|
|
return e.execIfNotBlocked(func() error {
|
2023-04-12 14:01:29 +00:00
|
|
|
return e.lock(ctx, idCnr, locker, locked)
|
2022-02-15 22:21:44 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
func (e *StorageEngine) lock(ctx context.Context, idCnr cid.ID, locker oid.ID, locked []oid.ID) error {
|
2022-02-15 22:21:44 +00:00
|
|
|
for i := range locked {
|
2023-04-12 14:01:29 +00:00
|
|
|
switch e.lockSingle(ctx, idCnr, locker, locked[i], true) {
|
2022-02-15 22:21:44 +00:00
|
|
|
case 1:
|
2023-08-04 11:14:07 +00:00
|
|
|
return logicerr.Wrap(new(apistatus.LockNonRegularObject))
|
2022-02-15 22:21:44 +00:00
|
|
|
case 0:
|
2023-04-12 14:01:29 +00:00
|
|
|
switch e.lockSingle(ctx, idCnr, locker, locked[i], false) {
|
2022-02-15 22:21:44 +00:00
|
|
|
case 1:
|
2023-08-04 11:14:07 +00:00
|
|
|
return logicerr.Wrap(new(apistatus.LockNonRegularObject))
|
2022-02-15 22:21:44 +00:00
|
|
|
case 0:
|
2022-10-26 12:23:12 +00:00
|
|
|
return logicerr.Wrap(errLockFailed)
|
2022-02-15 22:21:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns:
|
2022-08-15 16:20:20 +00:00
|
|
|
// - 0: fail
|
|
|
|
// - 1: locking irregular object
|
|
|
|
// - 2: ok
|
2023-04-12 14:01:29 +00:00
|
|
|
func (e *StorageEngine) lockSingle(ctx context.Context, idCnr cid.ID, locker, locked oid.ID, checkExists bool) (status uint8) {
|
2022-02-15 22:21:44 +00:00
|
|
|
// code is pretty similar to inhumeAddr, maybe unify?
|
|
|
|
root := false
|
2022-05-31 17:00:41 +00:00
|
|
|
var addrLocked oid.Address
|
|
|
|
addrLocked.SetContainer(idCnr)
|
|
|
|
addrLocked.SetObject(locked)
|
|
|
|
e.iterateOverSortedShards(addrLocked, func(_ int, sh hashedShard) (stop bool) {
|
2022-02-15 22:21:44 +00:00
|
|
|
defer func() {
|
|
|
|
// if object is root we continue since information about it
|
|
|
|
// can be presented in other shards
|
|
|
|
if checkExists && root {
|
|
|
|
stop = false
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if checkExists {
|
2022-05-20 18:08:59 +00:00
|
|
|
var existsPrm shard.ExistsPrm
|
2024-05-30 06:26:06 +00:00
|
|
|
existsPrm.Address = addrLocked
|
2023-04-12 14:01:29 +00:00
|
|
|
exRes, err := sh.Exists(ctx, existsPrm)
|
2022-02-15 22:21:44 +00:00
|
|
|
if err != nil {
|
|
|
|
var siErr *objectSDK.SplitInfoError
|
2024-05-27 19:07:43 +00:00
|
|
|
var eiErr *objectSDK.ECInfoError
|
|
|
|
if errors.As(err, &eiErr) {
|
|
|
|
eclocked := []oid.ID{locked}
|
|
|
|
for _, chunk := range eiErr.ECInfo().Chunks {
|
|
|
|
var objID oid.ID
|
|
|
|
err = objID.ReadFromV2(chunk.ID)
|
|
|
|
if err != nil {
|
2024-10-21 13:27:28 +00:00
|
|
|
e.reportShardError(ctx, sh, "could not lock object in shard", err, zap.Stringer("container_id", idCnr),
|
2024-08-05 10:24:48 +00:00
|
|
|
zap.Stringer("locker_id", locker), zap.Stringer("locked_id", locked))
|
2024-05-27 19:07:43 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
eclocked = append(eclocked, objID)
|
|
|
|
}
|
|
|
|
err = sh.Lock(ctx, idCnr, locker, eclocked)
|
|
|
|
if err != nil {
|
2024-10-21 13:27:28 +00:00
|
|
|
e.reportShardError(ctx, sh, "could not lock object in shard", err, zap.Stringer("container_id", idCnr),
|
2024-08-05 10:24:48 +00:00
|
|
|
zap.Stringer("locker_id", locker), zap.Stringer("locked_id", locked))
|
2024-05-27 19:07:43 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
root = true
|
|
|
|
return false
|
|
|
|
} else if !errors.As(err, &siErr) {
|
2022-07-27 18:38:28 +00:00
|
|
|
if shard.IsErrObjectExpired(err) {
|
|
|
|
// object is already expired =>
|
|
|
|
// do not lock it
|
|
|
|
return true
|
|
|
|
}
|
2024-10-21 13:27:28 +00:00
|
|
|
e.reportShardError(ctx, sh, "could not check locked object for presence in shard", err, zap.Stringer("container_id", idCnr),
|
2024-08-05 10:24:48 +00:00
|
|
|
zap.Stringer("locker_id", locker), zap.Stringer("locked_id", locked))
|
2022-02-15 22:21:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
root = true
|
|
|
|
} else if !exRes.Exists() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
err := sh.Lock(ctx, idCnr, locker, []oid.ID{locked})
|
2022-02-15 22:21:44 +00:00
|
|
|
if err != nil {
|
2024-10-21 13:27:28 +00:00
|
|
|
e.reportShardError(ctx, sh, "could not lock object in shard", err, zap.Stringer("container_id", idCnr),
|
2024-08-05 10:24:48 +00:00
|
|
|
zap.Stringer("locker_id", locker), zap.Stringer("locked_id", locked))
|
2022-02-15 22:21:44 +00:00
|
|
|
|
2023-08-04 11:14:07 +00:00
|
|
|
var errIrregular *apistatus.LockNonRegularObject
|
2022-02-16 15:43:46 +00:00
|
|
|
if errors.As(err, &errIrregular) {
|
2022-02-15 22:21:44 +00:00
|
|
|
status = 1
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
status = 2
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|