2022-02-15 22:21:44 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
|
|
|
apistatus "github.com/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
cid "github.com/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
objectSDK "github.com/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "github.com/TrueCloudLab/frostfs-sdk-go/object/id"
|
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.
|
|
|
|
func (e *StorageEngine) Lock(idCnr cid.ID, locker oid.ID, locked []oid.ID) error {
|
|
|
|
return e.execIfNotBlocked(func() error {
|
|
|
|
return e.lock(idCnr, locker, locked)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StorageEngine) lock(idCnr cid.ID, locker oid.ID, locked []oid.ID) error {
|
|
|
|
for i := range locked {
|
|
|
|
switch e.lockSingle(idCnr, locker, locked[i], true) {
|
|
|
|
case 1:
|
2022-10-26 12:23:12 +00:00
|
|
|
return logicerr.Wrap(apistatus.LockNonRegularObject{})
|
2022-02-15 22:21:44 +00:00
|
|
|
case 0:
|
2022-02-16 15:43:46 +00:00
|
|
|
switch e.lockSingle(idCnr, locker, locked[i], false) {
|
2022-02-15 22:21:44 +00:00
|
|
|
case 1:
|
2022-10-26 12:23:12 +00:00
|
|
|
return logicerr.Wrap(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
|
2022-02-15 22:21:44 +00:00
|
|
|
func (e *StorageEngine) lockSingle(idCnr cid.ID, locker, locked oid.ID, checkExists bool) (status uint8) {
|
|
|
|
// code is pretty similar to inhumeAddr, maybe unify?
|
|
|
|
root := false
|
2022-02-16 15:49:19 +00:00
|
|
|
var errIrregular apistatus.LockNonRegularObject
|
2022-02-15 22:21:44 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var addrLocked oid.Address
|
|
|
|
addrLocked.SetContainer(idCnr)
|
|
|
|
addrLocked.SetObject(locked)
|
2022-02-15 22:21:44 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
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
|
2022-07-13 12:43:04 +00:00
|
|
|
existsPrm.SetAddress(addrLocked)
|
2022-05-20 18:08:59 +00:00
|
|
|
|
|
|
|
exRes, err := sh.Exists(existsPrm)
|
2022-02-15 22:21:44 +00:00
|
|
|
if err != nil {
|
|
|
|
var siErr *objectSDK.SplitInfoError
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-15 22:21:44 +00:00
|
|
|
e.reportShardError(sh, "could not check locked object for presence in shard", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
root = true
|
|
|
|
} else if !exRes.Exists() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := sh.Lock(idCnr, locker, []oid.ID{locked})
|
|
|
|
if err != nil {
|
|
|
|
e.reportShardError(sh, "could not lock object in shard", err)
|
|
|
|
|
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
|
|
|
|
}
|