2022-12-23 17:35:35 +00:00
|
|
|
package frostfsapiclient
|
2021-10-27 12:12:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2022-02-25 09:20:49 +00:00
|
|
|
"errors"
|
2021-10-27 12:12:05 +00:00
|
|
|
"fmt"
|
2022-02-25 09:20:49 +00:00
|
|
|
"io"
|
2021-10-27 12:12:05 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
clientcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object_manager/storagegroup"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
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"
|
2021-10-27 12:12:05 +00:00
|
|
|
)
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// Client represents FrostFS API client cut down to the needs of a purely IR application.
|
2021-10-27 12:12:05 +00:00
|
|
|
type Client struct {
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
2022-01-13 15:01:50 +00:00
|
|
|
c clientcore.Client
|
2021-10-27 12:12:05 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// WrapBasicClient wraps a client.Client instance to use it for FrostFS API RPC.
|
2022-01-13 15:01:50 +00:00
|
|
|
func (x *Client) WrapBasicClient(c clientcore.Client) {
|
2021-10-27 12:12:05 +00:00
|
|
|
x.c = c
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SetPrivateKey sets a private key to sign RPC requests.
|
2021-10-27 12:12:05 +00:00
|
|
|
func (x *Client) SetPrivateKey(key *ecdsa.PrivateKey) {
|
|
|
|
x.key = key
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchSGPrm groups parameters of SearchSG operation.
|
|
|
|
type SearchSGPrm struct {
|
|
|
|
contextPrm
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
cnrID cid.ID
|
2021-10-27 12:12:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SetContainerID sets the ID of the container to search for storage groups.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (x *SearchSGPrm) SetContainerID(id cid.ID) {
|
2021-10-27 12:12:05 +00:00
|
|
|
x.cnrID = id
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SearchSGRes groups the resulting values of SearchSG operation.
|
2021-10-27 12:12:05 +00:00
|
|
|
type SearchSGRes struct {
|
2022-03-28 13:15:15 +00:00
|
|
|
cliRes []oid.ID
|
2021-10-27 12:12:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// IDList returns a list of IDs of storage groups in the container.
|
2022-03-28 13:15:15 +00:00
|
|
|
func (x SearchSGRes) IDList() []oid.ID {
|
2022-02-25 09:20:49 +00:00
|
|
|
return x.cliRes
|
2021-10-27 12:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sgFilter = storagegroup.SearchQuery()
|
|
|
|
|
|
|
|
// SearchSG lists objects of storage group type in the container.
|
2021-11-03 17:13:16 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns any error which prevented the operation from completing correctly in error return.
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x Client) SearchSG(prm SearchSGPrm) (*SearchSGRes, error) {
|
|
|
|
var cliPrm client.PrmObjectSearch
|
2022-05-31 17:00:41 +00:00
|
|
|
cliPrm.InContainer(prm.cnrID)
|
2022-02-25 09:20:49 +00:00
|
|
|
cliPrm.SetFilters(sgFilter)
|
2022-08-29 08:57:02 +00:00
|
|
|
cliPrm.UseKey(*x.key)
|
2021-10-27 12:12:05 +00:00
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
rdr, err := x.c.ObjectSearchInit(prm.ctx, cliPrm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("init object search: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]oid.ID, 10)
|
2022-03-28 13:15:15 +00:00
|
|
|
var list []oid.ID
|
2022-02-25 09:20:49 +00:00
|
|
|
var n int
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
for {
|
|
|
|
n, ok = rdr.Read(buf)
|
|
|
|
for i := 0; i < n; i++ {
|
2022-03-28 13:15:15 +00:00
|
|
|
list = append(list, buf[i])
|
2022-02-25 09:20:49 +00:00
|
|
|
}
|
2022-03-03 13:26:43 +00:00
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
2022-02-25 09:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res, err := rdr.Close()
|
2021-11-06 11:13:04 +00:00
|
|
|
if err == nil {
|
|
|
|
// pull out an error from status
|
2022-02-25 09:20:49 +00:00
|
|
|
err = apistatus.ErrFromStatus(res.Status())
|
2021-11-06 11:13:04 +00:00
|
|
|
}
|
2021-10-27 12:12:05 +00:00
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read object list: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &SearchSGRes{
|
|
|
|
cliRes: list,
|
|
|
|
}, nil
|
2021-10-27 12:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetObjectPrm groups parameters of GetObject operation.
|
|
|
|
type GetObjectPrm struct {
|
|
|
|
getObjectPrm
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// GetObjectRes groups the resulting values of GetObject operation.
|
2021-10-27 12:12:05 +00:00
|
|
|
type GetObjectRes struct {
|
2022-02-25 09:20:49 +00:00
|
|
|
obj *object.Object
|
2021-10-27 12:12:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Object returns the received object.
|
2021-10-27 12:12:05 +00:00
|
|
|
func (x GetObjectRes) Object() *object.Object {
|
2022-02-25 09:20:49 +00:00
|
|
|
return x.obj
|
2021-10-27 12:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetObject reads the object by address.
|
2021-11-03 17:13:16 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns any error which prevented the operation from completing correctly in error return.
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x Client) GetObject(prm GetObjectPrm) (*GetObjectRes, error) {
|
|
|
|
var cliPrm client.PrmObjectGet
|
2022-05-31 17:00:41 +00:00
|
|
|
cliPrm.FromContainer(prm.objAddr.Container())
|
|
|
|
cliPrm.ByID(prm.objAddr.Object())
|
2022-08-29 08:57:02 +00:00
|
|
|
cliPrm.UseKey(*x.key)
|
2021-10-27 12:12:05 +00:00
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
rdr, err := x.c.ObjectGetInit(prm.ctx, cliPrm)
|
2022-03-03 15:01:30 +00:00
|
|
|
if err != nil {
|
2022-02-25 09:20:49 +00:00
|
|
|
return nil, fmt.Errorf("init object search: %w", err)
|
2021-11-06 11:13:04 +00:00
|
|
|
}
|
2021-10-27 12:12:05 +00:00
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
var obj object.Object
|
|
|
|
|
|
|
|
if !rdr.ReadHeader(&obj) {
|
|
|
|
res, err := rdr.Close()
|
|
|
|
if err == nil {
|
|
|
|
// pull out an error from status
|
|
|
|
err = apistatus.ErrFromStatus(res.Status())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("read object header: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, obj.PayloadSize())
|
|
|
|
|
|
|
|
_, err = rdr.Read(buf)
|
|
|
|
if err != nil && !errors.Is(err, io.EOF) {
|
|
|
|
return nil, fmt.Errorf("read payload: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj.SetPayload(buf)
|
2022-02-25 09:20:49 +00:00
|
|
|
|
|
|
|
return &GetObjectRes{
|
|
|
|
obj: &obj,
|
|
|
|
}, nil
|
2021-10-27 12:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HeadObjectPrm groups parameters of HeadObject operation.
|
|
|
|
type HeadObjectPrm struct {
|
|
|
|
getObjectPrm
|
|
|
|
|
|
|
|
raw bool
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
local bool
|
2021-10-27 12:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetRawFlag sets flag of raw request.
|
|
|
|
func (x *HeadObjectPrm) SetRawFlag() {
|
|
|
|
x.raw = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTTL sets request TTL value.
|
|
|
|
func (x *HeadObjectPrm) SetTTL(ttl uint32) {
|
2022-02-25 09:20:49 +00:00
|
|
|
x.local = ttl < 2
|
2021-10-27 12:12:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// HeadObjectRes groups the resulting values of HeadObject operation.
|
2021-10-27 12:12:05 +00:00
|
|
|
type HeadObjectRes struct {
|
2022-02-25 09:20:49 +00:00
|
|
|
hdr *object.Object
|
2021-10-27 12:12:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Header returns the received object header.
|
2021-10-27 12:12:05 +00:00
|
|
|
func (x HeadObjectRes) Header() *object.Object {
|
2022-02-25 09:20:49 +00:00
|
|
|
return x.hdr
|
2021-10-27 12:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HeadObject reads short object header by address.
|
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns any error which prevented the operation from completing correctly in error return.
|
|
|
|
// For raw requests, returns *object.SplitInfoError error if the requested object is virtual.
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x Client) HeadObject(prm HeadObjectPrm) (*HeadObjectRes, error) {
|
|
|
|
var cliPrm client.PrmObjectHead
|
|
|
|
|
|
|
|
if prm.raw {
|
|
|
|
cliPrm.MarkRaw()
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.local {
|
|
|
|
cliPrm.MarkLocal()
|
|
|
|
}
|
2021-10-27 12:12:05 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
cliPrm.FromContainer(prm.objAddr.Container())
|
|
|
|
cliPrm.ByID(prm.objAddr.Object())
|
2022-11-17 11:00:37 +00:00
|
|
|
cliPrm.UseKey(*x.key)
|
2021-10-27 12:12:05 +00:00
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
cliRes, err := x.c.ObjectHead(prm.ctx, cliPrm)
|
2021-11-06 11:13:04 +00:00
|
|
|
if err == nil {
|
|
|
|
// pull out an error from status
|
2022-02-25 09:20:49 +00:00
|
|
|
err = apistatus.ErrFromStatus(cliRes.Status())
|
2021-11-06 11:13:04 +00:00
|
|
|
}
|
2021-10-27 12:12:05 +00:00
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
if err != nil {
|
2023-02-05 15:59:38 +00:00
|
|
|
return nil, fmt.Errorf("read object header from FrostFS: %w", err)
|
2022-02-25 09:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var hdr object.Object
|
|
|
|
|
|
|
|
if !cliRes.ReadHeader(&hdr) {
|
|
|
|
return nil, errors.New("missing object header in the response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &HeadObjectRes{
|
|
|
|
hdr: &hdr,
|
|
|
|
}, nil
|
2021-10-27 12:12:05 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// GetObjectPayload reads an object by address from FrostFS via Client and returns its payload.
|
2021-11-03 17:13:16 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns any error which prevented the operation from completing correctly in error return.
|
2022-05-31 17:00:41 +00:00
|
|
|
func GetObjectPayload(ctx context.Context, c Client, addr oid.Address) ([]byte, error) {
|
2021-10-27 12:12:05 +00:00
|
|
|
var prm GetObjectPrm
|
|
|
|
|
|
|
|
prm.SetContext(ctx)
|
|
|
|
prm.SetAddress(addr)
|
|
|
|
|
|
|
|
obj, err := c.GetObject(prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj.Object().Payload(), nil
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func headObject(ctx context.Context, c Client, addr oid.Address, raw bool, ttl uint32) (*object.Object, error) {
|
2021-10-27 12:12:05 +00:00
|
|
|
var prm HeadObjectPrm
|
|
|
|
|
|
|
|
prm.SetContext(ctx)
|
|
|
|
prm.SetAddress(addr)
|
|
|
|
prm.SetTTL(ttl)
|
|
|
|
|
|
|
|
if raw {
|
|
|
|
prm.SetRawFlag()
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err := c.HeadObject(prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj.Header(), nil
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// GetRawObjectHeaderLocally reads the raw short object header from the server's local storage by address via Client.
|
2022-05-31 17:00:41 +00:00
|
|
|
func GetRawObjectHeaderLocally(ctx context.Context, c Client, addr oid.Address) (*object.Object, error) {
|
2021-10-27 12:12:05 +00:00
|
|
|
return headObject(ctx, c, addr, true, 1)
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// GetObjectHeaderFromContainer reads the short object header by address via Client with TTL = 10
|
2021-10-27 12:12:05 +00:00
|
|
|
// for deep traversal of the container.
|
2022-05-31 17:00:41 +00:00
|
|
|
func GetObjectHeaderFromContainer(ctx context.Context, c Client, addr oid.Address) (*object.Object, error) {
|
2021-10-27 12:12:05 +00:00
|
|
|
return headObject(ctx, c, addr, false, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HashPayloadRangePrm groups parameters of HashPayloadRange operation.
|
|
|
|
type HashPayloadRangePrm struct {
|
|
|
|
getObjectPrm
|
|
|
|
|
|
|
|
rng *object.Range
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRange sets payload range to calculate the hash.
|
|
|
|
func (x *HashPayloadRangePrm) SetRange(rng *object.Range) {
|
|
|
|
x.rng = rng
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// HashPayloadRangeRes groups the resulting values of HashPayloadRange operation.
|
2021-10-27 12:12:05 +00:00
|
|
|
type HashPayloadRangeRes struct {
|
|
|
|
h []byte
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Hash returns the hash of the object payload range.
|
2021-10-27 12:12:05 +00:00
|
|
|
func (x HashPayloadRangeRes) Hash() []byte {
|
|
|
|
return x.h
|
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
// HashPayloadRange requests to calculate Tillich-Zemor hash of the payload range of the object
|
2021-10-27 12:12:05 +00:00
|
|
|
// from the remote server's local storage.
|
2021-11-03 17:13:16 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns any error which prevented the operation from completing correctly in error return.
|
2021-10-27 12:12:05 +00:00
|
|
|
func (x Client) HashPayloadRange(prm HashPayloadRangePrm) (res HashPayloadRangeRes, err error) {
|
2022-02-25 09:20:49 +00:00
|
|
|
var cliPrm client.PrmObjectHash
|
2022-05-31 17:00:41 +00:00
|
|
|
cliPrm.FromContainer(prm.objAddr.Container())
|
|
|
|
cliPrm.ByID(prm.objAddr.Object())
|
2022-02-25 09:20:49 +00:00
|
|
|
cliPrm.SetRangeList(prm.rng.GetOffset(), prm.rng.GetLength())
|
|
|
|
cliPrm.TillichZemorAlgo()
|
2021-10-27 12:12:05 +00:00
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
cliRes, err := x.c.ObjectHash(prm.ctx, cliPrm)
|
2021-10-27 12:12:05 +00:00
|
|
|
if err == nil {
|
2021-11-06 11:13:04 +00:00
|
|
|
// pull out an error from status
|
|
|
|
err = apistatus.ErrFromStatus(cliRes.Status())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
hs := cliRes.Checksums()
|
2021-10-27 12:12:05 +00:00
|
|
|
if ln := len(hs); ln != 1 {
|
2022-02-25 09:20:49 +00:00
|
|
|
err = fmt.Errorf("wrong number of checksums %d", ln)
|
2021-10-27 12:12:05 +00:00
|
|
|
} else {
|
2021-11-06 11:13:04 +00:00
|
|
|
res.h = hs[0]
|
2021-10-27 12:12:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// HashObjectRange reads Tillich-Zemor hash of the object payload range by address
|
|
|
|
// from the remote server's local storage via Client.
|
2021-11-03 17:13:16 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns any error which prevented the operation from completing correctly in error return.
|
2022-05-31 17:00:41 +00:00
|
|
|
func HashObjectRange(ctx context.Context, c Client, addr oid.Address, rng *object.Range) ([]byte, error) {
|
2021-10-27 12:12:05 +00:00
|
|
|
var prm HashPayloadRangePrm
|
|
|
|
|
|
|
|
prm.SetContext(ctx)
|
|
|
|
prm.SetAddress(addr)
|
|
|
|
prm.SetRange(rng)
|
|
|
|
|
|
|
|
res, err := c.HashPayloadRange(prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Hash(), nil
|
|
|
|
}
|