2022-02-15 22:20:44 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
2022-02-15 22:20:44 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-03-07 13:38:26 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
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"
|
2022-02-15 22:20:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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:20:44 +00:00
|
|
|
//
|
|
|
|
// Locked list should be unique. Panics if it is empty.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (s *Shard) Lock(ctx context.Context, idCnr cid.ID, locker oid.ID, locked []oid.ID) error {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "Shard.Lock",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("shard_id", s.ID().String()),
|
|
|
|
attribute.String("container_id", idCnr.EncodeToString()),
|
|
|
|
attribute.String("locker", locker.EncodeToString()),
|
|
|
|
attribute.Int("locked_count", len(locked)),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-12-07 17:42:35 +00:00
|
|
|
s.m.RLock()
|
|
|
|
defer s.m.RUnlock()
|
|
|
|
|
|
|
|
m := s.info.Mode
|
2022-06-29 11:27:36 +00:00
|
|
|
if m.ReadOnly() {
|
2022-02-15 22:20:44 +00:00
|
|
|
return ErrReadOnlyMode
|
2022-06-29 11:27:36 +00:00
|
|
|
} else if m.NoMetabase() {
|
|
|
|
return ErrDegradedMode
|
2022-02-15 22:20:44 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
err := s.metaBase.Lock(ctx, idCnr, locker, locked)
|
2022-02-15 22:20:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("metabase lock: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-12 11:59:58 +00:00
|
|
|
|
|
|
|
// IsLocked checks object locking relation of the provided object. Not found object is
|
|
|
|
// considered as not locked. Requires healthy metabase, returns ErrDegradedMode otherwise.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (s *Shard) IsLocked(ctx context.Context, addr oid.Address) (bool, error) {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "Shard.IsLocked",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("shard_id", s.ID().String()),
|
|
|
|
attribute.String("address", addr.EncodeToString()),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-11-12 11:59:58 +00:00
|
|
|
m := s.GetMode()
|
|
|
|
if m.NoMetabase() {
|
|
|
|
return false, ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
|
|
|
var prm meta.IsLockedPrm
|
|
|
|
prm.SetAddress(addr)
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
res, err := s.metaBase.IsLocked(ctx, prm)
|
2022-11-12 11:59:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Locked(), nil
|
|
|
|
}
|
2024-05-27 19:07:43 +00:00
|
|
|
|
|
|
|
// GetLocked return lock id's of the provided object. Not found object is
|
|
|
|
// considered as not locked. Requires healthy metabase, returns ErrDegradedMode otherwise.
|
|
|
|
func (s *Shard) GetLocked(ctx context.Context, addr oid.Address) ([]oid.ID, error) {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "Shard.GetLocked",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("shard_id", s.ID().String()),
|
|
|
|
attribute.String("address", addr.EncodeToString()),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
m := s.GetMode()
|
|
|
|
if m.NoMetabase() {
|
|
|
|
return nil, ErrDegradedMode
|
|
|
|
}
|
|
|
|
return s.metaBase.GetLocked(ctx, addr)
|
|
|
|
}
|