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"
|
2022-03-15 12:00:38 +00:00
|
|
|
sdkClient "github.com/nspcc-dev/neofs-sdk-go/client"
|
2021-11-09 08:20:09 +00:00
|
|
|
"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"
|
2022-03-15 12:00:38 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2021-11-09 08:20:09 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
// client is a wrapper for sdkClient.Client to generate mock.
|
|
|
|
type client interface {
|
|
|
|
BalanceGet(context.Context, sdkClient.PrmBalanceGet) (*sdkClient.ResBalanceGet, error)
|
|
|
|
ContainerPut(context.Context, sdkClient.PrmContainerPut) (*sdkClient.ResContainerPut, error)
|
|
|
|
ContainerGet(context.Context, sdkClient.PrmContainerGet) (*sdkClient.ResContainerGet, error)
|
|
|
|
ContainerList(context.Context, sdkClient.PrmContainerList) (*sdkClient.ResContainerList, error)
|
|
|
|
ContainerDelete(context.Context, sdkClient.PrmContainerDelete) (*sdkClient.ResContainerDelete, error)
|
|
|
|
ContainerEACL(context.Context, sdkClient.PrmContainerEACL) (*sdkClient.ResContainerEACL, error)
|
|
|
|
ContainerSetEACL(context.Context, sdkClient.PrmContainerSetEACL) (*sdkClient.ResContainerSetEACL, error)
|
|
|
|
EndpointInfo(context.Context, sdkClient.PrmEndpointInfo) (*sdkClient.ResEndpointInfo, error)
|
|
|
|
NetworkInfo(context.Context, sdkClient.PrmNetworkInfo) (*sdkClient.ResNetworkInfo, error)
|
|
|
|
ObjectPutInit(context.Context, sdkClient.PrmObjectPutInit) (*sdkClient.ObjectWriter, error)
|
|
|
|
ObjectDelete(context.Context, sdkClient.PrmObjectDelete) (*sdkClient.ResObjectDelete, error)
|
|
|
|
ObjectGetInit(context.Context, sdkClient.PrmObjectGet) (*sdkClient.ObjectReader, error)
|
|
|
|
ObjectHead(context.Context, sdkClient.PrmObjectHead) (*sdkClient.ResObjectHead, error)
|
|
|
|
ObjectRangeInit(context.Context, sdkClient.PrmObjectRange) (*sdkClient.ObjectRangeReader, error)
|
|
|
|
ObjectSearchInit(context.Context, sdkClient.PrmObjectSearch) (*sdkClient.ObjectListReader, error)
|
|
|
|
SessionCreate(context.Context, sdkClient.PrmSessionCreate) (*sdkClient.ResSessionCreate, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitParameters contains values used to initialize connection Pool.
|
2022-03-11 11:45:36 +00:00
|
|
|
type InitParameters struct {
|
2022-03-15 12:49:14 +00:00
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
logger *zap.Logger
|
|
|
|
nodeDialTimeout time.Duration
|
|
|
|
healthcheckTimeout time.Duration
|
|
|
|
clientRebalanceInterval time.Duration
|
|
|
|
sessionTokenThreshold time.Duration
|
|
|
|
sessionExpirationDuration uint64
|
|
|
|
nodeParams []NodeParam
|
2022-03-11 11:45:36 +00:00
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
clientBuilder func(endpoint string) (client, error)
|
2022-03-11 11:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 12:49:14 +00:00
|
|
|
// SetKey specifies default key to be used for the protocol communication by default.
|
|
|
|
func (x *InitParameters) SetKey(key *ecdsa.PrivateKey) {
|
|
|
|
x.key = key
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLogger specifies logger.
|
|
|
|
func (x *InitParameters) SetLogger(logger *zap.Logger) {
|
|
|
|
x.logger = logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNodeDialTimeout specifies the timeout for connection to be established.
|
|
|
|
func (x *InitParameters) SetNodeDialTimeout(timeout time.Duration) {
|
|
|
|
x.nodeDialTimeout = timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetHealthcheckTimeout specifies the timeout for request to node to decide if it is alive.
|
|
|
|
//
|
|
|
|
// See also Pool.Dial.
|
|
|
|
func (x *InitParameters) SetHealthcheckTimeout(timeout time.Duration) {
|
|
|
|
x.healthcheckTimeout = timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetClientRebalanceInterval specifies the interval for updating nodes health status.
|
|
|
|
//
|
|
|
|
// See also Pool.Dial.
|
|
|
|
func (x *InitParameters) SetClientRebalanceInterval(interval time.Duration) {
|
|
|
|
x.clientRebalanceInterval = interval
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSessionThreshold specifies the max session token life time for PutObject operation.
|
|
|
|
func (x *InitParameters) SetSessionThreshold(threshold time.Duration) {
|
|
|
|
x.sessionTokenThreshold = threshold
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSessionExpirationDuration specifies the session token lifetime in epochs.
|
|
|
|
func (x *InitParameters) SetSessionExpirationDuration(expirationDuration uint64) {
|
|
|
|
x.sessionExpirationDuration = expirationDuration
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddNode append information about the node to which you want to connect.
|
|
|
|
func (x *InitParameters) AddNode(nodeParam NodeParam) {
|
|
|
|
x.nodeParams = append(x.nodeParams, nodeParam)
|
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
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-15 12:49:14 +00:00
|
|
|
priority int
|
|
|
|
address string
|
|
|
|
weight float64
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeParam creates NodeParam using parameters.
|
|
|
|
func NewNodeParam(priority int, address string, weight float64) (prm NodeParam) {
|
|
|
|
prm.SetPriority(priority)
|
|
|
|
prm.SetAddress(address)
|
|
|
|
prm.SetWeight(weight)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPriority specifies priority of the node.
|
|
|
|
// Negative value is allowed. In the result node groups
|
|
|
|
// with the same priority will be sorted by descent.
|
|
|
|
func (x *NodeParam) SetPriority(priority int) {
|
|
|
|
x.priority = priority
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress specifies address of the node.
|
|
|
|
func (x *NodeParam) SetAddress(address string) {
|
|
|
|
x.address = address
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWeight specifies weight of the node.
|
|
|
|
func (x *NodeParam) SetWeight(weight float64) {
|
|
|
|
x.weight = weight
|
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 {
|
2022-03-15 12:49:14 +00:00
|
|
|
timeout time.Duration
|
|
|
|
pollInterval time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTimeout specifies the time to wait for the operation to complete.
|
|
|
|
func (x *ContainerPollingParams) SetTimeout(timeout time.Duration) {
|
|
|
|
x.timeout = timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPollInterval specifies the interval, once it will check the completion of the operation.
|
|
|
|
func (x *ContainerPollingParams) SetPollInterval(tick time.Duration) {
|
|
|
|
x.pollInterval = tick
|
2021-06-22 09:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultPollingParams creates ContainerPollingParams with default values.
|
|
|
|
func DefaultPollingParams() *ContainerPollingParams {
|
|
|
|
return &ContainerPollingParams{
|
2022-03-15 12:49:14 +00:00
|
|
|
timeout: 120 * time.Second,
|
|
|
|
pollInterval: 5 * time.Second,
|
2021-06-22 09:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 08:48:01 +00:00
|
|
|
type clientPack struct {
|
2022-03-15 12:00:38 +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-17 09:53:04 +00:00
|
|
|
// UseSession specifies session within which operation should be performed.
|
2022-03-11 13:06:08 +00:00
|
|
|
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 {
|
2022-03-15 12:49:14 +00:00
|
|
|
cnr container.Container
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainer specifies structured information about new NeoFS container.
|
2022-03-15 12:49:14 +00:00
|
|
|
func (x *PrmContainerPut) SetContainer(cnr container.Container) {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.cnr = cnr
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerGet groups parameters of GetContainer operation.
|
|
|
|
type PrmContainerGet struct {
|
2022-03-15 12:49:14 +00:00
|
|
|
cnrID cid.ID
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainerID specifies identifier of the container to be read.
|
2022-03-15 12:49:14 +00:00
|
|
|
func (x *PrmContainerGet) SetContainerID(cnrID cid.ID) {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.cnrID = cnrID
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerList groups parameters of ListContainers operation.
|
|
|
|
type PrmContainerList struct {
|
2022-03-15 12:49:14 +00:00
|
|
|
ownerID owner.ID
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetOwnerID specifies identifier of the NeoFS account to list the containers.
|
2022-03-15 12:49:14 +00:00
|
|
|
func (x *PrmContainerList) SetOwnerID(ownerID owner.ID) {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.ownerID = ownerID
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerDelete groups parameters of DeleteContainer operation.
|
|
|
|
type PrmContainerDelete struct {
|
2022-03-17 09:53:04 +00:00
|
|
|
stoken session.Token
|
|
|
|
cnrID cid.ID
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainerID specifies identifier of the NeoFS container to be removed.
|
2022-03-15 12:49:14 +00:00
|
|
|
func (x *PrmContainerDelete) SetContainerID(cnrID cid.ID) {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.cnrID = cnrID
|
|
|
|
}
|
|
|
|
|
2022-03-17 09:53:04 +00:00
|
|
|
// SetSessionToken specifies session within which operation should be performed.
|
|
|
|
func (x *PrmContainerDelete) SetSessionToken(token session.Token) {
|
|
|
|
x.stoken = token
|
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
// PrmContainerEACL groups parameters of GetEACL operation.
|
|
|
|
type PrmContainerEACL struct {
|
2022-03-15 12:49:14 +00:00
|
|
|
cnrID cid.ID
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainerID specifies identifier of the NeoFS container to read the eACL table.
|
2022-03-15 12:49:14 +00:00
|
|
|
func (x *PrmContainerEACL) SetContainerID(cnrID cid.ID) {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.cnrID = cnrID
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerSetEACL groups parameters of SetEACL operation.
|
|
|
|
type PrmContainerSetEACL struct {
|
2022-03-15 12:49:14 +00:00
|
|
|
table eacl.Table
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetTable specifies eACL table structure to be set for the container.
|
2022-03-15 12:49:14 +00:00
|
|
|
func (x *PrmContainerSetEACL) SetTable(table eacl.Table) {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.table = table
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmBalanceGet groups parameters of Balance operation.
|
|
|
|
type PrmBalanceGet struct {
|
2022-03-15 12:49:14 +00:00
|
|
|
ownerID owner.ID
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetOwnerID specifies identifier of the NeoFS account for which the balance is requested.
|
2022-03-15 12:49:14 +00:00
|
|
|
func (x *PrmBalanceGet) SetOwnerID(ownerID owner.ID) {
|
2022-03-11 13:06:08 +00:00
|
|
|
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.
|
2022-03-15 12:00:38 +00:00
|
|
|
// It is designed to provide a convenient abstraction from the multiple sdkClient.client types.
|
2022-03-15 08:54:15 +00:00
|
|
|
//
|
|
|
|
// 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.
|
2022-03-15 12:00:38 +00:00
|
|
|
// Certain statuses can be checked using `sdkClient` and standard `errors` packages.
|
2022-03-15 08:54:15 +00:00
|
|
|
// Note that package provides some helper functions to work with status returns
|
2022-03-15 12:00:38 +00:00
|
|
|
// (e.g. sdkClient.IsErrContainerNotFound, sdkClient.IsErrObjectNotFound).
|
2022-03-15 08:54:15 +00:00
|
|
|
//
|
|
|
|
// 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
|
2022-03-15 12:00:38 +00:00
|
|
|
clientBuilder func(endpoint string) (client, error)
|
2022-03-11 14:55:02 +00:00
|
|
|
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-15 12:49:14 +00:00
|
|
|
// NewPool creates connection pool using parameters.
|
2022-03-11 14:55:02 +00:00
|
|
|
func NewPool(options InitParameters) (*Pool, error) {
|
2022-03-15 12:49:14 +00:00
|
|
|
if options.key == nil {
|
2022-03-11 11:45:36 +00:00
|
|
|
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-15 12:49:14 +00:00
|
|
|
nodesParams, err := adjustNodeParams(options.nodeParams)
|
2022-03-11 11:45:36 +00:00
|
|
|
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{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: options.key,
|
|
|
|
owner: owner.NewIDFromPublicKey(&options.key.PublicKey),
|
2022-03-11 14:55:02 +00:00
|
|
|
cache: cache,
|
2022-03-15 12:49:14 +00:00
|
|
|
logger: options.logger,
|
|
|
|
stokenDuration: options.sessionExpirationDuration,
|
|
|
|
stokenThreshold: options.sessionTokenThreshold,
|
2022-03-11 14:55:02 +00:00
|
|
|
rebalanceParams: rebalanceParameters{
|
|
|
|
nodesParams: nodesParams,
|
2022-03-15 12:49:14 +00:00
|
|
|
nodeRequestTimeout: options.healthcheckTimeout,
|
|
|
|
clientRebalanceInterval: options.clientRebalanceInterval,
|
|
|
|
sessionExpirationDuration: options.sessionExpirationDuration,
|
2022-03-11 14:55:02 +00:00
|
|
|
},
|
|
|
|
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.
|
2022-03-15 12:49:14 +00:00
|
|
|
// It also starts a routine that checks the health of the nodes and
|
|
|
|
// updates the weights of the nodes for balancing.
|
|
|
|
// Returns an error describing failure reason.
|
|
|
|
//
|
|
|
|
// If failed, the Pool SHOULD NOT be used.
|
|
|
|
//
|
|
|
|
// See also InitParameters.SetClientRebalanceInterval.
|
2022-03-11 14:55:02 +00:00
|
|
|
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) {
|
2022-03-15 12:49:14 +00:00
|
|
|
if params.sessionExpirationDuration == 0 {
|
|
|
|
params.sessionExpirationDuration = defaultSessionTokenExpirationDuration
|
2022-03-11 11:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 12:49:14 +00:00
|
|
|
if params.sessionTokenThreshold <= 0 {
|
|
|
|
params.sessionTokenThreshold = defaultSessionTokenThreshold
|
2022-03-11 11:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 12:49:14 +00:00
|
|
|
if params.clientRebalanceInterval <= 0 {
|
|
|
|
params.clientRebalanceInterval = defaultRebalanceInterval
|
2022-03-11 11:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 12:49:14 +00:00
|
|
|
if params.healthcheckTimeout <= 0 {
|
|
|
|
params.healthcheckTimeout = defaultRequestTimeout
|
2022-03-11 11:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if params.clientBuilder == nil {
|
2022-03-15 12:00:38 +00:00
|
|
|
params.clientBuilder = func(addr string) (client, error) {
|
|
|
|
var c sdkClient.Client
|
2022-03-11 11:45:36 +00:00
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
var prmInit sdkClient.PrmInit
|
2022-03-11 11:45:36 +00:00
|
|
|
prmInit.ResolveNeoFSFailures()
|
2022-03-15 12:49:14 +00:00
|
|
|
prmInit.SetDefaultPrivateKey(*params.key)
|
2022-03-11 11:45:36 +00:00
|
|
|
|
|
|
|
c.Init(prmInit)
|
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
var prmDial sdkClient.PrmDial
|
2022-03-11 11:45:36 +00:00
|
|
|
prmDial.SetServerURI(addr)
|
2022-03-15 12:49:14 +00:00
|
|
|
prmDial.SetTimeout(params.nodeDialTimeout)
|
2022-03-11 11:45:36 +00:00
|
|
|
|
|
|
|
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 {
|
2022-03-15 12:49:14 +00:00
|
|
|
nodes, ok := nodesParamsMap[param.priority]
|
2022-03-11 11:45:36 +00:00
|
|
|
if !ok {
|
2022-03-15 12:49:14 +00:00
|
|
|
nodes = &nodesParam{priority: param.priority}
|
2022-03-11 11:45:36 +00:00
|
|
|
}
|
2022-03-15 12:49:14 +00:00
|
|
|
nodes.addresses = append(nodes.addresses, param.address)
|
|
|
|
nodes.weights = append(nodes.weights, param.weight)
|
|
|
|
nodesParamsMap[param.priority] = nodes
|
2022-03-11 11:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-03-15 12:00:38 +00:00
|
|
|
var prmEndpoint sdkClient.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)
|
2022-03-15 12:00:38 +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() (*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-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-03-15 12:00:38 +00:00
|
|
|
func createSessionTokenForDuration(ctx context.Context, c client, dur uint64) (*sdkClient.ResSessionCreate, error) {
|
|
|
|
ni, err := c.NetworkInfo(ctx, sdkClient.PrmNetworkInfo{})
|
2022-02-02 13:25:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
epoch := ni.Info().CurrentEpoch()
|
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
var prm sdkClient.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
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
client client
|
2022-02-07 20:27:56 +00:00
|
|
|
|
|
|
|
// 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-15 12:00:38 +00:00
|
|
|
var cliPrm sdkClient.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-15 12:00:38 +00:00
|
|
|
var cliPrm sdkClient.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-04-06 06:19:05 +00:00
|
|
|
if prm.btoken != nil {
|
|
|
|
cliPrm.WithBearerToken(*prm.btoken)
|
|
|
|
}
|
|
|
|
|
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-03-15 12:00:38 +00:00
|
|
|
type objectReadCloser sdkClient.ObjectReader
|
2022-02-07 20:27:56 +00:00
|
|
|
|
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) {
|
2022-03-15 12:00:38 +00:00
|
|
|
return (*sdkClient.ObjectReader)(x).Read(p)
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-03-15 12:00:38 +00:00
|
|
|
_, err := (*sdkClient.ObjectReader)(x).Close()
|
2022-02-07 20:27:56 +00:00
|
|
|
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-15 12:00:38 +00:00
|
|
|
var cliPrm sdkClient.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-04-06 06:19:05 +00:00
|
|
|
if prm.btoken != nil {
|
|
|
|
cliPrm.WithBearerToken(*prm.btoken)
|
|
|
|
}
|
|
|
|
|
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-15 12:00:38 +00:00
|
|
|
var cliPrm sdkClient.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-04-06 06:19:05 +00:00
|
|
|
if prm.btoken != nil {
|
|
|
|
cliPrm.WithBearerToken(*prm.btoken)
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-03-15 12:00:38 +00:00
|
|
|
payload *sdkClient.ObjectRangeReader
|
2022-02-12 09:26:38 +00:00
|
|
|
}
|
|
|
|
|
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-15 12:00:38 +00:00
|
|
|
var cliPrm sdkClient.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-04-06 06:19:05 +00:00
|
|
|
if prm.btoken != nil {
|
|
|
|
cliPrm.WithBearerToken(*prm.btoken)
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-03-15 12:00:38 +00:00
|
|
|
r *sdkClient.ObjectListReader
|
2022-02-12 20:30:16 +00:00
|
|
|
}
|
|
|
|
|
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-15 12:00:38 +00:00
|
|
|
var cliPrm sdkClient.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
|
|
|
|
2022-04-06 06:19:05 +00:00
|
|
|
if prm.btoken != nil {
|
|
|
|
cliPrm.WithBearerToken(*prm.btoken)
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-03-17 09:53:04 +00:00
|
|
|
cp, err := p.connection()
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
var cliPrm sdkClient.PrmContainerPut
|
2022-03-15 12:49:14 +00:00
|
|
|
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) {
|
2022-03-17 09:53:04 +00:00
|
|
|
cp, err := p.connection()
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
var cliPrm sdkClient.PrmContainerGet
|
2022-03-15 12:49:14 +00:00
|
|
|
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) {
|
2022-03-17 09:53:04 +00:00
|
|
|
cp, err := p.connection()
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
var cliPrm sdkClient.PrmContainerList
|
2022-03-15 12:49:14 +00:00
|
|
|
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 {
|
2022-03-17 09:53:04 +00:00
|
|
|
cp, err := p.connection()
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
var cliPrm sdkClient.PrmContainerDelete
|
2022-03-15 12:49:14 +00:00
|
|
|
cliPrm.SetContainer(prm.cnrID)
|
2022-03-17 09:53:04 +00:00
|
|
|
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) {
|
2022-03-17 09:53:04 +00:00
|
|
|
cp, err := p.connection()
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
var cliPrm sdkClient.PrmContainerEACL
|
2022-03-15 12:49:14 +00:00
|
|
|
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 {
|
2022-03-17 09:53:04 +00:00
|
|
|
cp, err := p.connection()
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
var cliPrm sdkClient.PrmContainerSetEACL
|
2022-03-15 12:49:14 +00:00
|
|
|
cliPrm.SetTable(prm.table)
|
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) {
|
2022-03-17 09:53:04 +00:00
|
|
|
cp, err := p.connection()
|
2021-12-17 10:36:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
var cliPrm sdkClient.PrmBalanceGet
|
2022-03-15 12:49:14 +00:00
|
|
|
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-16 13:06:43 +00:00
|
|
|
func WaitForContainerPresence(ctx context.Context, pool *Pool, cid *cid.ID, pollParams *ContainerPollingParams) error {
|
2022-03-15 12:49:14 +00:00
|
|
|
wctx, cancel := context.WithTimeout(ctx, pollParams.timeout)
|
2021-06-22 09:15:21 +00:00
|
|
|
defer cancel()
|
2022-03-15 12:49:14 +00:00
|
|
|
ticker := time.NewTimer(pollParams.pollInterval)
|
2021-06-22 09:15:21 +00:00
|
|
|
defer ticker.Stop()
|
|
|
|
wdone := wctx.Done()
|
|
|
|
done := ctx.Done()
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-03-16 13:06:43 +00:00
|
|
|
var prm PrmContainerGet
|
2021-12-02 12:21:54 +00:00
|
|
|
if cid != nil {
|
2022-03-16 13:06:43 +00:00
|
|
|
prm.SetContainerID(*cid)
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 09:15:21 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return ctx.Err()
|
|
|
|
case <-wdone:
|
|
|
|
return wctx.Err()
|
|
|
|
case <-ticker.C:
|
2022-03-16 13:06:43 +00:00
|
|
|
_, err := pool.GetContainer(ctx, prm)
|
2021-06-22 09:15:21 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-15 12:49:14 +00:00
|
|
|
ticker.Reset(pollParams.pollInterval)
|
2021-06-22 09:15:21 +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
|
|
|
|
2022-03-15 12:49:14 +00:00
|
|
|
// NetworkInfo requests information about the NeoFS network of which the remote server is a part.
|
2022-03-15 12:00:38 +00:00
|
|
|
func (p *Pool) NetworkInfo(ctx context.Context) (*netmap.NetworkInfo, error) {
|
|
|
|
cp, err := p.connection()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := cp.client.NetworkInfo(ctx, sdkClient.PrmNetworkInfo{})
|
|
|
|
if err != nil { // here err already carries both status and client errors
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Info(), nil
|
|
|
|
}
|
|
|
|
|
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-15 12:00:38 +00:00
|
|
|
func (p *Pool) newSessionToken(cliRes *sdkClient.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.
|
2022-03-15 12:00:38 +00:00
|
|
|
func sessionTokenForOwner(id *owner.ID, cliRes *sdkClient.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
|
|
|
|
}
|