2020-11-17 12:26:03 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-08-04 11:14:07 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
2023-03-07 13:38:26 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2020-11-17 12:26:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HeadPrm groups the parameters of Head operation.
|
|
|
|
type HeadPrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2020-12-08 11:58:38 +00:00
|
|
|
raw bool
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// HeadRes groups the resulting values of Head operation.
|
2020-11-17 12:26:03 +00:00
|
|
|
type HeadRes struct {
|
2022-03-03 14:19:05 +00:00
|
|
|
head *objectSDK.Object
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithAddress is a Head option to set the address of the requested object.
|
|
|
|
//
|
|
|
|
// Option is required.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (p *HeadPrm) WithAddress(addr oid.Address) {
|
2022-09-14 14:36:37 +00:00
|
|
|
p.addr = addr
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 11:58:38 +00:00
|
|
|
// WithRaw is a Head option to set raw flag value. If flag is unset, then Head
|
2022-04-21 11:28:05 +00:00
|
|
|
// returns the header of the virtual object, otherwise it returns SplitInfo of the virtual
|
2020-12-08 11:58:38 +00:00
|
|
|
// object.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (p *HeadPrm) WithRaw(raw bool) {
|
2022-09-14 14:36:37 +00:00
|
|
|
p.raw = raw
|
2020-12-08 11:58:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:26:03 +00:00
|
|
|
// Header returns the requested object header.
|
|
|
|
//
|
|
|
|
// Instance has empty payload.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (r HeadRes) Header() *objectSDK.Object {
|
2020-11-17 12:26:03 +00:00
|
|
|
return r.head
|
|
|
|
}
|
|
|
|
|
|
|
|
// Head reads object header from local storage.
|
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely read the object header.
|
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns an error of type apistatus.ObjectNotFound if the requested object is missing in local storage.
|
|
|
|
// Returns an error of type apistatus.ObjectAlreadyRemoved if the requested object was inhumed.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
|
|
|
// Returns an error if executions are blocked (see BlockExecution).
|
2023-03-13 11:37:35 +00:00
|
|
|
func (e *StorageEngine) Head(ctx context.Context, prm HeadPrm) (res HeadRes, err error) {
|
2021-11-10 15:00:30 +00:00
|
|
|
err = e.execIfNotBlocked(func() error {
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err = e.head(ctx, prm)
|
2021-11-09 15:46:12 +00:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
func (e *StorageEngine) head(ctx context.Context, prm HeadPrm) (HeadRes, error) {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "StorageEngine.head")
|
|
|
|
defer span.End()
|
2021-03-16 08:14:56 +00:00
|
|
|
if e.metrics != nil {
|
2023-06-13 16:48:15 +00:00
|
|
|
defer elapsed("Head", e.metrics.AddMethodDuration)()
|
2021-03-15 13:09:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 10:48:40 +00:00
|
|
|
var (
|
2024-04-23 12:21:29 +00:00
|
|
|
head *objectSDK.Object
|
|
|
|
siErr *objectSDK.SplitInfoError
|
|
|
|
outSI *objectSDK.SplitInfo
|
2023-08-04 11:14:07 +00:00
|
|
|
outError error = new(apistatus.ObjectNotFound)
|
2024-04-23 12:21:29 +00:00
|
|
|
shPrm shard.HeadPrm
|
2020-12-01 10:48:40 +00:00
|
|
|
)
|
2022-07-13 12:43:04 +00:00
|
|
|
shPrm.SetAddress(prm.addr)
|
|
|
|
shPrm.SetRaw(prm.raw)
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2022-01-31 14:58:32 +00:00
|
|
|
e.iterateOverSortedShards(prm.addr, func(_ int, sh hashedShard) (stop bool) {
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := sh.Head(ctx, shPrm)
|
2020-11-17 12:26:03 +00:00
|
|
|
if err != nil {
|
2020-12-01 10:48:40 +00:00
|
|
|
switch {
|
2023-08-04 11:14:07 +00:00
|
|
|
case client.IsErrObjectNotFound(err):
|
2020-12-01 10:48:40 +00:00
|
|
|
return false // ignore, go to next shard
|
2021-04-14 08:58:14 +00:00
|
|
|
case errors.As(err, &siErr):
|
|
|
|
if outSI == nil {
|
|
|
|
outSI = objectSDK.NewSplitInfo()
|
|
|
|
}
|
2021-04-15 07:23:52 +00:00
|
|
|
util.MergeSplitInfo(siErr.SplitInfo(), outSI)
|
2022-05-12 16:37:46 +00:00
|
|
|
_, withLink := outSI.Link()
|
|
|
|
_, withLast := outSI.LastPart()
|
2021-04-14 08:58:14 +00:00
|
|
|
// stop iterating over shards if SplitInfo structure is complete
|
2022-05-12 16:37:46 +00:00
|
|
|
if withLink && withLast {
|
2021-04-14 08:58:14 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2023-08-04 11:14:07 +00:00
|
|
|
case client.IsErrObjectAlreadyRemoved(err):
|
2020-12-03 10:01:09 +00:00
|
|
|
outError = err
|
2020-12-01 10:48:40 +00:00
|
|
|
return true // stop, return it back
|
2022-07-27 18:38:28 +00:00
|
|
|
case shard.IsErrObjectExpired(err):
|
|
|
|
// object is found but should not
|
|
|
|
// be returned
|
2023-08-04 11:14:07 +00:00
|
|
|
outError = new(apistatus.ObjectNotFound)
|
2022-07-27 18:38:28 +00:00
|
|
|
return true
|
2020-12-01 10:48:40 +00:00
|
|
|
default:
|
2022-01-31 14:58:32 +00:00
|
|
|
e.reportShardError(sh, "could not head object from shard", err)
|
2020-12-01 10:48:40 +00:00
|
|
|
return false
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-01 10:48:40 +00:00
|
|
|
head = res.Object()
|
|
|
|
return true
|
2020-11-17 12:26:03 +00:00
|
|
|
})
|
|
|
|
|
2021-04-14 08:58:14 +00:00
|
|
|
if outSI != nil {
|
2022-10-26 12:23:12 +00:00
|
|
|
return HeadRes{}, logicerr.Wrap(objectSDK.NewSplitInfoError(outSI))
|
2024-04-23 12:21:29 +00:00
|
|
|
} else if head == nil {
|
2022-05-31 11:56:59 +00:00
|
|
|
return HeadRes{}, outError
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 11:56:59 +00:00
|
|
|
return HeadRes{
|
2020-11-17 12:26:03 +00:00
|
|
|
head: head,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-11-19 08:15:49 +00:00
|
|
|
|
|
|
|
// Head reads object header from local storage by provided address.
|
2023-03-13 11:37:35 +00:00
|
|
|
func Head(ctx context.Context, storage *StorageEngine, addr oid.Address) (*objectSDK.Object, error) {
|
2022-05-23 13:12:32 +00:00
|
|
|
var headPrm HeadPrm
|
|
|
|
headPrm.WithAddress(addr)
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := storage.Head(ctx, headPrm)
|
2020-11-19 08:15:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Header(), nil
|
|
|
|
}
|
2020-12-08 11:58:38 +00:00
|
|
|
|
|
|
|
// HeadRaw reads object header from local storage by provided address and raw
|
|
|
|
// flag.
|
2023-03-13 11:37:35 +00:00
|
|
|
func HeadRaw(ctx context.Context, storage *StorageEngine, addr oid.Address, raw bool) (*objectSDK.Object, error) {
|
2022-05-23 13:12:32 +00:00
|
|
|
var headPrm HeadPrm
|
|
|
|
headPrm.WithAddress(addr)
|
|
|
|
headPrm.WithRaw(raw)
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := storage.Head(ctx, headPrm)
|
2020-12-08 11:58:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Header(), nil
|
|
|
|
}
|