2021-10-27 13:14:51 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2023-03-07 13:38:26 +00:00
|
|
|
objectcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
2023-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
|
|
|
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"
|
2021-10-27 13:14:51 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2021-11-17 11:31:31 +00:00
|
|
|
// ErrEndOfListing is returned from object listing with cursor
|
|
|
|
// when storage can't return any more objects after provided
|
|
|
|
// cursor. Use nil cursor object to start listing again.
|
2022-10-31 07:02:30 +00:00
|
|
|
var ErrEndOfListing = logicerr.New("end of object listing")
|
2021-11-17 11:31:31 +00:00
|
|
|
|
2021-11-11 14:27:11 +00:00
|
|
|
// Cursor is a type for continuous object listing.
|
|
|
|
type Cursor struct {
|
2021-11-17 11:01:41 +00:00
|
|
|
bucketName []byte
|
|
|
|
inBucketOffset []byte
|
2021-11-11 14:27:11 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 13:14:51 +00:00
|
|
|
// ListPrm contains parameters for ListWithCursor operation.
|
|
|
|
type ListPrm struct {
|
|
|
|
count int
|
2021-11-11 14:27:11 +00:00
|
|
|
cursor *Cursor
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetCount sets maximum amount of addresses that ListWithCursor should return.
|
|
|
|
func (l *ListPrm) SetCount(count uint32) {
|
2021-10-27 13:14:51 +00:00
|
|
|
l.count = int(count)
|
|
|
|
}
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetCursor sets cursor for ListWithCursor operation. For initial request
|
2021-11-12 09:52:19 +00:00
|
|
|
// ignore this param or use nil value. For consecutive requests, use value
|
2021-10-27 13:14:51 +00:00
|
|
|
// from ListRes.
|
2022-07-12 14:59:37 +00:00
|
|
|
func (l *ListPrm) SetCursor(cursor *Cursor) {
|
2021-10-27 13:14:51 +00:00
|
|
|
l.cursor = cursor
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListRes contains values returned from ListWithCursor operation.
|
|
|
|
type ListRes struct {
|
2022-11-12 13:48:44 +00:00
|
|
|
addrList []objectcore.AddressWithType
|
2021-11-11 14:27:11 +00:00
|
|
|
cursor *Cursor
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddressList returns addresses selected by ListWithCursor operation.
|
2022-11-12 13:48:44 +00:00
|
|
|
func (l ListRes) AddressList() []objectcore.AddressWithType {
|
2021-10-27 13:14:51 +00:00
|
|
|
return l.addrList
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cursor returns cursor for consecutive listing requests.
|
2021-11-11 14:27:11 +00:00
|
|
|
func (l ListRes) Cursor() *Cursor {
|
2021-10-27 13:14:51 +00:00
|
|
|
return l.cursor
|
|
|
|
}
|
|
|
|
|
2021-11-12 09:52:19 +00:00
|
|
|
// ListWithCursor lists physical objects available in metabase starting from
|
2022-02-15 12:51:56 +00:00
|
|
|
// cursor. Includes objects of all types. Does not include inhumed objects.
|
|
|
|
// Use cursor value from response for consecutive requests.
|
2021-11-12 09:52:19 +00:00
|
|
|
//
|
|
|
|
// Returns ErrEndOfListing if there are no more objects to return or count
|
|
|
|
// parameter set to zero.
|
2022-05-31 11:43:08 +00:00
|
|
|
func (db *DB) ListWithCursor(prm ListPrm) (res ListRes, err 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 res, ErrDegradedMode
|
|
|
|
}
|
|
|
|
|
2022-11-12 13:48:44 +00:00
|
|
|
result := make([]objectcore.AddressWithType, 0, prm.count)
|
2022-06-14 15:53:09 +00:00
|
|
|
|
2021-10-27 13:14:51 +00:00
|
|
|
err = db.boltDB.View(func(tx *bbolt.Tx) error {
|
2022-06-14 15:53:09 +00:00
|
|
|
res.addrList, res.cursor, err = db.listWithCursor(tx, result, prm.count, prm.cursor)
|
2021-10-27 13:14:51 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
2023-06-15 10:19:36 +00:00
|
|
|
return res, metaerr.Wrap(err)
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 13:48:44 +00:00
|
|
|
func (db *DB) listWithCursor(tx *bbolt.Tx, result []objectcore.AddressWithType, count int, cursor *Cursor) ([]objectcore.AddressWithType, *Cursor, error) {
|
2021-11-11 14:27:11 +00:00
|
|
|
threshold := cursor == nil // threshold is a flag to ignore cursor
|
2021-11-17 11:01:41 +00:00
|
|
|
var bucketName []byte
|
2021-10-27 13:14:51 +00:00
|
|
|
|
2021-11-10 10:06:43 +00:00
|
|
|
c := tx.Cursor()
|
|
|
|
name, _ := c.First()
|
|
|
|
|
|
|
|
if !threshold {
|
2021-11-17 11:01:41 +00:00
|
|
|
name, _ = c.Seek(cursor.bucketName)
|
2021-11-10 10:06:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 15:53:09 +00:00
|
|
|
var containerID cid.ID
|
2022-06-14 16:34:23 +00:00
|
|
|
var offset []byte
|
|
|
|
graveyardBkt := tx.Bucket(graveyardBucketName)
|
|
|
|
garbageBkt := tx.Bucket(garbageBucketName)
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
var rawAddr = make([]byte, cidSize, addressKeySize)
|
2022-06-14 15:53:09 +00:00
|
|
|
|
2021-11-10 10:06:43 +00:00
|
|
|
loop:
|
|
|
|
for ; name != nil; name, _ = c.Next() {
|
2022-09-08 11:54:21 +00:00
|
|
|
cidRaw, prefix := parseContainerIDWithPrefix(&containerID, name)
|
|
|
|
if cidRaw == nil {
|
2021-11-10 10:06:43 +00:00
|
|
|
continue
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 13:48:44 +00:00
|
|
|
var objType object.Type
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
switch prefix {
|
2022-11-12 13:48:44 +00:00
|
|
|
case primaryPrefix:
|
|
|
|
objType = object.TypeRegular
|
|
|
|
case lockersPrefix:
|
|
|
|
objType = object.TypeLock
|
|
|
|
case tombstonePrefix:
|
|
|
|
objType = object.TypeTombstone
|
2021-11-17 11:01:41 +00:00
|
|
|
default:
|
|
|
|
continue
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 16:34:23 +00:00
|
|
|
bkt := tx.Bucket(name)
|
|
|
|
if bkt != nil {
|
2022-09-08 11:54:21 +00:00
|
|
|
copy(rawAddr, cidRaw)
|
2022-11-12 13:48:44 +00:00
|
|
|
result, offset, cursor = selectNFromBucket(bkt, objType, graveyardBkt, garbageBkt, rawAddr, containerID,
|
2022-06-14 16:34:23 +00:00
|
|
|
result, count, cursor, threshold)
|
|
|
|
}
|
2021-11-17 11:01:41 +00:00
|
|
|
bucketName = name
|
|
|
|
if len(result) >= count {
|
|
|
|
break loop
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
2021-11-17 11:01:41 +00:00
|
|
|
|
|
|
|
// set threshold flag after first `selectNFromBucket` invocation
|
|
|
|
// first invocation must look for cursor object
|
|
|
|
threshold = true
|
2021-11-10 10:06:43 +00:00
|
|
|
}
|
2021-10-27 13:14:51 +00:00
|
|
|
|
2022-06-14 16:34:23 +00:00
|
|
|
if offset != nil {
|
|
|
|
// new slice is much faster but less memory efficient
|
|
|
|
// we need to copy, because offset exists during bbolt tx
|
|
|
|
cursor.inBucketOffset = make([]byte, len(offset))
|
|
|
|
copy(cursor.inBucketOffset, offset)
|
|
|
|
}
|
|
|
|
|
2021-10-27 13:14:51 +00:00
|
|
|
if len(result) == 0 {
|
2021-11-17 11:31:31 +00:00
|
|
|
return nil, nil, ErrEndOfListing
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 11:01:41 +00:00
|
|
|
// new slice is much faster but less memory efficient
|
|
|
|
// we need to copy, because bucketName exists during bbolt tx
|
|
|
|
cursor.bucketName = make([]byte, len(bucketName))
|
|
|
|
copy(cursor.bucketName, bucketName)
|
|
|
|
|
2021-11-11 14:27:11 +00:00
|
|
|
return result, cursor, nil
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// selectNFromBucket similar to selectAllFromBucket but uses cursor to find
|
|
|
|
// object to start selecting from. Ignores inhumed objects.
|
2022-06-14 16:34:23 +00:00
|
|
|
func selectNFromBucket(bkt *bbolt.Bucket, // main bucket
|
2022-11-12 13:48:44 +00:00
|
|
|
objType object.Type, // type of the objects stored in the main bucket
|
2022-06-14 16:34:23 +00:00
|
|
|
graveyardBkt, garbageBkt *bbolt.Bucket, // cached graveyard buckets
|
2022-09-08 11:54:21 +00:00
|
|
|
cidRaw []byte, // container ID prefix, optimization
|
2022-06-14 15:53:09 +00:00
|
|
|
cnt cid.ID, // container ID
|
2022-11-12 13:48:44 +00:00
|
|
|
to []objectcore.AddressWithType, // listing result
|
2021-10-27 13:14:51 +00:00
|
|
|
limit int, // stop listing at `limit` items in result
|
2021-11-11 14:27:11 +00:00
|
|
|
cursor *Cursor, // start from cursor object
|
2021-10-27 13:14:51 +00:00
|
|
|
threshold bool, // ignore cursor and start immediately
|
2022-11-12 13:48:44 +00:00
|
|
|
) ([]objectcore.AddressWithType, []byte, *Cursor) {
|
2021-11-11 14:27:11 +00:00
|
|
|
if cursor == nil {
|
|
|
|
cursor = new(Cursor)
|
|
|
|
}
|
2021-10-27 13:14:51 +00:00
|
|
|
|
2021-11-11 14:27:11 +00:00
|
|
|
count := len(to)
|
2021-11-10 10:06:43 +00:00
|
|
|
c := bkt.Cursor()
|
|
|
|
k, _ := c.First()
|
2021-10-27 13:14:51 +00:00
|
|
|
|
2021-11-17 11:01:41 +00:00
|
|
|
offset := cursor.inBucketOffset
|
|
|
|
|
2021-11-10 10:06:43 +00:00
|
|
|
if !threshold {
|
2021-11-17 11:01:41 +00:00
|
|
|
c.Seek(offset)
|
2021-11-10 10:06:43 +00:00
|
|
|
k, _ = c.Next() // we are looking for objects _after_ the cursor
|
|
|
|
}
|
2021-10-27 13:14:51 +00:00
|
|
|
|
2021-11-10 10:06:43 +00:00
|
|
|
for ; k != nil; k, _ = c.Next() {
|
|
|
|
if count >= limit {
|
|
|
|
break
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
2022-06-14 15:53:09 +00:00
|
|
|
|
|
|
|
var obj oid.ID
|
2022-09-08 11:54:21 +00:00
|
|
|
if err := obj.Decode(k); err != nil {
|
2021-11-10 10:06:43 +00:00
|
|
|
break
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
2022-06-14 15:53:09 +00:00
|
|
|
|
2021-11-17 11:01:41 +00:00
|
|
|
offset = k
|
2022-09-08 11:54:21 +00:00
|
|
|
if inGraveyardWithKey(append(cidRaw, k...), graveyardBkt, garbageBkt) > 0 {
|
2021-11-10 10:06:43 +00:00
|
|
|
continue
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
2022-06-14 15:53:09 +00:00
|
|
|
|
|
|
|
var a oid.Address
|
|
|
|
a.SetContainer(cnt)
|
|
|
|
a.SetObject(obj)
|
2022-11-12 13:48:44 +00:00
|
|
|
to = append(to, objectcore.AddressWithType{Address: a, Type: objType})
|
2021-10-27 13:14:51 +00:00
|
|
|
count++
|
2021-11-10 10:06:43 +00:00
|
|
|
}
|
2021-10-27 13:14:51 +00:00
|
|
|
|
2022-06-14 16:34:23 +00:00
|
|
|
return to, offset, cursor
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
2021-11-17 11:01:41 +00:00
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
func parseContainerIDWithPrefix(containerID *cid.ID, name []byte) ([]byte, byte) {
|
|
|
|
if len(name) < bucketKeySize {
|
|
|
|
return nil, 0
|
2021-11-17 11:01:41 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
rawID := name[1:bucketKeySize]
|
|
|
|
|
|
|
|
if err := containerID.Decode(rawID); err != nil {
|
|
|
|
return nil, 0
|
2021-11-17 11:01:41 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
return rawID, name[0]
|
2021-11-17 11:01:41 +00:00
|
|
|
}
|