2022-02-15 09:53:35 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2022-02-15 11:35:13 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-02-15 11:35:13 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2022-02-15 09:53:35 +00:00
|
|
|
)
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
var bucketNameLocked = []byte{lockedPrefix}
|
2022-02-15 11:35:13 +00:00
|
|
|
|
|
|
|
// returns name of the bucket with objects of type LOCK for specified container.
|
2022-09-08 11:54:21 +00:00
|
|
|
func bucketNameLockers(idCnr cid.ID, key []byte) []byte {
|
|
|
|
return bucketName(idCnr, lockersPrefix, key)
|
2022-02-15 11:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lock marks objects as locked with another object. All objects are from the
|
|
|
|
// specified container.
|
|
|
|
//
|
2022-03-05 08:46:02 +00:00
|
|
|
// Allows locking regular objects only (otherwise returns apistatus.LockNonRegularObject).
|
2022-02-15 11:35:13 +00:00
|
|
|
//
|
|
|
|
// Locked list should be unique. Panics if it is empty.
|
|
|
|
func (db *DB) Lock(cnr cid.ID, locker oid.ID, locked []oid.ID) error {
|
2022-07-21 13:26:25 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
2022-11-15 12:46:32 +00:00
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return ErrDegradedMode
|
2022-11-15 14:53:23 +00:00
|
|
|
} else if db.mode.ReadOnly() {
|
|
|
|
return ErrReadOnlyMode
|
2022-11-15 12:46:32 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 11:35:13 +00:00
|
|
|
if len(locked) == 0 {
|
|
|
|
panic("empty locked list")
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
// check if all objects are regular
|
|
|
|
bucketKeysLocked := make([][]byte, len(locked))
|
|
|
|
for i := range locked {
|
|
|
|
bucketKeysLocked[i] = objectKey(locked[i], make([]byte, objectKeySize))
|
|
|
|
}
|
|
|
|
key := make([]byte, cidSize)
|
2022-02-15 11:35:13 +00:00
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
return db.boltDB.Update(func(tx *bbolt.Tx) error {
|
2022-02-15 11:35:13 +00:00
|
|
|
if firstIrregularObjectType(tx, cnr, bucketKeysLocked...) != object.TypeRegular {
|
2022-10-26 12:23:12 +00:00
|
|
|
return logicerr.Wrap(apistatus.LockNonRegularObject{})
|
2022-02-15 11:35:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 06:10:18 +00:00
|
|
|
bucketLocked := tx.Bucket(bucketNameLocked)
|
2022-02-15 11:35:13 +00:00
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
cnr.Encode(key)
|
|
|
|
bucketLockedContainer, err := bucketLocked.CreateBucketIfNotExists(key)
|
2022-02-15 11:35:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create container bucket for locked objects %v: %w", cnr, err)
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
keyLocker := objectKey(locker, key)
|
2022-02-15 11:35:13 +00:00
|
|
|
var exLockers [][]byte
|
|
|
|
var updLockers []byte
|
|
|
|
|
|
|
|
loop:
|
|
|
|
for i := range bucketKeysLocked {
|
|
|
|
// decode list of already existing lockers
|
|
|
|
exLockers, err = decodeList(bucketLockedContainer.Get(bucketKeysLocked[i]))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode list of object lockers: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range exLockers {
|
|
|
|
if bytes.Equal(exLockers[i], keyLocker) {
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the list of lockers
|
2022-02-16 15:40:39 +00:00
|
|
|
updLockers, err = encodeList(append(exLockers, keyLocker))
|
|
|
|
if err != nil {
|
|
|
|
// maybe continue for the best effort?
|
|
|
|
return fmt.Errorf("encode list of object lockers: %w", err)
|
2022-02-15 11:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write updated list of lockers
|
|
|
|
err = bucketLockedContainer.Put(bucketKeysLocked[i], updLockers)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("update list of object lockers: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 09:53:35 +00:00
|
|
|
|
2022-02-15 11:35:13 +00:00
|
|
|
return nil
|
|
|
|
})
|
2022-02-15 09:53:35 +00:00
|
|
|
}
|
2022-02-15 18:22:06 +00:00
|
|
|
|
2022-03-10 17:58:58 +00:00
|
|
|
// FreeLockedBy unlocks all objects in DB which are locked by lockers.
|
2023-03-28 08:17:15 +00:00
|
|
|
// Returns slice of unlocked object ID's or an error.
|
|
|
|
func (db *DB) FreeLockedBy(lockers []oid.Address) ([]oid.Address, error) {
|
2022-11-15 12:46:32 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
2023-03-28 08:17:15 +00:00
|
|
|
return nil, ErrDegradedMode
|
2022-11-15 12:46:32 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 08:17:15 +00:00
|
|
|
var unlockedObjects []oid.Address
|
2022-03-10 17:58:58 +00:00
|
|
|
|
2023-03-28 08:17:15 +00:00
|
|
|
return unlockedObjects, db.boltDB.Update(func(tx *bbolt.Tx) error {
|
2022-05-31 17:00:41 +00:00
|
|
|
for i := range lockers {
|
2023-03-28 08:17:15 +00:00
|
|
|
unlocked, err := freePotentialLocks(tx, lockers[i].Container(), lockers[i].Object())
|
2022-03-10 17:58:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-28 08:17:15 +00:00
|
|
|
unlockedObjects = append(unlockedObjects, unlocked...)
|
2022-03-10 17:58:58 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 08:17:15 +00:00
|
|
|
return nil
|
2022-03-10 17:58:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-15 18:22:06 +00:00
|
|
|
// checks if specified object is locked in the specified container.
|
|
|
|
func objectLocked(tx *bbolt.Tx, idCnr cid.ID, idObj oid.ID) bool {
|
|
|
|
bucketLocked := tx.Bucket(bucketNameLocked)
|
|
|
|
if bucketLocked != nil {
|
2022-09-08 11:54:21 +00:00
|
|
|
key := make([]byte, cidSize)
|
|
|
|
idCnr.Encode(key)
|
|
|
|
bucketLockedContainer := bucketLocked.Bucket(key)
|
2022-02-15 18:22:06 +00:00
|
|
|
if bucketLockedContainer != nil {
|
2022-09-08 11:54:21 +00:00
|
|
|
return bucketLockedContainer.Get(objectKey(idObj, key)) != nil
|
2022-02-15 18:22:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2022-02-16 15:42:25 +00:00
|
|
|
|
|
|
|
// releases all records about the objects locked by the locker.
|
2023-03-28 08:17:15 +00:00
|
|
|
// Returns slice of unlocked object ID's or an error.
|
2022-02-16 15:42:25 +00:00
|
|
|
//
|
|
|
|
// Operation is very resource-intensive, which is caused by the admissibility
|
|
|
|
// of multiple locks. Also, if we knew what objects are locked, it would be
|
|
|
|
// possible to speed up the execution.
|
2023-03-28 08:17:15 +00:00
|
|
|
func freePotentialLocks(tx *bbolt.Tx, idCnr cid.ID, locker oid.ID) ([]oid.Address, error) {
|
|
|
|
var unlockedObjects []oid.Address
|
2022-02-16 15:42:25 +00:00
|
|
|
bucketLocked := tx.Bucket(bucketNameLocked)
|
2023-03-21 14:11:04 +00:00
|
|
|
if bucketLocked == nil {
|
2023-03-28 08:17:15 +00:00
|
|
|
return unlockedObjects, nil
|
2023-03-21 14:11:04 +00:00
|
|
|
}
|
2022-09-08 11:54:21 +00:00
|
|
|
|
2023-03-21 14:11:04 +00:00
|
|
|
key := make([]byte, cidSize)
|
|
|
|
idCnr.Encode(key)
|
|
|
|
|
|
|
|
bucketLockedContainer := bucketLocked.Bucket(key)
|
|
|
|
if bucketLockedContainer == nil {
|
2023-03-28 08:17:15 +00:00
|
|
|
return unlockedObjects, nil
|
2023-03-21 14:11:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keyLocker := objectKey(locker, key)
|
2023-03-28 08:17:15 +00:00
|
|
|
return unlockedObjects, bucketLockedContainer.ForEach(func(k, v []byte) error {
|
2023-03-21 14:11:04 +00:00
|
|
|
keyLockers, err := decodeList(v)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode list of lockers in locked bucket: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range keyLockers {
|
|
|
|
if bytes.Equal(keyLockers[i], keyLocker) {
|
|
|
|
if len(keyLockers) == 1 {
|
|
|
|
// locker was all alone
|
|
|
|
err = bucketLockedContainer.Delete(k)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete locked object record from locked bucket: %w", err)
|
|
|
|
}
|
2023-03-28 08:17:15 +00:00
|
|
|
|
|
|
|
var id oid.ID
|
|
|
|
err = id.Decode(k)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode unlocked object id error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(idCnr)
|
|
|
|
addr.SetObject(id)
|
|
|
|
|
|
|
|
unlockedObjects = append(unlockedObjects, addr)
|
2023-03-21 14:11:04 +00:00
|
|
|
} else {
|
|
|
|
// exclude locker
|
|
|
|
keyLockers = append(keyLockers[:i], keyLockers[i+1:]...)
|
2022-02-16 15:42:25 +00:00
|
|
|
|
2023-03-21 14:11:04 +00:00
|
|
|
v, err = encodeList(keyLockers)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("encode updated list of lockers: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the record
|
|
|
|
err = bucketLockedContainer.Put(k, v)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("update list of lockers: %w", err)
|
2022-02-16 15:42:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-03-21 14:11:04 +00:00
|
|
|
}
|
2022-02-16 15:42:25 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 14:11:04 +00:00
|
|
|
return nil
|
|
|
|
})
|
2022-02-16 15:42:25 +00:00
|
|
|
}
|
2022-11-12 11:46:44 +00:00
|
|
|
|
|
|
|
// IsLockedPrm groups the parameters of IsLocked operation.
|
|
|
|
type IsLockedPrm struct {
|
|
|
|
addr oid.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress sets object address that will be checked for lock relations.
|
|
|
|
func (i *IsLockedPrm) SetAddress(addr oid.Address) {
|
|
|
|
i.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLockedRes groups the resulting values of IsLocked operation.
|
|
|
|
type IsLockedRes struct {
|
|
|
|
locked bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Locked describes the requested object status according to the metabase
|
|
|
|
// current state.
|
|
|
|
func (i IsLockedRes) Locked() bool {
|
|
|
|
return i.locked
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLocked checks is the provided object is locked by any `LOCK`. Not found
|
|
|
|
// object is considered as non-locked.
|
|
|
|
//
|
|
|
|
// Returns only non-logical errors related to underlying database.
|
|
|
|
func (db *DB) IsLocked(prm IsLockedPrm) (res IsLockedRes, err error) {
|
2022-11-15 12:46:32 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return res, ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2022-11-12 11:46:44 +00:00
|
|
|
return res, db.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
res.locked = objectLocked(tx, prm.addr.Container(), prm.addr.Object())
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|