2021-10-27 14:51:58 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2023-06-06 09:27:19 +00:00
|
|
|
"context"
|
2023-04-16 14:03:42 +00:00
|
|
|
"math/rand"
|
2021-11-10 11:07:57 +00:00
|
|
|
"sort"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
objectcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard"
|
2024-11-11 09:46:55 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2021-10-27 14:51:58 +00:00
|
|
|
)
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ErrEndOfListing is returned from an object listing with cursor
|
|
|
|
// when the storage can't return any more objects after the provided
|
2021-11-17 11:31:31 +00:00
|
|
|
// cursor. Use nil cursor object to start listing again.
|
|
|
|
var ErrEndOfListing = shard.ErrEndOfListing
|
|
|
|
|
2023-04-16 14:03:42 +00:00
|
|
|
// Cursor is a type for continuous object listing. Cursor contains shard IDs to read
|
|
|
|
// and shard cursors that contain state from previous read.
|
2021-11-11 14:27:11 +00:00
|
|
|
type Cursor struct {
|
2023-04-16 14:03:42 +00:00
|
|
|
current string
|
|
|
|
shardIDs map[string]bool
|
|
|
|
shardIDToCursor map[string]*shard.Cursor
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cursor) getCurrentShardCursor() *shard.Cursor {
|
|
|
|
return c.shardIDToCursor[c.current]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cursor) setCurrentShardCursor(sc *shard.Cursor) {
|
|
|
|
c.shardIDToCursor[c.current] = sc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cursor) nextShard() bool {
|
|
|
|
var shardsToRead []string
|
|
|
|
for shardID, read := range c.shardIDs {
|
|
|
|
if !read {
|
|
|
|
shardsToRead = append(shardsToRead, shardID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(shardsToRead) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
c.current = shardsToRead[rand.Intn(len(shardsToRead))]
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cursor) setShardRead(shardID string) {
|
|
|
|
c.shardIDs[shardID] = true
|
2021-11-11 14:27:11 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 14:51:58 +00:00
|
|
|
// ListWithCursorPrm contains parameters for ListWithCursor operation.
|
|
|
|
type ListWithCursorPrm struct {
|
|
|
|
count uint32
|
2021-11-11 14:27:11 +00:00
|
|
|
cursor *Cursor
|
2021-10-27 14:51:58 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// WithCount sets the maximum amount of addresses that ListWithCursor should return.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (p *ListWithCursorPrm) WithCount(count uint32) {
|
2021-10-27 14:51:58 +00:00
|
|
|
p.count = count
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// WithCursor sets a cursor for ListWithCursor operation. For initial request
|
|
|
|
// ignore this param or use nil value. For consecutive requests, use value
|
2021-10-27 14:51:58 +00:00
|
|
|
// from ListWithCursorRes.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (p *ListWithCursorPrm) WithCursor(cursor *Cursor) {
|
2021-10-27 14:51:58 +00:00
|
|
|
p.cursor = cursor
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListWithCursorRes contains values returned from ListWithCursor operation.
|
|
|
|
type ListWithCursorRes 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 14:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddressList returns addresses selected by ListWithCursor operation.
|
2024-05-13 13:50:21 +00:00
|
|
|
func (l ListWithCursorRes) AddressList() []objectcore.Info {
|
2021-10-27 14:51:58 +00:00
|
|
|
return l.addrList
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cursor returns cursor for consecutive listing requests.
|
2021-11-11 14:27:11 +00:00
|
|
|
func (l ListWithCursorRes) Cursor() *Cursor {
|
2021-10-27 14:51:58 +00:00
|
|
|
return l.cursor
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ListWithCursor lists physical objects available in the engine starting
|
|
|
|
// from the cursor. It includes regular, tombstone and storage group objects.
|
|
|
|
// Does not include inhumed objects. Use cursor value from the response
|
2021-10-27 14:51:58 +00:00
|
|
|
// for consecutive requests.
|
2021-11-12 09:52:19 +00:00
|
|
|
//
|
2023-04-16 14:03:42 +00:00
|
|
|
// If count param is big enough, then the method reads objects from different shards
|
|
|
|
// by portions. In this case shards are chosen randomly, if they're not read out yet.
|
|
|
|
//
|
|
|
|
// Adding a shard between ListWithCursor does not invalidate the cursor but new shard
|
|
|
|
// won't be listed.
|
|
|
|
// Removing a shard between ListWithCursor leads to the undefined behavior
|
|
|
|
// (e.g. usage of the objects from the removed shard).
|
|
|
|
//
|
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 (e *StorageEngine) ListWithCursor(ctx context.Context, prm ListWithCursorPrm) (ListWithCursorRes, error) {
|
2024-11-11 09:46:55 +00:00
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "StorageEngine.ListWithCursor")
|
|
|
|
defer span.End()
|
|
|
|
defer elapsed("ListWithCursor", e.metrics.AddMethodDuration)()
|
|
|
|
|
2024-05-13 13:50:21 +00:00
|
|
|
result := make([]objectcore.Info, 0, prm.count)
|
2021-11-10 11:07:57 +00:00
|
|
|
|
2023-04-16 14:03:42 +00:00
|
|
|
// Set initial cursors
|
|
|
|
cursor := prm.cursor
|
|
|
|
if cursor == nil {
|
|
|
|
shardIDs := getSortedShardIDs(e)
|
|
|
|
if len(shardIDs) == 0 {
|
|
|
|
return ListWithCursorRes{}, ErrEndOfListing
|
|
|
|
}
|
|
|
|
cursor = newCursor(shardIDs)
|
2021-10-27 14:51:58 +00:00
|
|
|
}
|
|
|
|
|
2023-04-16 14:03:42 +00:00
|
|
|
const (
|
|
|
|
splitShardCountLimit = 100
|
|
|
|
shardsNum = 4
|
|
|
|
)
|
2021-11-10 11:07:57 +00:00
|
|
|
|
2023-04-16 14:03:42 +00:00
|
|
|
batchSize := prm.count
|
|
|
|
if batchSize >= splitShardCountLimit {
|
|
|
|
batchSize /= shardsNum
|
2021-11-10 11:07:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-16 14:03:42 +00:00
|
|
|
for cursor.nextShard() {
|
2021-11-10 11:07:57 +00:00
|
|
|
if len(result) >= int(prm.count) {
|
|
|
|
break
|
|
|
|
}
|
2023-04-16 14:03:42 +00:00
|
|
|
curr := cursor.current
|
2021-11-10 11:07:57 +00:00
|
|
|
|
|
|
|
e.mtx.RLock()
|
2023-04-16 14:03:42 +00:00
|
|
|
shardInstance, ok := e.shards[curr]
|
2021-11-10 11:07:57 +00:00
|
|
|
e.mtx.RUnlock()
|
|
|
|
if !ok {
|
2023-04-16 14:03:42 +00:00
|
|
|
cursor.setShardRead(curr)
|
2021-11-10 11:07:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-10 11:16:18 +00:00
|
|
|
count := min(prm.count-uint32(len(result)), batchSize)
|
2023-04-16 14:03:42 +00:00
|
|
|
|
2022-05-20 18:08:59 +00:00
|
|
|
var shardPrm shard.ListWithCursorPrm
|
|
|
|
shardPrm.WithCount(count)
|
2023-04-16 14:03:42 +00:00
|
|
|
shardPrm.WithCursor(cursor.getCurrentShardCursor())
|
2021-11-10 11:07:57 +00:00
|
|
|
|
2023-06-06 09:27:19 +00:00
|
|
|
res, err := shardInstance.ListWithCursor(ctx, shardPrm)
|
2021-11-10 11:07:57 +00:00
|
|
|
if err != nil {
|
2023-04-16 14:03:42 +00:00
|
|
|
cursor.setShardRead(curr)
|
2021-11-10 11:07:57 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
result = append(result, res.AddressList()...)
|
2023-04-16 14:03:42 +00:00
|
|
|
cursor.setCurrentShardCursor(res.Cursor())
|
2021-11-10 11:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(result) == 0 {
|
2022-05-31 11:56:59 +00:00
|
|
|
return ListWithCursorRes{}, ErrEndOfListing
|
2021-10-27 14:51:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 11:56:59 +00:00
|
|
|
return ListWithCursorRes{
|
2021-11-10 11:07:57 +00:00
|
|
|
addrList: result,
|
2021-11-11 14:27:11 +00:00
|
|
|
cursor: cursor,
|
2021-10-27 14:51:58 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2023-04-16 14:03:42 +00:00
|
|
|
|
|
|
|
func getSortedShardIDs(e *StorageEngine) []string {
|
|
|
|
e.mtx.RLock()
|
|
|
|
shardIDs := make([]string, 0, len(e.shards))
|
|
|
|
for id := range e.shards {
|
|
|
|
shardIDs = append(shardIDs, id)
|
|
|
|
}
|
|
|
|
e.mtx.RUnlock()
|
|
|
|
sort.Strings(shardIDs)
|
|
|
|
return shardIDs
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCursor(shardIDs []string) *Cursor {
|
|
|
|
shardIDsMap := make(map[string]bool)
|
|
|
|
shardIDToCursor := make(map[string]*shard.Cursor)
|
|
|
|
for _, shardID := range shardIDs {
|
|
|
|
shardIDsMap[shardID] = false
|
|
|
|
}
|
|
|
|
return &Cursor{shardIDs: shardIDsMap, shardIDToCursor: shardIDToCursor}
|
|
|
|
}
|