2021-10-27 13:14:51 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2023-06-06 09:27:19 +00:00
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
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"
|
2023-06-06 09:27:19 +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"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2021-10-27 13:14:51 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2023-06-06 09:27:19 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2021-10-27 13:14:51 +00:00
|
|
|
)
|
|
|
|
|
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 {
|
2024-05-13 13:50:21 +00:00
|
|
|
addrList []objectcore.Info
|
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.
|
2024-05-13 13:50:21 +00:00
|
|
|
func (l ListRes) AddressList() []objectcore.Info {
|
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.
|
2023-06-06 09:27:19 +00:00
|
|
|
func (db *DB) ListWithCursor(ctx context.Context, prm ListPrm) (res ListRes, err error) {
|
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
success = false
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
db.metrics.AddMethodDuration("ListWithCursor", time.Since(startedAt), success)
|
|
|
|
}()
|
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "metabase.ListWithCursor",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.Int("count", prm.count),
|
|
|
|
attribute.Bool("has_cursor", prm.cursor != nil),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-13 13:50:21 +00:00
|
|
|
result := make([]objectcore.Info, 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-06 09:27:19 +00:00
|
|
|
success = err == nil
|
2023-06-15 10:19:36 +00:00
|
|
|
return res, metaerr.Wrap(err)
|
2021-10-27 13:14:51 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 13:50:21 +00:00
|
|
|
func (db *DB) listWithCursor(tx *bbolt.Tx, result []objectcore.Info, count int, cursor *Cursor) ([]objectcore.Info, *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
|
2023-12-11 13:50:44 +00:00
|
|
|
var err error
|
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)
|
|
|
|
|
2023-10-31 11:56:55 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
var objType objectSDK.Type
|
2022-11-12 13:48:44 +00:00
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
switch prefix {
|
2022-11-12 13:48:44 +00:00
|
|
|
case primaryPrefix:
|
2023-07-06 12:36:41 +00:00
|
|
|
objType = objectSDK.TypeRegular
|
2022-11-12 13:48:44 +00:00
|
|
|
case lockersPrefix:
|
2023-07-06 12:36:41 +00:00
|
|
|
objType = objectSDK.TypeLock
|
2022-11-12 13:48:44 +00:00
|
|
|
case tombstonePrefix:
|
2023-07-06 12:36:41 +00:00
|
|
|
objType = objectSDK.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)
|
2023-12-11 13:50:44 +00:00
|
|
|
result, offset, cursor, err = selectNFromBucket(bkt, objType, graveyardBkt, garbageBkt, rawAddr, containerID,
|
2022-06-14 16:34:23 +00:00
|
|
|
result, count, cursor, threshold)
|
2023-12-11 13:50:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2022-06-14 16:34:23 +00:00
|
|
|
}
|
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
|
2023-07-06 12:36:41 +00:00
|
|
|
objType objectSDK.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
|
2024-05-13 13:50:21 +00:00
|
|
|
to []objectcore.Info, // 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
|
2024-05-13 13:50:21 +00:00
|
|
|
) ([]objectcore.Info, []byte, *Cursor, error) {
|
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()
|
2023-12-11 13:50:44 +00:00
|
|
|
k, v := 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)
|
2023-12-11 13:50:44 +00:00
|
|
|
k, v = c.Next() // we are looking for objects _after_ the cursor
|
2021-11-10 10:06:43 +00:00
|
|
|
}
|
2021-10-27 13:14:51 +00:00
|
|
|
|
2023-12-11 13:50:44 +00:00
|
|
|
for ; k != nil; k, v = c.Next() {
|
2021-11-10 10:06:43 +00:00
|
|
|
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
|
|
|
|
2023-12-11 13:50:44 +00:00
|
|
|
var isLinkingObj bool
|
2024-05-13 13:50:21 +00:00
|
|
|
var ecInfo *objectcore.ECInfo
|
2023-12-11 13:50:44 +00:00
|
|
|
if objType == objectSDK.TypeRegular {
|
|
|
|
var o objectSDK.Object
|
|
|
|
if err := o.Unmarshal(v); err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
isLinkingObj = isLinkObject(&o)
|
2024-05-13 13:50:21 +00:00
|
|
|
ecHeader := o.ECHeader()
|
|
|
|
if ecHeader != nil {
|
|
|
|
ecInfo = &objectcore.ECInfo{
|
|
|
|
ParentID: ecHeader.Parent(),
|
|
|
|
Index: ecHeader.Index(),
|
|
|
|
Total: ecHeader.Total(),
|
|
|
|
}
|
|
|
|
}
|
2023-12-11 13:50:44 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 15:53:09 +00:00
|
|
|
var a oid.Address
|
|
|
|
a.SetContainer(cnt)
|
|
|
|
a.SetObject(obj)
|
2024-05-13 13:50:21 +00:00
|
|
|
to = append(to, objectcore.Info{Address: a, Type: objType, IsLinkingObject: isLinkingObj, ECInfo: ecInfo})
|
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
|
|
|
|
2023-12-11 13:50:44 +00:00
|
|
|
return to, offset, cursor, nil
|
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
|
|
|
}
|