2022-02-15 09:53:35 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2022-02-15 11:35:13 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
2022-02-15 22:17:40 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2022-02-15 09:53:35 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-02-15 11:35:13 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
|
|
|
"go.etcd.io/bbolt"
|
2022-02-15 09:53:35 +00:00
|
|
|
)
|
|
|
|
|
2022-02-15 11:35:13 +00:00
|
|
|
// bucket name for locked objects.
|
|
|
|
var bucketNameLocked = []byte(invalidBase58String + "Locked")
|
|
|
|
|
|
|
|
// suffix for container buckets with objects of type LOCK.
|
|
|
|
const bucketNameSuffixLockers = invalidBase58String + "LOCKER"
|
|
|
|
|
|
|
|
// returns name of the bucket with objects of type LOCK for specified container.
|
|
|
|
func bucketNameLockers(idCnr cid.ID) []byte {
|
2022-05-31 17:00:41 +00:00
|
|
|
return []byte(idCnr.EncodeToString() + bucketNameSuffixLockers)
|
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-02-15 11:35:13 +00:00
|
|
|
if len(locked) == 0 {
|
|
|
|
panic("empty locked list")
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.boltDB.Update(func(tx *bbolt.Tx) error {
|
|
|
|
// check if all objects are regular
|
|
|
|
bucketKeysLocked := make([][]byte, len(locked))
|
|
|
|
|
|
|
|
for i := range locked {
|
2022-05-31 17:00:41 +00:00
|
|
|
bucketKeysLocked[i] = objectKey(locked[i])
|
2022-02-15 11:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if firstIrregularObjectType(tx, cnr, bucketKeysLocked...) != object.TypeRegular {
|
2022-02-16 15:49:19 +00:00
|
|
|
return apistatus.LockNonRegularObject{}
|
2022-02-15 11:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bucketLocked, err := tx.CreateBucketIfNotExists(bucketNameLocked)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create global bucket for locked objects: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
bucketLockedContainer, err := bucketLocked.CreateBucketIfNotExists([]byte(cnr.EncodeToString()))
|
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-05-31 17:00:41 +00:00
|
|
|
keyLocker := objectKey(locker)
|
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.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (db *DB) FreeLockedBy(lockers []oid.Address) error {
|
2022-03-10 17:58:58 +00:00
|
|
|
return db.boltDB.Update(func(tx *bbolt.Tx) error {
|
|
|
|
var err error
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
for i := range lockers {
|
|
|
|
err = freePotentialLocks(tx, lockers[i].Container(), lockers[i].Object())
|
2022-03-10 17:58:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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-05-31 17:00:41 +00:00
|
|
|
bucketLockedContainer := bucketLocked.Bucket([]byte(idCnr.EncodeToString()))
|
2022-02-15 18:22:06 +00:00
|
|
|
if bucketLockedContainer != nil {
|
2022-05-31 17:00:41 +00:00
|
|
|
return bucketLockedContainer.Get(objectKey(idObj)) != 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.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
func freePotentialLocks(tx *bbolt.Tx, idCnr cid.ID, locker oid.ID) error {
|
|
|
|
bucketLocked := tx.Bucket(bucketNameLocked)
|
|
|
|
if bucketLocked != nil {
|
2022-05-31 17:00:41 +00:00
|
|
|
bucketLockedContainer := bucketLocked.Bucket([]byte(idCnr.EncodeToString()))
|
2022-02-16 15:42:25 +00:00
|
|
|
if bucketLockedContainer != nil {
|
2022-05-31 17:00:41 +00:00
|
|
|
keyLocker := objectKey(locker)
|
2022-02-16 15:42:25 +00:00
|
|
|
return bucketLockedContainer.ForEach(func(k, v []byte) error {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// exclude locker
|
|
|
|
keyLockers = append(keyLockers[:i], keyLockers[i+1:]...)
|
|
|
|
|
2022-04-05 12:06:14 +00:00
|
|
|
v, err = encodeList(keyLockers)
|
2022-02-16 15:42:25 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|