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-09 09:40:23 +00:00
|
|
|
// BuilderOptions contains options used to build connection Pool.
|
2021-05-25 11:46:58 +00:00
|
|
|
type BuilderOptions 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
|
|
|
|
nodesParams []*NodesParam
|
2022-03-07 11:39:49 +00:00
|
|
|
clientBuilder func(endpoint string) (Client, error)
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 13:50:33 +00:00
|
|
|
type NodesParam struct {
|
|
|
|
priority int
|
|
|
|
addresses []string
|
|
|
|
weights []float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type NodeParam struct {
|
|
|
|
priority int
|
|
|
|
address string
|
|
|
|
weight float64
|
|
|
|
}
|
|
|
|
|
2021-05-25 11:46:58 +00:00
|
|
|
// Builder is an interim structure used to collect node addresses/weights and
|
2022-03-09 09:40:23 +00:00
|
|
|
// build connection Pool subsequently.
|
2021-05-25 11:46:58 +00:00
|
|
|
type Builder struct {
|
2021-11-16 13:50:33 +00:00
|
|
|
nodeParams []NodeParam
|
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
|
|
|
// AddNode adds address/weight pair to node PoolBuilder list.
|
2021-11-16 13:50:33 +00:00
|
|
|
func (pb *Builder) AddNode(address string, priority int, weight float64) *Builder {
|
|
|
|
pb.nodeParams = append(pb.nodeParams, NodeParam{
|
|
|
|
address: address,
|
|
|
|
priority: priority,
|
|
|
|
weight: weight,
|
|
|
|
})
|
2021-05-25 08:48:01 +00:00
|
|
|
return pb
|
|
|
|
}
|
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
// Build creates new Pool based on current PoolBuilder state and options.
|
|
|
|
func (pb *Builder) Build(ctx context.Context, options *BuilderOptions) (*Pool, error) {
|
2021-11-16 13:50:33 +00:00
|
|
|
if len(pb.nodeParams) == 0 {
|
2021-05-25 08:48:01 +00:00
|
|
|
return nil, errors.New("no NeoFS peers configured")
|
|
|
|
}
|
2021-06-09 11:56:11 +00:00
|
|
|
|
2021-11-16 13:50:33 +00:00
|
|
|
nodesParams := make(map[int]*NodesParam)
|
|
|
|
for _, param := range pb.nodeParams {
|
|
|
|
nodes, ok := nodesParams[param.priority]
|
|
|
|
if !ok {
|
|
|
|
nodes = &NodesParam{priority: param.priority}
|
|
|
|
}
|
|
|
|
nodes.addresses = append(nodes.addresses, param.address)
|
|
|
|
nodes.weights = append(nodes.weights, param.weight)
|
|
|
|
nodesParams[param.priority] = nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, nodes := range nodesParams {
|
|
|
|
nodes.weights = adjustWeights(nodes.weights)
|
|
|
|
options.nodesParams = append(options.nodesParams, nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(options.nodesParams, func(i, j int) bool {
|
|
|
|
return options.nodesParams[i].priority < options.nodesParams[j].priority
|
|
|
|
})
|
2021-07-20 08:02:14 +00:00
|
|
|
|
2021-05-28 20:12:58 +00:00
|
|
|
return newPool(ctx, options)
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-15 06:35:30 +00:00
|
|
|
type CallOption func(config *callConfig)
|
|
|
|
|
|
|
|
type callConfig struct {
|
2021-12-16 15:46:38 +00:00
|
|
|
useDefaultSession bool
|
2022-02-21 11:30:30 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithKey(key *ecdsa.PrivateKey) CallOption {
|
|
|
|
return func(config *callConfig) {
|
|
|
|
config.key = key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithBearer(token *token.BearerToken) CallOption {
|
|
|
|
return func(config *callConfig) {
|
|
|
|
config.btoken = token
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithSession(token *session.Token) CallOption {
|
|
|
|
return func(config *callConfig) {
|
|
|
|
config.stoken = token
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 15:46:38 +00:00
|
|
|
func useDefaultSession() CallOption {
|
|
|
|
return func(config *callConfig) {
|
|
|
|
config.useDefaultSession = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 11:30:30 +00:00
|
|
|
func useAddress(addr *address.Address) CallOption {
|
|
|
|
return func(config *callConfig) {
|
|
|
|
config.addr = addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func useVerb(verb sessionv2.ObjectSessionVerb) CallOption {
|
|
|
|
return func(config *callConfig) {
|
|
|
|
config.verb = verb
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 06:35:30 +00:00
|
|
|
func cfgFromOpts(opts ...CallOption) *callConfig {
|
|
|
|
var cfg = new(callConfig)
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(cfg)
|
|
|
|
}
|
|
|
|
return cfg
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
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{}
|
|
|
|
cache *SessionCache
|
|
|
|
stokenDuration uint64
|
|
|
|
stokenThreshold time.Duration
|
2021-11-16 13:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type innerPool struct {
|
2021-05-25 08:48:01 +00:00
|
|
|
lock sync.RWMutex
|
|
|
|
sampler *Sampler
|
|
|
|
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-02-02 11:21:23 +00:00
|
|
|
)
|
2022-02-02 13:45:01 +00:00
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
func newPool(ctx context.Context, options *BuilderOptions) (*Pool, error) {
|
2021-11-08 13:28:28 +00:00
|
|
|
cache, err := NewCache()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't create cache: %w", err)
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
|
2022-02-10 12:59:31 +00:00
|
|
|
if options.SessionExpirationDuration == 0 {
|
2022-02-11 14:40:09 +00:00
|
|
|
options.SessionExpirationDuration = defaultSessionTokenExpirationDuration
|
2022-02-10 12:59:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 11:21:23 +00:00
|
|
|
if options.SessionTokenThreshold <= 0 {
|
2022-02-11 14:40:09 +00:00
|
|
|
options.SessionTokenThreshold = defaultSessionTokenThreshold
|
2022-02-02 11:21:23 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 13:16:35 +00:00
|
|
|
ownerID := owner.NewIDFromPublicKey(&options.Key.PublicKey)
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2021-11-16 13:50:33 +00:00
|
|
|
inner := make([]*innerPool, len(options.nodesParams))
|
2021-09-07 14:34:20 +00:00
|
|
|
var atLeastOneHealthy bool
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-03-07 11:39:49 +00:00
|
|
|
if options.clientBuilder == nil {
|
|
|
|
options.clientBuilder = func(addr string) (Client, error) {
|
|
|
|
var c client.Client
|
|
|
|
|
|
|
|
var prmInit client.PrmInit
|
|
|
|
prmInit.ResolveNeoFSFailures()
|
|
|
|
prmInit.SetDefaultPrivateKey(*options.Key)
|
|
|
|
|
|
|
|
c.Init(prmInit)
|
|
|
|
|
|
|
|
var prmDial client.PrmDial
|
|
|
|
prmDial.SetServerURI(addr)
|
|
|
|
prmDial.SetTimeout(options.NodeConnectionTimeout)
|
|
|
|
|
|
|
|
return &c, c.Dial(prmDial)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 13:50:33 +00:00
|
|
|
for i, params := range options.nodesParams {
|
|
|
|
clientPacks := make([]*clientPack, len(params.weights))
|
2022-02-07 20:27:56 +00:00
|
|
|
for j, addr := range params.addresses {
|
2022-03-07 11:39:49 +00:00
|
|
|
c, err := options.clientBuilder(addr)
|
2021-11-16 13:50:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var healthy bool
|
2022-02-02 13:25:36 +00:00
|
|
|
cliRes, err := createSessionTokenForDuration(ctx, c, options.SessionExpirationDuration)
|
2021-11-16 13:50:33 +00:00
|
|
|
if err != nil && options.Logger != nil {
|
|
|
|
options.Logger.Warn("failed to create neofs session token for client",
|
2022-02-07 20:27:56 +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
|
|
|
|
st := sessionTokenForOwner(ownerID, cliRes)
|
2022-02-21 11:30:30 +00:00
|
|
|
_ = cache.Put(formCacheKey(addr, options.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())
|
|
|
|
sampler := NewSampler(params.weights, source)
|
|
|
|
|
|
|
|
inner[i] = &innerPool{
|
|
|
|
sampler: sampler,
|
|
|
|
clientPacks: clientPacks,
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
2021-09-07 14:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !atLeastOneHealthy {
|
|
|
|
return nil, 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-09 09:40:23 +00:00
|
|
|
pool := &Pool{
|
2022-02-02 11:21:23 +00:00
|
|
|
innerPools: inner,
|
|
|
|
key: options.Key,
|
|
|
|
owner: ownerID,
|
|
|
|
cancel: cancel,
|
|
|
|
closedCh: make(chan struct{}),
|
|
|
|
cache: cache,
|
|
|
|
stokenDuration: options.SessionExpirationDuration,
|
|
|
|
stokenThreshold: options.SessionTokenThreshold,
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
2021-06-21 11:12:08 +00:00
|
|
|
go startRebalance(ctx, pool, options)
|
|
|
|
return pool, nil
|
|
|
|
}
|
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
func startRebalance(ctx context.Context, p *Pool, options *BuilderOptions) {
|
2021-06-21 11:12:08 +00:00
|
|
|
ticker := time.NewTimer(options.ClientRebalanceInterval)
|
2021-11-16 13:50:33 +00:00
|
|
|
buffers := make([][]float64, len(options.nodesParams))
|
|
|
|
for i, params := range options.nodesParams {
|
|
|
|
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:
|
2021-11-16 13:50:33 +00:00
|
|
|
updateNodesHealth(ctx, p, options, buffers)
|
2021-07-20 08:02:14 +00:00
|
|
|
ticker.Reset(options.ClientRebalanceInterval)
|
|
|
|
}
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
func updateNodesHealth(ctx context.Context, p *Pool, options *BuilderOptions, 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()
|
|
|
|
updateInnerNodesHealth(ctx, p, i, options, bufferWeights)
|
|
|
|
}(i, inner)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
func updateInnerNodesHealth(ctx context.Context, pool *Pool, i int, options *BuilderOptions, bufferWeights []float64) {
|
2021-11-16 13:50:33 +00:00
|
|
|
if i > len(pool.innerPools)-1 {
|
|
|
|
return
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
2021-11-16 13:50:33 +00:00
|
|
|
p := pool.innerPools[i]
|
|
|
|
|
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
|
|
|
|
2021-11-16 13:50:33 +00:00
|
|
|
for j, cPack := range p.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
|
2021-06-21 11:12:08 +00:00
|
|
|
tctx, c := context.WithTimeout(ctx, options.NodeRequestTimeout)
|
|
|
|
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
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
p.lock.RLock()
|
2021-11-16 13:50:33 +00:00
|
|
|
cp := *p.clientPacks[j]
|
2021-10-25 12:57:55 +00:00
|
|
|
p.lock.RUnlock()
|
|
|
|
|
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-02-02 13:25:36 +00:00
|
|
|
cliRes, err := createSessionTokenForDuration(ctx, cli, options.SessionExpirationDuration)
|
|
|
|
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 {
|
2021-11-16 13:50:33 +00:00
|
|
|
tkn := pool.newSessionToken(cliRes)
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2021-11-16 13:50:33 +00:00
|
|
|
_ = pool.cache.Put(formCacheKey(cp.address, pool.key), tkn)
|
2021-07-28 08:42:55 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
} else {
|
2021-11-16 13:50:33 +00:00
|
|
|
pool.cache.DeleteByPrefix(cp.address)
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
2021-06-21 11:12:08 +00:00
|
|
|
|
|
|
|
p.lock.Lock()
|
2021-11-16 13:50:33 +00:00
|
|
|
if p.clientPacks[j].healthy != ok {
|
|
|
|
p.clientPacks[j].healthy = ok
|
2021-06-21 11:12:08 +00:00
|
|
|
healthyChanged = true
|
|
|
|
}
|
|
|
|
p.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())
|
|
|
|
p.lock.Lock()
|
|
|
|
p.sampler = NewSampler(probabilities, source)
|
|
|
|
p.lock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09 09:40:23 +00:00
|
|
|
func (p *Pool) conn(ctx context.Context, cfg *callConfig) (*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
|
2021-12-16 15:46:38 +00:00
|
|
|
if sessionToken == nil && cfg.useDefaultSession {
|
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-09 09:40:23 +00:00
|
|
|
func (p *Pool) removeSessionTokenAfterThreshold(cfg *callConfig) 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-09 09:40:23 +00:00
|
|
|
func (p *Pool) initCallContext(ctx *callContext, cfg *callConfig) 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
|
|
|
|
ctx.sessionDefault = cfg.stoken == nil && cfg.useDefaultSession
|
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-09 09:40:23 +00:00
|
|
|
func (p *Pool) initCallContextWithRetry(ctx *callContextWithRetry, cfg *callConfig) 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-09 09:40:23 +00:00
|
|
|
func (p *Pool) PutObject(ctx context.Context, hdr object.Object, payload io.Reader, opts ...CallOption) (*oid.ID, error) {
|
2022-02-21 11:30:30 +00:00
|
|
|
cfg := cfgFromOpts(append(opts,
|
|
|
|
useDefaultSession(),
|
|
|
|
useVerb(sessionv2.ObjectVerbPut),
|
|
|
|
useAddress(newAddressFromCnrID(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.
|
|
|
|
err := p.removeSessionTokenAfterThreshold(cfg)
|
|
|
|
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-02-11 14:40:09 +00:00
|
|
|
err = p.initCallContext(&ctxCall, cfg)
|
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-02-07 20:27:56 +00:00
|
|
|
var prm client.PrmObjectPutInit
|
|
|
|
|
|
|
|
wObj, err := ctxCall.client.ObjectPutInit(ctx, prm)
|
|
|
|
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-02-07 20:27:56 +00:00
|
|
|
if cfg.btoken != nil {
|
|
|
|
wObj.WithBearerToken(*cfg.btoken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if wObj.WriteHeader(hdr) {
|
|
|
|
sz := hdr.PayloadSize()
|
|
|
|
|
|
|
|
if data := hdr.Payload(); len(data) > 0 {
|
|
|
|
if payload != nil {
|
|
|
|
payload = io.MultiReader(bytes.NewReader(data), payload)
|
|
|
|
} else {
|
|
|
|
payload = bytes.NewReader(data)
|
|
|
|
sz = uint64(len(data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if 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 {
|
|
|
|
n, err = payload.Read(buf)
|
|
|
|
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-09 09:40:23 +00:00
|
|
|
func (p *Pool) DeleteObject(ctx context.Context, addr address.Address, opts ...CallOption) error {
|
2022-02-21 11:30:30 +00:00
|
|
|
cfg := cfgFromOpts(append(opts,
|
|
|
|
useDefaultSession(),
|
|
|
|
useVerb(sessionv2.ObjectVerbDelete),
|
|
|
|
useAddress(&addr))...)
|
2022-02-15 05:36:47 +00:00
|
|
|
|
|
|
|
var prm client.PrmObjectDelete
|
|
|
|
|
|
|
|
var cc callContextWithRetry
|
|
|
|
|
|
|
|
cc.Context = ctx
|
|
|
|
cc.sessionTarget = prm.WithinSession
|
|
|
|
|
|
|
|
err := p.initCallContextWithRetry(&cc, cfg)
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-02-15 05:36:47 +00:00
|
|
|
if cnr := addr.ContainerID(); cnr != nil {
|
|
|
|
prm.FromContainer(*cnr)
|
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-02-15 05:36:47 +00:00
|
|
|
if obj := addr.ObjectID(); obj != nil {
|
|
|
|
prm.ByID(*obj)
|
2021-11-03 14:34:02 +00:00
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-02-21 12:10:16 +00:00
|
|
|
prm.UseKey(*cc.key)
|
|
|
|
|
2022-02-15 05:36:47 +00:00
|
|
|
return p.callWithRetry(&cc, func() error {
|
|
|
|
_, err := cc.client.ObjectDelete(ctx, prm)
|
|
|
|
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
|
|
|
|
|
|
|
|
func (x *objectReadCloser) Read(p []byte) (int, error) {
|
|
|
|
return (*client.ObjectReader)(x).Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *objectReadCloser) Close() error {
|
|
|
|
_, err := (*client.ObjectReader)(x).Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResGetObject struct {
|
|
|
|
Header object.Object
|
|
|
|
|
|
|
|
Payload io.ReadCloser
|
|
|
|
}
|
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
func (p *Pool) GetObject(ctx context.Context, addr address.Address, opts ...CallOption) (*ResGetObject, error) {
|
2022-02-21 11:30:30 +00:00
|
|
|
cfg := cfgFromOpts(append(opts,
|
|
|
|
useDefaultSession(),
|
|
|
|
useVerb(sessionv2.ObjectVerbGet),
|
|
|
|
useAddress(&addr))...)
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
var prm client.PrmObjectGet
|
|
|
|
|
|
|
|
var cc callContextWithRetry
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
cc.Context = ctx
|
|
|
|
cc.sessionTarget = prm.WithinSession
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
err := p.initCallContextWithRetry(&cc, cfg)
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
if cnr := addr.ContainerID(); cnr != nil {
|
|
|
|
prm.FromContainer(*cnr)
|
2021-11-03 14:34:02 +00:00
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
if obj := addr.ObjectID(); obj != nil {
|
|
|
|
prm.ByID(*obj)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
rObj, err := cc.client.ObjectGetInit(ctx, prm)
|
|
|
|
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-09 09:40:23 +00:00
|
|
|
func (p *Pool) HeadObject(ctx context.Context, addr address.Address, opts ...CallOption) (*object.Object, error) {
|
2022-02-21 11:30:30 +00:00
|
|
|
cfg := cfgFromOpts(append(opts,
|
|
|
|
useDefaultSession(),
|
|
|
|
useVerb(sessionv2.ObjectVerbHead),
|
|
|
|
useAddress(&addr))...)
|
2022-02-11 16:13:53 +00:00
|
|
|
|
|
|
|
var prm client.PrmObjectHead
|
|
|
|
|
|
|
|
var cc callContextWithRetry
|
|
|
|
|
|
|
|
cc.Context = ctx
|
|
|
|
cc.sessionTarget = prm.WithinSession
|
|
|
|
|
|
|
|
err := p.initCallContextWithRetry(&cc, cfg)
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-02-11 16:13:53 +00:00
|
|
|
if cnr := addr.ContainerID(); cnr != nil {
|
|
|
|
prm.FromContainer(*cnr)
|
2021-11-03 14:34:02 +00:00
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-02-11 16:13:53 +00:00
|
|
|
if obj := addr.ObjectID(); obj != nil {
|
|
|
|
prm.ByID(*obj)
|
2021-11-17 11:22:32 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 12:10:16 +00:00
|
|
|
prm.UseKey(*cc.key)
|
|
|
|
|
2022-02-11 16:13:53 +00:00
|
|
|
var obj object.Object
|
|
|
|
|
|
|
|
err = p.callWithRetry(&cc, func() error {
|
|
|
|
res, err := cc.client.ObjectHead(ctx, prm)
|
|
|
|
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-02-12 09:26:38 +00:00
|
|
|
type ResObjectRange struct {
|
|
|
|
payload *client.ObjectRangeReader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *ResObjectRange) Read(p []byte) (int, error) {
|
|
|
|
return x.payload.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *ResObjectRange) Close() error {
|
|
|
|
_, err := x.payload.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
func (p *Pool) ObjectRange(ctx context.Context, addr address.Address, off, ln uint64, opts ...CallOption) (*ResObjectRange, error) {
|
2022-02-21 11:30:30 +00:00
|
|
|
cfg := cfgFromOpts(append(opts,
|
|
|
|
useDefaultSession(),
|
|
|
|
useVerb(sessionv2.ObjectVerbRange),
|
|
|
|
useAddress(&addr))...)
|
2022-02-12 09:26:38 +00:00
|
|
|
|
|
|
|
var prm client.PrmObjectRange
|
|
|
|
|
|
|
|
prm.SetOffset(off)
|
|
|
|
prm.SetLength(ln)
|
|
|
|
|
|
|
|
var cc callContextWithRetry
|
|
|
|
|
|
|
|
cc.Context = ctx
|
|
|
|
cc.sessionTarget = prm.WithinSession
|
|
|
|
|
|
|
|
err := p.initCallContextWithRetry(&cc, cfg)
|
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 09:26:38 +00:00
|
|
|
if cnr := addr.ContainerID(); cnr != nil {
|
|
|
|
prm.FromContainer(*cnr)
|
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-02-12 09:26:38 +00:00
|
|
|
if obj := addr.ObjectID(); obj != nil {
|
|
|
|
prm.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
|
|
|
|
|
|
|
|
res.payload, err = cc.client.ObjectRangeInit(ctx, prm)
|
|
|
|
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-02-12 20:30:16 +00:00
|
|
|
type ResObjectSearch struct {
|
|
|
|
r *client.ObjectListReader
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-25 10:20:15 +00:00
|
|
|
func (x *ResObjectSearch) Iterate(f func(oid.ID) bool) error {
|
|
|
|
return x.r.Iterate(f)
|
|
|
|
}
|
|
|
|
|
2022-02-12 20:30:16 +00:00
|
|
|
func (x *ResObjectSearch) Close() {
|
|
|
|
_, _ = x.r.Close()
|
|
|
|
}
|
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
func (p *Pool) SearchObjects(ctx context.Context, idCnr cid.ID, filters object.SearchFilters, opts ...CallOption) (*ResObjectSearch, error) {
|
2022-02-21 11:30:30 +00:00
|
|
|
cfg := cfgFromOpts(append(opts,
|
|
|
|
useDefaultSession(),
|
|
|
|
useVerb(sessionv2.ObjectVerbSearch),
|
|
|
|
useAddress(newAddressFromCnrID(&idCnr)))...)
|
2022-02-12 20:30:16 +00:00
|
|
|
|
|
|
|
var prm client.PrmObjectSearch
|
|
|
|
|
|
|
|
prm.InContainer(idCnr)
|
|
|
|
prm.SetFilters(filters)
|
|
|
|
|
|
|
|
var cc callContextWithRetry
|
|
|
|
|
|
|
|
cc.Context = ctx
|
|
|
|
cc.sessionTarget = prm.WithinSession
|
|
|
|
|
|
|
|
err := p.initCallContextWithRetry(&cc, cfg)
|
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-02-12 20:30:16 +00:00
|
|
|
res.r, err = cc.client.ObjectSearchInit(ctx, prm)
|
|
|
|
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-09 09:40:23 +00:00
|
|
|
func (p *Pool) PutContainer(ctx context.Context, cnr *container.Container, opts ...CallOption) (*cid.ID, error) {
|
2021-11-15 06:35:30 +00:00
|
|
|
cfg := cfgFromOpts(opts...)
|
2022-02-16 16:00:38 +00:00
|
|
|
cp, err := p.conn(ctx, cfg)
|
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
|
|
|
|
|
|
|
if cnr != nil {
|
|
|
|
cliPrm.SetContainer(*cnr)
|
|
|
|
}
|
|
|
|
|
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-09 09:40:23 +00:00
|
|
|
func (p *Pool) GetContainer(ctx context.Context, cid *cid.ID, opts ...CallOption) (*container.Container, error) {
|
2021-11-15 06:35:30 +00:00
|
|
|
cfg := cfgFromOpts(opts...)
|
2022-02-16 16:00:38 +00:00
|
|
|
cp, err := p.conn(ctx, cfg)
|
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
|
|
|
|
|
|
|
if cid != nil {
|
|
|
|
cliPrm.SetContainer(*cid)
|
|
|
|
}
|
|
|
|
|
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-09 09:40:23 +00:00
|
|
|
func (p *Pool) ListContainers(ctx context.Context, ownerID *owner.ID, opts ...CallOption) ([]*cid.ID, error) {
|
2021-11-15 06:35:30 +00:00
|
|
|
cfg := cfgFromOpts(opts...)
|
2022-02-16 16:00:38 +00:00
|
|
|
cp, err := p.conn(ctx, cfg)
|
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
|
|
|
|
|
|
|
if ownerID != nil {
|
|
|
|
cliPrm.SetAccount(*ownerID)
|
|
|
|
}
|
|
|
|
|
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-09 09:40:23 +00:00
|
|
|
func (p *Pool) DeleteContainer(ctx context.Context, cid *cid.ID, opts ...CallOption) error {
|
2021-11-15 06:35:30 +00:00
|
|
|
cfg := cfgFromOpts(opts...)
|
2022-02-16 16:00:38 +00:00
|
|
|
cp, err := p.conn(ctx, cfg)
|
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
|
|
|
|
|
|
|
if cid != nil {
|
|
|
|
cliPrm.SetContainer(*cid)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.stoken != nil {
|
|
|
|
cliPrm.SetSessionToken(*cfg.stoken)
|
|
|
|
}
|
|
|
|
|
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-09 09:40:23 +00:00
|
|
|
func (p *Pool) GetEACL(ctx context.Context, cid *cid.ID, opts ...CallOption) (*eacl.Table, error) {
|
2021-11-15 06:35:30 +00:00
|
|
|
cfg := cfgFromOpts(opts...)
|
2022-02-16 16:00:38 +00:00
|
|
|
cp, err := p.conn(ctx, cfg)
|
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
|
|
|
|
|
|
|
if cid != nil {
|
|
|
|
cliPrm.SetContainer(*cid)
|
|
|
|
}
|
|
|
|
|
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-09 09:40:23 +00:00
|
|
|
func (p *Pool) SetEACL(ctx context.Context, table *eacl.Table, opts ...CallOption) error {
|
2021-11-15 06:35:30 +00:00
|
|
|
cfg := cfgFromOpts(opts...)
|
2022-02-16 16:00:38 +00:00
|
|
|
cp, err := p.conn(ctx, cfg)
|
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
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
if table != nil {
|
|
|
|
cliPrm.SetTable(*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-09 09:40:23 +00:00
|
|
|
func (p *Pool) Balance(ctx context.Context, o *owner.ID, opts ...CallOption) (*accounting.Decimal, error) {
|
2021-12-17 10:36:07 +00:00
|
|
|
cfg := cfgFromOpts(opts...)
|
2022-02-16 16:00:38 +00:00
|
|
|
cp, err := p.conn(ctx, cfg)
|
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
|
|
|
|
|
|
|
if o != nil {
|
|
|
|
cliPrm.SetAccount(*o)
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
}
|