2020-11-17 12:26:03 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard"
|
2023-05-31 09:24:04 +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"
|
2023-04-12 14:01:29 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-11-17 12:26:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SelectPrm groups the parameters of Select operation.
|
|
|
|
type SelectPrm struct {
|
2024-10-02 11:52:54 +00:00
|
|
|
cnr cid.ID
|
|
|
|
filters objectSDK.SearchFilters
|
|
|
|
indexedContainer bool
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SelectRes groups the resulting values of Select operation.
|
2020-11-17 12:26:03 +00:00
|
|
|
type SelectRes struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addrList []oid.Address
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 13:24:27 +00:00
|
|
|
// WithContainerID is a Select option to set the container id to search in.
|
2024-10-02 11:52:54 +00:00
|
|
|
func (p *SelectPrm) WithContainerID(cnr cid.ID, indexedContainer bool) {
|
2022-09-14 14:36:37 +00:00
|
|
|
p.cnr = cnr
|
2024-10-02 11:52:54 +00:00
|
|
|
p.indexedContainer = indexedContainer
|
2020-12-10 13:24:27 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:26:03 +00:00
|
|
|
// WithFilters is a Select option to set the object filters.
|
2023-07-06 12:36:41 +00:00
|
|
|
func (p *SelectPrm) WithFilters(fs objectSDK.SearchFilters) {
|
2022-09-14 14:36:37 +00:00
|
|
|
p.filters = fs
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddressList returns list of addresses of the selected objects.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (r SelectRes) AddressList() []oid.Address {
|
2020-11-17 12:26:03 +00:00
|
|
|
return r.addrList
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select selects the objects from local storage that match select parameters.
|
|
|
|
//
|
|
|
|
// Returns any error encountered that did not allow to completely select the objects.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
|
|
|
// Returns an error if executions are blocked (see BlockExecution).
|
2023-04-12 14:01:29 +00:00
|
|
|
func (e *StorageEngine) Select(ctx context.Context, prm SelectPrm) (res SelectRes, err error) {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "StorageEngine.Select",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("container_id", prm.cnr.EncodeToString()),
|
|
|
|
))
|
|
|
|
defer span.End()
|
2024-11-11 09:46:55 +00:00
|
|
|
defer elapsed("Select", e.metrics.AddMethodDuration)()
|
2023-04-12 14:01:29 +00:00
|
|
|
|
2021-11-10 15:00:30 +00:00
|
|
|
err = e.execIfNotBlocked(func() error {
|
2023-04-12 14:01:29 +00:00
|
|
|
res, err = e._select(ctx, prm)
|
2021-11-09 15:46:12 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
func (e *StorageEngine) _select(ctx context.Context, prm SelectPrm) (SelectRes, error) {
|
2022-05-31 17:00:41 +00:00
|
|
|
addrList := make([]oid.Address, 0)
|
2020-12-01 10:54:29 +00:00
|
|
|
uniqueMap := make(map[string]struct{})
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2020-12-10 14:12:26 +00:00
|
|
|
var outError error
|
|
|
|
|
2022-05-20 18:08:59 +00:00
|
|
|
var shPrm shard.SelectPrm
|
2024-10-02 11:52:54 +00:00
|
|
|
shPrm.SetContainerID(prm.cnr, prm.indexedContainer)
|
2022-07-13 12:43:04 +00:00
|
|
|
shPrm.SetFilters(prm.filters)
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2022-01-31 14:58:32 +00:00
|
|
|
e.iterateOverUnsortedShards(func(sh hashedShard) (stop bool) {
|
2023-04-12 14:01:29 +00:00
|
|
|
res, err := sh.Select(ctx, shPrm)
|
2020-11-17 12:26:03 +00:00
|
|
|
if err != nil {
|
2024-10-21 13:27:28 +00:00
|
|
|
e.reportShardError(ctx, sh, "could not select objects from shard", err)
|
2022-05-31 17:00:41 +00:00
|
|
|
return false
|
2022-06-08 08:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range res.AddressList() { // save only unique values
|
|
|
|
if _, ok := uniqueMap[addr.EncodeToString()]; !ok {
|
|
|
|
uniqueMap[addr.EncodeToString()] = struct{}{}
|
|
|
|
addrList = append(addrList, addr)
|
2020-12-01 10:54:29 +00:00
|
|
|
}
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2022-05-31 11:56:59 +00:00
|
|
|
return SelectRes{
|
2020-11-17 12:26:03 +00:00
|
|
|
addrList: addrList,
|
2020-12-10 14:12:26 +00:00
|
|
|
}, outError
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
2020-11-19 08:15:49 +00:00
|
|
|
|
2020-12-03 12:01:45 +00:00
|
|
|
// List returns `limit` available physically storage object addresses in engine.
|
|
|
|
// If limit is zero, then returns all available object addresses.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
|
|
|
// Returns an error if executions are blocked (see BlockExecution).
|
2023-06-06 09:27:19 +00:00
|
|
|
func (e *StorageEngine) List(ctx context.Context, limit uint64) (res SelectRes, err error) {
|
2024-11-11 09:46:55 +00:00
|
|
|
defer elapsed("List", e.metrics.AddMethodDuration)()
|
2021-11-10 15:00:30 +00:00
|
|
|
err = e.execIfNotBlocked(func() error {
|
2023-06-06 09:27:19 +00:00
|
|
|
res, err = e.list(ctx, limit)
|
2021-11-09 15:46:12 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-06 09:27:19 +00:00
|
|
|
func (e *StorageEngine) list(ctx context.Context, limit uint64) (SelectRes, error) {
|
2022-05-31 17:00:41 +00:00
|
|
|
addrList := make([]oid.Address, 0, limit)
|
2020-12-03 12:01:45 +00:00
|
|
|
uniqueMap := make(map[string]struct{})
|
|
|
|
ln := uint64(0)
|
|
|
|
|
|
|
|
// consider iterating over shuffled shards
|
2022-01-31 14:58:32 +00:00
|
|
|
e.iterateOverUnsortedShards(func(sh hashedShard) (stop bool) {
|
2023-06-06 09:27:19 +00:00
|
|
|
res, err := sh.List(ctx) // consider limit result of shard iterator
|
2020-12-03 12:01:45 +00:00
|
|
|
if err != nil {
|
2024-10-21 13:27:28 +00:00
|
|
|
e.reportShardError(ctx, sh, "could not select objects from shard", err)
|
2020-12-03 12:01:45 +00:00
|
|
|
} else {
|
|
|
|
for _, addr := range res.AddressList() { // save only unique values
|
2022-05-31 17:00:41 +00:00
|
|
|
if _, ok := uniqueMap[addr.EncodeToString()]; !ok {
|
|
|
|
uniqueMap[addr.EncodeToString()] = struct{}{}
|
2020-12-03 12:01:45 +00:00
|
|
|
addrList = append(addrList, addr)
|
|
|
|
|
|
|
|
ln++
|
|
|
|
if limit > 0 && ln >= limit {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2022-05-31 11:56:59 +00:00
|
|
|
return SelectRes{
|
2020-12-03 12:01:45 +00:00
|
|
|
addrList: addrList,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 08:15:49 +00:00
|
|
|
// Select selects objects from local storage using provided filters.
|
2024-10-02 11:52:54 +00:00
|
|
|
func Select(ctx context.Context, storage *StorageEngine, cnr cid.ID, isIndexedContainer bool, fs objectSDK.SearchFilters) ([]oid.Address, error) {
|
2022-05-23 13:12:32 +00:00
|
|
|
var selectPrm SelectPrm
|
2024-10-02 11:52:54 +00:00
|
|
|
selectPrm.WithContainerID(cnr, isIndexedContainer)
|
2022-05-23 13:12:32 +00:00
|
|
|
selectPrm.WithFilters(fs)
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
res, err := storage.Select(ctx, selectPrm)
|
2020-11-19 08:15:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.AddressList(), nil
|
|
|
|
}
|
|
|
|
|
2020-12-03 12:01:45 +00:00
|
|
|
// List returns `limit` available physically storage object addresses in
|
|
|
|
// engine. If limit is zero, then returns all available object addresses.
|
2023-06-06 09:27:19 +00:00
|
|
|
func List(ctx context.Context, storage *StorageEngine, limit uint64) ([]oid.Address, error) {
|
|
|
|
res, err := storage.List(ctx, limit)
|
2020-12-03 12:01:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.AddressList(), nil
|
2020-11-19 08:15:49 +00:00
|
|
|
}
|