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"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
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 {
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr cid.ID
|
2020-11-17 12:26:03 +00:00
|
|
|
filters object.SearchFilters
|
|
|
|
}
|
|
|
|
|
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.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (p *SelectPrm) WithContainerID(cnr cid.ID) {
|
2022-09-14 14:36:37 +00:00
|
|
|
p.cnr = cnr
|
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.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (p *SelectPrm) WithFilters(fs object.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()
|
|
|
|
|
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) {
|
2021-03-16 08:14:56 +00:00
|
|
|
if e.metrics != nil {
|
2023-06-13 16:48:15 +00:00
|
|
|
defer elapsed("Search", e.metrics.AddMethodDuration)()
|
2021-03-15 13:09:27 +00:00
|
|
|
}
|
|
|
|
|
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
|
2022-07-13 12:43:04 +00:00
|
|
|
shPrm.SetContainerID(prm.cnr)
|
|
|
|
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 {
|
2022-05-31 17:00:41 +00:00
|
|
|
e.reportShardError(sh, "could not select objects from shard", err)
|
|
|
|
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) {
|
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) {
|
2021-03-16 08:14:56 +00:00
|
|
|
if e.metrics != nil {
|
2023-06-13 16:48:15 +00:00
|
|
|
defer elapsed("ListObjects", e.metrics.AddMethodDuration)()
|
2021-03-15 13:09:27 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-10-26 12:23:12 +00:00
|
|
|
e.reportShardError(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.
|
2023-04-12 14:01:29 +00:00
|
|
|
func Select(ctx context.Context, storage *StorageEngine, cnr cid.ID, fs object.SearchFilters) ([]oid.Address, error) {
|
2022-05-23 13:12:32 +00:00
|
|
|
var selectPrm SelectPrm
|
|
|
|
selectPrm.WithContainerID(cnr)
|
|
|
|
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
|
|
|
}
|