2021-05-25 08:48:01 +00:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-10-25 12:57:55 +00:00
|
|
|
"math"
|
2021-05-25 08:48:01 +00:00
|
|
|
"math/rand"
|
2021-10-25 12:57:55 +00:00
|
|
|
"strings"
|
2021-05-25 08:48:01 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-10-25 12:57:55 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
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"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
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 {
|
|
|
|
client.Client
|
|
|
|
}
|
|
|
|
|
2021-05-25 11:46:58 +00:00
|
|
|
// BuilderOptions contains options used to build connection pool.
|
|
|
|
type BuilderOptions struct {
|
2021-05-25 08:48:01 +00:00
|
|
|
Key *ecdsa.PrivateKey
|
2021-09-07 14:34:20 +00:00
|
|
|
Logger *zap.Logger
|
2021-05-25 08:48:01 +00:00
|
|
|
NodeConnectionTimeout time.Duration
|
|
|
|
NodeRequestTimeout time.Duration
|
|
|
|
ClientRebalanceInterval time.Duration
|
|
|
|
SessionExpirationEpoch uint64
|
|
|
|
weights []float64
|
2021-06-09 11:56:11 +00:00
|
|
|
addresses []string
|
2021-07-20 08:02:14 +00:00
|
|
|
clientBuilder func(opts ...client.Option) (client.Client, error)
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 11:46:58 +00:00
|
|
|
// Builder is an interim structure used to collect node addresses/weights and
|
2021-05-25 08:48:01 +00:00
|
|
|
// build connection pool subsequently.
|
2021-05-25 11:46:58 +00:00
|
|
|
type Builder struct {
|
2021-05-25 08:48:01 +00:00
|
|
|
addresses []string
|
|
|
|
weights []float64
|
|
|
|
}
|
|
|
|
|
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-05-25 11:46:58 +00:00
|
|
|
func (pb *Builder) AddNode(address string, weight float64) *Builder {
|
2021-05-25 08:48:01 +00:00
|
|
|
pb.addresses = append(pb.addresses, address)
|
|
|
|
pb.weights = append(pb.weights, weight)
|
|
|
|
return pb
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build creates new pool based on current PoolBuilder state and options.
|
2021-05-25 11:46:58 +00:00
|
|
|
func (pb *Builder) Build(ctx context.Context, options *BuilderOptions) (Pool, error) {
|
2021-05-25 08:48:01 +00:00
|
|
|
if len(pb.addresses) == 0 {
|
|
|
|
return nil, errors.New("no NeoFS peers configured")
|
|
|
|
}
|
2021-06-09 11:56:11 +00:00
|
|
|
|
2021-06-21 11:12:08 +00:00
|
|
|
options.weights = adjustWeights(pb.weights)
|
2021-06-09 11:56:11 +00:00
|
|
|
options.addresses = pb.addresses
|
2021-07-20 08:02:14 +00:00
|
|
|
|
|
|
|
if options.clientBuilder == nil {
|
|
|
|
options.clientBuilder = client.New
|
|
|
|
}
|
|
|
|
|
2021-05-28 20:12:58 +00:00
|
|
|
return newPool(ctx, options)
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pool is an interface providing connection artifacts on request.
|
|
|
|
type Pool interface {
|
2021-06-13 12:18:43 +00:00
|
|
|
client.Object
|
2021-06-22 09:15:21 +00:00
|
|
|
client.Container
|
2021-06-04 10:34:57 +00:00
|
|
|
Connection() (client.Client, *session.Token, error)
|
2021-05-28 20:12:58 +00:00
|
|
|
OwnerID() *owner.ID
|
2021-06-22 14:28:12 +00:00
|
|
|
WaitForContainerPresence(context.Context, *cid.ID, *ContainerPollingParams) error
|
[#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()
|
2021-10-25 12:57:55 +00:00
|
|
|
|
2021-11-03 14:34:02 +00:00
|
|
|
ClientParam
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientParam is analogue client.Object, client.Container but uses session token cache.
|
|
|
|
type ClientParam interface {
|
2021-10-25 13:24:43 +00:00
|
|
|
PutObjectParam(ctx context.Context, params *client.PutObjectParams, callParam *CallParam) (*object.ID, error)
|
|
|
|
DeleteObjectParam(ctx context.Context, params *client.DeleteObjectParams, callParam *CallParam) error
|
|
|
|
GetObjectParam(ctx context.Context, params *client.GetObjectParams, callParam *CallParam) (*object.Object, error)
|
|
|
|
GetObjectHeaderParam(ctx context.Context, params *client.ObjectHeaderParams, callParam *CallParam) (*object.Object, error)
|
|
|
|
ObjectPayloadRangeDataParam(ctx context.Context, params *client.RangeDataParams, callParam *CallParam) ([]byte, error)
|
|
|
|
ObjectPayloadRangeSHA256Param(ctx context.Context, params *client.RangeChecksumParams, callParam *CallParam) ([][32]byte, error)
|
|
|
|
ObjectPayloadRangeTZParam(ctx context.Context, params *client.RangeChecksumParams, callParam *CallParam) ([][64]byte, error)
|
|
|
|
SearchObjectParam(ctx context.Context, params *client.SearchObjectParams, callParam *CallParam) ([]*object.ID, error)
|
|
|
|
PutContainerParam(ctx context.Context, cnr *container.Container, callParam *CallParam) (*cid.ID, error)
|
|
|
|
GetContainerParam(ctx context.Context, cid *cid.ID, callParam *CallParam) (*container.Container, error)
|
|
|
|
ListContainersParam(ctx context.Context, ownerID *owner.ID, callParam *CallParam) ([]*cid.ID, error)
|
|
|
|
DeleteContainerParam(ctx context.Context, cid *cid.ID, callParam *CallParam) error
|
|
|
|
GetEACLParam(ctx context.Context, cid *cid.ID, callParam *CallParam) (*client.EACLWithSignature, error)
|
|
|
|
SetEACLParam(ctx context.Context, table *eacl.Table, callParam *CallParam) error
|
|
|
|
AnnounceContainerUsedSpaceParam(ctx context.Context, announce []container.UsedSpaceAnnouncement, callParam *CallParam) error
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientPack struct {
|
2021-10-25 12:57:55 +00:00
|
|
|
client client.Client
|
|
|
|
healthy bool
|
|
|
|
address string
|
|
|
|
}
|
|
|
|
|
|
|
|
type CallParam struct {
|
2021-11-03 14:34:02 +00:00
|
|
|
isRetry bool
|
|
|
|
Key *ecdsa.PrivateKey
|
2021-10-25 12:57:55 +00:00
|
|
|
|
|
|
|
Options []client.CallOption
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 07:46:44 +00:00
|
|
|
var _ Pool = (*pool)(nil)
|
|
|
|
|
2021-05-25 08:48:01 +00:00
|
|
|
type pool struct {
|
|
|
|
lock sync.RWMutex
|
|
|
|
sampler *Sampler
|
2021-10-25 12:57:55 +00:00
|
|
|
key *ecdsa.PrivateKey
|
2021-05-28 20:12:58 +00:00
|
|
|
owner *owner.ID
|
2021-05-25 08:48:01 +00:00
|
|
|
clientPacks []*clientPack
|
[#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
|
|
|
cancel context.CancelFunc
|
|
|
|
closedCh chan struct{}
|
2021-10-25 12:57:55 +00:00
|
|
|
cache *SessionCache
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 20:12:58 +00:00
|
|
|
func newPool(ctx context.Context, options *BuilderOptions) (Pool, error) {
|
2021-10-25 12:57:55 +00:00
|
|
|
cache := NewCache()
|
|
|
|
|
2021-05-25 08:48:01 +00:00
|
|
|
clientPacks := make([]*clientPack, len(options.weights))
|
2021-09-07 14:34:20 +00:00
|
|
|
var atLeastOneHealthy bool
|
2021-06-09 11:56:11 +00:00
|
|
|
for i, address := range options.addresses {
|
2021-07-20 08:02:14 +00:00
|
|
|
c, err := options.clientBuilder(client.WithDefaultPrivateKey(options.Key),
|
2021-06-09 11:56:11 +00:00
|
|
|
client.WithURIAddress(address, nil),
|
|
|
|
client.WithDialTimeout(options.NodeConnectionTimeout))
|
2021-05-25 08:48:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-07 14:34:20 +00:00
|
|
|
var healthy bool
|
2021-05-25 08:48:01 +00:00
|
|
|
st, err := c.CreateSession(ctx, options.SessionExpirationEpoch)
|
2021-09-07 14:34:20 +00:00
|
|
|
if err != nil && options.Logger != nil {
|
|
|
|
options.Logger.Warn("failed to create neofs session token for client",
|
|
|
|
zap.String("address", address),
|
|
|
|
zap.Error(err))
|
|
|
|
} else if err == nil {
|
|
|
|
healthy, atLeastOneHealthy = true, true
|
2021-10-25 12:57:55 +00:00
|
|
|
_ = cache.Put(formCacheKey(address, options.Key), st)
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
clientPacks[i] = &clientPack{client: c, healthy: healthy, address: address}
|
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
|
|
|
|
2021-05-25 08:48:01 +00:00
|
|
|
source := rand.NewSource(time.Now().UnixNano())
|
|
|
|
sampler := NewSampler(options.weights, source)
|
2021-05-28 20:12:58 +00:00
|
|
|
wallet, err := owner.NEO3WalletFromPublicKey(&options.Key.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ownerID := owner.NewIDFromNeo3Wallet(wallet)
|
|
|
|
|
[#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)
|
2021-10-25 12:57:55 +00:00
|
|
|
pool := &pool{
|
|
|
|
sampler: sampler,
|
|
|
|
key: options.Key,
|
|
|
|
owner: ownerID,
|
|
|
|
clientPacks: clientPacks,
|
|
|
|
cancel: cancel,
|
|
|
|
closedCh: make(chan struct{}),
|
|
|
|
cache: cache,
|
|
|
|
}
|
2021-06-21 11:12:08 +00:00
|
|
|
go startRebalance(ctx, pool, options)
|
|
|
|
return pool, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func startRebalance(ctx context.Context, p *pool, options *BuilderOptions) {
|
|
|
|
ticker := time.NewTimer(options.ClientRebalanceInterval)
|
|
|
|
buffer := make([]float64, len(options.weights))
|
|
|
|
|
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:
|
|
|
|
updateNodesHealth(ctx, p, options, buffer)
|
|
|
|
ticker.Reset(options.ClientRebalanceInterval)
|
|
|
|
}
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateNodesHealth(ctx context.Context, p *pool, options *BuilderOptions, bufferWeights []float64) {
|
|
|
|
if len(bufferWeights) != len(p.clientPacks) {
|
|
|
|
bufferWeights = make([]float64, len(p.clientPacks))
|
|
|
|
}
|
|
|
|
healthyChanged := false
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
for i, cPack := range p.clientPacks {
|
|
|
|
wg.Add(1)
|
2021-07-28 10:43:51 +00:00
|
|
|
|
2021-07-28 08:42:55 +00:00
|
|
|
go func(i int, client client.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-10-25 12:57:55 +00:00
|
|
|
if _, err := client.EndpointInfo(tctx); err != nil {
|
2021-06-21 11:12:08 +00:00
|
|
|
ok = false
|
|
|
|
bufferWeights[i] = 0
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
p.lock.RLock()
|
|
|
|
cp := *p.clientPacks[i]
|
|
|
|
p.lock.RUnlock()
|
|
|
|
|
2021-06-21 11:12:08 +00:00
|
|
|
if ok {
|
|
|
|
bufferWeights[i] = options.weights[i]
|
2021-10-25 12:57:55 +00:00
|
|
|
if !cp.healthy {
|
|
|
|
if tkn, err := client.CreateSession(ctx, options.SessionExpirationEpoch); err != nil {
|
2021-07-28 08:42:55 +00:00
|
|
|
ok = false
|
|
|
|
bufferWeights[i] = 0
|
2021-10-25 12:57:55 +00:00
|
|
|
} else {
|
|
|
|
_ = 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 {
|
|
|
|
p.cache.DeleteByPrefix(cp.address)
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
2021-06-21 11:12:08 +00:00
|
|
|
|
|
|
|
p.lock.Lock()
|
|
|
|
if p.clientPacks[i].healthy != ok {
|
|
|
|
p.clientPacks[i].healthy = ok
|
|
|
|
healthyChanged = true
|
|
|
|
}
|
|
|
|
p.lock.Unlock()
|
|
|
|
}(i, cPack.client)
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-06-04 10:34:57 +00:00
|
|
|
func (p *pool) Connection() (client.Client, *session.Token, error) {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, err := p.connection()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
token := p.cache.Get(formCacheKey(cp.address, p.key))
|
|
|
|
return cp.client, token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pool) 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-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
|
|
|
|
|
|
|
func (p *pool) OwnerID() *owner.ID {
|
|
|
|
return p.owner
|
|
|
|
}
|
2021-06-13 12:18:43 +00:00
|
|
|
|
|
|
|
func (p *pool) conn(option []client.CallOption) (client.Client, []client.CallOption, error) {
|
|
|
|
conn, token, err := p.Connection()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-06-23 14:55:17 +00:00
|
|
|
return conn, append([]client.CallOption{client.WithSession(token)}, option...), nil
|
2021-06-13 12:18:43 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 12:57:55 +00:00
|
|
|
func formCacheKey(address string, key *ecdsa.PrivateKey) string {
|
|
|
|
k := keys.PrivateKey{PrivateKey: *key}
|
|
|
|
return address + k.String()
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func (p *pool) connParam(ctx context.Context, param *CallParam) (*clientPack, []client.CallOption, error) {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, err := p.connection()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key := p.key
|
|
|
|
if param.Key != nil {
|
|
|
|
key = param.Key
|
|
|
|
}
|
|
|
|
|
|
|
|
param.Options = append(param.Options, client.WithKey(key))
|
|
|
|
cacheKey := formCacheKey(cp.address, key)
|
|
|
|
token := p.cache.Get(cacheKey)
|
|
|
|
if token == nil {
|
|
|
|
token, err = cp.client.CreateSession(ctx, math.MaxUint32, param.Options...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
_ = p.cache.Put(cacheKey, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cp, append([]client.CallOption{client.WithSession(token)}, param.Options...), nil
|
|
|
|
}
|
|
|
|
|
2021-06-13 12:18:43 +00:00
|
|
|
func (p *pool) PutObject(ctx context.Context, params *client.PutObjectParams, option ...client.CallOption) (*object.ID, error) {
|
|
|
|
conn, options, err := p.conn(option)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn.PutObject(ctx, params, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pool) DeleteObject(ctx context.Context, params *client.DeleteObjectParams, option ...client.CallOption) error {
|
|
|
|
conn, options, err := p.conn(option)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return conn.DeleteObject(ctx, params, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pool) GetObject(ctx context.Context, params *client.GetObjectParams, option ...client.CallOption) (*object.Object, error) {
|
|
|
|
conn, options, err := p.conn(option)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn.GetObject(ctx, params, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pool) GetObjectHeader(ctx context.Context, params *client.ObjectHeaderParams, option ...client.CallOption) (*object.Object, error) {
|
|
|
|
conn, options, err := p.conn(option)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn.GetObjectHeader(ctx, params, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pool) ObjectPayloadRangeData(ctx context.Context, params *client.RangeDataParams, option ...client.CallOption) ([]byte, error) {
|
|
|
|
conn, options, err := p.conn(option)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn.ObjectPayloadRangeData(ctx, params, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pool) ObjectPayloadRangeSHA256(ctx context.Context, params *client.RangeChecksumParams, option ...client.CallOption) ([][32]byte, error) {
|
|
|
|
conn, options, err := p.conn(option)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn.ObjectPayloadRangeSHA256(ctx, params, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pool) ObjectPayloadRangeTZ(ctx context.Context, params *client.RangeChecksumParams, option ...client.CallOption) ([][64]byte, error) {
|
|
|
|
conn, options, err := p.conn(option)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn.ObjectPayloadRangeTZ(ctx, params, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pool) SearchObject(ctx context.Context, params *client.SearchObjectParams, option ...client.CallOption) ([]*object.ID, error) {
|
|
|
|
conn, options, err := p.conn(option)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn.SearchObject(ctx, params, options...)
|
|
|
|
}
|
2021-06-22 09:15:21 +00:00
|
|
|
|
|
|
|
func (p *pool) PutContainer(ctx context.Context, cnr *container.Container, option ...client.CallOption) (*cid.ID, error) {
|
|
|
|
conn, options, err := p.conn(option)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn.PutContainer(ctx, cnr, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pool) GetContainer(ctx context.Context, cid *cid.ID, option ...client.CallOption) (*container.Container, error) {
|
|
|
|
conn, options, err := p.conn(option)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn.GetContainer(ctx, cid, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pool) ListContainers(ctx context.Context, ownerID *owner.ID, option ...client.CallOption) ([]*cid.ID, error) {
|
|
|
|
conn, options, err := p.conn(option)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn.ListContainers(ctx, ownerID, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pool) DeleteContainer(ctx context.Context, cid *cid.ID, option ...client.CallOption) error {
|
|
|
|
conn, options, err := p.conn(option)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return conn.DeleteContainer(ctx, cid, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pool) GetEACL(ctx context.Context, cid *cid.ID, option ...client.CallOption) (*client.EACLWithSignature, error) {
|
|
|
|
conn, options, err := p.conn(option)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn.GetEACL(ctx, cid, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pool) SetEACL(ctx context.Context, table *eacl.Table, option ...client.CallOption) error {
|
|
|
|
conn, options, err := p.conn(option)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return conn.SetEACL(ctx, table, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pool) AnnounceContainerUsedSpace(ctx context.Context, announce []container.UsedSpaceAnnouncement, option ...client.CallOption) error {
|
|
|
|
conn, options, err := p.conn(option)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return conn.AnnounceContainerUsedSpace(ctx, announce, options...)
|
|
|
|
}
|
|
|
|
|
2021-11-03 14:34:02 +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
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(err.Error(), "session token does not exist") {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func (p *pool) PutObjectParam(ctx context.Context, params *client.PutObjectParams, callParam *CallParam) (*object.ID, error) {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, options, err := p.connParam(ctx, callParam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res, err := cp.client.PutObject(ctx, params, options...)
|
2021-11-03 14:34:02 +00:00
|
|
|
if p.checkSessionTokenErr(err, cp.address) && !callParam.isRetry {
|
|
|
|
callParam.isRetry = true
|
|
|
|
return p.PutObjectParam(ctx, params, callParam)
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func (p *pool) DeleteObjectParam(ctx context.Context, params *client.DeleteObjectParams, callParam *CallParam) error {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, options, err := p.connParam(ctx, callParam)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = cp.client.DeleteObject(ctx, params, options...)
|
2021-11-03 14:34:02 +00:00
|
|
|
if p.checkSessionTokenErr(err, cp.address) && !callParam.isRetry {
|
|
|
|
callParam.isRetry = true
|
|
|
|
return p.DeleteObjectParam(ctx, params, callParam)
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func (p *pool) GetObjectParam(ctx context.Context, params *client.GetObjectParams, callParam *CallParam) (*object.Object, error) {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, options, err := p.connParam(ctx, callParam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res, err := cp.client.GetObject(ctx, params, options...)
|
2021-11-03 14:34:02 +00:00
|
|
|
if p.checkSessionTokenErr(err, cp.address) && !callParam.isRetry {
|
|
|
|
callParam.isRetry = true
|
|
|
|
return p.GetObjectParam(ctx, params, callParam)
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func (p *pool) GetObjectHeaderParam(ctx context.Context, params *client.ObjectHeaderParams, callParam *CallParam) (*object.Object, error) {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, options, err := p.connParam(ctx, callParam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res, err := cp.client.GetObjectHeader(ctx, params, options...)
|
2021-11-03 14:34:02 +00:00
|
|
|
if p.checkSessionTokenErr(err, cp.address) && !callParam.isRetry {
|
|
|
|
callParam.isRetry = true
|
|
|
|
return p.GetObjectHeaderParam(ctx, params, callParam)
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func (p *pool) ObjectPayloadRangeDataParam(ctx context.Context, params *client.RangeDataParams, callParam *CallParam) ([]byte, error) {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, options, err := p.connParam(ctx, callParam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res, err := cp.client.ObjectPayloadRangeData(ctx, params, options...)
|
2021-11-03 14:34:02 +00:00
|
|
|
if p.checkSessionTokenErr(err, cp.address) && !callParam.isRetry {
|
|
|
|
callParam.isRetry = true
|
|
|
|
return p.ObjectPayloadRangeDataParam(ctx, params, callParam)
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func (p *pool) ObjectPayloadRangeSHA256Param(ctx context.Context, params *client.RangeChecksumParams, callParam *CallParam) ([][32]byte, error) {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, options, err := p.connParam(ctx, callParam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res, err := cp.client.ObjectPayloadRangeSHA256(ctx, params, options...)
|
2021-11-03 14:34:02 +00:00
|
|
|
if p.checkSessionTokenErr(err, cp.address) && !callParam.isRetry {
|
|
|
|
callParam.isRetry = true
|
|
|
|
return p.ObjectPayloadRangeSHA256Param(ctx, params, callParam)
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func (p *pool) ObjectPayloadRangeTZParam(ctx context.Context, params *client.RangeChecksumParams, callParam *CallParam) ([][64]byte, error) {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, options, err := p.connParam(ctx, callParam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res, err := cp.client.ObjectPayloadRangeTZ(ctx, params, options...)
|
2021-11-03 14:34:02 +00:00
|
|
|
if p.checkSessionTokenErr(err, cp.address) && !callParam.isRetry {
|
|
|
|
callParam.isRetry = true
|
|
|
|
return p.ObjectPayloadRangeTZParam(ctx, params, callParam)
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func (p *pool) SearchObjectParam(ctx context.Context, params *client.SearchObjectParams, callParam *CallParam) ([]*object.ID, error) {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, options, err := p.connParam(ctx, callParam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res, err := cp.client.SearchObject(ctx, params, options...)
|
2021-11-03 14:34:02 +00:00
|
|
|
if p.checkSessionTokenErr(err, cp.address) && !callParam.isRetry {
|
|
|
|
callParam.isRetry = true
|
|
|
|
return p.SearchObjectParam(ctx, params, callParam)
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func (p *pool) PutContainerParam(ctx context.Context, cnr *container.Container, callParam *CallParam) (*cid.ID, error) {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, options, err := p.connParam(ctx, callParam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res, err := cp.client.PutContainer(ctx, cnr, options...)
|
2021-11-03 14:34:02 +00:00
|
|
|
if p.checkSessionTokenErr(err, cp.address) && !callParam.isRetry {
|
|
|
|
callParam.isRetry = true
|
|
|
|
return p.PutContainerParam(ctx, cnr, callParam)
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func (p *pool) GetContainerParam(ctx context.Context, cid *cid.ID, callParam *CallParam) (*container.Container, error) {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, options, err := p.connParam(ctx, callParam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res, err := cp.client.GetContainer(ctx, cid, options...)
|
2021-11-03 14:34:02 +00:00
|
|
|
if p.checkSessionTokenErr(err, cp.address) && !callParam.isRetry {
|
|
|
|
callParam.isRetry = true
|
|
|
|
return p.GetContainerParam(ctx, cid, callParam)
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func (p *pool) ListContainersParam(ctx context.Context, ownerID *owner.ID, callParam *CallParam) ([]*cid.ID, error) {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, options, err := p.connParam(ctx, callParam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res, err := cp.client.ListContainers(ctx, ownerID, options...)
|
2021-11-03 14:34:02 +00:00
|
|
|
if p.checkSessionTokenErr(err, cp.address) && !callParam.isRetry {
|
|
|
|
callParam.isRetry = true
|
|
|
|
return p.ListContainersParam(ctx, ownerID, callParam)
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func (p *pool) DeleteContainerParam(ctx context.Context, cid *cid.ID, callParam *CallParam) error {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, options, err := p.connParam(ctx, callParam)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = cp.client.DeleteContainer(ctx, cid, options...)
|
2021-11-03 14:34:02 +00:00
|
|
|
if p.checkSessionTokenErr(err, cp.address) && !callParam.isRetry {
|
|
|
|
callParam.isRetry = true
|
|
|
|
return p.DeleteContainerParam(ctx, cid, callParam)
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func (p *pool) GetEACLParam(ctx context.Context, cid *cid.ID, callParam *CallParam) (*client.EACLWithSignature, error) {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, options, err := p.connParam(ctx, callParam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res, err := cp.client.GetEACL(ctx, cid, options...)
|
2021-11-03 14:34:02 +00:00
|
|
|
if p.checkSessionTokenErr(err, cp.address) && !callParam.isRetry {
|
|
|
|
callParam.isRetry = true
|
|
|
|
return p.GetEACLParam(ctx, cid, callParam)
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func (p *pool) SetEACLParam(ctx context.Context, table *eacl.Table, callParam *CallParam) error {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, options, err := p.connParam(ctx, callParam)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = cp.client.SetEACL(ctx, table, options...)
|
2021-11-03 14:34:02 +00:00
|
|
|
if p.checkSessionTokenErr(err, cp.address) && !callParam.isRetry {
|
|
|
|
callParam.isRetry = true
|
|
|
|
return p.SetEACLParam(ctx, table, callParam)
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func (p *pool) AnnounceContainerUsedSpaceParam(ctx context.Context, announce []container.UsedSpaceAnnouncement, callParam *CallParam) error {
|
2021-10-25 12:57:55 +00:00
|
|
|
cp, options, err := p.connParam(ctx, callParam)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = cp.client.AnnounceContainerUsedSpace(ctx, announce, options...)
|
2021-11-03 14:34:02 +00:00
|
|
|
if p.checkSessionTokenErr(err, cp.address) && !callParam.isRetry {
|
|
|
|
callParam.isRetry = true
|
|
|
|
return p.AnnounceContainerUsedSpaceParam(ctx, announce, callParam)
|
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-22 09:15:21 +00:00
|
|
|
func (p *pool) WaitForContainerPresence(ctx context.Context, cid *cid.ID, pollParams *ContainerPollingParams) error {
|
|
|
|
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()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return ctx.Err()
|
|
|
|
case <-wdone:
|
|
|
|
return wctx.Err()
|
|
|
|
case <-ticker.C:
|
|
|
|
_, err = conn.GetContainer(ctx, cid)
|
|
|
|
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
|
|
|
|
|
|
|
// Cloce closes the pool and releases all the associated resources.
|
|
|
|
func (p *pool) Close() {
|
|
|
|
p.cancel()
|
|
|
|
<-p.closedCh
|
|
|
|
}
|