2021-05-25 08:48:01 +00:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
2022-02-07 20:27:56 +00:00
|
|
|
"bytes"
|
2021-05-25 08:48:01 +00:00
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-12-29 14:40:37 +00:00
|
|
|
"io"
|
2021-10-25 12:57:55 +00:00
|
|
|
"math"
|
2021-05-25 08:48:01 +00:00
|
|
|
"math/rand"
|
2021-11-16 13:50:33 +00:00
|
|
|
"sort"
|
2021-10-25 12:57:55 +00:00
|
|
|
"strings"
|
2021-05-25 08:48:01 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-12-10 13:56:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-02-21 11:30:30 +00:00
|
|
|
sessionv2 "github.com/nspcc-dev/neofs-api-go/v2/session"
|
2021-12-17 10:36:07 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/accounting"
|
2021-11-09 08:20:09 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/client"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/eacl"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-02-07 20:27:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object/address"
|
2022-01-25 16:20:32 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-11-09 08:20:09 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
2021-11-15 06:35:30 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/token"
|
2021-09-07 14:34:20 +00:00
|
|
|
"go.uber.org/zap"
|
2021-05-25 08:48:01 +00:00
|
|
|
)
|
|
|
|
|
2021-07-20 08:02:14 +00:00
|
|
|
// Client is a wrapper for client.Client to generate mock.
|
|
|
|
type Client interface {
|
2022-02-17 16:10:49 +00:00
|
|
|
BalanceGet(context.Context, client.PrmBalanceGet) (*client.ResBalanceGet, error)
|
|
|
|
ContainerPut(context.Context, client.PrmContainerPut) (*client.ResContainerPut, error)
|
|
|
|
ContainerGet(context.Context, client.PrmContainerGet) (*client.ResContainerGet, error)
|
|
|
|
ContainerList(context.Context, client.PrmContainerList) (*client.ResContainerList, error)
|
|
|
|
ContainerDelete(context.Context, client.PrmContainerDelete) (*client.ResContainerDelete, error)
|
|
|
|
ContainerEACL(context.Context, client.PrmContainerEACL) (*client.ResContainerEACL, error)
|
|
|
|
ContainerSetEACL(context.Context, client.PrmContainerSetEACL) (*client.ResContainerSetEACL, error)
|
|
|
|
EndpointInfo(context.Context, client.PrmEndpointInfo) (*client.ResEndpointInfo, error)
|
|
|
|
NetworkInfo(context.Context, client.PrmNetworkInfo) (*client.ResNetworkInfo, error)
|
2022-02-07 20:27:56 +00:00
|
|
|
ObjectPutInit(context.Context, client.PrmObjectPutInit) (*client.ObjectWriter, error)
|
2022-02-15 05:36:47 +00:00
|
|
|
ObjectDelete(context.Context, client.PrmObjectDelete) (*client.ResObjectDelete, error)
|
2022-02-07 20:27:56 +00:00
|
|
|
ObjectGetInit(context.Context, client.PrmObjectGet) (*client.ObjectReader, error)
|
2022-02-11 16:13:53 +00:00
|
|
|
ObjectHead(context.Context, client.PrmObjectHead) (*client.ResObjectHead, error)
|
2022-02-12 09:26:38 +00:00
|
|
|
ObjectRangeInit(context.Context, client.PrmObjectRange) (*client.ObjectRangeReader, error)
|
2022-02-12 20:30:16 +00:00
|
|
|
ObjectSearchInit(context.Context, client.PrmObjectSearch) (*client.ObjectListReader, error)
|
2022-02-17 16:10:49 +00:00
|
|
|
SessionCreate(context.Context, client.PrmSessionCreate) (*client.ResSessionCreate, error)
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
// InitParameters contains options used to create connection Pool.
|
|
|
|
type InitParameters struct {
|
2022-02-02 13:25:36 +00:00
|
|
|
Key *ecdsa.PrivateKey
|
|
|
|
Logger *zap.Logger
|
|
|
|
NodeConnectionTimeout time.Duration
|
|
|
|
NodeRequestTimeout time.Duration
|
|
|
|
ClientRebalanceInterval time.Duration
|
2022-02-02 11:21:23 +00:00
|
|
|
SessionTokenThreshold time.Duration
|
2022-02-02 13:25:36 +00:00
|
|
|
SessionExpirationDuration uint64
|
2022-03-11 11:45:36 +00:00
|
|
|
NodeParams []NodeParam
|
|
|
|
|
|
|
|
clientBuilder func(endpoint string) (Client, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type rebalanceParameters struct {
|
|
|
|
nodesParams []*nodesParam
|
|
|
|
nodeRequestTimeout time.Duration
|
|
|
|
clientRebalanceInterval time.Duration
|
|
|
|
sessionExpirationDuration uint64
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
type nodesParam struct {
|
2021-11-16 13:50:33 +00:00
|
|
|
priority int
|
|
|
|
addresses []string
|
|
|
|
weights []float64
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// NodeParam groups parameters of remote node.
|
2021-11-16 13:50:33 +00:00
|
|
|
type NodeParam struct {
|
2022-03-11 11:45:36 +00:00
|
|
|
Priority int
|
|
|
|
Address string
|
|
|
|
Weight float64
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 09:15:21 +00:00
|
|
|
// ContainerPollingParams contains parameters used in polling is a container created or not.
|
|
|
|
type ContainerPollingParams struct {
|
|
|
|
CreationTimeout time.Duration
|
|
|
|
PollInterval time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultPollingParams creates ContainerPollingParams with default values.
|
|
|
|
func DefaultPollingParams() *ContainerPollingParams {
|
|
|
|
return &ContainerPollingParams{
|
|
|
|
CreationTimeout: 120 * time.Second,
|
|
|
|
PollInterval: 5 * time.Second,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 08:48:01 +00:00
|
|
|
type clientPack struct {
|
2021-12-29 14:40:37 +00:00
|
|
|
client Client
|
2021-10-25 12:57:55 +00:00
|
|
|
healthy bool
|
|
|
|
address string
|
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
type prmCommon struct {
|
|
|
|
defaultSession bool
|
|
|
|
verb sessionv2.ObjectSessionVerb
|
|
|
|
addr *address.Address
|
2021-10-25 12:57:55 +00:00
|
|
|
|
2021-11-15 06:35:30 +00:00
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
btoken *token.BearerToken
|
|
|
|
stoken *session.Token
|
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
func (x *prmCommon) useDefaultSession() {
|
|
|
|
x.defaultSession = true
|
2021-11-15 06:35:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
func (x *prmCommon) useAddress(addr *address.Address) {
|
|
|
|
x.addr = addr
|
2021-11-15 06:35:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
func (x *prmCommon) useVerb(verb sessionv2.ObjectSessionVerb) {
|
|
|
|
x.verb = verb
|
2021-11-15 06:35:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
// UseKey specifies private key to sign the requests.
|
|
|
|
// If key is not provided, then Pool default key is used.
|
|
|
|
func (x *prmCommon) UseKey(key *ecdsa.PrivateKey) {
|
|
|
|
x.key = key
|
2021-12-16 15:46:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
// UseBearer attaches bearer token to be used for the operation.
|
|
|
|
func (x *prmCommon) UseBearer(token *token.BearerToken) {
|
|
|
|
x.btoken = token
|
2022-02-21 11:30:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
// UseSession specifies session within which object should be read.
|
|
|
|
func (x *prmCommon) UseSession(token *session.Token) {
|
|
|
|
x.stoken = token
|
2022-02-21 11:30:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
// PrmObjectPut groups parameters of PutObject operation.
|
|
|
|
type PrmObjectPut struct {
|
|
|
|
prmCommon
|
|
|
|
|
|
|
|
hdr object.Object
|
|
|
|
|
|
|
|
payload io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetHeader specifies header of the object.
|
|
|
|
func (x *PrmObjectPut) SetHeader(hdr object.Object) {
|
|
|
|
x.hdr = hdr
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPayload specifies payload of the object.
|
|
|
|
func (x *PrmObjectPut) SetPayload(payload io.Reader) {
|
|
|
|
x.payload = payload
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmObjectDelete groups parameters of DeleteObject operation.
|
|
|
|
type PrmObjectDelete struct {
|
|
|
|
prmCommon
|
|
|
|
|
|
|
|
addr address.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress specifies NeoFS address of the object.
|
|
|
|
func (x *PrmObjectDelete) SetAddress(addr address.Address) {
|
|
|
|
x.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmObjectGet groups parameters of GetObject operation.
|
|
|
|
type PrmObjectGet struct {
|
|
|
|
prmCommon
|
|
|
|
|
|
|
|
addr address.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress specifies NeoFS address of the object.
|
|
|
|
func (x *PrmObjectGet) SetAddress(addr address.Address) {
|
|
|
|
x.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmObjectHead groups parameters of HeadObject operation.
|
|
|
|
type PrmObjectHead struct {
|
|
|
|
prmCommon
|
|
|
|
|
|
|
|
addr address.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress specifies NeoFS address of the object.
|
|
|
|
func (x *PrmObjectHead) SetAddress(addr address.Address) {
|
|
|
|
x.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmObjectRange groups parameters of RangeObject operation.
|
|
|
|
type PrmObjectRange struct {
|
|
|
|
prmCommon
|
|
|
|
|
|
|
|
addr address.Address
|
|
|
|
off, ln uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress specifies NeoFS address of the object.
|
|
|
|
func (x *PrmObjectRange) SetAddress(addr address.Address) {
|
|
|
|
x.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOffset sets offset of the payload range to be read.
|
|
|
|
func (x *PrmObjectRange) SetOffset(offset uint64) {
|
|
|
|
x.off = offset
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLength sets length of the payload range to be read.
|
|
|
|
func (x *PrmObjectRange) SetLength(length uint64) {
|
|
|
|
x.ln = length
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmObjectSearch groups parameters of SearchObjects operation.
|
|
|
|
type PrmObjectSearch struct {
|
|
|
|
prmCommon
|
|
|
|
|
|
|
|
cnrID cid.ID
|
|
|
|
filters object.SearchFilters
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainerID specifies the container in which to look for objects.
|
|
|
|
func (x *PrmObjectSearch) SetContainerID(cnrID cid.ID) {
|
|
|
|
x.cnrID = cnrID
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFilters specifies filters by which to select objects.
|
|
|
|
func (x *PrmObjectSearch) SetFilters(filters object.SearchFilters) {
|
|
|
|
x.filters = filters
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerPut groups parameters of PutContainer operation.
|
|
|
|
type PrmContainerPut struct {
|
|
|
|
prmCommon
|
|
|
|
|
|
|
|
cnr *container.Container
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainer specifies structured information about new NeoFS container.
|
|
|
|
func (x *PrmContainerPut) SetContainer(cnr *container.Container) {
|
|
|
|
x.cnr = cnr
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerGet groups parameters of GetContainer operation.
|
|
|
|
type PrmContainerGet struct {
|
|
|
|
prmCommon
|
|
|
|
|
|
|
|
cnrID *cid.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainerID specifies identifier of the container to be read.
|
|
|
|
func (x *PrmContainerGet) SetContainerID(cnrID *cid.ID) {
|
|
|
|
x.cnrID = cnrID
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerList groups parameters of ListContainers operation.
|
|
|
|
type PrmContainerList struct {
|
|
|
|
prmCommon
|
|
|
|
|
|
|
|
ownerID *owner.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOwnerID specifies identifier of the NeoFS account to list the containers.
|
|
|
|
func (x *PrmContainerList) SetOwnerID(ownerID *owner.ID) {
|
|
|
|
x.ownerID = ownerID
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerDelete groups parameters of DeleteContainer operation.
|
|
|
|
type PrmContainerDelete struct {
|
|
|
|
prmCommon
|
|
|
|
|
|
|
|
cnrID *cid.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainerID specifies identifier of the NeoFS container to be removed.
|
|
|
|
func (x *PrmContainerDelete) SetContainerID(cnrID *cid.ID) {
|
|
|
|
x.cnrID = cnrID
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerEACL groups parameters of GetEACL operation.
|
|
|
|
type PrmContainerEACL struct {
|
|
|
|
prmCommon
|
|
|
|
|
|
|
|
cnrID *cid.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainerID specifies identifier of the NeoFS container to read the eACL table.
|
|
|
|
func (x *PrmContainerEACL) SetContainerID(cnrID *cid.ID) {
|
|
|
|
x.cnrID = cnrID
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerSetEACL groups parameters of SetEACL operation.
|
|
|
|
type PrmContainerSetEACL struct {
|
|
|
|
prmCommon
|
|
|
|
|
|
|
|
table *eacl.Table
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTable specifies eACL table structure to be set for the container.
|
|
|
|
func (x *PrmContainerSetEACL) SetTable(table *eacl.Table) {
|
|
|
|
x.table = table
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmBalanceGet groups parameters of Balance operation.
|
|
|
|
type PrmBalanceGet struct {
|
|
|
|
prmCommon
|
|
|
|
|
|
|
|
ownerID *owner.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOwnerID specifies identifier of the NeoFS account for which the balance is requested.
|
|
|
|
func (x *PrmBalanceGet) SetOwnerID(ownerID *owner.ID) {
|
|
|
|
x.ownerID = ownerID
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// Pool represents virtual connection to the NeoFS network to communicate
|
|
|
|
// with multiple NeoFS servers without thinking about switching between servers
|
|
|
|
// due to load balancing proportions or their unavailability.
|
|
|
|
// It is designed to provide a convenient abstraction from the multiple client.Client types.
|
|
|
|
//
|
|
|
|
// Pool can be created and initialized using NewPool function.
|
|
|
|
// Before executing the NeoFS operations using the Pool, connection to the
|
|
|
|
// servers MUST BE correctly established (see Dial method).
|
|
|
|
// Using the Pool before connecting have been established can lead to a panic.
|
|
|
|
// After the work, the Pool SHOULD BE closed (see Close method): it frees internal
|
|
|
|
// and system resources which were allocated for the period of work of the Pool.
|
|
|
|
// Calling Dial/Close methods during the communication process step strongly discouraged
|
|
|
|
// as it leads to undefined behavior.
|
|
|
|
//
|
|
|
|
// Each method which produces a NeoFS API call may return an error.
|
|
|
|
// Status of underlying server response is casted to built-in error instance.
|
|
|
|
// Certain statuses can be checked using `client` and standard `errors` packages.
|
|
|
|
// Note that package provides some helper functions to work with status returns
|
|
|
|
// (e.g. client.IsErrContainerNotFound, client.IsErrObjectNotFound).
|
|
|
|
//
|
|
|
|
// See pool package overview to get some examples.
|
2022-03-09 09:40:23 +00:00
|
|
|
type Pool struct {
|
2022-02-02 11:21:23 +00:00
|
|
|
innerPools []*innerPool
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
owner *owner.ID
|
|
|
|
cancel context.CancelFunc
|
|
|
|
closedCh chan struct{}
|
2022-03-11 11:57:35 +00:00
|
|
|
cache *sessionCache
|
2022-02-02 11:21:23 +00:00
|
|
|
stokenDuration uint64
|
|
|
|
stokenThreshold time.Duration
|
2022-03-11 14:55:02 +00:00
|
|
|
rebalanceParams rebalanceParameters
|
|
|
|
clientBuilder func(endpoint string) (Client, error)
|
|
|
|
logger *zap.Logger
|
2021-11-16 13:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type innerPool struct {
|
2021-05-25 08:48:01 +00:00
|
|
|
lock sync.RWMutex
|
2022-03-11 11:57:35 +00:00
|
|
|
sampler *sampler
|
2021-05-25 08:48:01 +00:00
|
|
|
clientPacks []*clientPack
|
|
|
|
}
|
|
|
|
|
2022-02-02 11:21:23 +00:00
|
|
|
const (
|
2022-02-11 14:40:09 +00:00
|
|
|
defaultSessionTokenExpirationDuration = 100 // in blocks
|
|
|
|
|
|
|
|
defaultSessionTokenThreshold = 5 * time.Second
|
2022-03-11 11:45:36 +00:00
|
|
|
defaultRebalanceInterval = 25 * time.Second
|
|
|
|
defaultRequestTimeout = 4 * time.Second
|
2022-02-02 11:21:23 +00:00
|
|
|
)
|
2022-02-02 13:45:01 +00:00
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
// NewPool create connection pool using parameters.
|
2022-03-11 14:55:02 +00:00
|
|
|
func NewPool(options InitParameters) (*Pool, error) {
|
2022-03-11 11:45:36 +00:00
|
|
|
if options.Key == nil {
|
|
|
|
return nil, fmt.Errorf("missed required parameter 'Key'")
|
2021-11-08 13:28:28 +00:00
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
nodesParams, err := adjustNodeParams(options.NodeParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-10 12:59:31 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
fillDefaultInitParams(&options)
|
|
|
|
|
2022-03-11 11:57:35 +00:00
|
|
|
cache, err := newCache()
|
2022-03-11 11:45:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't create cache: %w", err)
|
2022-02-02 11:21:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool := &Pool{
|
|
|
|
key: options.Key,
|
|
|
|
owner: owner.NewIDFromPublicKey(&options.Key.PublicKey),
|
|
|
|
cache: cache,
|
|
|
|
logger: options.Logger,
|
|
|
|
stokenDuration: options.SessionExpirationDuration,
|
|
|
|
stokenThreshold: options.SessionTokenThreshold,
|
|
|
|
rebalanceParams: rebalanceParameters{
|
|
|
|
nodesParams: nodesParams,
|
|
|
|
nodeRequestTimeout: options.NodeRequestTimeout,
|
|
|
|
clientRebalanceInterval: options.ClientRebalanceInterval,
|
|
|
|
sessionExpirationDuration: options.SessionExpirationDuration,
|
|
|
|
},
|
|
|
|
clientBuilder: options.clientBuilder,
|
|
|
|
}
|
|
|
|
|
|
|
|
return pool, nil
|
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
// Dial establishes a connection to the servers from the NeoFS network.
|
|
|
|
// Returns an error describing failure reason. If failed, the Pool
|
|
|
|
// SHOULD NOT be used.
|
|
|
|
func (p *Pool) Dial(ctx context.Context) error {
|
|
|
|
inner := make([]*innerPool, len(p.rebalanceParams.nodesParams))
|
2021-09-07 14:34:20 +00:00
|
|
|
var atLeastOneHealthy bool
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
for i, params := range p.rebalanceParams.nodesParams {
|
2021-11-16 13:50:33 +00:00
|
|
|
clientPacks := make([]*clientPack, len(params.weights))
|
2022-02-07 20:27:56 +00:00
|
|
|
for j, addr := range params.addresses {
|
2022-03-11 14:55:02 +00:00
|
|
|
c, err := p.clientBuilder(addr)
|
2021-11-16 13:50:33 +00:00
|
|
|
if err != nil {
|
2022-03-11 14:55:02 +00:00
|
|
|
return err
|
2021-11-16 13:50:33 +00:00
|
|
|
}
|
|
|
|
var healthy bool
|
2022-03-11 14:55:02 +00:00
|
|
|
cliRes, err := createSessionTokenForDuration(ctx, c, p.rebalanceParams.sessionExpirationDuration)
|
|
|
|
if err != nil && p.logger != nil {
|
|
|
|
p.logger.Warn("failed to create neofs session token for client",
|
2022-03-11 11:45:36 +00:00
|
|
|
zap.String("Address", addr),
|
2021-11-16 13:50:33 +00:00
|
|
|
zap.Error(err))
|
|
|
|
} else if err == nil {
|
|
|
|
healthy, atLeastOneHealthy = true, true
|
2022-03-11 14:55:02 +00:00
|
|
|
st := sessionTokenForOwner(p.owner, cliRes)
|
|
|
|
_ = p.cache.Put(formCacheKey(addr, p.key), st)
|
2021-11-16 13:50:33 +00:00
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
clientPacks[j] = &clientPack{client: c, healthy: healthy, address: addr}
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
2021-11-16 13:50:33 +00:00
|
|
|
source := rand.NewSource(time.Now().UnixNano())
|
2022-03-11 14:55:02 +00:00
|
|
|
sampl := newSampler(params.weights, source)
|
2021-11-16 13:50:33 +00:00
|
|
|
|
|
|
|
inner[i] = &innerPool{
|
2022-03-11 14:55:02 +00:00
|
|
|
sampler: sampl,
|
2021-11-16 13:50:33 +00:00
|
|
|
clientPacks: clientPacks,
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
2021-09-07 14:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !atLeastOneHealthy {
|
2022-03-11 14:55:02 +00:00
|
|
|
return fmt.Errorf("at least one node must be healthy")
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
2021-09-07 14:34:20 +00:00
|
|
|
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2022-03-11 14:55:02 +00:00
|
|
|
p.cancel = cancel
|
|
|
|
p.closedCh = make(chan struct{})
|
|
|
|
p.innerPools = inner
|
2022-03-11 11:45:36 +00:00
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
go p.startRebalance(ctx)
|
|
|
|
return nil
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
func fillDefaultInitParams(params *InitParameters) {
|
|
|
|
if params.SessionExpirationDuration == 0 {
|
|
|
|
params.SessionExpirationDuration = defaultSessionTokenExpirationDuration
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.SessionTokenThreshold <= 0 {
|
|
|
|
params.SessionTokenThreshold = defaultSessionTokenThreshold
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.ClientRebalanceInterval <= 0 {
|
|
|
|
params.ClientRebalanceInterval = defaultRebalanceInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.NodeRequestTimeout <= 0 {
|
|
|
|
params.NodeRequestTimeout = defaultRequestTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.clientBuilder == nil {
|
|
|
|
params.clientBuilder = func(addr string) (Client, error) {
|
|
|
|
var c client.Client
|
|
|
|
|
|
|
|
var prmInit client.PrmInit
|
|
|
|
prmInit.ResolveNeoFSFailures()
|
|
|
|
prmInit.SetDefaultPrivateKey(*params.Key)
|
|
|
|
|
|
|
|
c.Init(prmInit)
|
|
|
|
|
|
|
|
var prmDial client.PrmDial
|
|
|
|
prmDial.SetServerURI(addr)
|
|
|
|
prmDial.SetTimeout(params.NodeConnectionTimeout)
|
|
|
|
|
|
|
|
return &c, c.Dial(prmDial)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func adjustNodeParams(nodeParams []NodeParam) ([]*nodesParam, error) {
|
|
|
|
if len(nodeParams) == 0 {
|
|
|
|
return nil, errors.New("no NeoFS peers configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
nodesParamsMap := make(map[int]*nodesParam)
|
|
|
|
for _, param := range nodeParams {
|
|
|
|
nodes, ok := nodesParamsMap[param.Priority]
|
|
|
|
if !ok {
|
|
|
|
nodes = &nodesParam{priority: param.Priority}
|
|
|
|
}
|
|
|
|
nodes.addresses = append(nodes.addresses, param.Address)
|
|
|
|
nodes.weights = append(nodes.weights, param.Weight)
|
|
|
|
nodesParamsMap[param.Priority] = nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
nodesParams := make([]*nodesParam, 0, len(nodesParamsMap))
|
|
|
|
for _, nodes := range nodesParamsMap {
|
|
|
|
nodes.weights = adjustWeights(nodes.weights)
|
|
|
|
nodesParams = append(nodesParams, nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(nodesParams, func(i, j int) bool {
|
|
|
|
return nodesParams[i].priority < nodesParams[j].priority
|
|
|
|
})
|
|
|
|
|
|
|
|
return nodesParams, nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
func (p *Pool) startRebalance(ctx context.Context) {
|
|
|
|
ticker := time.NewTimer(p.rebalanceParams.clientRebalanceInterval)
|
|
|
|
buffers := make([][]float64, len(p.rebalanceParams.nodesParams))
|
|
|
|
for i, params := range p.rebalanceParams.nodesParams {
|
2021-11-16 13:50:33 +00:00
|
|
|
buffers[i] = make([]float64, len(params.weights))
|
|
|
|
}
|
2021-06-21 11:12:08 +00:00
|
|
|
|
2021-07-20 08:02:14 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
close(p.closedCh)
|
2021-07-20 08:02:14 +00:00
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2022-03-11 14:55:02 +00:00
|
|
|
p.updateNodesHealth(ctx, buffers)
|
|
|
|
ticker.Reset(p.rebalanceParams.clientRebalanceInterval)
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
func (p *Pool) updateNodesHealth(ctx context.Context, buffers [][]float64) {
|
2021-11-16 13:50:33 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
for i, inner := range p.innerPools {
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
bufferWeights := buffers[i]
|
|
|
|
go func(i int, innerPool *innerPool) {
|
|
|
|
defer wg.Done()
|
2022-03-11 14:55:02 +00:00
|
|
|
p.updateInnerNodesHealth(ctx, i, bufferWeights)
|
2021-11-16 13:50:33 +00:00
|
|
|
}(i, inner)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
func (p *Pool) updateInnerNodesHealth(ctx context.Context, i int, bufferWeights []float64) {
|
|
|
|
if i > len(p.innerPools)-1 {
|
2021-11-16 13:50:33 +00:00
|
|
|
return
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
2022-03-11 14:55:02 +00:00
|
|
|
pool := p.innerPools[i]
|
|
|
|
options := p.rebalanceParams
|
2021-11-16 13:50:33 +00:00
|
|
|
|
2021-06-21 11:12:08 +00:00
|
|
|
healthyChanged := false
|
|
|
|
wg := sync.WaitGroup{}
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
var prmEndpoint client.PrmEndpointInfo
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
for j, cPack := range pool.clientPacks {
|
2021-06-21 11:12:08 +00:00
|
|
|
wg.Add(1)
|
2021-12-02 12:21:54 +00:00
|
|
|
go func(j int, cli Client) {
|
2021-06-21 11:12:08 +00:00
|
|
|
defer wg.Done()
|
2021-05-25 08:48:01 +00:00
|
|
|
ok := true
|
2022-03-11 11:45:36 +00:00
|
|
|
tctx, c := context.WithTimeout(ctx, options.nodeRequestTimeout)
|
2021-06-21 11:12:08 +00:00
|
|
|
defer c()
|
2021-12-02 12:21:54 +00:00
|
|
|
|
|
|
|
if _, err := cli.EndpointInfo(tctx, prmEndpoint); err != nil {
|
2021-06-21 11:12:08 +00:00
|
|
|
ok = false
|
2021-11-16 13:50:33 +00:00
|
|
|
bufferWeights[j] = 0
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
2022-03-11 14:55:02 +00:00
|
|
|
pool.lock.RLock()
|
|
|
|
cp := *pool.clientPacks[j]
|
|
|
|
pool.lock.RUnlock()
|
2021-10-25 12:57:55 +00:00
|
|
|
|
2021-06-21 11:12:08 +00:00
|
|
|
if ok {
|
2021-11-16 13:50:33 +00:00
|
|
|
bufferWeights[j] = options.nodesParams[i].weights[j]
|
2021-10-25 12:57:55 +00:00
|
|
|
if !cp.healthy {
|
2022-03-11 11:45:36 +00:00
|
|
|
cliRes, err := createSessionTokenForDuration(ctx, cli, options.sessionExpirationDuration)
|
2022-02-02 13:25:36 +00:00
|
|
|
if err != nil {
|
2021-07-28 08:42:55 +00:00
|
|
|
ok = false
|
2021-11-16 13:50:33 +00:00
|
|
|
bufferWeights[j] = 0
|
2021-10-25 12:57:55 +00:00
|
|
|
} else {
|
2022-03-11 14:55:02 +00:00
|
|
|
tkn := p.newSessionToken(cliRes)
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
_ = p.cache.Put(formCacheKey(cp.address, p.key), tkn)
|
2021-07-28 08:42:55 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
} else {
|
2022-03-11 14:55:02 +00:00
|
|
|
p.cache.DeleteByPrefix(cp.address)
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
2021-06-21 11:12:08 +00:00
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool.lock.Lock()
|
|
|
|
if pool.clientPacks[j].healthy != ok {
|
|
|
|
pool.clientPacks[j].healthy = ok
|
2021-06-21 11:12:08 +00:00
|
|
|
healthyChanged = true
|
|
|
|
}
|
2022-03-11 14:55:02 +00:00
|
|
|
pool.lock.Unlock()
|
2021-11-16 13:50:33 +00:00
|
|
|
}(j, cPack.client)
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if healthyChanged {
|
|
|
|
probabilities := adjustWeights(bufferWeights)
|
|
|
|
source := rand.NewSource(time.Now().UnixNano())
|
2022-03-11 14:55:02 +00:00
|
|
|
pool.lock.Lock()
|
|
|
|
pool.sampler = newSampler(probabilities, source)
|
|
|
|
pool.lock.Unlock()
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func adjustWeights(weights []float64) []float64 {
|
|
|
|
adjusted := make([]float64, len(weights))
|
|
|
|
sum := 0.0
|
|
|
|
for _, weight := range weights {
|
|
|
|
sum += weight
|
|
|
|
}
|
|
|
|
if sum > 0 {
|
|
|
|
for i, weight := range weights {
|
|
|
|
adjusted[i] = weight / sum
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return adjusted
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
func (p *Pool) Connection() (Client, *session.Token, error) {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, err := p.connection()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
tok := p.cache.Get(formCacheKey(cp.address, p.key))
|
|
|
|
return cp.client, tok, nil
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
func (p *Pool) connection() (*clientPack, error) {
|
2021-11-16 13:50:33 +00:00
|
|
|
for _, inner := range p.innerPools {
|
|
|
|
cp, err := inner.connection()
|
|
|
|
if err == nil {
|
|
|
|
return cp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("no healthy client")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *innerPool) connection() (*clientPack, error) {
|
2021-05-25 08:48:01 +00:00
|
|
|
p.lock.RLock()
|
|
|
|
defer p.lock.RUnlock()
|
|
|
|
if len(p.clientPacks) == 1 {
|
|
|
|
cp := p.clientPacks[0]
|
|
|
|
if cp.healthy {
|
2021-10-25 12:57:55 +00:00
|
|
|
return cp, nil
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
return nil, errors.New("no healthy client")
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
attempts := 3 * len(p.clientPacks)
|
|
|
|
for k := 0; k < attempts; k++ {
|
|
|
|
i := p.sampler.Next()
|
|
|
|
if cp := p.clientPacks[i]; cp.healthy {
|
2021-10-25 12:57:55 +00:00
|
|
|
return cp, nil
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-16 13:50:33 +00:00
|
|
|
|
2021-10-25 12:57:55 +00:00
|
|
|
return nil, errors.New("no healthy client")
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
2021-05-28 20:12:58 +00:00
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
func (p *Pool) OwnerID() *owner.ID {
|
2021-05-28 20:12:58 +00:00
|
|
|
return p.owner
|
|
|
|
}
|
2021-06-13 12:18:43 +00:00
|
|
|
|
2021-10-25 12:57:55 +00:00
|
|
|
func formCacheKey(address string, key *ecdsa.PrivateKey) string {
|
2021-12-10 13:56:04 +00:00
|
|
|
k := keys.PrivateKey{PrivateKey: *key}
|
|
|
|
return address + k.String()
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) conn(ctx context.Context, cfg prmCommon) (*clientPack, error) {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, err := p.connection()
|
|
|
|
if err != nil {
|
2022-02-16 16:00:38 +00:00
|
|
|
return nil, err
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 06:35:30 +00:00
|
|
|
key := p.key
|
|
|
|
if cfg.key != nil {
|
|
|
|
key = cfg.key
|
|
|
|
}
|
|
|
|
|
|
|
|
sessionToken := cfg.stoken
|
2022-03-11 13:06:08 +00:00
|
|
|
if sessionToken == nil && cfg.defaultSession {
|
2021-11-15 06:35:30 +00:00
|
|
|
cacheKey := formCacheKey(cp.address, key)
|
|
|
|
sessionToken = p.cache.Get(cacheKey)
|
|
|
|
if sessionToken == nil {
|
2022-02-02 13:25:36 +00:00
|
|
|
cliRes, err := createSessionTokenForDuration(ctx, cp.client, p.stokenDuration)
|
2021-11-15 06:35:30 +00:00
|
|
|
if err != nil {
|
2022-02-16 16:00:38 +00:00
|
|
|
return nil, err
|
2021-11-15 06:35:30 +00:00
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-01-13 13:16:35 +00:00
|
|
|
ownerID := owner.NewIDFromPublicKey(&key.PublicKey)
|
2022-01-17 10:35:39 +00:00
|
|
|
sessionToken = sessionTokenForOwner(ownerID, cliRes)
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
cfg.stoken = sessionToken
|
|
|
|
|
2021-11-15 06:35:30 +00:00
|
|
|
_ = p.cache.Put(cacheKey, sessionToken)
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
2021-06-22 09:15:21 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 16:00:38 +00:00
|
|
|
return cp, nil
|
2021-06-22 09:15:21 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
func (p *Pool) checkSessionTokenErr(err error, address string) bool {
|
2021-10-25 12:57:55 +00:00
|
|
|
if err == nil {
|
2021-11-03 14:34:02 +00:00
|
|
|
return false
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 13:27:20 +00:00
|
|
|
if strings.Contains(err.Error(), "session token does not exist") ||
|
|
|
|
strings.Contains(err.Error(), "session token has been expired") {
|
2021-10-25 12:57:55 +00:00
|
|
|
p.cache.DeleteByPrefix(address)
|
2021-11-03 14:34:02 +00:00
|
|
|
return true
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
2021-11-03 14:34:02 +00:00
|
|
|
|
|
|
|
return false
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
func createSessionTokenForDuration(ctx context.Context, c Client, dur uint64) (*client.ResSessionCreate, error) {
|
|
|
|
ni, err := c.NetworkInfo(ctx, client.PrmNetworkInfo{})
|
2022-02-02 13:25:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
epoch := ni.Info().CurrentEpoch()
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
var prm client.PrmSessionCreate
|
2022-02-02 13:25:36 +00:00
|
|
|
if math.MaxUint64-epoch < dur {
|
|
|
|
prm.SetExp(math.MaxUint64)
|
|
|
|
} else {
|
|
|
|
prm.SetExp(epoch + dur)
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
return c.SessionCreate(ctx, prm)
|
2022-02-02 13:25:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) removeSessionTokenAfterThreshold(cfg prmCommon) error {
|
2022-02-02 11:21:23 +00:00
|
|
|
cp, err := p.connection()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
key := p.key
|
|
|
|
if cfg.key != nil {
|
|
|
|
key = cfg.key
|
|
|
|
}
|
|
|
|
|
|
|
|
ts, ok := p.cache.GetAccessTime(formCacheKey(cp.address, key))
|
|
|
|
if ok && time.Since(ts) > p.stokenThreshold {
|
|
|
|
p.cache.DeleteByPrefix(cp.address)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
type callContext struct {
|
2022-02-11 14:40:09 +00:00
|
|
|
// base context for RPC
|
|
|
|
context.Context
|
2022-02-07 20:27:56 +00:00
|
|
|
|
|
|
|
client Client
|
|
|
|
|
|
|
|
// client endpoint
|
|
|
|
endpoint string
|
|
|
|
|
|
|
|
// request signer
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
// flag to open default session if session token is missing
|
|
|
|
sessionDefault bool
|
2022-02-11 14:40:09 +00:00
|
|
|
sessionTarget func(session.Token)
|
2022-02-21 11:30:30 +00:00
|
|
|
sessionContext *session.ObjectContext
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) initCallContext(ctx *callContext, cfg prmCommon) error {
|
2022-02-07 20:27:56 +00:00
|
|
|
cp, err := p.connection()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.key = cfg.key
|
|
|
|
if ctx.key == nil {
|
2022-02-11 14:40:09 +00:00
|
|
|
// use pool key if caller didn't specify its own
|
2022-02-07 20:27:56 +00:00
|
|
|
ctx.key = p.key
|
|
|
|
}
|
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
ctx.endpoint = cp.address
|
|
|
|
ctx.client = cp.client
|
|
|
|
|
2022-02-16 15:19:32 +00:00
|
|
|
if ctx.sessionTarget != nil && cfg.stoken != nil {
|
2022-02-11 14:40:09 +00:00
|
|
|
ctx.sessionTarget(*cfg.stoken)
|
|
|
|
}
|
|
|
|
|
|
|
|
// note that we don't override session provided by the caller
|
2022-03-11 13:06:08 +00:00
|
|
|
ctx.sessionDefault = cfg.stoken == nil && cfg.defaultSession
|
2022-02-21 11:30:30 +00:00
|
|
|
if ctx.sessionDefault {
|
|
|
|
ctx.sessionContext = session.NewObjectContext()
|
|
|
|
ctx.sessionContext.ToV2().SetVerb(cfg.verb)
|
|
|
|
ctx.sessionContext.ApplyTo(cfg.addr)
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
type callContextWithRetry struct {
|
|
|
|
callContext
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
noRetry bool
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) initCallContextWithRetry(ctx *callContextWithRetry, cfg prmCommon) error {
|
2022-02-11 14:40:09 +00:00
|
|
|
err := p.initCallContext(&ctx.callContext, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
// don't retry if session was specified by the caller
|
|
|
|
ctx.noRetry = cfg.stoken != nil
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:13:53 +00:00
|
|
|
// opens new session or uses cached one.
|
|
|
|
// Must be called only on initialized callContext with set sessionTarget.
|
2022-03-09 09:40:23 +00:00
|
|
|
func (p *Pool) openDefaultSession(ctx *callContext) error {
|
2022-02-11 14:40:09 +00:00
|
|
|
cacheKey := formCacheKey(ctx.endpoint, ctx.key)
|
|
|
|
|
|
|
|
tok := p.cache.Get(cacheKey)
|
2022-02-21 11:30:30 +00:00
|
|
|
if tok == nil {
|
|
|
|
// open new session
|
|
|
|
cliRes, err := createSessionTokenForDuration(ctx, ctx.client, p.stokenDuration)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("session API client: %w", err)
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-21 11:30:30 +00:00
|
|
|
tok = sessionTokenForOwner(owner.NewIDFromPublicKey(&ctx.key.PublicKey), cliRes)
|
|
|
|
// cache the opened session
|
|
|
|
p.cache.Put(cacheKey, tok)
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 11:30:30 +00:00
|
|
|
tokToSign := *tok
|
|
|
|
tokToSign.SetContext(ctx.sessionContext)
|
2022-02-11 14:40:09 +00:00
|
|
|
|
|
|
|
// sign the token
|
2022-02-21 11:30:30 +00:00
|
|
|
if err := tokToSign.Sign(ctx.key); err != nil {
|
2022-02-11 14:40:09 +00:00
|
|
|
return fmt.Errorf("sign token of the opened session: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:30:30 +00:00
|
|
|
ctx.sessionTarget(tokToSign)
|
2022-02-11 14:40:09 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:13:53 +00:00
|
|
|
// opens default session (if sessionDefault is set), and calls f. If f returns
|
|
|
|
// session-related error (*), and retrying is enabled, then f is called once more.
|
|
|
|
//
|
|
|
|
// (*) in this case cached token is removed.
|
2022-03-09 09:40:23 +00:00
|
|
|
func (p *Pool) callWithRetry(ctx *callContextWithRetry, f func() error) error {
|
2022-02-11 16:13:53 +00:00
|
|
|
var err error
|
2022-02-11 14:40:09 +00:00
|
|
|
|
2022-02-11 16:13:53 +00:00
|
|
|
if ctx.sessionDefault {
|
|
|
|
err = p.openDefaultSession(&ctx.callContext)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("open default session: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 14:40:09 +00:00
|
|
|
|
2022-02-11 16:13:53 +00:00
|
|
|
err = f()
|
|
|
|
|
|
|
|
if p.checkSessionTokenErr(err, ctx.endpoint) && !ctx.noRetry {
|
|
|
|
// don't retry anymore
|
|
|
|
ctx.noRetry = true
|
|
|
|
return p.callWithRetry(ctx, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// PutObject writes an object through a remote server using NeoFS API protocol.
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) PutObject(ctx context.Context, prm PrmObjectPut) (*oid.ID, error) {
|
|
|
|
prm.useDefaultSession()
|
|
|
|
prm.useVerb(sessionv2.ObjectVerbPut)
|
|
|
|
prm.useAddress(newAddressFromCnrID(prm.hdr.ContainerID()))
|
2022-02-02 11:21:23 +00:00
|
|
|
|
|
|
|
// Put object is different from other object service methods. Put request
|
|
|
|
// can't be resent in case of session token failures (i.e. session token is
|
|
|
|
// invalid due to lifetime expiration or server restart). The reason is that
|
|
|
|
// object's payload can be provided as a stream that should be read only once.
|
|
|
|
//
|
|
|
|
// To solve this issue, pool regenerates session tokens upon each request.
|
|
|
|
// In case of subsequent requests, pool avoids session token initialization
|
|
|
|
// by checking when the session token was accessed for the last time. If it
|
|
|
|
// hits a threshold, session token is removed from cache for a new one to be
|
|
|
|
// issued.
|
2022-03-11 13:06:08 +00:00
|
|
|
err := p.removeSessionTokenAfterThreshold(prm.prmCommon)
|
2022-02-02 11:21:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
var ctxCall callContext
|
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
ctxCall.Context = ctx
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
err = p.initCallContext(&ctxCall, prm.prmCommon)
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
2022-02-11 14:40:09 +00:00
|
|
|
return nil, fmt.Errorf("init call context")
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
var cliPrm client.PrmObjectPutInit
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
wObj, err := ctxCall.client.ObjectPutInit(ctx, cliPrm)
|
2022-02-07 20:27:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("init writing on API client: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
if ctxCall.sessionDefault {
|
|
|
|
ctxCall.sessionTarget = wObj.WithinSession
|
|
|
|
err = p.openDefaultSession(&ctxCall)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("open default session: %w", err)
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
wObj.UseKey(*ctxCall.key)
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if prm.btoken != nil {
|
|
|
|
wObj.WithBearerToken(*prm.btoken)
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if wObj.WriteHeader(prm.hdr) {
|
|
|
|
sz := prm.hdr.PayloadSize()
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if data := prm.hdr.Payload(); len(data) > 0 {
|
|
|
|
if prm.payload != nil {
|
|
|
|
prm.payload = io.MultiReader(bytes.NewReader(data), prm.payload)
|
2022-02-07 20:27:56 +00:00
|
|
|
} else {
|
2022-03-11 13:06:08 +00:00
|
|
|
prm.payload = bytes.NewReader(data)
|
2022-02-07 20:27:56 +00:00
|
|
|
sz = uint64(len(data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if prm.payload != nil {
|
2022-02-21 18:39:31 +00:00
|
|
|
const defaultBufferSizePut = 3 << 20 // configure?
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
if sz == 0 || sz > defaultBufferSizePut {
|
|
|
|
sz = defaultBufferSizePut
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, sz)
|
|
|
|
|
|
|
|
var n int
|
|
|
|
|
|
|
|
for {
|
2022-03-11 13:06:08 +00:00
|
|
|
n, err = prm.payload.Read(buf)
|
2022-02-07 20:27:56 +00:00
|
|
|
if n > 0 {
|
2022-02-11 05:03:14 +00:00
|
|
|
if !wObj.WritePayloadChunk(buf[:n]) {
|
2022-02-07 20:27:56 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("read payload: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
res, err := wObj.Close()
|
2021-11-17 11:22:32 +00:00
|
|
|
if err != nil { // here err already carries both status and client errors
|
2022-02-07 20:27:56 +00:00
|
|
|
// removes session token from cache in case of token error
|
|
|
|
p.checkSessionTokenErr(err, ctxCall.endpoint)
|
|
|
|
return nil, fmt.Errorf("client failure: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var id oid.ID
|
|
|
|
|
2022-02-11 15:16:04 +00:00
|
|
|
if !res.ReadStoredObjectID(&id) {
|
2022-02-07 20:27:56 +00:00
|
|
|
return nil, errors.New("missing ID of the stored object")
|
2021-11-17 11:22:32 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
return &id, nil
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// DeleteObject marks an object for deletion from the container using NeoFS API protocol.
|
|
|
|
// As a marker, a special unit called a tombstone is placed in the container.
|
|
|
|
// It confirms the user's intent to delete the object, and is itself a container object.
|
|
|
|
// Explicit deletion is done asynchronously, and is generally not guaranteed.
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) DeleteObject(ctx context.Context, prm PrmObjectDelete) error {
|
|
|
|
prm.useDefaultSession()
|
|
|
|
prm.useVerb(sessionv2.ObjectVerbDelete)
|
|
|
|
prm.useAddress(&prm.addr)
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
var cliPrm client.PrmObjectDelete
|
2022-02-15 05:36:47 +00:00
|
|
|
|
|
|
|
var cc callContextWithRetry
|
|
|
|
|
|
|
|
cc.Context = ctx
|
2022-03-11 13:06:08 +00:00
|
|
|
cc.sessionTarget = cliPrm.WithinSession
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
err := p.initCallContextWithRetry(&cc, prm.prmCommon)
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if cnr := prm.addr.ContainerID(); cnr != nil {
|
|
|
|
cliPrm.FromContainer(*cnr)
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if obj := prm.addr.ObjectID(); obj != nil {
|
|
|
|
cliPrm.ByID(*obj)
|
2021-11-03 14:34:02 +00:00
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
cliPrm.UseKey(*cc.key)
|
2022-02-21 12:10:16 +00:00
|
|
|
|
2022-02-15 05:36:47 +00:00
|
|
|
return p.callWithRetry(&cc, func() error {
|
2022-03-11 13:06:08 +00:00
|
|
|
_, err := cc.client.ObjectDelete(ctx, cliPrm)
|
2022-02-15 05:36:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("remove object via client: %w", err)
|
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-02-15 05:36:47 +00:00
|
|
|
return nil
|
|
|
|
})
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
type objectReadCloser client.ObjectReader
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// Read implements io.Reader of the object payload.
|
2022-02-07 20:27:56 +00:00
|
|
|
func (x *objectReadCloser) Read(p []byte) (int, error) {
|
|
|
|
return (*client.ObjectReader)(x).Read(p)
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// Close implements io.Closer of the object payload.
|
2022-02-07 20:27:56 +00:00
|
|
|
func (x *objectReadCloser) Close() error {
|
|
|
|
_, err := (*client.ObjectReader)(x).Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// ResGetObject is designed to provide object header nad read one object payload from NeoFS system.
|
2022-02-07 20:27:56 +00:00
|
|
|
type ResGetObject struct {
|
|
|
|
Header object.Object
|
|
|
|
|
|
|
|
Payload io.ReadCloser
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// GetObject reads object header and initiates reading an object payload through a remote server using NeoFS API protocol.
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) GetObject(ctx context.Context, prm PrmObjectGet) (*ResGetObject, error) {
|
|
|
|
prm.useDefaultSession()
|
|
|
|
prm.useVerb(sessionv2.ObjectVerbGet)
|
|
|
|
prm.useAddress(&prm.addr)
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
var cliPrm client.PrmObjectGet
|
2022-02-11 14:40:09 +00:00
|
|
|
|
|
|
|
var cc callContextWithRetry
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
cc.Context = ctx
|
2022-03-11 13:06:08 +00:00
|
|
|
cc.sessionTarget = cliPrm.WithinSession
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
err := p.initCallContextWithRetry(&cc, prm.prmCommon)
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if cnr := prm.addr.ContainerID(); cnr != nil {
|
|
|
|
cliPrm.FromContainer(*cnr)
|
2021-11-03 14:34:02 +00:00
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if obj := prm.addr.ObjectID(); obj != nil {
|
|
|
|
cliPrm.ByID(*obj)
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
var res ResGetObject
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
err = p.callWithRetry(&cc, func() error {
|
2022-03-11 13:06:08 +00:00
|
|
|
rObj, err := cc.client.ObjectGetInit(ctx, cliPrm)
|
2022-02-11 14:40:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("init object reading on client: %w", err)
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
rObj.UseKey(*cc.key)
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
if !rObj.ReadHeader(&res.Header) {
|
|
|
|
_, err = rObj.Close()
|
|
|
|
return fmt.Errorf("read header: %w", err)
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
res.Payload = (*objectReadCloser)(rObj)
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
return nil
|
|
|
|
})
|
2022-02-11 16:19:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
|
|
|
return &res, nil
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// HeadObject reads object header through a remote server using NeoFS API protocol.
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) HeadObject(ctx context.Context, prm PrmObjectHead) (*object.Object, error) {
|
|
|
|
prm.useDefaultSession()
|
|
|
|
prm.useVerb(sessionv2.ObjectVerbHead)
|
|
|
|
prm.useAddress(&prm.addr)
|
2022-02-11 16:13:53 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
var cliPrm client.PrmObjectHead
|
2022-02-11 16:13:53 +00:00
|
|
|
|
|
|
|
var cc callContextWithRetry
|
|
|
|
|
|
|
|
cc.Context = ctx
|
2022-03-11 13:06:08 +00:00
|
|
|
cc.sessionTarget = cliPrm.WithinSession
|
2022-02-11 16:13:53 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
err := p.initCallContextWithRetry(&cc, prm.prmCommon)
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if cnr := prm.addr.ContainerID(); cnr != nil {
|
|
|
|
cliPrm.FromContainer(*cnr)
|
2021-11-03 14:34:02 +00:00
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if obj := prm.addr.ObjectID(); obj != nil {
|
|
|
|
cliPrm.ByID(*obj)
|
2021-11-17 11:22:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
cliPrm.UseKey(*cc.key)
|
2022-02-21 12:10:16 +00:00
|
|
|
|
2022-02-11 16:13:53 +00:00
|
|
|
var obj object.Object
|
|
|
|
|
|
|
|
err = p.callWithRetry(&cc, func() error {
|
2022-03-11 13:06:08 +00:00
|
|
|
res, err := cc.client.ObjectHead(ctx, cliPrm)
|
2022-02-11 16:13:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("read object header via client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !res.ReadHeader(&obj) {
|
|
|
|
return errors.New("missing object header in response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-11 16:19:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-11 16:13:53 +00:00
|
|
|
|
|
|
|
return &obj, nil
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// ResObjectRange is designed to read payload range of one object
|
|
|
|
// from NeoFS system.
|
|
|
|
//
|
|
|
|
// Must be initialized using Pool.ObjectRange, any other
|
|
|
|
// usage is unsafe.
|
2022-02-12 09:26:38 +00:00
|
|
|
type ResObjectRange struct {
|
|
|
|
payload *client.ObjectRangeReader
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// Read implements io.Reader of the object payload.
|
2022-02-12 09:26:38 +00:00
|
|
|
func (x *ResObjectRange) Read(p []byte) (int, error) {
|
|
|
|
return x.payload.Read(p)
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// Close ends reading the payload range and returns the result of the operation
|
|
|
|
// along with the final results. Must be called after using the ResObjectRange.
|
2022-02-12 09:26:38 +00:00
|
|
|
func (x *ResObjectRange) Close() error {
|
|
|
|
_, err := x.payload.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// ObjectRange initiates reading an object's payload range through a remote
|
|
|
|
// server using NeoFS API protocol.
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) ObjectRange(ctx context.Context, prm PrmObjectRange) (*ResObjectRange, error) {
|
|
|
|
prm.useDefaultSession()
|
|
|
|
prm.useVerb(sessionv2.ObjectVerbRange)
|
|
|
|
prm.useAddress(&prm.addr)
|
2022-02-12 09:26:38 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
var cliPrm client.PrmObjectRange
|
2022-02-12 09:26:38 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
cliPrm.SetOffset(prm.off)
|
|
|
|
cliPrm.SetLength(prm.ln)
|
2022-02-12 09:26:38 +00:00
|
|
|
|
|
|
|
var cc callContextWithRetry
|
|
|
|
|
|
|
|
cc.Context = ctx
|
2022-03-11 13:06:08 +00:00
|
|
|
cc.sessionTarget = cliPrm.WithinSession
|
2022-02-12 09:26:38 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
err := p.initCallContextWithRetry(&cc, prm.prmCommon)
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if cnr := prm.addr.ContainerID(); cnr != nil {
|
|
|
|
cliPrm.FromContainer(*cnr)
|
2022-02-12 09:26:38 +00:00
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if obj := prm.addr.ObjectID(); obj != nil {
|
|
|
|
cliPrm.ByID(*obj)
|
2021-11-03 14:34:02 +00:00
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-02-12 09:26:38 +00:00
|
|
|
var res ResObjectRange
|
|
|
|
|
|
|
|
err = p.callWithRetry(&cc, func() error {
|
|
|
|
var err error
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
res.payload, err = cc.client.ObjectRangeInit(ctx, cliPrm)
|
2022-02-12 09:26:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("init payload range reading on client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.payload.UseKey(*cc.key)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-17 11:22:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-12 09:26:38 +00:00
|
|
|
return &res, nil
|
2021-11-17 11:22:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// ResObjectSearch is designed to read list of object identifiers from NeoFS system.
|
|
|
|
//
|
|
|
|
// Must be initialized using Pool.SearchObjects, any other usage is unsafe.
|
2022-02-12 20:30:16 +00:00
|
|
|
type ResObjectSearch struct {
|
|
|
|
r *client.ObjectListReader
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// Read reads another list of the object identifiers.
|
2022-02-12 20:30:16 +00:00
|
|
|
func (x *ResObjectSearch) Read(buf []oid.ID) (int, error) {
|
|
|
|
n, ok := x.r.Read(buf)
|
|
|
|
if !ok {
|
|
|
|
_, err := x.r.Close()
|
|
|
|
if err == nil {
|
|
|
|
return n, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// Iterate iterates over the list of found object identifiers.
|
|
|
|
// f can return true to stop iteration earlier.
|
|
|
|
//
|
|
|
|
// Returns an error if object can't be read.
|
2022-02-25 10:20:15 +00:00
|
|
|
func (x *ResObjectSearch) Iterate(f func(oid.ID) bool) error {
|
|
|
|
return x.r.Iterate(f)
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// Close ends reading list of the matched objects and returns the result of the operation
|
|
|
|
// along with the final results. Must be called after using the ResObjectSearch.
|
2022-02-12 20:30:16 +00:00
|
|
|
func (x *ResObjectSearch) Close() {
|
|
|
|
_, _ = x.r.Close()
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// SearchObjects initiates object selection through a remote server using NeoFS API protocol.
|
|
|
|
//
|
|
|
|
// The call only opens the transmission channel, explicit fetching of matched objects
|
|
|
|
// is done using the ResObjectSearch. Exactly one return value is non-nil.
|
|
|
|
// Resulting reader must be finally closed.
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) SearchObjects(ctx context.Context, prm PrmObjectSearch) (*ResObjectSearch, error) {
|
|
|
|
prm.useDefaultSession()
|
|
|
|
prm.useVerb(sessionv2.ObjectVerbSearch)
|
|
|
|
prm.useAddress(newAddressFromCnrID(&prm.cnrID))
|
2022-02-12 20:30:16 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
var cliPrm client.PrmObjectSearch
|
2022-02-12 20:30:16 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
cliPrm.InContainer(prm.cnrID)
|
|
|
|
cliPrm.SetFilters(prm.filters)
|
2022-02-12 20:30:16 +00:00
|
|
|
|
|
|
|
var cc callContextWithRetry
|
|
|
|
|
|
|
|
cc.Context = ctx
|
2022-03-11 13:06:08 +00:00
|
|
|
cc.sessionTarget = cliPrm.WithinSession
|
2022-02-12 20:30:16 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
err := p.initCallContextWithRetry(&cc, prm.prmCommon)
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-02-12 20:30:16 +00:00
|
|
|
var res ResObjectSearch
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-02-12 20:30:16 +00:00
|
|
|
err = p.callWithRetry(&cc, func() error {
|
|
|
|
var err error
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
res.r, err = cc.client.ObjectSearchInit(ctx, cliPrm)
|
2022-02-12 20:30:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("init object searching on client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.r.UseKey(*cc.key)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-17 11:22:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-12 20:30:16 +00:00
|
|
|
return &res, nil
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// PutContainer sends request to save container in NeoFS.
|
|
|
|
//
|
|
|
|
// Operation is asynchronous and no guaranteed even in the absence of errors.
|
|
|
|
// The required time is also not predictable.
|
|
|
|
//
|
|
|
|
// Success can be verified by reading by identifier (see GetContainer).
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) PutContainer(ctx context.Context, prm PrmContainerPut) (*cid.ID, error) {
|
|
|
|
cp, err := p.conn(ctx, prm.prmCommon)
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
var cliPrm client.PrmContainerPut
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if prm.cnr != nil {
|
|
|
|
cliPrm.SetContainer(*prm.cnr)
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
res, err := cp.client.ContainerPut(ctx, cliPrm)
|
2021-11-17 11:22:32 +00:00
|
|
|
if err != nil { // here err already carries both status and client errors
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.ID(), nil
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// GetContainer reads NeoFS container by ID.
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) GetContainer(ctx context.Context, prm PrmContainerGet) (*container.Container, error) {
|
|
|
|
cp, err := p.conn(ctx, prm.prmCommon)
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
var cliPrm client.PrmContainerGet
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if prm.cnrID != nil {
|
|
|
|
cliPrm.SetContainer(*prm.cnrID)
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
res, err := cp.client.ContainerGet(ctx, cliPrm)
|
2021-11-17 11:22:32 +00:00
|
|
|
if err != nil { // here err already carries both status and client errors
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Container(), nil
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// ListContainers requests identifiers of the account-owned containers.
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) ListContainers(ctx context.Context, prm PrmContainerList) ([]cid.ID, error) {
|
|
|
|
cp, err := p.conn(ctx, prm.prmCommon)
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
var cliPrm client.PrmContainerList
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if prm.ownerID != nil {
|
|
|
|
cliPrm.SetAccount(*prm.ownerID)
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
res, err := cp.client.ContainerList(ctx, cliPrm)
|
2021-11-17 11:22:32 +00:00
|
|
|
if err != nil { // here err already carries both status and client errors
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
return res.Containers(), nil
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// DeleteContainer sends request to remove the NeoFS container.
|
|
|
|
//
|
|
|
|
// Operation is asynchronous and no guaranteed even in the absence of errors.
|
|
|
|
// The required time is also not predictable.
|
|
|
|
//
|
|
|
|
// Success can be verified by reading by identifier (see GetContainer).
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) DeleteContainer(ctx context.Context, prm PrmContainerDelete) error {
|
|
|
|
cp, err := p.conn(ctx, prm.prmCommon)
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
var cliPrm client.PrmContainerDelete
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if prm.cnrID != nil {
|
|
|
|
cliPrm.SetContainer(*prm.cnrID)
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if prm.stoken != nil {
|
|
|
|
cliPrm.SetSessionToken(*prm.stoken)
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
_, err = cp.client.ContainerDelete(ctx, cliPrm)
|
2021-11-17 11:22:32 +00:00
|
|
|
|
|
|
|
// here err already carries both status and client errors
|
|
|
|
|
2021-10-25 12:57:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// GetEACL reads eACL table of the NeoFS container.
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) GetEACL(ctx context.Context, prm PrmContainerEACL) (*eacl.Table, error) {
|
|
|
|
cp, err := p.conn(ctx, prm.prmCommon)
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
var cliPrm client.PrmContainerEACL
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if prm.cnrID != nil {
|
|
|
|
cliPrm.SetContainer(*prm.cnrID)
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
res, err := cp.client.ContainerEACL(ctx, cliPrm)
|
2021-11-17 11:22:32 +00:00
|
|
|
if err != nil { // here err already carries both status and client errors
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Table(), nil
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// SetEACL sends request to update eACL table of the NeoFS container.
|
|
|
|
//
|
|
|
|
// Operation is asynchronous and no guaranteed even in the absence of errors.
|
|
|
|
// The required time is also not predictable.
|
|
|
|
//
|
|
|
|
// Success can be verified by reading by identifier (see GetEACL).
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) SetEACL(ctx context.Context, prm PrmContainerSetEACL) error {
|
|
|
|
cp, err := p.conn(ctx, prm.prmCommon)
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
var cliPrm client.PrmContainerSetEACL
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if prm.table != nil {
|
|
|
|
cliPrm.SetTable(*prm.table)
|
2021-11-03 14:34:02 +00:00
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
_, err = cp.client.ContainerSetEACL(ctx, cliPrm)
|
2021-11-17 11:22:32 +00:00
|
|
|
|
|
|
|
// here err already carries both status and client errors
|
|
|
|
|
2021-10-25 12:57:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// Balance requests current balance of the NeoFS account.
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) Balance(ctx context.Context, prm PrmBalanceGet) (*accounting.Decimal, error) {
|
|
|
|
cp, err := p.conn(ctx, prm.prmCommon)
|
2021-12-17 10:36:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
var cliPrm client.PrmBalanceGet
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
if prm.ownerID != nil {
|
|
|
|
cliPrm.SetAccount(*prm.ownerID)
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
res, err := cp.client.BalanceGet(ctx, cliPrm)
|
2022-01-11 17:17:31 +00:00
|
|
|
if err != nil { // here err already carries both status and client errors
|
2021-12-17 10:36:07 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Amount(), nil
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// WaitForContainerPresence waits until the container is found on the NeoFS network.
|
2022-03-09 09:40:23 +00:00
|
|
|
func (p *Pool) WaitForContainerPresence(ctx context.Context, cid *cid.ID, pollParams *ContainerPollingParams) error {
|
2021-06-22 09:15:21 +00:00
|
|
|
conn, _, err := p.Connection()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
wctx, cancel := context.WithTimeout(ctx, pollParams.CreationTimeout)
|
|
|
|
defer cancel()
|
|
|
|
ticker := time.NewTimer(pollParams.PollInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
wdone := wctx.Done()
|
|
|
|
done := ctx.Done()
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
var cliPrm client.PrmContainerGet
|
2021-12-02 12:21:54 +00:00
|
|
|
|
|
|
|
if cid != nil {
|
|
|
|
cliPrm.SetContainer(*cid)
|
|
|
|
}
|
|
|
|
|
2021-06-22 09:15:21 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return ctx.Err()
|
|
|
|
case <-wdone:
|
|
|
|
return wctx.Err()
|
|
|
|
case <-ticker.C:
|
2022-02-17 16:10:49 +00:00
|
|
|
_, err = conn.ContainerGet(ctx, cliPrm)
|
2021-06-22 09:15:21 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ticker.Reset(pollParams.PollInterval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
// Close closes the Pool and releases all the associated resources.
|
|
|
|
func (p *Pool) Close() {
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
p.cancel()
|
|
|
|
<-p.closedCh
|
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// creates new session token from SessionCreate call result.
|
2022-03-09 09:40:23 +00:00
|
|
|
func (p *Pool) newSessionToken(cliRes *client.ResSessionCreate) *session.Token {
|
2021-11-17 11:22:32 +00:00
|
|
|
return sessionTokenForOwner(p.owner, cliRes)
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// creates new session token with specified owner from SessionCreate call result.
|
|
|
|
func sessionTokenForOwner(id *owner.ID, cliRes *client.ResSessionCreate) *session.Token {
|
2021-11-17 11:22:32 +00:00
|
|
|
st := session.NewToken()
|
|
|
|
st.SetOwnerID(id)
|
|
|
|
st.SetID(cliRes.ID())
|
2021-12-02 12:21:54 +00:00
|
|
|
st.SetSessionKey(cliRes.PublicKey())
|
2021-11-17 11:22:32 +00:00
|
|
|
|
|
|
|
return st
|
|
|
|
}
|
2022-02-21 11:30:30 +00:00
|
|
|
|
|
|
|
func newAddressFromCnrID(cnrID *cid.ID) *address.Address {
|
|
|
|
addr := address.NewAddress()
|
|
|
|
addr.SetContainerID(cnrID)
|
|
|
|
return addr
|
|
|
|
}
|