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-05-25 08:48:01 +00:00
|
|
|
"sync"
|
2023-05-19 11:58:30 +00:00
|
|
|
"sync/atomic"
|
2021-05-25 08:48:01 +00:00
|
|
|
"time"
|
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/accounting"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
sdkClient "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
frostfsecdsa "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/eacl"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/relations"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2022-04-07 16:09:15 +00:00
|
|
|
"github.com/google/uuid"
|
2021-12-10 13:56:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-09-07 14:34:20 +00:00
|
|
|
"go.uber.org/zap"
|
2023-01-11 12:48:42 +00:00
|
|
|
"go.uber.org/zap/zapcore"
|
2023-06-08 12:54:48 +00:00
|
|
|
"google.golang.org/grpc"
|
2021-05-25 08:48:01 +00:00
|
|
|
)
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// client represents virtual connection to the single FrostFS network endpoint from which Pool is formed.
|
2022-07-22 14:41:44 +00:00
|
|
|
// This interface is expected to have exactly one production implementation - clientWrapper.
|
|
|
|
// Others are expected to be for test purposes only.
|
2022-03-15 12:00:38 +00:00
|
|
|
type client interface {
|
2022-07-22 14:41:44 +00:00
|
|
|
// see clientWrapper.balanceGet.
|
2022-08-08 09:15:10 +00:00
|
|
|
balanceGet(context.Context, PrmBalanceGet) (accounting.Decimal, error)
|
2022-07-22 14:41:44 +00:00
|
|
|
// see clientWrapper.containerPut.
|
2022-08-08 09:15:10 +00:00
|
|
|
containerPut(context.Context, PrmContainerPut) (cid.ID, error)
|
2022-07-22 14:41:44 +00:00
|
|
|
// see clientWrapper.containerGet.
|
2022-08-08 09:15:10 +00:00
|
|
|
containerGet(context.Context, PrmContainerGet) (container.Container, error)
|
2022-07-22 14:41:44 +00:00
|
|
|
// see clientWrapper.containerList.
|
2022-03-28 09:17:56 +00:00
|
|
|
containerList(context.Context, PrmContainerList) ([]cid.ID, error)
|
2022-07-22 14:41:44 +00:00
|
|
|
// see clientWrapper.containerDelete.
|
2022-03-28 09:17:56 +00:00
|
|
|
containerDelete(context.Context, PrmContainerDelete) error
|
2022-07-22 14:41:44 +00:00
|
|
|
// see clientWrapper.containerEACL.
|
2022-08-08 09:15:10 +00:00
|
|
|
containerEACL(context.Context, PrmContainerEACL) (eacl.Table, error)
|
2022-07-22 14:41:44 +00:00
|
|
|
// see clientWrapper.containerSetEACL.
|
2022-03-28 09:17:56 +00:00
|
|
|
containerSetEACL(context.Context, PrmContainerSetEACL) error
|
2022-07-22 14:41:44 +00:00
|
|
|
// see clientWrapper.endpointInfo.
|
2022-08-08 09:15:10 +00:00
|
|
|
endpointInfo(context.Context, prmEndpointInfo) (netmap.NodeInfo, error)
|
2022-07-22 14:41:44 +00:00
|
|
|
// see clientWrapper.networkInfo.
|
2022-08-08 09:15:10 +00:00
|
|
|
networkInfo(context.Context, prmNetworkInfo) (netmap.NetworkInfo, error)
|
2023-10-18 08:30:25 +00:00
|
|
|
// see clientWrapper.netMapSnapshot
|
|
|
|
netMapSnapshot(context.Context, prmNetMapSnapshot) (netmap.NetMap, error)
|
2022-07-22 14:41:44 +00:00
|
|
|
// see clientWrapper.objectPut.
|
2022-08-08 09:15:10 +00:00
|
|
|
objectPut(context.Context, PrmObjectPut) (oid.ID, error)
|
2022-07-22 14:41:44 +00:00
|
|
|
// see clientWrapper.objectDelete.
|
2022-03-28 09:17:56 +00:00
|
|
|
objectDelete(context.Context, PrmObjectDelete) error
|
2022-07-22 14:41:44 +00:00
|
|
|
// see clientWrapper.objectGet.
|
2022-08-08 09:15:10 +00:00
|
|
|
objectGet(context.Context, PrmObjectGet) (ResGetObject, error)
|
2022-07-22 14:41:44 +00:00
|
|
|
// see clientWrapper.objectHead.
|
2022-08-08 09:15:10 +00:00
|
|
|
objectHead(context.Context, PrmObjectHead) (object.Object, error)
|
2022-07-22 14:41:44 +00:00
|
|
|
// see clientWrapper.objectRange.
|
2022-08-08 09:15:10 +00:00
|
|
|
objectRange(context.Context, PrmObjectRange) (ResObjectRange, error)
|
2022-07-22 14:41:44 +00:00
|
|
|
// see clientWrapper.objectSearch.
|
2022-08-08 09:15:10 +00:00
|
|
|
objectSearch(context.Context, PrmObjectSearch) (ResObjectSearch, error)
|
2022-07-22 14:41:44 +00:00
|
|
|
// see clientWrapper.sessionCreate.
|
2022-08-08 09:15:10 +00:00
|
|
|
sessionCreate(context.Context, prmCreateSession) (resCreateSession, error)
|
2022-07-12 18:55:59 +00:00
|
|
|
|
2022-07-13 06:57:04 +00:00
|
|
|
clientStatus
|
2022-11-03 14:58:38 +00:00
|
|
|
|
|
|
|
// see clientWrapper.dial.
|
|
|
|
dial(ctx context.Context) error
|
|
|
|
// see clientWrapper.restartIfUnhealthy.
|
|
|
|
restartIfUnhealthy(ctx context.Context) (bool, bool)
|
2023-09-20 09:14:37 +00:00
|
|
|
// see clientWrapper.close.
|
|
|
|
close() error
|
2022-07-13 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// clientStatus provide access to some metrics for connection.
|
2022-07-13 06:57:04 +00:00
|
|
|
type clientStatus interface {
|
2022-07-22 14:41:44 +00:00
|
|
|
// isHealthy checks if the connection can handle requests.
|
2022-07-12 18:55:59 +00:00
|
|
|
isHealthy() bool
|
2023-10-03 12:07:07 +00:00
|
|
|
// isDialed checks if the connection was created.
|
|
|
|
isDialed() bool
|
2022-11-03 14:58:38 +00:00
|
|
|
// setUnhealthy marks client as unhealthy.
|
|
|
|
setUnhealthy()
|
2022-07-22 14:41:44 +00:00
|
|
|
// address return address of endpoint.
|
2022-07-12 18:55:59 +00:00
|
|
|
address() string
|
2022-07-22 14:41:44 +00:00
|
|
|
// currentErrorRate returns current errors rate.
|
|
|
|
// After specific threshold connection is considered as unhealthy.
|
|
|
|
// Pool.startRebalance routine can make this connection healthy again.
|
2022-07-13 09:51:43 +00:00
|
|
|
currentErrorRate() uint32
|
2022-07-22 14:41:44 +00:00
|
|
|
// overallErrorRate returns the number of all happened errors.
|
2022-07-13 09:51:43 +00:00
|
|
|
overallErrorRate() uint64
|
2022-07-22 14:41:44 +00:00
|
|
|
// methodsStatus returns statistic for all used methods.
|
2022-07-22 08:06:16 +00:00
|
|
|
methodsStatus() []statusSnapshot
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 11:53:57 +00:00
|
|
|
// errPoolClientUnhealthy is an error to indicate that client in pool is unhealthy.
|
|
|
|
var errPoolClientUnhealthy = errors.New("pool client unhealthy")
|
2022-11-03 14:58:38 +00:00
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// clientStatusMonitor count error rate and other statistics for connection.
|
2022-07-13 06:57:04 +00:00
|
|
|
type clientStatusMonitor struct {
|
2023-01-11 12:48:42 +00:00
|
|
|
logger *zap.Logger
|
2022-07-18 08:42:29 +00:00
|
|
|
addr string
|
2023-10-03 10:57:58 +00:00
|
|
|
healthy *atomic.Uint32
|
2022-07-18 08:42:29 +00:00
|
|
|
errorThreshold uint32
|
|
|
|
|
|
|
|
mu sync.RWMutex // protect counters
|
|
|
|
currentErrorCount uint32
|
|
|
|
overallErrorCount uint64
|
2022-07-22 08:06:16 +00:00
|
|
|
methods []*methodStatus
|
2022-07-13 06:57:04 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 10:57:58 +00:00
|
|
|
// values for healthy status of clientStatusMonitor.
|
|
|
|
const (
|
|
|
|
// statusUnhealthyOnDial is set when dialing to the endpoint is failed,
|
|
|
|
// so there is no connection to the endpoint, and pool should not close it
|
|
|
|
// before re-establishing connection once again.
|
|
|
|
statusUnhealthyOnDial = iota
|
|
|
|
|
|
|
|
// statusUnhealthyOnRequest is set when communication after dialing to the
|
|
|
|
// endpoint is failed due to immediate or accumulated errors, connection is
|
|
|
|
// available and pool should close it before re-establishing connection once again.
|
|
|
|
statusUnhealthyOnRequest
|
|
|
|
|
|
|
|
// statusHealthy is set when connection is ready to be used by the pool.
|
|
|
|
statusHealthy
|
|
|
|
)
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// methodStatus provide statistic for specific method.
|
2022-07-19 13:13:59 +00:00
|
|
|
type methodStatus struct {
|
2022-07-22 08:06:16 +00:00
|
|
|
name string
|
|
|
|
mu sync.RWMutex // protect counters
|
|
|
|
statusSnapshot
|
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// statusSnapshot is statistic for specific method.
|
2022-07-22 08:06:16 +00:00
|
|
|
type statusSnapshot struct {
|
2022-07-19 13:13:59 +00:00
|
|
|
allTime uint64
|
|
|
|
allRequests uint64
|
|
|
|
}
|
|
|
|
|
2022-07-22 08:06:16 +00:00
|
|
|
// MethodIndex index of method in list of statuses in clientStatusMonitor.
|
|
|
|
type MethodIndex int
|
|
|
|
|
2022-07-19 13:13:59 +00:00
|
|
|
const (
|
2022-07-22 08:06:16 +00:00
|
|
|
methodBalanceGet MethodIndex = iota
|
|
|
|
methodContainerPut
|
|
|
|
methodContainerGet
|
|
|
|
methodContainerList
|
|
|
|
methodContainerDelete
|
|
|
|
methodContainerEACL
|
|
|
|
methodContainerSetEACL
|
|
|
|
methodEndpointInfo
|
|
|
|
methodNetworkInfo
|
2023-10-18 08:30:25 +00:00
|
|
|
methodNetMapSnapshot
|
2022-07-22 08:06:16 +00:00
|
|
|
methodObjectPut
|
|
|
|
methodObjectDelete
|
|
|
|
methodObjectGet
|
|
|
|
methodObjectHead
|
|
|
|
methodObjectRange
|
|
|
|
methodSessionCreate
|
2022-07-25 06:52:05 +00:00
|
|
|
methodLast
|
2022-07-19 13:13:59 +00:00
|
|
|
)
|
|
|
|
|
2022-07-22 08:06:16 +00:00
|
|
|
// String implements fmt.Stringer.
|
|
|
|
func (m MethodIndex) String() string {
|
|
|
|
switch m {
|
|
|
|
case methodBalanceGet:
|
|
|
|
return "balanceGet"
|
|
|
|
case methodContainerPut:
|
|
|
|
return "containerPut"
|
|
|
|
case methodContainerGet:
|
|
|
|
return "containerGet"
|
|
|
|
case methodContainerList:
|
|
|
|
return "containerList"
|
|
|
|
case methodContainerDelete:
|
|
|
|
return "containerDelete"
|
|
|
|
case methodContainerEACL:
|
|
|
|
return "containerEACL"
|
|
|
|
case methodContainerSetEACL:
|
|
|
|
return "containerSetEACL"
|
|
|
|
case methodEndpointInfo:
|
|
|
|
return "endpointInfo"
|
|
|
|
case methodNetworkInfo:
|
|
|
|
return "networkInfo"
|
2023-10-18 08:30:25 +00:00
|
|
|
case methodNetMapSnapshot:
|
|
|
|
return "netMapSnapshot"
|
2022-07-22 08:06:16 +00:00
|
|
|
case methodObjectPut:
|
|
|
|
return "objectPut"
|
|
|
|
case methodObjectDelete:
|
|
|
|
return "objectDelete"
|
|
|
|
case methodObjectGet:
|
|
|
|
return "objectGet"
|
|
|
|
case methodObjectHead:
|
|
|
|
return "objectHead"
|
|
|
|
case methodObjectRange:
|
|
|
|
return "objectRange"
|
|
|
|
case methodSessionCreate:
|
|
|
|
return "sessionCreate"
|
2022-07-25 06:52:05 +00:00
|
|
|
case methodLast:
|
|
|
|
return "it's a system name rather than a method"
|
2022-07-22 08:06:16 +00:00
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 12:48:42 +00:00
|
|
|
func newClientStatusMonitor(logger *zap.Logger, addr string, errorThreshold uint32) clientStatusMonitor {
|
2022-07-25 06:52:05 +00:00
|
|
|
methods := make([]*methodStatus, methodLast)
|
|
|
|
for i := methodBalanceGet; i < methodLast; i++ {
|
2022-07-22 08:06:16 +00:00
|
|
|
methods[i] = &methodStatus{name: i.String()}
|
|
|
|
}
|
|
|
|
|
2023-10-03 10:57:58 +00:00
|
|
|
healthy := new(atomic.Uint32)
|
|
|
|
healthy.Store(statusHealthy)
|
2023-05-19 11:58:30 +00:00
|
|
|
|
2022-07-22 08:06:16 +00:00
|
|
|
return clientStatusMonitor{
|
2023-01-11 12:48:42 +00:00
|
|
|
logger: logger,
|
2022-07-22 08:06:16 +00:00
|
|
|
addr: addr,
|
2023-05-19 11:58:30 +00:00
|
|
|
healthy: healthy,
|
2022-07-22 08:06:16 +00:00
|
|
|
errorThreshold: errorThreshold,
|
|
|
|
methods: methods,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *methodStatus) snapshot() statusSnapshot {
|
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
|
|
|
return m.statusSnapshot
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *methodStatus) incRequests(elapsed time.Duration) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
m.allTime += uint64(elapsed)
|
|
|
|
m.allRequests++
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
// clientWrapper is used by default, alternative implementations are intended for testing purposes only.
|
|
|
|
type clientWrapper struct {
|
2022-11-03 14:58:38 +00:00
|
|
|
clientMutex sync.RWMutex
|
|
|
|
client *sdkClient.Client
|
|
|
|
prm wrapperPrm
|
|
|
|
|
2022-07-18 08:42:29 +00:00
|
|
|
clientStatusMonitor
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// wrapperPrm is params to create clientWrapper.
|
2022-03-21 11:29:29 +00:00
|
|
|
type wrapperPrm struct {
|
2023-01-11 12:48:42 +00:00
|
|
|
logger *zap.Logger
|
2022-11-29 12:03:50 +00:00
|
|
|
address string
|
|
|
|
key ecdsa.PrivateKey
|
|
|
|
dialTimeout time.Duration
|
|
|
|
streamTimeout time.Duration
|
|
|
|
errorThreshold uint32
|
|
|
|
responseInfoCallback func(sdkClient.ResponseMetaInfo) error
|
|
|
|
poolRequestInfoCallback func(RequestInfo)
|
2023-06-08 12:54:48 +00:00
|
|
|
dialOptions []grpc.DialOption
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// setAddress sets endpoint to connect in FrostFS network.
|
2022-03-21 11:29:29 +00:00
|
|
|
func (x *wrapperPrm) setAddress(address string) {
|
|
|
|
x.address = address
|
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// setKey sets sdkClient.Client private key to be used for the protocol communication by default.
|
2022-03-21 11:29:29 +00:00
|
|
|
func (x *wrapperPrm) setKey(key ecdsa.PrivateKey) {
|
|
|
|
x.key = key
|
|
|
|
}
|
|
|
|
|
2023-08-01 08:50:18 +00:00
|
|
|
// setLogger sets sdkClient.Client logger.
|
|
|
|
func (x *wrapperPrm) setLogger(logger *zap.Logger) {
|
|
|
|
x.logger = logger
|
|
|
|
}
|
|
|
|
|
2022-11-15 11:32:25 +00:00
|
|
|
// setDialTimeout sets the timeout for connection to be established.
|
|
|
|
func (x *wrapperPrm) setDialTimeout(timeout time.Duration) {
|
|
|
|
x.dialTimeout = timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
// setStreamTimeout sets the timeout for individual operations in streaming RPC.
|
|
|
|
func (x *wrapperPrm) setStreamTimeout(timeout time.Duration) {
|
|
|
|
x.streamTimeout = timeout
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// setErrorThreshold sets threshold after reaching which connection is considered unhealthy
|
|
|
|
// until Pool.startRebalance routing updates its status.
|
2022-07-13 06:57:04 +00:00
|
|
|
func (x *wrapperPrm) setErrorThreshold(threshold uint32) {
|
|
|
|
x.errorThreshold = threshold
|
|
|
|
}
|
|
|
|
|
2022-11-29 12:03:50 +00:00
|
|
|
// setPoolRequestCallback sets callback that will be invoked after every pool response.
|
|
|
|
func (x *wrapperPrm) setPoolRequestCallback(f func(RequestInfo)) {
|
|
|
|
x.poolRequestInfoCallback = f
|
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// setResponseInfoCallback sets callback that will be invoked after every response.
|
2022-03-21 11:29:29 +00:00
|
|
|
func (x *wrapperPrm) setResponseInfoCallback(f func(sdkClient.ResponseMetaInfo) error) {
|
|
|
|
x.responseInfoCallback = f
|
|
|
|
}
|
|
|
|
|
2023-06-08 12:54:48 +00:00
|
|
|
// setGRPCDialOptions sets the gRPC dial options for new gRPC client connection.
|
|
|
|
func (x *wrapperPrm) setGRPCDialOptions(opts []grpc.DialOption) {
|
|
|
|
x.dialOptions = opts
|
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// newWrapper creates a clientWrapper that implements the client interface.
|
2022-11-03 14:58:38 +00:00
|
|
|
func newWrapper(prm wrapperPrm) *clientWrapper {
|
|
|
|
var cl sdkClient.Client
|
2023-11-13 12:18:07 +00:00
|
|
|
prmInit := sdkClient.PrmInit{
|
|
|
|
Key: prm.key,
|
|
|
|
ResponseInfoCallback: prm.responseInfoCallback,
|
|
|
|
}
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
cl.Init(prmInit)
|
|
|
|
|
2022-07-12 18:55:59 +00:00
|
|
|
res := &clientWrapper{
|
2022-11-03 14:58:38 +00:00
|
|
|
client: &cl,
|
2023-01-11 12:48:42 +00:00
|
|
|
clientStatusMonitor: newClientStatusMonitor(prm.logger, prm.address, prm.errorThreshold),
|
2022-11-03 14:58:38 +00:00
|
|
|
prm: prm,
|
2022-07-12 18:55:59 +00:00
|
|
|
}
|
2022-03-29 11:12:15 +00:00
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// dial establishes a connection to the server from the FrostFS network.
|
2022-11-03 14:58:38 +00:00
|
|
|
// Returns an error describing failure reason. If failed, the client
|
|
|
|
// SHOULD NOT be used.
|
|
|
|
func (c *clientWrapper) dial(ctx context.Context) error {
|
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2023-11-13 12:35:30 +00:00
|
|
|
prmDial := sdkClient.PrmDial{
|
|
|
|
Endpoint: c.prm.address,
|
|
|
|
DialTimeout: c.prm.dialTimeout,
|
|
|
|
StreamTimeout: c.prm.streamTimeout,
|
|
|
|
GRPCDialOptions: c.prm.dialOptions,
|
|
|
|
}
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2023-04-07 05:55:12 +00:00
|
|
|
if err = cl.Dial(ctx, prmDial); err != nil {
|
2023-10-03 10:57:58 +00:00
|
|
|
c.setUnhealthyOnDial()
|
2022-11-03 14:58:38 +00:00
|
|
|
return err
|
2022-03-29 11:12:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// restartIfUnhealthy checks healthy status of client and recreate it if status is unhealthy.
|
|
|
|
// Return current healthy status and indicating if status was changed by this function call.
|
|
|
|
func (c *clientWrapper) restartIfUnhealthy(ctx context.Context) (healthy, changed bool) {
|
|
|
|
var wasHealthy bool
|
|
|
|
if _, err := c.endpointInfo(ctx, prmEndpointInfo{}); err == nil {
|
|
|
|
return true, false
|
2022-11-08 11:53:57 +00:00
|
|
|
} else if !errors.Is(err, errPoolClientUnhealthy) {
|
2022-11-03 14:58:38 +00:00
|
|
|
wasHealthy = true
|
|
|
|
}
|
|
|
|
|
2023-10-03 10:57:58 +00:00
|
|
|
// if connection is dialed before, to avoid routine / connection leak,
|
|
|
|
// pool has to close it and then initialize once again.
|
|
|
|
if c.isDialed() {
|
|
|
|
_ = c.close()
|
|
|
|
}
|
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
var cl sdkClient.Client
|
2023-11-13 12:18:07 +00:00
|
|
|
prmInit := sdkClient.PrmInit{
|
|
|
|
Key: c.prm.key,
|
|
|
|
ResponseInfoCallback: c.prm.responseInfoCallback,
|
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
|
|
|
|
cl.Init(prmInit)
|
|
|
|
|
2023-11-13 12:35:30 +00:00
|
|
|
prmDial := sdkClient.PrmDial{
|
|
|
|
Endpoint: c.prm.address,
|
|
|
|
DialTimeout: c.prm.dialTimeout,
|
|
|
|
StreamTimeout: c.prm.streamTimeout,
|
|
|
|
GRPCDialOptions: c.prm.dialOptions,
|
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
|
2023-04-07 05:55:12 +00:00
|
|
|
if err := cl.Dial(ctx, prmDial); err != nil {
|
2023-10-03 10:57:58 +00:00
|
|
|
c.setUnhealthyOnDial()
|
2022-11-03 14:58:38 +00:00
|
|
|
return false, wasHealthy
|
|
|
|
}
|
|
|
|
|
|
|
|
c.clientMutex.Lock()
|
|
|
|
c.client = &cl
|
|
|
|
c.clientMutex.Unlock()
|
|
|
|
|
|
|
|
if _, err := cl.EndpointInfo(ctx, sdkClient.PrmEndpointInfo{}); err != nil {
|
|
|
|
c.setUnhealthy()
|
|
|
|
return false, wasHealthy
|
|
|
|
}
|
|
|
|
|
|
|
|
c.setHealthy()
|
|
|
|
return true, !wasHealthy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientWrapper) getClient() (*sdkClient.Client, error) {
|
|
|
|
c.clientMutex.RLock()
|
|
|
|
defer c.clientMutex.RUnlock()
|
|
|
|
if c.isHealthy() {
|
|
|
|
return c.client, nil
|
|
|
|
}
|
2022-11-08 11:53:57 +00:00
|
|
|
return nil, errPoolClientUnhealthy
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// balanceGet invokes sdkClient.BalanceGet parse response status to error and return result as is.
|
2022-08-08 09:15:10 +00:00
|
|
|
func (c *clientWrapper) balanceGet(ctx context.Context, prm PrmBalanceGet) (accounting.Decimal, error) {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return accounting.Decimal{}, err
|
|
|
|
}
|
|
|
|
|
2023-10-23 14:16:13 +00:00
|
|
|
cliPrm := sdkClient.PrmBalanceGet{
|
2023-11-21 08:35:10 +00:00
|
|
|
Account: prm.account,
|
2023-10-23 14:16:13 +00:00
|
|
|
}
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
start := time.Now()
|
2022-11-03 14:58:38 +00:00
|
|
|
res, err := cl.BalanceGet(ctx, cliPrm)
|
2022-07-19 13:13:59 +00:00
|
|
|
c.incRequests(time.Since(start), methodBalanceGet)
|
2022-07-14 12:45:25 +00:00
|
|
|
var st apistatus.Status
|
|
|
|
if res != nil {
|
|
|
|
st = res.Status()
|
|
|
|
}
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, st, err); err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return accounting.Decimal{}, fmt.Errorf("balance get on client: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
2022-07-13 09:51:43 +00:00
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
return res.Amount(), nil
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// containerPut invokes sdkClient.ContainerPut parse response status to error and return result as is.
|
|
|
|
// It also waits for the container to appear on the network.
|
2022-08-08 09:15:10 +00:00
|
|
|
func (c *clientWrapper) containerPut(ctx context.Context, prm PrmContainerPut) (cid.ID, error) {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return cid.ID{}, err
|
|
|
|
}
|
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
start := time.Now()
|
2023-07-31 11:08:50 +00:00
|
|
|
res, err := cl.ContainerPut(ctx, prm.ClientParams)
|
2022-07-19 13:13:59 +00:00
|
|
|
c.incRequests(time.Since(start), methodContainerPut)
|
2022-07-14 12:45:25 +00:00
|
|
|
var st apistatus.Status
|
|
|
|
if res != nil {
|
|
|
|
st = res.Status()
|
|
|
|
}
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, st, err); err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return cid.ID{}, fmt.Errorf("container put on client: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 11:08:50 +00:00
|
|
|
if prm.WaitParams == nil {
|
|
|
|
prm.WaitParams = defaultWaitParams()
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
2023-08-03 09:14:00 +00:00
|
|
|
if err = prm.WaitParams.CheckValidity(); err != nil {
|
|
|
|
return cid.ID{}, fmt.Errorf("invalid wait parameters: %w", err)
|
|
|
|
}
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2022-08-05 07:56:49 +00:00
|
|
|
idCnr := res.ID()
|
|
|
|
|
2024-03-01 12:16:16 +00:00
|
|
|
getPrm := PrmContainerGet{
|
|
|
|
ContainerID: idCnr,
|
|
|
|
Session: prm.ClientParams.Session,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = waitForContainerPresence(ctx, c, getPrm, prm.WaitParams)
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, nil, err); err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return cid.ID{}, fmt.Errorf("wait container presence on client: %w", err)
|
2022-07-12 18:55:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
return idCnr, nil
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// containerGet invokes sdkClient.ContainerGet parse response status to error and return result as is.
|
2022-08-08 09:15:10 +00:00
|
|
|
func (c *clientWrapper) containerGet(ctx context.Context, prm PrmContainerGet) (container.Container, error) {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return container.Container{}, err
|
|
|
|
}
|
|
|
|
|
2023-08-02 15:56:41 +00:00
|
|
|
cliPrm := sdkClient.PrmContainerGet{
|
|
|
|
ContainerID: &prm.ContainerID,
|
2024-03-01 12:16:16 +00:00
|
|
|
Session: prm.Session,
|
2023-08-02 15:56:41 +00:00
|
|
|
}
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
start := time.Now()
|
2022-11-03 14:58:38 +00:00
|
|
|
res, err := cl.ContainerGet(ctx, cliPrm)
|
2022-07-19 13:13:59 +00:00
|
|
|
c.incRequests(time.Since(start), methodContainerGet)
|
2022-07-14 12:45:25 +00:00
|
|
|
var st apistatus.Status
|
|
|
|
if res != nil {
|
|
|
|
st = res.Status()
|
|
|
|
}
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, st, err); err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return container.Container{}, fmt.Errorf("container get on client: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
2022-06-25 09:54:42 +00:00
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
return res.Container(), nil
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// containerList invokes sdkClient.ContainerList parse response status to error and return result as is.
|
2022-03-28 09:17:56 +00:00
|
|
|
func (c *clientWrapper) containerList(ctx context.Context, prm PrmContainerList) ([]cid.ID, error) {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-23 14:25:46 +00:00
|
|
|
cliPrm := sdkClient.PrmContainerList{
|
2024-03-01 12:16:16 +00:00
|
|
|
Account: prm.OwnerID,
|
|
|
|
Session: prm.Session,
|
2023-10-23 14:25:46 +00:00
|
|
|
}
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
start := time.Now()
|
2022-11-03 14:58:38 +00:00
|
|
|
res, err := cl.ContainerList(ctx, cliPrm)
|
2022-07-19 13:13:59 +00:00
|
|
|
c.incRequests(time.Since(start), methodContainerList)
|
2022-07-14 12:45:25 +00:00
|
|
|
var st apistatus.Status
|
|
|
|
if res != nil {
|
|
|
|
st = res.Status()
|
|
|
|
}
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, st, err); err != nil {
|
2022-07-12 18:55:59 +00:00
|
|
|
return nil, fmt.Errorf("container list on client: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
return res.Containers(), nil
|
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// containerDelete invokes sdkClient.ContainerDelete parse response status to error.
|
|
|
|
// It also waits for the container to be removed from the network.
|
2022-03-28 09:17:56 +00:00
|
|
|
func (c *clientWrapper) containerDelete(ctx context.Context, prm PrmContainerDelete) error {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-03 09:11:14 +00:00
|
|
|
cliPrm := sdkClient.PrmContainerDelete{
|
|
|
|
ContainerID: &prm.ContainerID,
|
|
|
|
Session: prm.Session,
|
2022-04-12 10:16:07 +00:00
|
|
|
}
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
start := time.Now()
|
2022-11-03 14:58:38 +00:00
|
|
|
res, err := cl.ContainerDelete(ctx, cliPrm)
|
2022-07-19 13:13:59 +00:00
|
|
|
c.incRequests(time.Since(start), methodContainerDelete)
|
2022-07-14 12:45:25 +00:00
|
|
|
var st apistatus.Status
|
|
|
|
if res != nil {
|
|
|
|
st = res.Status()
|
|
|
|
}
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, st, err); err != nil {
|
2022-07-12 18:55:59 +00:00
|
|
|
return fmt.Errorf("container delete on client: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 09:11:14 +00:00
|
|
|
if prm.WaitParams == nil {
|
|
|
|
prm.WaitParams = defaultWaitParams()
|
|
|
|
}
|
|
|
|
if err := prm.WaitParams.CheckValidity(); err != nil {
|
|
|
|
return fmt.Errorf("invalid wait parameters: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 12:16:16 +00:00
|
|
|
getPrm := PrmContainerGet{
|
|
|
|
ContainerID: prm.ContainerID,
|
|
|
|
Session: prm.Session,
|
|
|
|
}
|
|
|
|
|
|
|
|
return waitForContainerRemoved(ctx, c, getPrm, prm.WaitParams)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// containerEACL invokes sdkClient.ContainerEACL parse response status to error and return result as is.
|
2022-08-08 09:15:10 +00:00
|
|
|
func (c *clientWrapper) containerEACL(ctx context.Context, prm PrmContainerEACL) (eacl.Table, error) {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return eacl.Table{}, err
|
|
|
|
}
|
|
|
|
|
2023-08-11 08:06:45 +00:00
|
|
|
cliPrm := sdkClient.PrmContainerEACL{
|
|
|
|
ContainerID: &prm.ContainerID,
|
2024-03-01 12:16:16 +00:00
|
|
|
Session: prm.Session,
|
2023-08-11 08:06:45 +00:00
|
|
|
}
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
start := time.Now()
|
2022-11-03 14:58:38 +00:00
|
|
|
res, err := cl.ContainerEACL(ctx, cliPrm)
|
2022-07-19 13:13:59 +00:00
|
|
|
c.incRequests(time.Since(start), methodContainerEACL)
|
2022-07-14 12:45:25 +00:00
|
|
|
var st apistatus.Status
|
|
|
|
if res != nil {
|
|
|
|
st = res.Status()
|
|
|
|
}
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, st, err); err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return eacl.Table{}, fmt.Errorf("get eacl on client: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
2022-08-05 07:56:49 +00:00
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
return res.Table(), nil
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// containerSetEACL invokes sdkClient.ContainerSetEACL parse response status to error.
|
|
|
|
// It also waits for the EACL to appear on the network.
|
2022-03-28 09:17:56 +00:00
|
|
|
func (c *clientWrapper) containerSetEACL(ctx context.Context, prm PrmContainerSetEACL) error {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-18 15:07:51 +00:00
|
|
|
cliPrm := sdkClient.PrmContainerSetEACL{
|
|
|
|
Table: &prm.Table,
|
|
|
|
Session: prm.Session,
|
2022-06-21 14:50:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
start := time.Now()
|
2022-11-03 14:58:38 +00:00
|
|
|
res, err := cl.ContainerSetEACL(ctx, cliPrm)
|
2022-07-19 13:13:59 +00:00
|
|
|
c.incRequests(time.Since(start), methodContainerSetEACL)
|
2022-07-14 12:45:25 +00:00
|
|
|
var st apistatus.Status
|
|
|
|
if res != nil {
|
|
|
|
st = res.Status()
|
|
|
|
}
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, st, err); err != nil {
|
2022-07-12 18:55:59 +00:00
|
|
|
return fmt.Errorf("set eacl on client: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 15:07:51 +00:00
|
|
|
if prm.WaitParams == nil {
|
|
|
|
prm.WaitParams = defaultWaitParams()
|
|
|
|
}
|
|
|
|
if err := prm.WaitParams.CheckValidity(); err != nil {
|
|
|
|
return fmt.Errorf("invalid wait parameters: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 12:16:16 +00:00
|
|
|
cnrID, _ := prm.Table.CID()
|
|
|
|
eaclPrm := PrmContainerEACL{
|
|
|
|
ContainerID: cnrID,
|
|
|
|
Session: prm.Session,
|
2022-04-12 16:23:16 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 12:16:16 +00:00
|
|
|
err = waitForEACLPresence(ctx, c, eaclPrm, &prm.Table, prm.WaitParams)
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, nil, err); err != nil {
|
2022-07-12 18:55:59 +00:00
|
|
|
return fmt.Errorf("wait eacl presence on client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// endpointInfo invokes sdkClient.EndpointInfo parse response status to error and return result as is.
|
2022-08-08 09:15:10 +00:00
|
|
|
func (c *clientWrapper) endpointInfo(ctx context.Context, _ prmEndpointInfo) (netmap.NodeInfo, error) {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return netmap.NodeInfo{}, err
|
|
|
|
}
|
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
start := time.Now()
|
2022-11-03 14:58:38 +00:00
|
|
|
res, err := cl.EndpointInfo(ctx, sdkClient.PrmEndpointInfo{})
|
2022-07-19 13:13:59 +00:00
|
|
|
c.incRequests(time.Since(start), methodEndpointInfo)
|
2022-07-14 12:45:25 +00:00
|
|
|
var st apistatus.Status
|
|
|
|
if res != nil {
|
|
|
|
st = res.Status()
|
|
|
|
}
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, st, err); err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return netmap.NodeInfo{}, fmt.Errorf("endpoint info on client: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
2022-08-05 07:56:49 +00:00
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
return res.NodeInfo(), nil
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// networkInfo invokes sdkClient.NetworkInfo parse response status to error and return result as is.
|
2022-08-08 09:15:10 +00:00
|
|
|
func (c *clientWrapper) networkInfo(ctx context.Context, _ prmNetworkInfo) (netmap.NetworkInfo, error) {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return netmap.NetworkInfo{}, err
|
|
|
|
}
|
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
start := time.Now()
|
2022-11-03 14:58:38 +00:00
|
|
|
res, err := cl.NetworkInfo(ctx, sdkClient.PrmNetworkInfo{})
|
2022-07-19 13:13:59 +00:00
|
|
|
c.incRequests(time.Since(start), methodNetworkInfo)
|
2022-07-14 12:45:25 +00:00
|
|
|
var st apistatus.Status
|
|
|
|
if res != nil {
|
|
|
|
st = res.Status()
|
|
|
|
}
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, st, err); err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return netmap.NetworkInfo{}, fmt.Errorf("network info on client: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
2022-08-05 07:56:49 +00:00
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
return res.Info(), nil
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 08:30:25 +00:00
|
|
|
// networkInfo invokes sdkClient.NetworkInfo parse response status to error and return result as is.
|
|
|
|
func (c *clientWrapper) netMapSnapshot(ctx context.Context, _ prmNetMapSnapshot) (netmap.NetMap, error) {
|
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return netmap.NetMap{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
res, err := cl.NetMapSnapshot(ctx, sdkClient.PrmNetMapSnapshot{})
|
|
|
|
c.incRequests(time.Since(start), methodNetMapSnapshot)
|
|
|
|
var st apistatus.Status
|
|
|
|
if res != nil {
|
|
|
|
st = res.Status()
|
|
|
|
}
|
|
|
|
if err = c.handleError(ctx, st, err); err != nil {
|
|
|
|
return netmap.NetMap{}, fmt.Errorf("network map snapshot on client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.NetMap(), nil
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// objectPut writes object to FrostFS.
|
2022-08-08 09:15:10 +00:00
|
|
|
func (c *clientWrapper) objectPut(ctx context.Context, prm PrmObjectPut) (oid.ID, error) {
|
2023-08-25 06:36:08 +00:00
|
|
|
if prm.bufferMaxSize == 0 {
|
|
|
|
prm.bufferMaxSize = defaultBufferMaxSizeForPut
|
|
|
|
}
|
|
|
|
|
2023-07-11 09:02:23 +00:00
|
|
|
if prm.clientCut {
|
|
|
|
return c.objectPutClientCut(ctx, prm)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.objectPutServerCut(ctx, prm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientWrapper) objectPutServerCut(ctx context.Context, prm PrmObjectPut) (oid.ID, error) {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return oid.ID{}, err
|
|
|
|
}
|
|
|
|
|
2023-09-07 08:53:49 +00:00
|
|
|
cliPrm := sdkClient.PrmObjectPutInit{
|
|
|
|
CopiesNumber: prm.copiesNumber,
|
|
|
|
Session: prm.stoken,
|
|
|
|
Key: prm.key,
|
|
|
|
BearerToken: prm.btoken,
|
2022-08-25 12:14:18 +00:00
|
|
|
}
|
2022-07-29 15:42:24 +00:00
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
start := time.Now()
|
2022-11-03 14:58:38 +00:00
|
|
|
wObj, err := cl.ObjectPutInit(ctx, cliPrm)
|
2022-07-19 13:13:59 +00:00
|
|
|
c.incRequests(time.Since(start), methodObjectPut)
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, nil, err); err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return oid.ID{}, fmt.Errorf("init writing on API client: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 08:53:53 +00:00
|
|
|
if wObj.WriteHeader(ctx, prm.hdr) {
|
2022-03-21 11:29:29 +00:00
|
|
|
sz := prm.hdr.PayloadSize()
|
|
|
|
|
|
|
|
if data := prm.hdr.Payload(); len(data) > 0 {
|
|
|
|
if prm.payload != nil {
|
|
|
|
prm.payload = io.MultiReader(bytes.NewReader(data), prm.payload)
|
|
|
|
} else {
|
|
|
|
prm.payload = bytes.NewReader(data)
|
|
|
|
sz = uint64(len(data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.payload != nil {
|
2023-08-25 06:36:08 +00:00
|
|
|
if sz == 0 || sz > prm.bufferMaxSize {
|
|
|
|
sz = prm.bufferMaxSize
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, sz)
|
|
|
|
|
|
|
|
var n int
|
|
|
|
|
|
|
|
for {
|
|
|
|
n, err = prm.payload.Read(buf)
|
|
|
|
if n > 0 {
|
2022-07-13 09:51:43 +00:00
|
|
|
start = time.Now()
|
2023-06-27 08:53:53 +00:00
|
|
|
successWrite := wObj.WritePayloadChunk(ctx, buf[:n])
|
2022-07-19 13:13:59 +00:00
|
|
|
c.incRequests(time.Since(start), methodObjectPut)
|
2022-07-13 09:51:43 +00:00
|
|
|
if !successWrite {
|
2022-03-21 11:29:29 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2023-03-29 10:20:37 +00:00
|
|
|
return oid.ID{}, fmt.Errorf("read payload: %w", c.handleError(ctx, nil, err))
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-27 08:53:53 +00:00
|
|
|
res, err := wObj.Close(ctx)
|
2022-07-14 12:45:25 +00:00
|
|
|
var st apistatus.Status
|
|
|
|
if res != nil {
|
|
|
|
st = res.Status()
|
|
|
|
}
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, st, err); err != nil { // here err already carries both status and client errors
|
2022-08-08 09:15:10 +00:00
|
|
|
return oid.ID{}, fmt.Errorf("client failure: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
return res.StoredObjectID(), nil
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 09:02:23 +00:00
|
|
|
func (c *clientWrapper) objectPutClientCut(ctx context.Context, prm PrmObjectPut) (oid.ID, error) {
|
|
|
|
putInitPrm := PrmObjectPutClientCutInit{
|
2023-08-01 08:50:18 +00:00
|
|
|
PrmObjectPut: prm,
|
2023-07-11 09:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
wObj, err := c.objectPutInitTransformer(putInitPrm)
|
|
|
|
c.incRequests(time.Since(start), methodObjectPut)
|
|
|
|
if err = c.handleError(ctx, nil, err); err != nil {
|
|
|
|
return oid.ID{}, fmt.Errorf("init writing on API client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if wObj.WriteHeader(ctx, prm.hdr) {
|
2023-08-01 09:52:18 +00:00
|
|
|
sz := prm.hdr.PayloadSize()
|
|
|
|
|
2023-07-11 09:02:23 +00:00
|
|
|
if data := prm.hdr.Payload(); len(data) > 0 {
|
|
|
|
if prm.payload != nil {
|
|
|
|
prm.payload = io.MultiReader(bytes.NewReader(data), prm.payload)
|
|
|
|
} else {
|
|
|
|
prm.payload = bytes.NewReader(data)
|
2023-08-01 09:52:18 +00:00
|
|
|
sz = uint64(len(data))
|
2023-07-11 09:02:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.payload != nil {
|
2023-08-25 06:36:08 +00:00
|
|
|
if sz == 0 || sz > prm.bufferMaxSize {
|
|
|
|
sz = prm.bufferMaxSize
|
2023-08-01 09:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, sz)
|
|
|
|
|
2023-07-11 09:02:23 +00:00
|
|
|
var n int
|
|
|
|
|
|
|
|
for {
|
2023-08-01 09:52:18 +00:00
|
|
|
n, err = prm.payload.Read(buf)
|
2023-07-11 09:02:23 +00:00
|
|
|
if n > 0 {
|
|
|
|
start = time.Now()
|
2023-08-01 09:52:18 +00:00
|
|
|
successWrite := wObj.WritePayloadChunk(ctx, buf[:n])
|
2023-07-11 09:02:23 +00:00
|
|
|
c.incRequests(time.Since(start), methodObjectPut)
|
|
|
|
if !successWrite {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return oid.ID{}, fmt.Errorf("read payload: %w", c.handleError(ctx, nil, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := wObj.Close(ctx)
|
|
|
|
var st apistatus.Status
|
|
|
|
if res != nil {
|
|
|
|
st = res.Status
|
|
|
|
}
|
|
|
|
if err = c.handleError(ctx, st, err); err != nil { // here err already carries both status and client errors
|
|
|
|
return oid.ID{}, fmt.Errorf("client failure: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.OID, nil
|
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// objectDelete invokes sdkClient.ObjectDelete parse response status to error.
|
2022-03-28 09:17:56 +00:00
|
|
|
func (c *clientWrapper) objectDelete(ctx context.Context, prm PrmObjectDelete) error {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-04 10:23:10 +00:00
|
|
|
cnr := prm.addr.Container()
|
|
|
|
obj := prm.addr.Object()
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2023-09-04 10:23:10 +00:00
|
|
|
cliPrm := sdkClient.PrmObjectDelete{
|
|
|
|
BearerToken: prm.btoken,
|
|
|
|
Session: prm.stoken,
|
|
|
|
ContainerID: &cnr,
|
|
|
|
ObjectID: &obj,
|
|
|
|
Key: prm.key,
|
2022-04-12 13:48:55 +00:00
|
|
|
}
|
2022-07-13 09:51:43 +00:00
|
|
|
|
|
|
|
start := time.Now()
|
2022-11-03 14:58:38 +00:00
|
|
|
res, err := cl.ObjectDelete(ctx, cliPrm)
|
2022-07-19 13:13:59 +00:00
|
|
|
c.incRequests(time.Since(start), methodObjectDelete)
|
2022-07-14 12:45:25 +00:00
|
|
|
var st apistatus.Status
|
|
|
|
if res != nil {
|
|
|
|
st = res.Status()
|
|
|
|
}
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, st, err); err != nil {
|
2022-07-12 18:55:59 +00:00
|
|
|
return fmt.Errorf("delete object on client: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// objectGet returns reader for object.
|
2022-08-08 09:15:10 +00:00
|
|
|
func (c *clientWrapper) objectGet(ctx context.Context, prm PrmObjectGet) (ResGetObject, error) {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return ResGetObject{}, err
|
|
|
|
}
|
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
prmCnr := prm.addr.Container()
|
|
|
|
prmObj := prm.addr.Object()
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
cliPrm := sdkClient.PrmObjectGet{
|
|
|
|
BearerToken: prm.btoken,
|
|
|
|
Session: prm.stoken,
|
|
|
|
ContainerID: &prmCnr,
|
|
|
|
ObjectID: &prmObj,
|
|
|
|
Key: prm.key,
|
2022-08-25 11:45:50 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
var res ResGetObject
|
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
rObj, err := cl.ObjectGetInit(ctx, cliPrm)
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, nil, err); err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return ResGetObject{}, fmt.Errorf("init object reading on client: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
start := time.Now()
|
|
|
|
successReadHeader := rObj.ReadHeader(&res.Header)
|
2022-07-19 13:13:59 +00:00
|
|
|
c.incRequests(time.Since(start), methodObjectGet)
|
2022-07-13 09:51:43 +00:00
|
|
|
if !successReadHeader {
|
2022-07-12 18:55:59 +00:00
|
|
|
rObjRes, err := rObj.Close()
|
2022-07-14 12:45:25 +00:00
|
|
|
var st apistatus.Status
|
|
|
|
if rObjRes != nil {
|
|
|
|
st = rObjRes.Status()
|
|
|
|
}
|
2023-03-29 10:20:37 +00:00
|
|
|
err = c.handleError(ctx, st, err)
|
2022-08-08 09:15:10 +00:00
|
|
|
return res, fmt.Errorf("read header: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
res.Payload = &objectReadCloser{
|
2022-07-19 13:13:59 +00:00
|
|
|
reader: rObj,
|
|
|
|
elapsedTimeCallback: func(elapsed time.Duration) {
|
|
|
|
c.incRequests(elapsed, methodObjectGet)
|
|
|
|
},
|
2022-07-13 09:51:43 +00:00
|
|
|
}
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
return res, nil
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// objectHead invokes sdkClient.ObjectHead parse response status to error and return result as is.
|
2022-08-08 09:15:10 +00:00
|
|
|
func (c *clientWrapper) objectHead(ctx context.Context, prm PrmObjectHead) (object.Object, error) {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return object.Object{}, err
|
|
|
|
}
|
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
prmCnr := prm.addr.Container()
|
|
|
|
prmObj := prm.addr.Object()
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
cliPrm := sdkClient.PrmObjectHead{
|
|
|
|
BearerToken: prm.btoken,
|
|
|
|
Session: prm.stoken,
|
|
|
|
Raw: prm.raw,
|
|
|
|
ContainerID: &prmCnr,
|
|
|
|
ObjectID: &prmObj,
|
|
|
|
Key: prm.key,
|
2022-04-12 13:48:55 +00:00
|
|
|
}
|
2022-03-21 11:29:29 +00:00
|
|
|
|
|
|
|
var obj object.Object
|
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
start := time.Now()
|
2022-11-03 14:58:38 +00:00
|
|
|
res, err := cl.ObjectHead(ctx, cliPrm)
|
2022-07-19 13:13:59 +00:00
|
|
|
c.incRequests(time.Since(start), methodObjectHead)
|
2022-07-14 12:45:25 +00:00
|
|
|
var st apistatus.Status
|
|
|
|
if res != nil {
|
|
|
|
st = res.Status()
|
|
|
|
}
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, st, err); err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return obj, fmt.Errorf("read object header via client: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
if !res.ReadHeader(&obj) {
|
2022-08-08 09:15:10 +00:00
|
|
|
return obj, errors.New("missing object header in response")
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
return obj, nil
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// objectRange returns object range reader.
|
2022-08-08 09:15:10 +00:00
|
|
|
func (c *clientWrapper) objectRange(ctx context.Context, prm PrmObjectRange) (ResObjectRange, error) {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return ResObjectRange{}, err
|
|
|
|
}
|
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
prmCnr := prm.addr.Container()
|
|
|
|
prmObj := prm.addr.Object()
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2023-08-28 08:07:18 +00:00
|
|
|
cliPrm := sdkClient.PrmObjectRange{
|
|
|
|
BearerToken: prm.btoken,
|
|
|
|
Session: prm.stoken,
|
|
|
|
ContainerID: &prmCnr,
|
|
|
|
ObjectID: &prmObj,
|
|
|
|
Offset: prm.off,
|
|
|
|
Length: prm.ln,
|
|
|
|
Key: prm.key,
|
2022-08-25 11:32:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
start := time.Now()
|
2022-11-03 14:58:38 +00:00
|
|
|
res, err := cl.ObjectRangeInit(ctx, cliPrm)
|
2022-07-19 13:13:59 +00:00
|
|
|
c.incRequests(time.Since(start), methodObjectRange)
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, nil, err); err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return ResObjectRange{}, fmt.Errorf("init payload range reading on client: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
return ResObjectRange{
|
2022-07-19 13:13:59 +00:00
|
|
|
payload: res,
|
|
|
|
elapsedTimeCallback: func(elapsed time.Duration) {
|
|
|
|
c.incRequests(elapsed, methodObjectRange)
|
|
|
|
},
|
2022-07-13 09:51:43 +00:00
|
|
|
}, nil
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// objectSearch invokes sdkClient.ObjectSearchInit parse response status to error and return result as is.
|
2022-08-08 09:15:10 +00:00
|
|
|
func (c *clientWrapper) objectSearch(ctx context.Context, prm PrmObjectSearch) (ResObjectSearch, error) {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return ResObjectSearch{}, err
|
|
|
|
}
|
|
|
|
|
2023-10-23 13:55:36 +00:00
|
|
|
cliPrm := sdkClient.PrmObjectSearch{
|
|
|
|
ContainerID: &prm.cnrID,
|
|
|
|
Filters: prm.filters,
|
|
|
|
Session: prm.stoken,
|
|
|
|
BearerToken: prm.btoken,
|
|
|
|
Key: prm.key,
|
2022-08-23 18:56:55 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
res, err := cl.ObjectSearchInit(ctx, cliPrm)
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, nil, err); err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return ResObjectSearch{}, fmt.Errorf("init object searching on client: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
return ResObjectSearch{r: res}, nil
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// sessionCreate invokes sdkClient.SessionCreate parse response status to error and return result as is.
|
2022-08-08 09:15:10 +00:00
|
|
|
func (c *clientWrapper) sessionCreate(ctx context.Context, prm prmCreateSession) (resCreateSession, error) {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return resCreateSession{}, err
|
|
|
|
}
|
|
|
|
|
2023-09-05 14:32:03 +00:00
|
|
|
cliPrm := sdkClient.PrmSessionCreate{
|
|
|
|
Expiration: prm.exp,
|
|
|
|
Key: &prm.key,
|
|
|
|
}
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
start := time.Now()
|
2022-11-03 14:58:38 +00:00
|
|
|
res, err := cl.SessionCreate(ctx, cliPrm)
|
2022-07-19 13:13:59 +00:00
|
|
|
c.incRequests(time.Since(start), methodSessionCreate)
|
2022-07-14 12:45:25 +00:00
|
|
|
var st apistatus.Status
|
|
|
|
if res != nil {
|
|
|
|
st = res.Status()
|
|
|
|
}
|
2023-03-29 10:20:37 +00:00
|
|
|
if err = c.handleError(ctx, st, err); err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return resCreateSession{}, fmt.Errorf("session creation on client: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
return resCreateSession{
|
2022-03-21 11:29:29 +00:00
|
|
|
id: res.ID(),
|
|
|
|
sessionKey: res.PublicKey(),
|
|
|
|
}, nil
|
2022-03-15 12:00:38 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:57:04 +00:00
|
|
|
func (c *clientStatusMonitor) isHealthy() bool {
|
2023-10-03 10:57:58 +00:00
|
|
|
return c.healthy.Load() == statusHealthy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientStatusMonitor) isDialed() bool {
|
|
|
|
return c.healthy.Load() != statusUnhealthyOnDial
|
2022-07-12 18:55:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
func (c *clientStatusMonitor) setHealthy() {
|
2023-10-03 10:57:58 +00:00
|
|
|
c.healthy.Store(statusHealthy)
|
2022-11-03 14:58:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientStatusMonitor) setUnhealthy() {
|
2023-10-03 10:57:58 +00:00
|
|
|
c.healthy.Store(statusUnhealthyOnRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientStatusMonitor) setUnhealthyOnDial() {
|
|
|
|
c.healthy.Store(statusUnhealthyOnDial)
|
2022-07-12 18:55:59 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:57:04 +00:00
|
|
|
func (c *clientStatusMonitor) address() string {
|
2022-07-12 18:55:59 +00:00
|
|
|
return c.addr
|
|
|
|
}
|
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
func (c *clientStatusMonitor) incErrorRate() {
|
2022-07-18 08:42:29 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
c.currentErrorCount++
|
|
|
|
c.overallErrorCount++
|
2023-01-11 12:48:42 +00:00
|
|
|
|
|
|
|
thresholdReached := c.currentErrorCount >= c.errorThreshold
|
|
|
|
if thresholdReached {
|
2022-11-03 14:58:38 +00:00
|
|
|
c.setUnhealthy()
|
2022-07-18 08:42:29 +00:00
|
|
|
c.currentErrorCount = 0
|
2022-07-13 10:31:24 +00:00
|
|
|
}
|
2023-01-11 12:48:42 +00:00
|
|
|
c.mu.Unlock()
|
|
|
|
|
2023-08-01 08:50:18 +00:00
|
|
|
if thresholdReached {
|
|
|
|
c.log(zapcore.WarnLevel, "error threshold reached",
|
2023-01-11 12:48:42 +00:00
|
|
|
zap.String("address", c.addr), zap.Uint32("threshold", c.errorThreshold))
|
|
|
|
}
|
2022-07-13 09:51:43 +00:00
|
|
|
}
|
|
|
|
|
2023-08-01 08:50:18 +00:00
|
|
|
func (c *clientStatusMonitor) log(level zapcore.Level, msg string, fields ...zap.Field) {
|
|
|
|
if c.logger == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.Log(level, msg, fields...)
|
|
|
|
}
|
|
|
|
|
2022-07-13 09:51:43 +00:00
|
|
|
func (c *clientStatusMonitor) currentErrorRate() uint32 {
|
2022-07-18 08:42:29 +00:00
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
return c.currentErrorCount
|
2022-07-13 09:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientStatusMonitor) overallErrorRate() uint64 {
|
2022-07-18 08:42:29 +00:00
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
return c.overallErrorCount
|
2022-07-13 09:51:43 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 08:06:16 +00:00
|
|
|
func (c *clientStatusMonitor) methodsStatus() []statusSnapshot {
|
|
|
|
result := make([]statusSnapshot, len(c.methods))
|
|
|
|
for i, val := range c.methods {
|
|
|
|
result[i] = val.snapshot()
|
2022-07-13 10:16:55 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 13:13:59 +00:00
|
|
|
return result
|
2022-07-13 09:51:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 12:03:50 +00:00
|
|
|
func (c *clientWrapper) incRequests(elapsed time.Duration, method MethodIndex) {
|
2022-07-22 08:06:16 +00:00
|
|
|
methodStat := c.methods[method]
|
|
|
|
methodStat.incRequests(elapsed)
|
2022-11-29 12:03:50 +00:00
|
|
|
if c.prm.poolRequestInfoCallback != nil {
|
|
|
|
c.prm.poolRequestInfoCallback(RequestInfo{
|
|
|
|
Address: c.prm.address,
|
|
|
|
Method: method,
|
|
|
|
Elapsed: elapsed,
|
|
|
|
})
|
|
|
|
}
|
2022-07-12 18:55:59 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 09:14:37 +00:00
|
|
|
func (c *clientWrapper) close() error {
|
|
|
|
if c.client != nil {
|
|
|
|
return c.client.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-29 10:20:37 +00:00
|
|
|
func (c *clientStatusMonitor) handleError(ctx context.Context, st apistatus.Status, err error) error {
|
2022-07-12 18:55:59 +00:00
|
|
|
if err != nil {
|
2023-03-29 10:20:37 +00:00
|
|
|
if needCountError(ctx, err) {
|
2023-01-19 12:04:21 +00:00
|
|
|
c.incErrorRate()
|
|
|
|
}
|
|
|
|
|
2022-07-12 18:55:59 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = apistatus.ErrFromStatus(st)
|
|
|
|
switch err.(type) {
|
2023-08-02 07:01:03 +00:00
|
|
|
case *apistatus.ServerInternal,
|
|
|
|
*apistatus.WrongMagicNumber,
|
|
|
|
*apistatus.SignatureVerification,
|
|
|
|
*apistatus.NodeUnderMaintenance:
|
2022-07-13 09:51:43 +00:00
|
|
|
c.incErrorRate()
|
2022-07-12 18:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-29 10:20:37 +00:00
|
|
|
func needCountError(ctx context.Context, err error) bool {
|
2023-02-20 13:00:09 +00:00
|
|
|
// non-status logic error that could be returned
|
|
|
|
// from the SDK client; should not be considered
|
|
|
|
// as a connection error
|
|
|
|
var siErr *object.SplitInfoError
|
|
|
|
if errors.As(err, &siErr) {
|
|
|
|
return false
|
|
|
|
}
|
2024-04-22 06:51:20 +00:00
|
|
|
var eiErr *object.ECInfoError
|
|
|
|
if errors.As(err, &eiErr) {
|
|
|
|
return false
|
|
|
|
}
|
2023-02-20 13:00:09 +00:00
|
|
|
|
2023-03-29 10:20:37 +00:00
|
|
|
if errors.Is(ctx.Err(), context.Canceled) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2023-02-20 13:00:09 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 11:04:30 +00:00
|
|
|
// clientBuilder is a type alias of client constructors which open connection
|
|
|
|
// to the given endpoint.
|
2022-11-03 14:58:38 +00:00
|
|
|
type clientBuilder = func(endpoint string) client
|
2022-10-03 11:04:30 +00:00
|
|
|
|
2022-11-29 12:03:50 +00:00
|
|
|
// RequestInfo groups info about pool request.
|
|
|
|
type RequestInfo struct {
|
|
|
|
Address string
|
|
|
|
Method MethodIndex
|
|
|
|
Elapsed time.Duration
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
// InitParameters contains values used to initialize connection Pool.
|
2022-03-11 11:45:36 +00:00
|
|
|
type InitParameters struct {
|
2022-03-15 12:49:14 +00:00
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
logger *zap.Logger
|
|
|
|
nodeDialTimeout time.Duration
|
2022-11-15 11:32:25 +00:00
|
|
|
nodeStreamTimeout time.Duration
|
2022-03-15 12:49:14 +00:00
|
|
|
healthcheckTimeout time.Duration
|
|
|
|
clientRebalanceInterval time.Duration
|
|
|
|
sessionExpirationDuration uint64
|
2022-07-13 06:57:04 +00:00
|
|
|
errorThreshold uint32
|
2022-03-15 12:49:14 +00:00
|
|
|
nodeParams []NodeParam
|
2022-11-29 12:03:50 +00:00
|
|
|
requestCallback func(RequestInfo)
|
2023-06-08 12:54:48 +00:00
|
|
|
dialOptions []grpc.DialOption
|
2022-03-11 11:45:36 +00:00
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
clientBuilder clientBuilder
|
2022-03-11 11:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 12:49:14 +00:00
|
|
|
// SetKey specifies default key to be used for the protocol communication by default.
|
|
|
|
func (x *InitParameters) SetKey(key *ecdsa.PrivateKey) {
|
|
|
|
x.key = key
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLogger specifies logger.
|
|
|
|
func (x *InitParameters) SetLogger(logger *zap.Logger) {
|
|
|
|
x.logger = logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNodeDialTimeout specifies the timeout for connection to be established.
|
|
|
|
func (x *InitParameters) SetNodeDialTimeout(timeout time.Duration) {
|
|
|
|
x.nodeDialTimeout = timeout
|
|
|
|
}
|
|
|
|
|
2022-11-15 11:32:25 +00:00
|
|
|
// SetNodeStreamTimeout specifies the timeout for individual operations in streaming RPC.
|
|
|
|
func (x *InitParameters) SetNodeStreamTimeout(timeout time.Duration) {
|
|
|
|
x.nodeStreamTimeout = timeout
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:49:14 +00:00
|
|
|
// SetHealthcheckTimeout specifies the timeout for request to node to decide if it is alive.
|
|
|
|
//
|
|
|
|
// See also Pool.Dial.
|
|
|
|
func (x *InitParameters) SetHealthcheckTimeout(timeout time.Duration) {
|
|
|
|
x.healthcheckTimeout = timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetClientRebalanceInterval specifies the interval for updating nodes health status.
|
|
|
|
//
|
|
|
|
// See also Pool.Dial.
|
|
|
|
func (x *InitParameters) SetClientRebalanceInterval(interval time.Duration) {
|
|
|
|
x.clientRebalanceInterval = interval
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSessionExpirationDuration specifies the session token lifetime in epochs.
|
|
|
|
func (x *InitParameters) SetSessionExpirationDuration(expirationDuration uint64) {
|
|
|
|
x.sessionExpirationDuration = expirationDuration
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:57:04 +00:00
|
|
|
// SetErrorThreshold specifies the number of errors on connection after which node is considered as unhealthy.
|
|
|
|
func (x *InitParameters) SetErrorThreshold(threshold uint32) {
|
|
|
|
x.errorThreshold = threshold
|
|
|
|
}
|
|
|
|
|
2022-11-29 12:03:50 +00:00
|
|
|
// SetRequestCallback makes the pool client to pass RequestInfo for each
|
|
|
|
// request to f. Nil (default) means ignore RequestInfo.
|
|
|
|
func (x *InitParameters) SetRequestCallback(f func(RequestInfo)) {
|
|
|
|
x.requestCallback = f
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:49:14 +00:00
|
|
|
// AddNode append information about the node to which you want to connect.
|
|
|
|
func (x *InitParameters) AddNode(nodeParam NodeParam) {
|
|
|
|
x.nodeParams = append(x.nodeParams, nodeParam)
|
|
|
|
}
|
|
|
|
|
2023-06-08 12:54:48 +00:00
|
|
|
// SetGRPCDialOptions sets the gRPC dial options for new gRPC client connection.
|
|
|
|
func (x *InitParameters) SetGRPCDialOptions(opts ...grpc.DialOption) {
|
|
|
|
x.dialOptions = opts
|
|
|
|
}
|
|
|
|
|
2022-10-03 11:04:30 +00:00
|
|
|
// setClientBuilder sets clientBuilder used for client construction.
|
|
|
|
// Wraps setClientBuilderContext without a context.
|
|
|
|
func (x *InitParameters) setClientBuilder(builder clientBuilder) {
|
|
|
|
x.clientBuilder = builder
|
|
|
|
}
|
|
|
|
|
|
|
|
// isMissingClientBuilder checks if client constructor was not specified.
|
|
|
|
func (x *InitParameters) isMissingClientBuilder() bool {
|
|
|
|
return x.clientBuilder == nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
type rebalanceParameters struct {
|
|
|
|
nodesParams []*nodesParam
|
|
|
|
nodeRequestTimeout time.Duration
|
|
|
|
clientRebalanceInterval time.Duration
|
|
|
|
sessionExpirationDuration uint64
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
type nodesParam struct {
|
2021-11-16 13:50:33 +00:00
|
|
|
priority int
|
|
|
|
addresses []string
|
|
|
|
weights []float64
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// NodeParam groups parameters of remote node.
|
2021-11-16 13:50:33 +00:00
|
|
|
type NodeParam struct {
|
2022-03-15 12:49:14 +00:00
|
|
|
priority int
|
|
|
|
address string
|
|
|
|
weight float64
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeParam creates NodeParam using parameters.
|
|
|
|
func NewNodeParam(priority int, address string, weight float64) (prm NodeParam) {
|
|
|
|
prm.SetPriority(priority)
|
|
|
|
prm.SetAddress(address)
|
|
|
|
prm.SetWeight(weight)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPriority specifies priority of the node.
|
|
|
|
// Negative value is allowed. In the result node groups
|
|
|
|
// with the same priority will be sorted by descent.
|
|
|
|
func (x *NodeParam) SetPriority(priority int) {
|
|
|
|
x.priority = priority
|
|
|
|
}
|
|
|
|
|
2023-06-06 06:28:51 +00:00
|
|
|
// Priority returns priority of the node.
|
|
|
|
func (x *NodeParam) Priority() int {
|
|
|
|
return x.priority
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:49:14 +00:00
|
|
|
// SetAddress specifies address of the node.
|
|
|
|
func (x *NodeParam) SetAddress(address string) {
|
|
|
|
x.address = address
|
|
|
|
}
|
|
|
|
|
2023-06-06 06:28:51 +00:00
|
|
|
// Address returns address of the node.
|
|
|
|
func (x *NodeParam) Address() string {
|
|
|
|
return x.address
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:49:14 +00:00
|
|
|
// SetWeight specifies weight of the node.
|
|
|
|
func (x *NodeParam) SetWeight(weight float64) {
|
|
|
|
x.weight = weight
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
2023-06-06 06:28:51 +00:00
|
|
|
// Weight returns weight of the node.
|
|
|
|
func (x *NodeParam) Weight() float64 {
|
|
|
|
return x.weight
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// WaitParams contains parameters used in polling is a something applied on FrostFS network.
|
2022-03-23 07:50:34 +00:00
|
|
|
type WaitParams struct {
|
2023-08-03 09:14:00 +00:00
|
|
|
Timeout time.Duration
|
|
|
|
PollInterval time.Duration
|
2022-03-15 12:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetTimeout specifies the time to wait for the operation to complete.
|
2023-08-03 09:14:00 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use WaitParams.Timeout instead.
|
2022-03-23 07:50:34 +00:00
|
|
|
func (x *WaitParams) SetTimeout(timeout time.Duration) {
|
2023-08-03 09:14:00 +00:00
|
|
|
x.Timeout = timeout
|
2022-03-15 12:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetPollInterval specifies the interval, once it will check the completion of the operation.
|
2023-08-03 09:14:00 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use WaitParams.PollInterval instead.
|
2022-03-23 07:50:34 +00:00
|
|
|
func (x *WaitParams) SetPollInterval(tick time.Duration) {
|
2023-08-03 09:14:00 +00:00
|
|
|
x.PollInterval = tick
|
2021-06-22 09:15:21 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 11:08:50 +00:00
|
|
|
func defaultWaitParams() *WaitParams {
|
|
|
|
return &WaitParams{
|
2023-08-03 09:14:00 +00:00
|
|
|
Timeout: 120 * time.Second,
|
|
|
|
PollInterval: 5 * time.Second,
|
2023-07-31 11:08:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 08:06:23 +00:00
|
|
|
// checkForPositive panics if any of the wait params isn't positive.
|
|
|
|
func (x *WaitParams) checkForPositive() {
|
2023-08-03 09:14:00 +00:00
|
|
|
if x.Timeout <= 0 || x.PollInterval <= 0 {
|
2022-03-28 08:06:23 +00:00
|
|
|
panic("all wait params must be positive")
|
2021-06-22 09:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 09:11:14 +00:00
|
|
|
// CheckForValid checks if all wait params are non-negative.
|
2023-08-03 09:14:00 +00:00
|
|
|
func (x *WaitParams) CheckValidity() error {
|
|
|
|
if x.Timeout <= 0 {
|
2023-08-03 09:11:14 +00:00
|
|
|
return errors.New("timeout cannot be negative")
|
|
|
|
}
|
2023-08-03 09:14:00 +00:00
|
|
|
if x.PollInterval <= 0 {
|
2023-08-03 09:11:14 +00:00
|
|
|
return errors.New("poll interval cannot be negative")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
type prmContext struct {
|
2022-03-11 13:06:08 +00:00
|
|
|
defaultSession bool
|
2022-04-07 16:09:15 +00:00
|
|
|
verb session.ObjectVerb
|
2022-05-25 09:03:22 +00:00
|
|
|
cnr cid.ID
|
|
|
|
|
|
|
|
objSet bool
|
2022-11-11 10:17:26 +00:00
|
|
|
objs []oid.ID
|
2021-11-15 06:35:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
func (x *prmContext) useDefaultSession() {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.defaultSession = true
|
2021-11-15 06:35:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 09:03:22 +00:00
|
|
|
func (x *prmContext) useContainer(cnr cid.ID) {
|
|
|
|
x.cnr = cnr
|
|
|
|
}
|
|
|
|
|
2022-11-11 10:17:26 +00:00
|
|
|
func (x *prmContext) useObjects(ids []oid.ID) {
|
|
|
|
x.objs = ids
|
|
|
|
x.objSet = true
|
|
|
|
}
|
|
|
|
|
2022-05-25 09:03:22 +00:00
|
|
|
func (x *prmContext) useAddress(addr oid.Address) {
|
|
|
|
x.cnr = addr.Container()
|
2022-11-11 10:17:26 +00:00
|
|
|
x.objs = []oid.ID{addr.Object()}
|
2022-05-25 09:03:22 +00:00
|
|
|
x.objSet = true
|
2021-11-15 06:35:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
func (x *prmContext) useVerb(verb session.ObjectVerb) {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.verb = verb
|
2021-11-15 06:35:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
type prmCommon struct {
|
|
|
|
key *ecdsa.PrivateKey
|
2022-03-24 08:05:41 +00:00
|
|
|
btoken *bearer.Token
|
2022-04-07 16:09:15 +00:00
|
|
|
stoken *session.Object
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
// UseKey specifies private key to sign the requests.
|
|
|
|
// If key is not provided, then Pool default key is used.
|
|
|
|
func (x *prmCommon) UseKey(key *ecdsa.PrivateKey) {
|
|
|
|
x.key = key
|
2021-12-16 15:46:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
// UseBearer attaches bearer token to be used for the operation.
|
2022-03-24 08:05:41 +00:00
|
|
|
func (x *prmCommon) UseBearer(token bearer.Token) {
|
|
|
|
x.btoken = &token
|
2022-02-21 11:30:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 09:53:04 +00:00
|
|
|
// UseSession specifies session within which operation should be performed.
|
2022-04-07 16:09:15 +00:00
|
|
|
func (x *prmCommon) UseSession(token session.Object) {
|
2022-03-21 11:29:29 +00:00
|
|
|
x.stoken = &token
|
2022-02-21 11:30:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
// PrmObjectPut groups parameters of PutObject operation.
|
|
|
|
type PrmObjectPut struct {
|
|
|
|
prmCommon
|
|
|
|
|
|
|
|
hdr object.Object
|
|
|
|
|
|
|
|
payload io.Reader
|
2022-07-29 15:42:24 +00:00
|
|
|
|
2023-04-17 18:59:50 +00:00
|
|
|
copiesNumber []uint32
|
2023-07-11 09:02:23 +00:00
|
|
|
|
|
|
|
clientCut bool
|
|
|
|
networkInfo netmap.NetworkInfo
|
2023-08-25 06:36:08 +00:00
|
|
|
|
|
|
|
withoutHomomorphicHash bool
|
|
|
|
|
|
|
|
bufferMaxSize uint64
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetHeader specifies header of the object.
|
|
|
|
func (x *PrmObjectPut) SetHeader(hdr object.Object) {
|
|
|
|
x.hdr = hdr
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPayload specifies payload of the object.
|
|
|
|
func (x *PrmObjectPut) SetPayload(payload io.Reader) {
|
|
|
|
x.payload = payload
|
|
|
|
}
|
|
|
|
|
2022-07-29 15:42:24 +00:00
|
|
|
// SetCopiesNumber sets number of object copies that is enough to consider put successful.
|
|
|
|
// Zero means using default behavior.
|
|
|
|
func (x *PrmObjectPut) SetCopiesNumber(copiesNumber uint32) {
|
2023-04-17 18:59:50 +00:00
|
|
|
x.copiesNumber = []uint32{copiesNumber}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCopiesNumberVector sets number of object copies that is enough to consider put successful, provided as array.
|
|
|
|
// Nil/empty vector means using default behavior.
|
|
|
|
func (x *PrmObjectPut) SetCopiesNumberVector(copiesNumber []uint32) {
|
2022-07-29 15:42:24 +00:00
|
|
|
x.copiesNumber = copiesNumber
|
|
|
|
}
|
|
|
|
|
2023-07-11 09:02:23 +00:00
|
|
|
// SetClientCut enables client cut for objects. It means that full object is prepared on client side
|
|
|
|
// and retrying is possible. But this leads to additional memory using for buffering object parts.
|
|
|
|
// Buffer size for every put is MaxObjectSize value from FrostFS network.
|
|
|
|
// There is limit for total memory allocation for in-flight request and
|
|
|
|
// can be set by InitParameters.SetMaxClientCutMemory (default value is 1gb).
|
|
|
|
// Put requests will fail if this limit be reached.
|
|
|
|
func (x *PrmObjectPut) SetClientCut(clientCut bool) {
|
|
|
|
x.clientCut = clientCut
|
|
|
|
}
|
|
|
|
|
2023-08-25 06:36:08 +00:00
|
|
|
// WithoutHomomorphicHash if set to true do not use Tillich-Zémor hash for payload.
|
|
|
|
func (x *PrmObjectPut) WithoutHomomorphicHash(v bool) {
|
|
|
|
x.withoutHomomorphicHash = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBufferMaxSize sets max buffer size to read payload.
|
|
|
|
// This value isn't used if object size is set explicitly and less than this value.
|
|
|
|
// Default value 3MB.
|
|
|
|
func (x *PrmObjectPut) SetBufferMaxSize(size uint64) {
|
|
|
|
x.bufferMaxSize = size
|
|
|
|
}
|
|
|
|
|
2023-07-11 09:02:23 +00:00
|
|
|
func (x *PrmObjectPut) setNetworkInfo(ni netmap.NetworkInfo) {
|
|
|
|
x.networkInfo = ni
|
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
// PrmObjectDelete groups parameters of DeleteObject operation.
|
|
|
|
type PrmObjectDelete struct {
|
|
|
|
prmCommon
|
|
|
|
|
2022-05-25 09:03:22 +00:00
|
|
|
addr oid.Address
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// SetAddress specifies FrostFS address of the object.
|
2022-05-25 09:03:22 +00:00
|
|
|
func (x *PrmObjectDelete) SetAddress(addr oid.Address) {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmObjectGet groups parameters of GetObject operation.
|
|
|
|
type PrmObjectGet struct {
|
|
|
|
prmCommon
|
|
|
|
|
2022-05-25 09:03:22 +00:00
|
|
|
addr oid.Address
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// SetAddress specifies FrostFS address of the object.
|
2022-05-25 09:03:22 +00:00
|
|
|
func (x *PrmObjectGet) SetAddress(addr oid.Address) {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmObjectHead groups parameters of HeadObject operation.
|
|
|
|
type PrmObjectHead struct {
|
|
|
|
prmCommon
|
|
|
|
|
2022-05-25 09:03:22 +00:00
|
|
|
addr oid.Address
|
2022-11-11 10:17:26 +00:00
|
|
|
raw bool
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// SetAddress specifies FrostFS address of the object.
|
2022-05-25 09:03:22 +00:00
|
|
|
func (x *PrmObjectHead) SetAddress(addr oid.Address) {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.addr = addr
|
|
|
|
}
|
|
|
|
|
2022-11-11 10:17:26 +00:00
|
|
|
// MarkRaw marks an intent to read physically stored object.
|
|
|
|
func (x *PrmObjectHead) MarkRaw() {
|
|
|
|
x.raw = true
|
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
// PrmObjectRange groups parameters of RangeObject operation.
|
|
|
|
type PrmObjectRange struct {
|
|
|
|
prmCommon
|
|
|
|
|
2022-05-25 09:03:22 +00:00
|
|
|
addr oid.Address
|
2022-03-11 13:06:08 +00:00
|
|
|
off, ln uint64
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// SetAddress specifies FrostFS address of the object.
|
2022-05-25 09:03:22 +00:00
|
|
|
func (x *PrmObjectRange) SetAddress(addr oid.Address) {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOffset sets offset of the payload range to be read.
|
|
|
|
func (x *PrmObjectRange) SetOffset(offset uint64) {
|
|
|
|
x.off = offset
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLength sets length of the payload range to be read.
|
|
|
|
func (x *PrmObjectRange) SetLength(length uint64) {
|
|
|
|
x.ln = length
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmObjectSearch groups parameters of SearchObjects operation.
|
|
|
|
type PrmObjectSearch struct {
|
|
|
|
prmCommon
|
|
|
|
|
|
|
|
cnrID cid.ID
|
|
|
|
filters object.SearchFilters
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainerID specifies the container in which to look for objects.
|
|
|
|
func (x *PrmObjectSearch) SetContainerID(cnrID cid.ID) {
|
|
|
|
x.cnrID = cnrID
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFilters specifies filters by which to select objects.
|
|
|
|
func (x *PrmObjectSearch) SetFilters(filters object.SearchFilters) {
|
|
|
|
x.filters = filters
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerPut groups parameters of PutContainer operation.
|
|
|
|
type PrmContainerPut struct {
|
2023-07-31 11:08:50 +00:00
|
|
|
ClientParams sdkClient.PrmContainerPut
|
2022-03-28 08:06:23 +00:00
|
|
|
|
2023-07-31 11:08:50 +00:00
|
|
|
WaitParams *WaitParams
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 14:50:39 +00:00
|
|
|
// SetContainer container structure to be used as a parameter of the base
|
|
|
|
// client's operation.
|
|
|
|
//
|
2023-03-07 11:20:03 +00:00
|
|
|
// See git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client.PrmContainerPut.SetContainer.
|
2023-07-31 11:08:50 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmContainerPut.ClientParams.Container instead.
|
2022-03-15 12:49:14 +00:00
|
|
|
func (x *PrmContainerPut) SetContainer(cnr container.Container) {
|
2023-07-31 11:08:50 +00:00
|
|
|
x.ClientParams.SetContainer(cnr)
|
2022-06-21 14:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithinSession specifies session to be used as a parameter of the base
|
|
|
|
// client's operation.
|
|
|
|
//
|
2023-03-07 11:20:03 +00:00
|
|
|
// See git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client.PrmContainerPut.WithinSession.
|
2023-07-31 11:08:50 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmContainerPut.ClientParams.Session instead.
|
2022-06-21 14:50:39 +00:00
|
|
|
func (x *PrmContainerPut) WithinSession(s session.Container) {
|
2023-07-31 11:08:50 +00:00
|
|
|
x.ClientParams.WithinSession(s)
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 08:06:23 +00:00
|
|
|
// SetWaitParams specifies timeout params to complete operation.
|
|
|
|
// If not provided the default one will be used.
|
|
|
|
// Panics if any of the wait params isn't positive.
|
2023-07-31 11:08:50 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmContainerPut.ClientParams.WaitParams instead.
|
2022-03-28 08:06:23 +00:00
|
|
|
func (x *PrmContainerPut) SetWaitParams(waitParams WaitParams) {
|
|
|
|
waitParams.checkForPositive()
|
2023-07-31 11:08:50 +00:00
|
|
|
x.WaitParams = &waitParams
|
2022-03-28 08:06:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
// PrmContainerGet groups parameters of GetContainer operation.
|
|
|
|
type PrmContainerGet struct {
|
2023-08-02 15:56:41 +00:00
|
|
|
ContainerID cid.ID
|
2024-03-01 12:16:16 +00:00
|
|
|
|
|
|
|
Session *session.Container
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainerID specifies identifier of the container to be read.
|
2023-08-02 15:56:41 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmContainerGet.ContainerID instead.
|
|
|
|
func (prm *PrmContainerGet) SetContainerID(cnrID cid.ID) {
|
|
|
|
prm.ContainerID = cnrID
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerList groups parameters of ListContainers operation.
|
|
|
|
type PrmContainerList struct {
|
2024-03-01 12:16:16 +00:00
|
|
|
OwnerID user.ID
|
|
|
|
|
|
|
|
Session *session.Container
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// SetOwnerID specifies identifier of the FrostFS account to list the containers.
|
2024-03-01 12:16:16 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmContainerList.OwnerID instead.
|
2022-04-11 06:30:22 +00:00
|
|
|
func (x *PrmContainerList) SetOwnerID(ownerID user.ID) {
|
2024-03-01 12:16:16 +00:00
|
|
|
x.OwnerID = ownerID
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerDelete groups parameters of DeleteContainer operation.
|
|
|
|
type PrmContainerDelete struct {
|
2023-08-03 09:11:14 +00:00
|
|
|
ContainerID cid.ID
|
2022-04-12 10:16:07 +00:00
|
|
|
|
2023-08-03 09:11:14 +00:00
|
|
|
Session *session.Container
|
2022-03-28 08:06:23 +00:00
|
|
|
|
2023-08-03 09:11:14 +00:00
|
|
|
WaitParams *WaitParams
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// SetContainerID specifies identifier of the FrostFS container to be removed.
|
2023-08-03 09:11:14 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmContainerDelete.ContainerID instead.
|
2022-03-15 12:49:14 +00:00
|
|
|
func (x *PrmContainerDelete) SetContainerID(cnrID cid.ID) {
|
2023-08-03 09:11:14 +00:00
|
|
|
x.ContainerID = cnrID
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 09:53:04 +00:00
|
|
|
// SetSessionToken specifies session within which operation should be performed.
|
2023-08-03 09:11:14 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmContainerDelete.Session instead.
|
2022-04-07 16:09:15 +00:00
|
|
|
func (x *PrmContainerDelete) SetSessionToken(token session.Container) {
|
2023-08-03 09:11:14 +00:00
|
|
|
x.Session = &token
|
2022-03-17 09:53:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 08:06:23 +00:00
|
|
|
// SetWaitParams specifies timeout params to complete operation.
|
|
|
|
// If not provided the default one will be used.
|
|
|
|
// Panics if any of the wait params isn't positive.
|
2023-08-03 09:11:14 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmContainerDelete.WaitParams instead.
|
2022-03-28 08:06:23 +00:00
|
|
|
func (x *PrmContainerDelete) SetWaitParams(waitParams WaitParams) {
|
|
|
|
waitParams.checkForPositive()
|
2023-08-03 09:11:14 +00:00
|
|
|
x.WaitParams = &waitParams
|
2022-03-28 08:06:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
// PrmContainerEACL groups parameters of GetEACL operation.
|
|
|
|
type PrmContainerEACL struct {
|
2023-08-11 08:06:45 +00:00
|
|
|
ContainerID cid.ID
|
2024-03-01 12:16:16 +00:00
|
|
|
|
|
|
|
Session *session.Container
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// SetContainerID specifies identifier of the FrostFS container to read the eACL table.
|
2024-03-01 12:16:16 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmContainerEACL.ContainerID instead.
|
2022-03-15 12:49:14 +00:00
|
|
|
func (x *PrmContainerEACL) SetContainerID(cnrID cid.ID) {
|
2023-08-11 08:06:45 +00:00
|
|
|
x.ContainerID = cnrID
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerSetEACL groups parameters of SetEACL operation.
|
|
|
|
type PrmContainerSetEACL struct {
|
2023-08-18 15:07:51 +00:00
|
|
|
Table eacl.Table
|
2022-03-28 08:06:23 +00:00
|
|
|
|
2023-08-18 15:07:51 +00:00
|
|
|
Session *session.Container
|
2022-06-21 14:50:39 +00:00
|
|
|
|
2023-08-18 15:07:51 +00:00
|
|
|
WaitParams *WaitParams
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 14:50:39 +00:00
|
|
|
// SetTable sets structure of container's extended ACL to be used as a
|
|
|
|
// parameter of the base client's operation.
|
|
|
|
//
|
2023-03-07 11:20:03 +00:00
|
|
|
// See git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client.PrmContainerSetEACL.SetTable.
|
2023-08-18 15:07:51 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmContainerSetEACL.Table instead.
|
2022-03-15 12:49:14 +00:00
|
|
|
func (x *PrmContainerSetEACL) SetTable(table eacl.Table) {
|
2023-08-18 15:07:51 +00:00
|
|
|
x.Table = table
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 14:50:39 +00:00
|
|
|
// WithinSession specifies session to be used as a parameter of the base
|
|
|
|
// client's operation.
|
|
|
|
//
|
2023-03-07 11:20:03 +00:00
|
|
|
// See git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client.PrmContainerSetEACL.WithinSession.
|
2023-08-18 15:07:51 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmContainerSetEACL.Session instead.
|
2022-06-21 14:50:39 +00:00
|
|
|
func (x *PrmContainerSetEACL) WithinSession(s session.Container) {
|
2023-08-18 15:07:51 +00:00
|
|
|
x.Session = &s
|
2022-06-21 14:50:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 08:06:23 +00:00
|
|
|
// SetWaitParams specifies timeout params to complete operation.
|
|
|
|
// If not provided the default one will be used.
|
|
|
|
// Panics if any of the wait params isn't positive.
|
2023-08-18 15:07:51 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmContainerSetEACL.WaitParams instead.
|
2022-03-28 08:06:23 +00:00
|
|
|
func (x *PrmContainerSetEACL) SetWaitParams(waitParams WaitParams) {
|
|
|
|
waitParams.checkForPositive()
|
2023-08-18 15:07:51 +00:00
|
|
|
x.WaitParams = &waitParams
|
2022-03-28 08:06:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
// PrmBalanceGet groups parameters of Balance operation.
|
|
|
|
type PrmBalanceGet struct {
|
2022-04-11 06:30:22 +00:00
|
|
|
account user.ID
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// SetAccount specifies identifier of the FrostFS account for which the balance is requested.
|
2022-04-11 06:30:22 +00:00
|
|
|
func (x *PrmBalanceGet) SetAccount(id user.ID) {
|
|
|
|
x.account = id
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 09:17:56 +00:00
|
|
|
// prmEndpointInfo groups parameters of sessionCreate operation.
|
|
|
|
type prmCreateSession struct {
|
2022-03-21 11:29:29 +00:00
|
|
|
exp uint64
|
2022-05-30 08:40:28 +00:00
|
|
|
key ecdsa.PrivateKey
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// setExp sets number of the last FrostFS epoch in the lifetime of the session after which it will be expired.
|
2022-05-30 08:40:28 +00:00
|
|
|
func (x *prmCreateSession) setExp(exp uint64) {
|
2022-03-21 11:29:29 +00:00
|
|
|
x.exp = exp
|
|
|
|
}
|
|
|
|
|
2022-05-30 08:40:28 +00:00
|
|
|
// useKey specifies owner private key for session token.
|
|
|
|
// If key is not provided, then Pool default key is used.
|
|
|
|
func (x *prmCreateSession) useKey(key ecdsa.PrivateKey) {
|
|
|
|
x.key = key
|
|
|
|
}
|
|
|
|
|
2022-03-28 09:17:56 +00:00
|
|
|
// prmEndpointInfo groups parameters of endpointInfo operation.
|
|
|
|
type prmEndpointInfo struct{}
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2022-03-28 09:17:56 +00:00
|
|
|
// prmNetworkInfo groups parameters of networkInfo operation.
|
|
|
|
type prmNetworkInfo struct{}
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2023-10-18 08:30:25 +00:00
|
|
|
// prmNetMapSnapshot groups parameters of netMapSnapshot operation.
|
|
|
|
type prmNetMapSnapshot struct{}
|
|
|
|
|
2022-03-28 09:17:56 +00:00
|
|
|
// resCreateSession groups resulting values of sessionCreate operation.
|
|
|
|
type resCreateSession struct {
|
2022-03-21 11:29:29 +00:00
|
|
|
id []byte
|
|
|
|
|
|
|
|
sessionKey []byte
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// Pool represents virtual connection to the FrostFS network to communicate
|
|
|
|
// with multiple FrostFS servers without thinking about switching between servers
|
2022-03-15 08:54:15 +00:00
|
|
|
// due to load balancing proportions or their unavailability.
|
2022-03-15 12:00:38 +00:00
|
|
|
// It is designed to provide a convenient abstraction from the multiple sdkClient.client types.
|
2022-03-15 08:54:15 +00:00
|
|
|
//
|
|
|
|
// Pool can be created and initialized using NewPool function.
|
2022-12-29 10:46:18 +00:00
|
|
|
// Before executing the FrostFS operations using the Pool, connection to the
|
2022-03-15 08:54:15 +00:00
|
|
|
// servers MUST BE correctly established (see Dial method).
|
|
|
|
// Using the Pool before connecting have been established can lead to a panic.
|
|
|
|
// After the work, the Pool SHOULD BE closed (see Close method): it frees internal
|
|
|
|
// and system resources which were allocated for the period of work of the Pool.
|
|
|
|
// Calling Dial/Close methods during the communication process step strongly discouraged
|
|
|
|
// as it leads to undefined behavior.
|
|
|
|
//
|
2022-12-29 10:46:18 +00:00
|
|
|
// Each method which produces a FrostFS API call may return an error.
|
2022-03-15 08:54:15 +00:00
|
|
|
// Status of underlying server response is casted to built-in error instance.
|
2022-03-15 12:00:38 +00:00
|
|
|
// Certain statuses can be checked using `sdkClient` and standard `errors` packages.
|
2022-03-15 08:54:15 +00:00
|
|
|
// Note that package provides some helper functions to work with status returns
|
2022-03-15 12:00:38 +00:00
|
|
|
// (e.g. sdkClient.IsErrContainerNotFound, sdkClient.IsErrObjectNotFound).
|
2022-03-15 08:54:15 +00:00
|
|
|
//
|
|
|
|
// See pool package overview to get some examples.
|
2022-03-09 09:40:23 +00:00
|
|
|
type Pool struct {
|
2022-02-02 11:21:23 +00:00
|
|
|
innerPools []*innerPool
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
cancel context.CancelFunc
|
|
|
|
closedCh chan struct{}
|
2022-03-11 11:57:35 +00:00
|
|
|
cache *sessionCache
|
2022-02-02 11:21:23 +00:00
|
|
|
stokenDuration uint64
|
2022-03-11 14:55:02 +00:00
|
|
|
rebalanceParams rebalanceParameters
|
2022-11-03 14:58:38 +00:00
|
|
|
clientBuilder clientBuilder
|
2022-03-11 14:55:02 +00:00
|
|
|
logger *zap.Logger
|
2023-07-11 09:02:23 +00:00
|
|
|
|
2023-08-01 09:52:18 +00:00
|
|
|
maxObjectSize uint64
|
2021-11-16 13:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type innerPool struct {
|
2022-07-12 18:55:59 +00:00
|
|
|
lock sync.RWMutex
|
|
|
|
sampler *sampler
|
|
|
|
clients []client
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 11:21:23 +00:00
|
|
|
const (
|
2022-02-11 14:40:09 +00:00
|
|
|
defaultSessionTokenExpirationDuration = 100 // in blocks
|
2022-07-13 06:57:04 +00:00
|
|
|
defaultErrorThreshold = 100
|
2022-02-11 14:40:09 +00:00
|
|
|
|
2023-01-11 12:53:02 +00:00
|
|
|
defaultRebalanceInterval = 15 * time.Second
|
2022-11-15 11:32:25 +00:00
|
|
|
defaultHealthcheckTimeout = 4 * time.Second
|
|
|
|
defaultDialTimeout = 5 * time.Second
|
|
|
|
defaultStreamTimeout = 10 * time.Second
|
2023-08-25 06:36:08 +00:00
|
|
|
|
|
|
|
defaultBufferMaxSizeForPut = 3 * 1024 * 1024 // 3 MB
|
2022-02-02 11:21:23 +00:00
|
|
|
)
|
2022-02-02 13:45:01 +00:00
|
|
|
|
2022-03-15 12:49:14 +00:00
|
|
|
// NewPool creates connection pool using parameters.
|
2022-03-11 14:55:02 +00:00
|
|
|
func NewPool(options InitParameters) (*Pool, error) {
|
2022-03-15 12:49:14 +00:00
|
|
|
if options.key == nil {
|
2022-03-11 11:45:36 +00:00
|
|
|
return nil, fmt.Errorf("missed required parameter 'Key'")
|
2021-11-08 13:28:28 +00:00
|
|
|
}
|
2021-10-25 12:57:55 +00:00
|
|
|
|
2022-03-15 12:49:14 +00:00
|
|
|
nodesParams, err := adjustNodeParams(options.nodeParams)
|
2022-03-11 11:45:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-10 12:59:31 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 11:57:35 +00:00
|
|
|
cache, err := newCache()
|
2022-03-11 11:45:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't create cache: %w", err)
|
2022-02-02 11:21:23 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 12:27:49 +00:00
|
|
|
fillDefaultInitParams(&options, cache)
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool := &Pool{
|
2022-03-24 14:35:09 +00:00
|
|
|
key: options.key,
|
|
|
|
cache: cache,
|
|
|
|
logger: options.logger,
|
|
|
|
stokenDuration: options.sessionExpirationDuration,
|
2022-03-11 14:55:02 +00:00
|
|
|
rebalanceParams: rebalanceParameters{
|
|
|
|
nodesParams: nodesParams,
|
2022-03-15 12:49:14 +00:00
|
|
|
nodeRequestTimeout: options.healthcheckTimeout,
|
|
|
|
clientRebalanceInterval: options.clientRebalanceInterval,
|
|
|
|
sessionExpirationDuration: options.sessionExpirationDuration,
|
2022-03-11 14:55:02 +00:00
|
|
|
},
|
2023-08-01 09:52:18 +00:00
|
|
|
clientBuilder: options.clientBuilder,
|
2022-03-11 14:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pool, nil
|
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// Dial establishes a connection to the servers from the FrostFS network.
|
2022-03-15 12:49:14 +00:00
|
|
|
// It also starts a routine that checks the health of the nodes and
|
|
|
|
// updates the weights of the nodes for balancing.
|
|
|
|
// Returns an error describing failure reason.
|
|
|
|
//
|
|
|
|
// If failed, the Pool SHOULD NOT be used.
|
|
|
|
//
|
|
|
|
// See also InitParameters.SetClientRebalanceInterval.
|
2022-03-11 14:55:02 +00:00
|
|
|
func (p *Pool) Dial(ctx context.Context) error {
|
|
|
|
inner := make([]*innerPool, len(p.rebalanceParams.nodesParams))
|
2021-09-07 14:34:20 +00:00
|
|
|
var atLeastOneHealthy bool
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
for i, params := range p.rebalanceParams.nodesParams {
|
2022-07-12 18:55:59 +00:00
|
|
|
clients := make([]client, len(params.weights))
|
2022-02-07 20:27:56 +00:00
|
|
|
for j, addr := range params.addresses {
|
2022-11-08 09:50:23 +00:00
|
|
|
clients[j] = p.clientBuilder(addr)
|
|
|
|
if err := clients[j].dial(ctx); err != nil {
|
2023-01-11 12:48:42 +00:00
|
|
|
p.log(zap.WarnLevel, "failed to build client", zap.String("address", addr), zap.Error(err))
|
2022-11-08 09:50:23 +00:00
|
|
|
continue
|
2021-11-16 13:50:33 +00:00
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
|
2022-04-26 10:09:13 +00:00
|
|
|
var st session.Object
|
2023-07-11 09:02:23 +00:00
|
|
|
err := initSessionForDuration(ctx, &st, clients[j], p.rebalanceParams.sessionExpirationDuration, *p.key, false)
|
2022-11-03 14:58:38 +00:00
|
|
|
if err != nil {
|
2022-11-08 09:50:23 +00:00
|
|
|
clients[j].setUnhealthy()
|
2023-01-11 12:48:42 +00:00
|
|
|
p.log(zap.WarnLevel, "failed to create frostfs session token for client",
|
|
|
|
zap.String("address", addr), zap.Error(err))
|
2022-11-08 09:50:23 +00:00
|
|
|
continue
|
2021-11-16 13:50:33 +00:00
|
|
|
}
|
2022-11-03 14:58:38 +00:00
|
|
|
|
2023-07-11 09:02:23 +00:00
|
|
|
_ = p.cache.Put(formCacheKey(addr, p.key, false), st)
|
2022-11-08 09:50:23 +00:00
|
|
|
atLeastOneHealthy = true
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
2021-11-16 13:50:33 +00:00
|
|
|
source := rand.NewSource(time.Now().UnixNano())
|
2022-03-11 14:55:02 +00:00
|
|
|
sampl := newSampler(params.weights, source)
|
2021-11-16 13:50:33 +00:00
|
|
|
|
|
|
|
inner[i] = &innerPool{
|
2022-07-12 18:55:59 +00:00
|
|
|
sampler: sampl,
|
|
|
|
clients: clients,
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
2021-09-07 14:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !atLeastOneHealthy {
|
2022-03-11 14:55:02 +00:00
|
|
|
return fmt.Errorf("at least one node must be healthy")
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
2021-09-07 14:34:20 +00:00
|
|
|
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2022-03-11 14:55:02 +00:00
|
|
|
p.cancel = cancel
|
|
|
|
p.closedCh = make(chan struct{})
|
|
|
|
p.innerPools = inner
|
2022-03-11 11:45:36 +00:00
|
|
|
|
2023-07-11 09:02:23 +00:00
|
|
|
ni, err := p.NetworkInfo(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get network info for max object size: %w", err)
|
|
|
|
}
|
2023-08-01 09:52:18 +00:00
|
|
|
p.maxObjectSize = ni.MaxObjectSize()
|
2023-07-11 09:02:23 +00:00
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
go p.startRebalance(ctx)
|
|
|
|
return nil
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 12:48:42 +00:00
|
|
|
func (p *Pool) log(level zapcore.Level, msg string, fields ...zap.Field) {
|
|
|
|
if p.logger == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-17 14:13:05 +00:00
|
|
|
p.logger.Log(level, msg, fields...)
|
2023-01-11 12:48:42 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 12:27:49 +00:00
|
|
|
func fillDefaultInitParams(params *InitParameters, cache *sessionCache) {
|
2022-03-15 12:49:14 +00:00
|
|
|
if params.sessionExpirationDuration == 0 {
|
|
|
|
params.sessionExpirationDuration = defaultSessionTokenExpirationDuration
|
2022-03-11 11:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:57:04 +00:00
|
|
|
if params.errorThreshold == 0 {
|
|
|
|
params.errorThreshold = defaultErrorThreshold
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:49:14 +00:00
|
|
|
if params.clientRebalanceInterval <= 0 {
|
|
|
|
params.clientRebalanceInterval = defaultRebalanceInterval
|
2022-03-11 11:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 12:49:14 +00:00
|
|
|
if params.healthcheckTimeout <= 0 {
|
2022-11-15 11:32:25 +00:00
|
|
|
params.healthcheckTimeout = defaultHealthcheckTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.nodeDialTimeout <= 0 {
|
|
|
|
params.nodeDialTimeout = defaultDialTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.nodeStreamTimeout <= 0 {
|
|
|
|
params.nodeStreamTimeout = defaultStreamTimeout
|
2022-03-11 11:45:36 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 11:04:30 +00:00
|
|
|
if params.isMissingClientBuilder() {
|
2022-11-03 14:58:38 +00:00
|
|
|
params.setClientBuilder(func(addr string) client {
|
2022-03-21 11:29:29 +00:00
|
|
|
var prm wrapperPrm
|
|
|
|
prm.setAddress(addr)
|
|
|
|
prm.setKey(*params.key)
|
2023-08-01 08:50:18 +00:00
|
|
|
prm.setLogger(params.logger)
|
2022-11-15 11:32:25 +00:00
|
|
|
prm.setDialTimeout(params.nodeDialTimeout)
|
|
|
|
prm.setStreamTimeout(params.nodeStreamTimeout)
|
2022-07-13 06:57:04 +00:00
|
|
|
prm.setErrorThreshold(params.errorThreshold)
|
2022-11-29 12:03:50 +00:00
|
|
|
prm.setPoolRequestCallback(params.requestCallback)
|
2023-06-08 12:54:48 +00:00
|
|
|
prm.setGRPCDialOptions(params.dialOptions)
|
2022-03-21 11:29:29 +00:00
|
|
|
prm.setResponseInfoCallback(func(info sdkClient.ResponseMetaInfo) error {
|
2022-04-05 09:15:13 +00:00
|
|
|
cache.updateEpoch(info.Epoch())
|
2022-02-17 12:27:49 +00:00
|
|
|
return nil
|
|
|
|
})
|
2022-03-21 11:29:29 +00:00
|
|
|
return newWrapper(prm)
|
2022-10-03 11:04:30 +00:00
|
|
|
})
|
2022-03-11 11:45:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func adjustNodeParams(nodeParams []NodeParam) ([]*nodesParam, error) {
|
|
|
|
if len(nodeParams) == 0 {
|
2022-12-29 10:46:18 +00:00
|
|
|
return nil, errors.New("no FrostFS peers configured")
|
2022-03-11 11:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nodesParamsMap := make(map[int]*nodesParam)
|
|
|
|
for _, param := range nodeParams {
|
2022-03-15 12:49:14 +00:00
|
|
|
nodes, ok := nodesParamsMap[param.priority]
|
2022-03-11 11:45:36 +00:00
|
|
|
if !ok {
|
2022-03-15 12:49:14 +00:00
|
|
|
nodes = &nodesParam{priority: param.priority}
|
2022-03-11 11:45:36 +00:00
|
|
|
}
|
2022-03-15 12:49:14 +00:00
|
|
|
nodes.addresses = append(nodes.addresses, param.address)
|
|
|
|
nodes.weights = append(nodes.weights, param.weight)
|
|
|
|
nodesParamsMap[param.priority] = nodes
|
2022-03-11 11:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nodesParams := make([]*nodesParam, 0, len(nodesParamsMap))
|
|
|
|
for _, nodes := range nodesParamsMap {
|
|
|
|
nodes.weights = adjustWeights(nodes.weights)
|
|
|
|
nodesParams = append(nodesParams, nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(nodesParams, func(i, j int) bool {
|
|
|
|
return nodesParams[i].priority < nodesParams[j].priority
|
|
|
|
})
|
|
|
|
|
|
|
|
return nodesParams, nil
|
|
|
|
}
|
|
|
|
|
2022-07-22 14:41:44 +00:00
|
|
|
// startRebalance runs loop to monitor connection healthy status.
|
2022-03-11 14:55:02 +00:00
|
|
|
func (p *Pool) startRebalance(ctx context.Context) {
|
|
|
|
ticker := time.NewTimer(p.rebalanceParams.clientRebalanceInterval)
|
|
|
|
buffers := make([][]float64, len(p.rebalanceParams.nodesParams))
|
|
|
|
for i, params := range p.rebalanceParams.nodesParams {
|
2021-11-16 13:50:33 +00:00
|
|
|
buffers[i] = make([]float64, len(params.weights))
|
|
|
|
}
|
2021-06-21 11:12:08 +00:00
|
|
|
|
2021-07-20 08:02:14 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
close(p.closedCh)
|
2021-07-20 08:02:14 +00:00
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2022-03-11 14:55:02 +00:00
|
|
|
p.updateNodesHealth(ctx, buffers)
|
|
|
|
ticker.Reset(p.rebalanceParams.clientRebalanceInterval)
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
func (p *Pool) updateNodesHealth(ctx context.Context, buffers [][]float64) {
|
2021-11-16 13:50:33 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
for i, inner := range p.innerPools {
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
bufferWeights := buffers[i]
|
2024-03-14 09:27:43 +00:00
|
|
|
go func(i int, _ *innerPool) {
|
2021-11-16 13:50:33 +00:00
|
|
|
defer wg.Done()
|
2022-03-11 14:55:02 +00:00
|
|
|
p.updateInnerNodesHealth(ctx, i, bufferWeights)
|
2021-11-16 13:50:33 +00:00
|
|
|
}(i, inner)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
func (p *Pool) updateInnerNodesHealth(ctx context.Context, i int, bufferWeights []float64) {
|
|
|
|
if i > len(p.innerPools)-1 {
|
2021-11-16 13:50:33 +00:00
|
|
|
return
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
2022-03-11 14:55:02 +00:00
|
|
|
pool := p.innerPools[i]
|
|
|
|
options := p.rebalanceParams
|
2021-11-16 13:50:33 +00:00
|
|
|
|
2023-05-19 11:58:30 +00:00
|
|
|
healthyChanged := new(atomic.Bool)
|
2021-06-21 11:12:08 +00:00
|
|
|
wg := sync.WaitGroup{}
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-07-12 18:55:59 +00:00
|
|
|
for j, cli := range pool.clients {
|
2021-06-21 11:12:08 +00:00
|
|
|
wg.Add(1)
|
2022-03-15 12:00:38 +00:00
|
|
|
go func(j int, cli client) {
|
2021-06-21 11:12:08 +00:00
|
|
|
defer wg.Done()
|
2022-11-03 14:58:38 +00:00
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
tctx, c := context.WithTimeout(ctx, options.nodeRequestTimeout)
|
2021-06-21 11:12:08 +00:00
|
|
|
defer c()
|
2021-12-02 12:21:54 +00:00
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
healthy, changed := cli.restartIfUnhealthy(tctx)
|
|
|
|
if healthy {
|
2021-11-16 13:50:33 +00:00
|
|
|
bufferWeights[j] = options.nodesParams[i].weights[j]
|
2021-10-25 12:57:55 +00:00
|
|
|
} else {
|
2022-11-03 14:58:38 +00:00
|
|
|
bufferWeights[j] = 0
|
2022-07-12 18:55:59 +00:00
|
|
|
p.cache.DeleteByPrefix(cli.address())
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
2021-06-21 11:12:08 +00:00
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
if changed {
|
2023-01-11 12:48:42 +00:00
|
|
|
p.log(zap.DebugLevel, "health has changed",
|
|
|
|
zap.String("address", cli.address()), zap.Bool("healthy", healthy))
|
2022-07-12 18:55:59 +00:00
|
|
|
healthyChanged.Store(true)
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
2022-07-12 18:55:59 +00:00
|
|
|
}(j, cli)
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
2022-07-12 18:55:59 +00:00
|
|
|
if healthyChanged.Load() {
|
2021-06-21 11:12:08 +00:00
|
|
|
probabilities := adjustWeights(bufferWeights)
|
|
|
|
source := rand.NewSource(time.Now().UnixNano())
|
2022-03-11 14:55:02 +00:00
|
|
|
pool.lock.Lock()
|
|
|
|
pool.sampler = newSampler(probabilities, source)
|
|
|
|
pool.lock.Unlock()
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func adjustWeights(weights []float64) []float64 {
|
|
|
|
adjusted := make([]float64, len(weights))
|
|
|
|
sum := 0.0
|
|
|
|
for _, weight := range weights {
|
|
|
|
sum += weight
|
|
|
|
}
|
|
|
|
if sum > 0 {
|
|
|
|
for i, weight := range weights {
|
|
|
|
adjusted[i] = weight / sum
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return adjusted
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:55:59 +00:00
|
|
|
func (p *Pool) connection() (client, 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")
|
|
|
|
}
|
|
|
|
|
2022-07-12 18:55:59 +00:00
|
|
|
func (p *innerPool) connection() (client, error) {
|
2022-11-03 14:58:38 +00:00
|
|
|
p.lock.RLock() // need lock because of using p.sampler
|
2021-05-25 08:48:01 +00:00
|
|
|
defer p.lock.RUnlock()
|
2022-07-12 18:55:59 +00:00
|
|
|
if len(p.clients) == 1 {
|
|
|
|
cp := p.clients[0]
|
|
|
|
if cp.isHealthy() {
|
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
|
|
|
}
|
2022-07-12 18:55:59 +00:00
|
|
|
attempts := 3 * len(p.clients)
|
2021-05-25 08:48:01 +00:00
|
|
|
for k := 0; k < attempts; k++ {
|
|
|
|
i := p.sampler.Next()
|
2022-07-12 18:55:59 +00:00
|
|
|
if cp := p.clients[i]; cp.isHealthy() {
|
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
|
|
|
|
2023-07-11 09:02:23 +00:00
|
|
|
func formCacheKey(address string, key *ecdsa.PrivateKey, clientCut bool) string {
|
2021-12-10 13:56:04 +00:00
|
|
|
k := keys.PrivateKey{PrivateKey: *key}
|
2023-07-11 09:02:23 +00:00
|
|
|
|
|
|
|
stype := "server"
|
|
|
|
if clientCut {
|
|
|
|
stype = "client"
|
|
|
|
}
|
|
|
|
|
|
|
|
return address + stype + k.String()
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
func (p *Pool) checkSessionTokenErr(err error, address string) bool {
|
2021-10-25 12:57:55 +00:00
|
|
|
if err == nil {
|
2021-11-03 14:34:02 +00:00
|
|
|
return false
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 07:48:27 +00:00
|
|
|
if sdkClient.IsErrSessionNotFound(err) || sdkClient.IsErrSessionExpired(err) {
|
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
|
|
|
}
|
|
|
|
|
2023-07-11 09:02:23 +00:00
|
|
|
func initSessionForDuration(ctx context.Context, dst *session.Object, c client, dur uint64, ownerKey ecdsa.PrivateKey, clientCut bool) error {
|
2022-03-28 09:17:56 +00:00
|
|
|
ni, err := c.networkInfo(ctx, prmNetworkInfo{})
|
2022-02-02 13:25:36 +00:00
|
|
|
if err != nil {
|
2022-04-26 10:09:13 +00:00
|
|
|
return err
|
2022-02-02 13:25:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
epoch := ni.CurrentEpoch()
|
2022-02-02 13:25:36 +00:00
|
|
|
|
2022-04-05 09:15:13 +00:00
|
|
|
var exp uint64
|
2022-02-02 13:25:36 +00:00
|
|
|
if math.MaxUint64-epoch < dur {
|
2022-04-05 09:15:13 +00:00
|
|
|
exp = math.MaxUint64
|
2022-02-02 13:25:36 +00:00
|
|
|
} else {
|
2022-04-05 09:15:13 +00:00
|
|
|
exp = epoch + dur
|
2022-02-02 13:25:36 +00:00
|
|
|
}
|
2022-03-28 09:17:56 +00:00
|
|
|
var prm prmCreateSession
|
2022-05-30 08:40:28 +00:00
|
|
|
prm.setExp(exp)
|
|
|
|
prm.useKey(ownerKey)
|
2022-02-02 13:25:36 +00:00
|
|
|
|
2023-07-11 09:02:23 +00:00
|
|
|
var (
|
|
|
|
id uuid.UUID
|
|
|
|
key frostfsecdsa.PublicKey
|
|
|
|
)
|
2022-04-05 09:15:13 +00:00
|
|
|
|
2023-07-11 09:02:23 +00:00
|
|
|
if clientCut {
|
|
|
|
id = uuid.New()
|
|
|
|
key = frostfsecdsa.PublicKey(ownerKey.PublicKey)
|
|
|
|
} else {
|
|
|
|
res, err := c.sessionCreate(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = id.UnmarshalBinary(res.id); err != nil {
|
|
|
|
return fmt.Errorf("invalid session token ID: %w", err)
|
|
|
|
}
|
|
|
|
if err = key.Decode(res.sessionKey); err != nil {
|
|
|
|
return fmt.Errorf("invalid public session key: %w", err)
|
|
|
|
}
|
2022-04-07 16:09:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 10:09:13 +00:00
|
|
|
dst.SetID(id)
|
|
|
|
dst.SetAuthKey(&key)
|
|
|
|
dst.SetExp(exp)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2022-04-26 10:09:13 +00:00
|
|
|
return nil
|
2022-02-02 13:25:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
type callContext struct {
|
2022-03-15 12:00:38 +00:00
|
|
|
client client
|
2022-02-07 20:27:56 +00:00
|
|
|
|
|
|
|
// client endpoint
|
|
|
|
endpoint string
|
|
|
|
|
|
|
|
// request signer
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
// flag to open default session if session token is missing
|
|
|
|
sessionDefault bool
|
2022-04-07 16:09:15 +00:00
|
|
|
sessionTarget func(session.Object)
|
|
|
|
sessionVerb session.ObjectVerb
|
2022-05-25 09:03:22 +00:00
|
|
|
sessionCnr cid.ID
|
|
|
|
sessionObjSet bool
|
2022-11-11 10:17:26 +00:00
|
|
|
sessionObjs []oid.ID
|
2023-07-11 09:02:23 +00:00
|
|
|
|
|
|
|
sessionClientCut bool
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
func (p *Pool) initCallContext(ctx *callContext, cfg prmCommon, prmCtx prmContext) 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-07-12 18:55:59 +00:00
|
|
|
ctx.endpoint = cp.address()
|
|
|
|
ctx.client = cp
|
2022-02-11 14:40:09 +00:00
|
|
|
|
2022-02-16 15:19:32 +00:00
|
|
|
if ctx.sessionTarget != nil && cfg.stoken != nil {
|
2022-02-11 14:40:09 +00:00
|
|
|
ctx.sessionTarget(*cfg.stoken)
|
|
|
|
}
|
|
|
|
|
|
|
|
// note that we don't override session provided by the caller
|
2022-03-21 11:29:29 +00:00
|
|
|
ctx.sessionDefault = cfg.stoken == nil && prmCtx.defaultSession
|
2022-02-21 11:30:30 +00:00
|
|
|
if ctx.sessionDefault {
|
2022-04-07 16:09:15 +00:00
|
|
|
ctx.sessionVerb = prmCtx.verb
|
2022-05-25 09:03:22 +00:00
|
|
|
ctx.sessionCnr = prmCtx.cnr
|
|
|
|
ctx.sessionObjSet = prmCtx.objSet
|
2022-11-11 10:17:26 +00:00
|
|
|
ctx.sessionObjs = prmCtx.objs
|
2022-02-21 11:30:30 +00:00
|
|
|
}
|
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 16:13:53 +00:00
|
|
|
// opens new session or uses cached one.
|
|
|
|
// Must be called only on initialized callContext with set sessionTarget.
|
2023-04-07 05:51:20 +00:00
|
|
|
func (p *Pool) openDefaultSession(ctx context.Context, cc *callContext) error {
|
2023-07-11 09:02:23 +00:00
|
|
|
cacheKey := formCacheKey(cc.endpoint, cc.key, cc.sessionClientCut)
|
2022-02-11 14:40:09 +00:00
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
tok, ok := p.cache.Get(cacheKey)
|
|
|
|
if !ok {
|
2022-04-26 10:09:13 +00:00
|
|
|
// init new session
|
2023-07-11 09:02:23 +00:00
|
|
|
err := initSessionForDuration(ctx, &tok, cc.client, p.stokenDuration, *cc.key, cc.sessionClientCut)
|
2022-02-21 11:30:30 +00:00
|
|
|
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
|
|
|
// cache the opened session
|
2022-04-26 10:09:13 +00:00
|
|
|
p.cache.Put(cacheKey, tok)
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
2023-04-07 05:51:20 +00:00
|
|
|
tok.ForVerb(cc.sessionVerb)
|
|
|
|
tok.BindContainer(cc.sessionCnr)
|
2022-05-25 09:03:22 +00:00
|
|
|
|
2023-04-07 05:51:20 +00:00
|
|
|
if cc.sessionObjSet {
|
|
|
|
tok.LimitByObjects(cc.sessionObjs...)
|
2022-05-25 09:03:22 +00:00
|
|
|
}
|
2022-02-11 14:40:09 +00:00
|
|
|
|
|
|
|
// sign the token
|
2023-04-07 05:51:20 +00:00
|
|
|
if err := tok.Sign(*cc.key); err != nil {
|
2022-02-11 14:40:09 +00:00
|
|
|
return fmt.Errorf("sign token of the opened session: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-04-07 05:51:20 +00:00
|
|
|
cc.sessionTarget(tok)
|
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
|
2022-03-24 14:19:30 +00:00
|
|
|
// session-related error then cached token is removed.
|
2023-04-07 05:51:20 +00:00
|
|
|
func (p *Pool) call(ctx context.Context, cc *callContext, f func() error) error {
|
2022-02-11 16:13:53 +00:00
|
|
|
var err error
|
2022-02-11 14:40:09 +00:00
|
|
|
|
2023-04-07 05:51:20 +00:00
|
|
|
if cc.sessionDefault {
|
|
|
|
err = p.openDefaultSession(ctx, cc)
|
2022-02-11 16:13:53 +00:00
|
|
|
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()
|
2023-04-07 05:51:20 +00:00
|
|
|
_ = p.checkSessionTokenErr(err, cc.endpoint)
|
2022-02-11 16:13:53 +00:00
|
|
|
|
|
|
|
return err
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 13:48:55 +00:00
|
|
|
// fillAppropriateKey use pool key if caller didn't specify its own.
|
|
|
|
func (p *Pool) fillAppropriateKey(prm *prmCommon) {
|
|
|
|
if prm.key == nil {
|
|
|
|
prm.key = p.key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// PutObject writes an object through a remote server using FrostFS API protocol.
|
2022-08-08 09:15:10 +00:00
|
|
|
//
|
|
|
|
// Main return value MUST NOT be processed on an erroneous return.
|
|
|
|
func (p *Pool) PutObject(ctx context.Context, prm PrmObjectPut) (oid.ID, error) {
|
2022-05-25 09:03:22 +00:00
|
|
|
cnr, _ := prm.hdr.ContainerID()
|
2022-04-12 16:23:16 +00:00
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
var prmCtx prmContext
|
|
|
|
prmCtx.useDefaultSession()
|
2022-04-07 16:09:15 +00:00
|
|
|
prmCtx.useVerb(session.VerbObjectPut)
|
2022-05-25 09:03:22 +00:00
|
|
|
prmCtx.useContainer(cnr)
|
2022-02-02 11:21:23 +00:00
|
|
|
|
2022-04-12 13:48:55 +00:00
|
|
|
p.fillAppropriateKey(&prm.prmCommon)
|
|
|
|
|
2022-02-07 20:27:56 +00:00
|
|
|
var ctxCall callContext
|
2023-07-11 09:02:23 +00:00
|
|
|
ctxCall.sessionClientCut = prm.clientCut
|
2022-03-21 11:29:29 +00:00
|
|
|
if err := p.initCallContext(&ctxCall, prm.prmCommon, prmCtx); err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return oid.ID{}, fmt.Errorf("init call context: %w", err)
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
if ctxCall.sessionDefault {
|
|
|
|
ctxCall.sessionTarget = prm.UseSession
|
2023-04-07 05:51:20 +00:00
|
|
|
if err := p.openDefaultSession(ctx, &ctxCall); err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return oid.ID{}, fmt.Errorf("open default session: %w", err)
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2023-07-14 12:23:28 +00:00
|
|
|
if prm.clientCut {
|
|
|
|
var ni netmap.NetworkInfo
|
|
|
|
ni.SetCurrentEpoch(p.cache.Epoch())
|
2023-08-01 09:52:18 +00:00
|
|
|
ni.SetMaxObjectSize(p.maxObjectSize) // we want to use initial max object size in PayloadSizeLimiter
|
2023-07-14 12:23:28 +00:00
|
|
|
prm.setNetworkInfo(ni)
|
|
|
|
}
|
2023-07-11 09:02:23 +00:00
|
|
|
|
2022-03-28 09:17:56 +00:00
|
|
|
id, err := ctxCall.client.objectPut(ctx, prm)
|
2022-02-07 20:27:56 +00:00
|
|
|
if err != nil {
|
2022-03-21 11:29:29 +00:00
|
|
|
// removes session token from cache in case of token error
|
|
|
|
p.checkSessionTokenErr(err, ctxCall.endpoint)
|
2023-08-15 06:51:40 +00:00
|
|
|
return id, fmt.Errorf("init writing on API client %s: %w", ctxCall.endpoint, err)
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
return id, nil
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// DeleteObject marks an object for deletion from the container using FrostFS API protocol.
|
2022-03-15 08:54:15 +00:00
|
|
|
// As a marker, a special unit called a tombstone is placed in the container.
|
|
|
|
// It confirms the user's intent to delete the object, and is itself a container object.
|
|
|
|
// Explicit deletion is done asynchronously, and is generally not guaranteed.
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) DeleteObject(ctx context.Context, prm PrmObjectDelete) error {
|
2022-03-21 11:29:29 +00:00
|
|
|
var prmCtx prmContext
|
|
|
|
prmCtx.useDefaultSession()
|
2022-04-07 16:09:15 +00:00
|
|
|
prmCtx.useVerb(session.VerbObjectDelete)
|
|
|
|
prmCtx.useAddress(prm.addr)
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2022-11-11 13:14:10 +00:00
|
|
|
if prm.stoken == nil { // collect phy objects only if we are about to open default session
|
|
|
|
var tokens relations.Tokens
|
|
|
|
tokens.Bearer = prm.btoken
|
|
|
|
|
|
|
|
relatives, err := relations.ListAllRelations(ctx, p, prm.addr.Container(), prm.addr.Object(), tokens)
|
2022-11-11 10:17:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to collect relatives: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(relatives) != 0 {
|
|
|
|
prmCtx.useContainer(prm.addr.Container())
|
|
|
|
prmCtx.useObjects(append(relatives, prm.addr.Object()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:48:55 +00:00
|
|
|
p.fillAppropriateKey(&prm.prmCommon)
|
|
|
|
|
2022-03-24 14:19:30 +00:00
|
|
|
var cc callContext
|
2022-03-21 11:29:29 +00:00
|
|
|
cc.sessionTarget = prm.UseSession
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
err := p.initCallContext(&cc, prm.prmCommon, prmCtx)
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2023-04-07 05:51:20 +00:00
|
|
|
return p.call(ctx, &cc, func() error {
|
2022-03-28 09:17:56 +00:00
|
|
|
if err = cc.client.objectDelete(ctx, prm); err != nil {
|
2023-08-15 06:51:40 +00:00
|
|
|
return fmt.Errorf("remove object via client %s: %w", cc.endpoint, err)
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
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-07-13 09:51:43 +00:00
|
|
|
type objectReadCloser struct {
|
|
|
|
reader *sdkClient.ObjectReader
|
|
|
|
elapsedTimeCallback func(time.Duration)
|
|
|
|
}
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// Read implements io.Reader of the object payload.
|
2022-02-07 20:27:56 +00:00
|
|
|
func (x *objectReadCloser) Read(p []byte) (int, error) {
|
2022-07-13 09:51:43 +00:00
|
|
|
start := time.Now()
|
|
|
|
n, err := x.reader.Read(p)
|
|
|
|
x.elapsedTimeCallback(time.Since(start))
|
|
|
|
return n, err
|
2022-02-07 20:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// Close implements io.Closer of the object payload.
|
2022-02-07 20:27:56 +00:00
|
|
|
func (x *objectReadCloser) Close() error {
|
2022-07-13 09:51:43 +00:00
|
|
|
_, err := x.reader.Close()
|
2022-02-07 20:27:56 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// ResGetObject is designed to provide object header nad read one object payload from FrostFS system.
|
2022-02-07 20:27:56 +00:00
|
|
|
type ResGetObject struct {
|
|
|
|
Header object.Object
|
|
|
|
|
|
|
|
Payload io.ReadCloser
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// GetObject reads object header and initiates reading an object payload through a remote server using FrostFS API protocol.
|
2022-08-08 09:15:10 +00:00
|
|
|
//
|
|
|
|
// Main return value MUST NOT be processed on an erroneous return.
|
|
|
|
func (p *Pool) GetObject(ctx context.Context, prm PrmObjectGet) (ResGetObject, error) {
|
2022-04-12 13:48:55 +00:00
|
|
|
p.fillAppropriateKey(&prm.prmCommon)
|
|
|
|
|
2022-03-24 14:19:30 +00:00
|
|
|
var cc callContext
|
2022-03-21 11:29:29 +00:00
|
|
|
cc.sessionTarget = prm.UseSession
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
var res ResGetObject
|
|
|
|
|
2022-11-15 08:41:28 +00:00
|
|
|
err := p.initCallContext(&cc, prm.prmCommon, prmContext{})
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return res, err
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2023-04-07 05:51:20 +00:00
|
|
|
return res, p.call(ctx, &cc, func() error {
|
2022-03-28 09:17:56 +00:00
|
|
|
res, err = cc.client.objectGet(ctx, prm)
|
2023-08-15 06:51:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get object via client %s: %w", cc.endpoint, err)
|
|
|
|
}
|
|
|
|
return nil
|
2022-02-11 14:40:09 +00:00
|
|
|
})
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// HeadObject reads object header through a remote server using FrostFS API protocol.
|
2022-08-08 09:15:10 +00:00
|
|
|
//
|
|
|
|
// Main return value MUST NOT be processed on an erroneous return.
|
|
|
|
func (p *Pool) HeadObject(ctx context.Context, prm PrmObjectHead) (object.Object, error) {
|
2022-04-12 13:48:55 +00:00
|
|
|
p.fillAppropriateKey(&prm.prmCommon)
|
|
|
|
|
2022-03-24 14:19:30 +00:00
|
|
|
var cc callContext
|
2022-03-21 11:29:29 +00:00
|
|
|
cc.sessionTarget = prm.UseSession
|
2022-02-11 16:13:53 +00:00
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
var obj object.Object
|
|
|
|
|
2022-11-15 08:41:28 +00:00
|
|
|
err := p.initCallContext(&cc, prm.prmCommon, prmContext{})
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return obj, err
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2023-04-07 05:51:20 +00:00
|
|
|
return obj, p.call(ctx, &cc, func() error {
|
2022-03-28 09:17:56 +00:00
|
|
|
obj, err = cc.client.objectHead(ctx, prm)
|
2023-08-15 06:51:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("head object via client %s: %w", cc.endpoint, err)
|
|
|
|
}
|
|
|
|
return nil
|
2022-02-11 16:13:53 +00:00
|
|
|
})
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// ResObjectRange is designed to read payload range of one object
|
2022-12-29 10:46:18 +00:00
|
|
|
// from FrostFS system.
|
2022-03-15 08:54:15 +00:00
|
|
|
//
|
|
|
|
// Must be initialized using Pool.ObjectRange, any other
|
|
|
|
// usage is unsafe.
|
2022-02-12 09:26:38 +00:00
|
|
|
type ResObjectRange struct {
|
2022-07-13 09:51:43 +00:00
|
|
|
payload *sdkClient.ObjectRangeReader
|
|
|
|
elapsedTimeCallback func(time.Duration)
|
2022-02-12 09:26:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// Read implements io.Reader of the object payload.
|
2022-02-12 09:26:38 +00:00
|
|
|
func (x *ResObjectRange) Read(p []byte) (int, error) {
|
2022-07-13 09:51:43 +00:00
|
|
|
start := time.Now()
|
|
|
|
n, err := x.payload.Read(p)
|
|
|
|
x.elapsedTimeCallback(time.Since(start))
|
|
|
|
return n, err
|
2022-02-12 09:26:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// Close ends reading the payload range and returns the result of the operation
|
|
|
|
// along with the final results. Must be called after using the ResObjectRange.
|
2022-02-12 09:26:38 +00:00
|
|
|
func (x *ResObjectRange) Close() error {
|
|
|
|
_, err := x.payload.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// ObjectRange initiates reading an object's payload range through a remote
|
2022-12-29 10:46:18 +00:00
|
|
|
// server using FrostFS API protocol.
|
2022-08-08 09:15:10 +00:00
|
|
|
//
|
|
|
|
// Main return value MUST NOT be processed on an erroneous return.
|
|
|
|
func (p *Pool) ObjectRange(ctx context.Context, prm PrmObjectRange) (ResObjectRange, error) {
|
2022-04-12 13:48:55 +00:00
|
|
|
p.fillAppropriateKey(&prm.prmCommon)
|
|
|
|
|
2022-03-24 14:19:30 +00:00
|
|
|
var cc callContext
|
2022-03-21 11:29:29 +00:00
|
|
|
cc.sessionTarget = prm.UseSession
|
2022-02-12 09:26:38 +00:00
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
var res ResObjectRange
|
|
|
|
|
2022-11-15 08:41:28 +00:00
|
|
|
err := p.initCallContext(&cc, prm.prmCommon, prmContext{})
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return res, err
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2023-04-07 05:51:20 +00:00
|
|
|
return res, p.call(ctx, &cc, func() error {
|
2022-03-28 09:17:56 +00:00
|
|
|
res, err = cc.client.objectRange(ctx, prm)
|
2023-08-15 06:51:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("object range via client %s: %w", cc.endpoint, err)
|
|
|
|
}
|
|
|
|
return nil
|
2022-02-12 09:26:38 +00:00
|
|
|
})
|
2021-11-17 11:22:32 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// ResObjectSearch is designed to read list of object identifiers from FrostFS system.
|
2022-03-15 08:54:15 +00:00
|
|
|
//
|
|
|
|
// Must be initialized using Pool.SearchObjects, any other usage is unsafe.
|
2022-02-12 20:30:16 +00:00
|
|
|
type ResObjectSearch struct {
|
2022-03-15 12:00:38 +00:00
|
|
|
r *sdkClient.ObjectListReader
|
2022-02-12 20:30:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// Read reads another list of the object identifiers.
|
2022-02-12 20:30:16 +00:00
|
|
|
func (x *ResObjectSearch) Read(buf []oid.ID) (int, error) {
|
|
|
|
n, ok := x.r.Read(buf)
|
|
|
|
if !ok {
|
|
|
|
_, err := x.r.Close()
|
|
|
|
if err == nil {
|
|
|
|
return n, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// Iterate iterates over the list of found object identifiers.
|
|
|
|
// f can return true to stop iteration earlier.
|
|
|
|
//
|
|
|
|
// Returns an error if object can't be read.
|
2022-02-25 10:20:15 +00:00
|
|
|
func (x *ResObjectSearch) Iterate(f func(oid.ID) bool) error {
|
|
|
|
return x.r.Iterate(f)
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// Close ends reading list of the matched objects and returns the result of the operation
|
|
|
|
// along with the final results. Must be called after using the ResObjectSearch.
|
2022-02-12 20:30:16 +00:00
|
|
|
func (x *ResObjectSearch) Close() {
|
|
|
|
_, _ = x.r.Close()
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// SearchObjects initiates object selection through a remote server using FrostFS API protocol.
|
2022-03-15 08:54:15 +00:00
|
|
|
//
|
|
|
|
// The call only opens the transmission channel, explicit fetching of matched objects
|
2022-08-08 09:15:10 +00:00
|
|
|
// is done using the ResObjectSearch. Resulting reader must be finally closed.
|
|
|
|
//
|
|
|
|
// Main return value MUST NOT be processed on an erroneous return.
|
|
|
|
func (p *Pool) SearchObjects(ctx context.Context, prm PrmObjectSearch) (ResObjectSearch, error) {
|
2022-04-12 13:48:55 +00:00
|
|
|
p.fillAppropriateKey(&prm.prmCommon)
|
|
|
|
|
2022-03-24 14:19:30 +00:00
|
|
|
var cc callContext
|
2022-03-21 11:29:29 +00:00
|
|
|
cc.sessionTarget = prm.UseSession
|
2022-02-12 20:30:16 +00:00
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
var res ResObjectSearch
|
|
|
|
|
2022-11-15 08:41:28 +00:00
|
|
|
err := p.initCallContext(&cc, prm.prmCommon, prmContext{})
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return res, err
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2023-04-07 05:51:20 +00:00
|
|
|
return res, p.call(ctx, &cc, func() error {
|
2022-03-28 09:17:56 +00:00
|
|
|
res, err = cc.client.objectSearch(ctx, prm)
|
2023-08-15 06:51:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("search object via client %s: %w", cc.endpoint, err)
|
|
|
|
}
|
|
|
|
return nil
|
2022-02-12 20:30:16 +00:00
|
|
|
})
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// PutContainer sends request to save container in FrostFS and waits for the operation to complete.
|
2022-03-15 08:54:15 +00:00
|
|
|
//
|
2022-03-28 08:06:23 +00:00
|
|
|
// Waiting parameters can be specified using SetWaitParams. If not called, defaults are used:
|
2022-08-24 14:17:40 +00:00
|
|
|
//
|
|
|
|
// polling interval: 5s
|
|
|
|
// waiting timeout: 120s
|
2022-03-15 08:54:15 +00:00
|
|
|
//
|
|
|
|
// Success can be verified by reading by identifier (see GetContainer).
|
2022-08-08 09:15:10 +00:00
|
|
|
//
|
|
|
|
// Main return value MUST NOT be processed on an erroneous return.
|
|
|
|
func (p *Pool) PutContainer(ctx context.Context, prm PrmContainerPut) (cid.ID, error) {
|
2022-03-17 09:53:04 +00:00
|
|
|
cp, err := p.connection()
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return cid.ID{}, err
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2023-08-15 06:51:40 +00:00
|
|
|
cnrID, err := cp.containerPut(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return cid.ID{}, fmt.Errorf("put container via client '%s': %w", cp.address(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cnrID, nil
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// GetContainer reads FrostFS container by ID.
|
2022-08-08 09:15:10 +00:00
|
|
|
//
|
|
|
|
// Main return value MUST NOT be processed on an erroneous return.
|
|
|
|
func (p *Pool) GetContainer(ctx context.Context, prm PrmContainerGet) (container.Container, error) {
|
2022-03-17 09:53:04 +00:00
|
|
|
cp, err := p.connection()
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return container.Container{}, err
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2023-08-15 06:51:40 +00:00
|
|
|
cnrs, err := cp.containerGet(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return container.Container{}, fmt.Errorf("get container via client '%s': %w", cp.address(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cnrs, nil
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// ListContainers requests identifiers of the account-owned containers.
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) ListContainers(ctx context.Context, prm PrmContainerList) ([]cid.ID, error) {
|
2022-03-17 09:53:04 +00:00
|
|
|
cp, err := p.connection()
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2023-08-15 06:51:40 +00:00
|
|
|
cnrIDs, err := cp.containerList(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return []cid.ID{}, fmt.Errorf("list containers via client '%s': %w", cp.address(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cnrIDs, nil
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// DeleteContainer sends request to remove the FrostFS container and waits for the operation to complete.
|
2022-03-15 08:54:15 +00:00
|
|
|
//
|
2022-03-28 08:06:23 +00:00
|
|
|
// Waiting parameters can be specified using SetWaitParams. If not called, defaults are used:
|
2022-08-24 14:17:40 +00:00
|
|
|
//
|
|
|
|
// polling interval: 5s
|
|
|
|
// waiting timeout: 120s
|
2022-03-15 08:54:15 +00:00
|
|
|
//
|
|
|
|
// Success can be verified by reading by identifier (see GetContainer).
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) DeleteContainer(ctx context.Context, prm PrmContainerDelete) error {
|
2022-03-17 09:53:04 +00:00
|
|
|
cp, err := p.connection()
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2023-08-15 06:51:40 +00:00
|
|
|
err = cp.containerDelete(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("delete container via client '%s': %w", cp.address(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// GetEACL reads eACL table of the FrostFS container.
|
2022-08-08 09:15:10 +00:00
|
|
|
//
|
|
|
|
// Main return value MUST NOT be processed on an erroneous return.
|
|
|
|
func (p *Pool) GetEACL(ctx context.Context, prm PrmContainerEACL) (eacl.Table, error) {
|
2022-03-17 09:53:04 +00:00
|
|
|
cp, err := p.connection()
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return eacl.Table{}, err
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2023-08-15 06:51:40 +00:00
|
|
|
eaclResult, err := cp.containerEACL(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return eacl.Table{}, fmt.Errorf("get EACL via client '%s': %w", cp.address(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return eaclResult, nil
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// SetEACL sends request to update eACL table of the FrostFS container and waits for the operation to complete.
|
2022-03-15 08:54:15 +00:00
|
|
|
//
|
2022-03-28 08:06:23 +00:00
|
|
|
// Waiting parameters can be specified using SetWaitParams. If not called, defaults are used:
|
2022-08-24 14:17:40 +00:00
|
|
|
//
|
|
|
|
// polling interval: 5s
|
|
|
|
// waiting timeout: 120s
|
2022-03-15 08:54:15 +00:00
|
|
|
//
|
|
|
|
// Success can be verified by reading by identifier (see GetEACL).
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) SetEACL(ctx context.Context, prm PrmContainerSetEACL) error {
|
2022-03-17 09:53:04 +00:00
|
|
|
cp, err := p.connection()
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2023-08-15 06:51:40 +00:00
|
|
|
err = cp.containerSetEACL(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("set EACL via client '%s': %w", cp.address(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// Balance requests current balance of the FrostFS account.
|
2022-08-08 09:15:10 +00:00
|
|
|
//
|
|
|
|
// Main return value MUST NOT be processed on an erroneous return.
|
|
|
|
func (p *Pool) Balance(ctx context.Context, prm PrmBalanceGet) (accounting.Decimal, error) {
|
2022-03-17 09:53:04 +00:00
|
|
|
cp, err := p.connection()
|
2021-12-17 10:36:07 +00:00
|
|
|
if err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return accounting.Decimal{}, err
|
2021-12-17 10:36:07 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 06:51:40 +00:00
|
|
|
balance, err := cp.balanceGet(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return accounting.Decimal{}, fmt.Errorf("get balance via client '%s': %w", cp.address(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return balance, nil
|
2021-12-17 10:36:07 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 09:55:38 +00:00
|
|
|
// Statistic returns connection statistics.
|
|
|
|
func (p Pool) Statistic() Statistic {
|
|
|
|
stat := Statistic{}
|
|
|
|
for _, inner := range p.innerPools {
|
2023-08-15 06:51:13 +00:00
|
|
|
nodes := make([]string, 0, len(inner.clients))
|
2022-07-13 09:55:38 +00:00
|
|
|
inner.lock.RLock()
|
|
|
|
for _, cl := range inner.clients {
|
2023-08-15 06:51:13 +00:00
|
|
|
if cl.isHealthy() {
|
|
|
|
nodes = append(nodes, cl.address())
|
|
|
|
}
|
2022-07-18 08:42:29 +00:00
|
|
|
node := NodeStatistic{
|
2022-07-13 09:55:38 +00:00
|
|
|
address: cl.address(),
|
2022-07-19 13:13:59 +00:00
|
|
|
methods: cl.methodsStatus(),
|
2022-07-13 09:55:38 +00:00
|
|
|
overallErrors: cl.overallErrorRate(),
|
|
|
|
currentErrors: cl.currentErrorRate(),
|
|
|
|
}
|
|
|
|
stat.nodes = append(stat.nodes, node)
|
|
|
|
stat.overallErrors += node.overallErrors
|
|
|
|
}
|
|
|
|
inner.lock.RUnlock()
|
2023-08-15 06:51:13 +00:00
|
|
|
if len(stat.currentNodes) == 0 {
|
|
|
|
stat.currentNodes = nodes
|
|
|
|
}
|
2022-07-13 09:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stat
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// waitForContainerPresence waits until the container is found on the FrostFS network.
|
2024-03-01 12:16:16 +00:00
|
|
|
func waitForContainerPresence(ctx context.Context, cli client, prm PrmContainerGet, waitParams *WaitParams) error {
|
2022-03-23 07:50:34 +00:00
|
|
|
return waitFor(ctx, waitParams, func(ctx context.Context) bool {
|
2022-03-28 09:17:56 +00:00
|
|
|
_, err := cli.containerGet(ctx, prm)
|
2022-03-23 07:50:34 +00:00
|
|
|
return err == nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// waitForEACLPresence waits until the container eacl is applied on the FrostFS network.
|
2024-03-01 12:16:16 +00:00
|
|
|
func waitForEACLPresence(ctx context.Context, cli client, prm PrmContainerEACL, table *eacl.Table, waitParams *WaitParams) error {
|
2022-03-23 07:50:34 +00:00
|
|
|
return waitFor(ctx, waitParams, func(ctx context.Context) bool {
|
2022-03-28 09:17:56 +00:00
|
|
|
eaclTable, err := cli.containerEACL(ctx, prm)
|
2022-03-23 07:50:34 +00:00
|
|
|
if err == nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return eacl.EqualTables(*table, eaclTable)
|
2022-03-23 07:50:34 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// waitForContainerRemoved waits until the container is removed from the FrostFS network.
|
2024-03-01 12:16:16 +00:00
|
|
|
func waitForContainerRemoved(ctx context.Context, cli client, prm PrmContainerGet, waitParams *WaitParams) error {
|
2022-03-24 12:42:49 +00:00
|
|
|
return waitFor(ctx, waitParams, func(ctx context.Context) bool {
|
2022-03-28 09:17:56 +00:00
|
|
|
_, err := cli.containerGet(ctx, prm)
|
2022-06-27 07:48:27 +00:00
|
|
|
return sdkClient.IsErrContainerNotFound(err)
|
2022-03-24 12:42:49 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-23 07:50:34 +00:00
|
|
|
// waitFor await that given condition will be met in waitParams time.
|
|
|
|
func waitFor(ctx context.Context, params *WaitParams, condition func(context.Context) bool) error {
|
2023-08-03 09:14:00 +00:00
|
|
|
wctx, cancel := context.WithTimeout(ctx, params.Timeout)
|
2021-06-22 09:15:21 +00:00
|
|
|
defer cancel()
|
2023-08-03 09:14:00 +00:00
|
|
|
ticker := time.NewTimer(params.PollInterval)
|
2021-06-22 09:15:21 +00:00
|
|
|
defer ticker.Stop()
|
|
|
|
wdone := wctx.Done()
|
|
|
|
done := ctx.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return ctx.Err()
|
|
|
|
case <-wdone:
|
|
|
|
return wctx.Err()
|
|
|
|
case <-ticker.C:
|
2022-03-23 07:50:34 +00:00
|
|
|
if condition(ctx) {
|
2021-06-22 09:15:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-08-03 09:14:00 +00:00
|
|
|
ticker.Reset(params.PollInterval)
|
2021-06-22 09:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// NetworkInfo requests information about the FrostFS network of which the remote server is a part.
|
2022-08-08 09:15:10 +00:00
|
|
|
//
|
|
|
|
// Main return value MUST NOT be processed on an erroneous return.
|
|
|
|
func (p *Pool) NetworkInfo(ctx context.Context) (netmap.NetworkInfo, error) {
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := p.connection()
|
|
|
|
if err != nil {
|
2022-08-08 09:15:10 +00:00
|
|
|
return netmap.NetworkInfo{}, err
|
2022-03-15 12:00:38 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 06:51:40 +00:00
|
|
|
netInfo, err := cp.networkInfo(ctx, prmNetworkInfo{})
|
|
|
|
if err != nil {
|
|
|
|
return netmap.NetworkInfo{}, fmt.Errorf("get network info via client '%s': %w", cp.address(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return netInfo, nil
|
2022-03-15 12:00:38 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 08:30:25 +00:00
|
|
|
// NetMapSnapshot requests information about the FrostFS network map.
|
|
|
|
//
|
|
|
|
// Main return value MUST NOT be processed on an erroneous return.
|
|
|
|
func (p *Pool) NetMapSnapshot(ctx context.Context) (netmap.NetMap, error) {
|
|
|
|
cp, err := p.connection()
|
|
|
|
if err != nil {
|
|
|
|
return netmap.NetMap{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
netMap, err := cp.netMapSnapshot(ctx, prmNetMapSnapshot{})
|
|
|
|
if err != nil {
|
|
|
|
return netmap.NetMap{}, fmt.Errorf("get network map via client '%s': %w", cp.address(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return netMap, nil
|
|
|
|
}
|
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
// Close closes the Pool and releases all the associated resources.
|
|
|
|
func (p *Pool) Close() {
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
p.cancel()
|
|
|
|
<-p.closedCh
|
2023-09-20 09:14:37 +00:00
|
|
|
|
|
|
|
// close all clients
|
|
|
|
for _, pools := range p.innerPools {
|
|
|
|
for _, cli := range pools.clients {
|
2023-10-03 12:07:07 +00:00
|
|
|
if cli.isDialed() {
|
|
|
|
_ = cli.close()
|
|
|
|
}
|
2023-09-20 09:14:37 +00:00
|
|
|
}
|
|
|
|
}
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
}
|
2022-07-05 10:20:10 +00:00
|
|
|
|
|
|
|
// SyncContainerWithNetwork applies network configuration received via
|
|
|
|
// the Pool to the container. Changes the container if it does not satisfy
|
|
|
|
// network configuration.
|
|
|
|
//
|
|
|
|
// Pool and container MUST not be nil.
|
|
|
|
//
|
|
|
|
// Returns any error that does not allow reading configuration
|
|
|
|
// from the network.
|
|
|
|
func SyncContainerWithNetwork(ctx context.Context, cnr *container.Container, p *Pool) error {
|
|
|
|
ni, err := p.NetworkInfo(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("network info: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
container.ApplyNetworkConfig(cnr, ni)
|
2022-07-05 10:20:10 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-11 13:14:10 +00:00
|
|
|
|
|
|
|
// GetSplitInfo implements relations.Relations.
|
|
|
|
func (p *Pool) GetSplitInfo(ctx context.Context, cnrID cid.ID, objID oid.ID, tokens relations.Tokens) (*object.SplitInfo, error) {
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnrID)
|
|
|
|
addr.SetObject(objID)
|
|
|
|
|
|
|
|
var prm PrmObjectHead
|
|
|
|
prm.SetAddress(addr)
|
|
|
|
if tokens.Bearer != nil {
|
|
|
|
prm.UseBearer(*tokens.Bearer)
|
|
|
|
}
|
|
|
|
if tokens.Session != nil {
|
|
|
|
prm.UseSession(*tokens.Session)
|
|
|
|
}
|
|
|
|
prm.MarkRaw()
|
|
|
|
|
|
|
|
_, err := p.HeadObject(ctx, prm)
|
|
|
|
|
|
|
|
var errSplit *object.SplitInfoError
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case errors.As(err, &errSplit):
|
|
|
|
return errSplit.SplitInfo(), nil
|
|
|
|
case err == nil:
|
|
|
|
return nil, relations.ErrNoSplitInfo
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("failed to get raw object header: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListChildrenByLinker implements relations.Relations.
|
|
|
|
func (p *Pool) ListChildrenByLinker(ctx context.Context, cnrID cid.ID, objID oid.ID, tokens relations.Tokens) ([]oid.ID, error) {
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnrID)
|
|
|
|
addr.SetObject(objID)
|
|
|
|
|
|
|
|
var prm PrmObjectHead
|
|
|
|
prm.SetAddress(addr)
|
|
|
|
if tokens.Bearer != nil {
|
|
|
|
prm.UseBearer(*tokens.Bearer)
|
|
|
|
}
|
|
|
|
if tokens.Session != nil {
|
|
|
|
prm.UseSession(*tokens.Session)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := p.HeadObject(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get linking object's header: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Children(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLeftSibling implements relations.Relations.
|
|
|
|
func (p *Pool) GetLeftSibling(ctx context.Context, cnrID cid.ID, objID oid.ID, tokens relations.Tokens) (oid.ID, error) {
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnrID)
|
|
|
|
addr.SetObject(objID)
|
|
|
|
|
|
|
|
var prm PrmObjectHead
|
|
|
|
prm.SetAddress(addr)
|
|
|
|
if tokens.Bearer != nil {
|
|
|
|
prm.UseBearer(*tokens.Bearer)
|
|
|
|
}
|
|
|
|
if tokens.Session != nil {
|
|
|
|
prm.UseSession(*tokens.Session)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := p.HeadObject(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return oid.ID{}, fmt.Errorf("failed to read split chain member's header: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
idMember, ok := res.PreviousID()
|
|
|
|
if !ok {
|
|
|
|
return oid.ID{}, relations.ErrNoLeftSibling
|
|
|
|
}
|
|
|
|
return idMember, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindSiblingBySplitID implements relations.Relations.
|
|
|
|
func (p *Pool) FindSiblingBySplitID(ctx context.Context, cnrID cid.ID, splitID *object.SplitID, tokens relations.Tokens) ([]oid.ID, error) {
|
|
|
|
var query object.SearchFilters
|
|
|
|
query.AddSplitIDFilter(object.MatchStringEqual, splitID)
|
|
|
|
|
|
|
|
var prm PrmObjectSearch
|
|
|
|
prm.SetContainerID(cnrID)
|
|
|
|
prm.SetFilters(query)
|
|
|
|
if tokens.Bearer != nil {
|
|
|
|
prm.UseBearer(*tokens.Bearer)
|
|
|
|
}
|
|
|
|
if tokens.Session != nil {
|
|
|
|
prm.UseSession(*tokens.Session)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := p.SearchObjects(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to search objects by split ID: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var members []oid.ID
|
|
|
|
err = res.Iterate(func(id oid.ID) bool {
|
|
|
|
members = append(members, id)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to iterate found objects: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return members, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindSiblingByParentID implements relations.Relations.
|
|
|
|
func (p *Pool) FindSiblingByParentID(ctx context.Context, cnrID cid.ID, objID oid.ID, tokens relations.Tokens) ([]oid.ID, error) {
|
|
|
|
var query object.SearchFilters
|
|
|
|
query.AddParentIDFilter(object.MatchStringEqual, objID)
|
|
|
|
|
|
|
|
var prm PrmObjectSearch
|
|
|
|
prm.SetContainerID(cnrID)
|
|
|
|
prm.SetFilters(query)
|
|
|
|
if tokens.Bearer != nil {
|
|
|
|
prm.UseBearer(*tokens.Bearer)
|
|
|
|
}
|
|
|
|
if tokens.Session != nil {
|
|
|
|
prm.UseSession(*tokens.Session)
|
|
|
|
}
|
|
|
|
|
|
|
|
resSearch, err := p.SearchObjects(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to find object children: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var res []oid.ID
|
|
|
|
err = resSearch.Iterate(func(id oid.ID) bool {
|
|
|
|
res = append(res, id)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to iterate found objects: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|