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"
|
|
|
|
"time"
|
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/accounting"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
sdkClient "github.com/TrueCloudLab/frostfs-sdk-go/client"
|
|
|
|
apistatus "github.com/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/container"
|
|
|
|
cid "github.com/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
frostfsecdsa "github.com/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/eacl"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "github.com/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/object/relations"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/session"
|
|
|
|
"github.com/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"
|
2022-07-12 18:55:59 +00:00
|
|
|
"go.uber.org/atomic"
|
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"
|
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)
|
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)
|
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
|
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
|
|
|
|
healthy *atomic.Bool
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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"
|
|
|
|
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()}
|
|
|
|
}
|
|
|
|
|
|
|
|
return clientStatusMonitor{
|
2023-01-11 12:48:42 +00:00
|
|
|
logger: logger,
|
2022-07-22 08:06:16 +00:00
|
|
|
addr: addr,
|
|
|
|
healthy: atomic.NewBool(true),
|
|
|
|
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)
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
2022-03-21 11:29:29 +00:00
|
|
|
var prmInit sdkClient.PrmInit
|
|
|
|
prmInit.SetDefaultPrivateKey(prm.key)
|
|
|
|
prmInit.SetResponseInfoCallback(prm.responseInfoCallback)
|
|
|
|
|
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
|
|
|
|
|
|
|
var prmDial sdkClient.PrmDial
|
2022-11-03 14:58:38 +00:00
|
|
|
prmDial.SetServerURI(c.prm.address)
|
2022-11-15 11:32:25 +00:00
|
|
|
prmDial.SetTimeout(c.prm.dialTimeout)
|
|
|
|
prmDial.SetStreamTimeout(c.prm.streamTimeout)
|
2022-11-03 14:58:38 +00:00
|
|
|
prmDial.SetContext(ctx)
|
2022-03-21 11:29:29 +00:00
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
if err = cl.Dial(prmDial); err != nil {
|
|
|
|
c.setUnhealthy()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
var cl sdkClient.Client
|
|
|
|
var prmInit sdkClient.PrmInit
|
|
|
|
prmInit.SetDefaultPrivateKey(c.prm.key)
|
|
|
|
prmInit.SetResponseInfoCallback(c.prm.responseInfoCallback)
|
|
|
|
|
|
|
|
cl.Init(prmInit)
|
|
|
|
|
|
|
|
var prmDial sdkClient.PrmDial
|
|
|
|
prmDial.SetServerURI(c.prm.address)
|
2022-11-15 11:32:25 +00:00
|
|
|
prmDial.SetTimeout(c.prm.dialTimeout)
|
|
|
|
prmDial.SetStreamTimeout(c.prm.streamTimeout)
|
2022-11-03 14:58:38 +00:00
|
|
|
prmDial.SetContext(ctx)
|
|
|
|
|
|
|
|
if err := cl.Dial(prmDial); err != nil {
|
|
|
|
c.setUnhealthy()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
var cliPrm sdkClient.PrmBalanceGet
|
2022-04-11 06:30:22 +00:00
|
|
|
cliPrm.SetAccount(prm.account)
|
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()
|
|
|
|
}
|
|
|
|
if err = c.handleError(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()
|
2022-11-03 14:58:38 +00:00
|
|
|
res, err := cl.ContainerPut(ctx, prm.prmClient)
|
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()
|
|
|
|
}
|
|
|
|
if err = c.handleError(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
|
|
|
}
|
|
|
|
|
|
|
|
if !prm.waitParamsSet {
|
|
|
|
prm.waitParams.setDefaults()
|
|
|
|
}
|
|
|
|
|
2022-08-05 07:56:49 +00:00
|
|
|
idCnr := res.ID()
|
|
|
|
|
|
|
|
err = waitForContainerPresence(ctx, c, idCnr, &prm.waitParams)
|
2022-07-12 18:55:59 +00:00
|
|
|
if err = c.handleError(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
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
var cliPrm sdkClient.PrmContainerGet
|
|
|
|
cliPrm.SetContainer(prm.cnrID)
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
if err = c.handleError(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
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
var cliPrm sdkClient.PrmContainerList
|
|
|
|
cliPrm.SetAccount(prm.ownerID)
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
if err = c.handleError(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
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
var cliPrm sdkClient.PrmContainerDelete
|
|
|
|
cliPrm.SetContainer(prm.cnrID)
|
2022-04-12 10:16:07 +00:00
|
|
|
if prm.stokenSet {
|
2022-04-07 16:09:15 +00:00
|
|
|
cliPrm.WithinSession(prm.stoken)
|
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()
|
|
|
|
}
|
|
|
|
if err = c.handleError(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
|
|
|
}
|
|
|
|
|
|
|
|
if !prm.waitParamsSet {
|
|
|
|
prm.waitParams.setDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
return waitForContainerRemoved(ctx, c, &prm.cnrID, &prm.waitParams)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
var cliPrm sdkClient.PrmContainerEACL
|
|
|
|
cliPrm.SetContainer(prm.cnrID)
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
if err = c.handleError(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
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
var cliPrm sdkClient.PrmContainerSetEACL
|
|
|
|
cliPrm.SetTable(prm.table)
|
|
|
|
|
2022-06-21 14:50:39 +00:00
|
|
|
if prm.sessionSet {
|
|
|
|
cliPrm.WithinSession(prm.session)
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
if err = c.handleError(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
|
|
|
}
|
|
|
|
|
|
|
|
if !prm.waitParamsSet {
|
|
|
|
prm.waitParams.setDefaults()
|
|
|
|
}
|
|
|
|
|
2022-04-12 16:23:16 +00:00
|
|
|
var cIDp *cid.ID
|
|
|
|
if cID, set := prm.table.CID(); set {
|
|
|
|
cIDp = &cID
|
|
|
|
}
|
|
|
|
|
2022-07-12 18:55:59 +00:00
|
|
|
err = waitForEACLPresence(ctx, c, cIDp, &prm.table, &prm.waitParams)
|
|
|
|
if err = c.handleError(nil, err); err != nil {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
if err = c.handleError(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()
|
|
|
|
}
|
|
|
|
if err = c.handleError(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
|
|
|
}
|
|
|
|
|
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) {
|
2022-11-03 14:58:38 +00:00
|
|
|
cl, err := c.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return oid.ID{}, err
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
var cliPrm sdkClient.PrmObjectPutInit
|
2022-07-29 15:42:24 +00:00
|
|
|
cliPrm.SetCopiesNumber(prm.copiesNumber)
|
2022-08-25 12:14:18 +00:00
|
|
|
if prm.stoken != nil {
|
|
|
|
cliPrm.WithinSession(*prm.stoken)
|
|
|
|
}
|
|
|
|
if prm.key != nil {
|
|
|
|
cliPrm.UseKey(*prm.key)
|
|
|
|
}
|
|
|
|
if prm.btoken != nil {
|
|
|
|
cliPrm.WithBearerToken(*prm.btoken)
|
|
|
|
}
|
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)
|
2022-07-12 18:55:59 +00:00
|
|
|
if err = c.handleError(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
|
|
|
}
|
|
|
|
|
|
|
|
if wObj.WriteHeader(prm.hdr) {
|
|
|
|
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 {
|
|
|
|
const defaultBufferSizePut = 3 << 20 // configure?
|
|
|
|
|
|
|
|
if sz == 0 || sz > defaultBufferSizePut {
|
|
|
|
sz = defaultBufferSizePut
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
successWrite := wObj.WritePayloadChunk(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
|
|
|
|
}
|
|
|
|
|
2022-08-08 09:15:10 +00:00
|
|
|
return oid.ID{}, fmt.Errorf("read payload: %w", c.handleError(nil, err))
|
2022-03-21 11:29:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := wObj.Close()
|
2022-07-14 12:45:25 +00:00
|
|
|
var st apistatus.Status
|
|
|
|
if res != nil {
|
|
|
|
st = res.Status()
|
|
|
|
}
|
|
|
|
if err = c.handleError(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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
var cliPrm sdkClient.PrmObjectDelete
|
2022-05-25 09:03:22 +00:00
|
|
|
cliPrm.FromContainer(prm.addr.Container())
|
|
|
|
cliPrm.ByID(prm.addr.Object())
|
2022-03-21 11:29:29 +00:00
|
|
|
|
|
|
|
if prm.stoken != nil {
|
|
|
|
cliPrm.WithinSession(*prm.stoken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.btoken != nil {
|
|
|
|
cliPrm.WithBearerToken(*prm.btoken)
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:48:55 +00:00
|
|
|
if prm.key != nil {
|
|
|
|
cliPrm.UseKey(*prm.key)
|
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
if err = c.handleError(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
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
var cliPrm sdkClient.PrmObjectGet
|
2022-05-25 09:03:22 +00:00
|
|
|
cliPrm.FromContainer(prm.addr.Container())
|
|
|
|
cliPrm.ByID(prm.addr.Object())
|
2022-03-21 11:29:29 +00:00
|
|
|
|
|
|
|
if prm.stoken != nil {
|
|
|
|
cliPrm.WithinSession(*prm.stoken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.btoken != nil {
|
|
|
|
cliPrm.WithBearerToken(*prm.btoken)
|
|
|
|
}
|
|
|
|
|
2022-08-25 11:45:50 +00:00
|
|
|
if prm.key != nil {
|
|
|
|
cliPrm.UseKey(*prm.key)
|
|
|
|
}
|
|
|
|
|
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)
|
2022-07-12 18:55:59 +00:00
|
|
|
if err = c.handleError(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()
|
|
|
|
}
|
|
|
|
err = c.handleError(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
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
var cliPrm sdkClient.PrmObjectHead
|
2022-05-25 09:03:22 +00:00
|
|
|
cliPrm.FromContainer(prm.addr.Container())
|
|
|
|
cliPrm.ByID(prm.addr.Object())
|
2022-11-11 10:17:26 +00:00
|
|
|
if prm.raw {
|
|
|
|
cliPrm.MarkRaw()
|
|
|
|
}
|
2022-03-21 11:29:29 +00:00
|
|
|
|
|
|
|
if prm.stoken != nil {
|
|
|
|
cliPrm.WithinSession(*prm.stoken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.btoken != nil {
|
|
|
|
cliPrm.WithBearerToken(*prm.btoken)
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:48:55 +00:00
|
|
|
if prm.key != nil {
|
|
|
|
cliPrm.UseKey(*prm.key)
|
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
if err = c.handleError(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
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
var cliPrm sdkClient.PrmObjectRange
|
2022-05-25 09:03:22 +00:00
|
|
|
cliPrm.FromContainer(prm.addr.Container())
|
|
|
|
cliPrm.ByID(prm.addr.Object())
|
2022-03-21 11:29:29 +00:00
|
|
|
cliPrm.SetOffset(prm.off)
|
|
|
|
cliPrm.SetLength(prm.ln)
|
|
|
|
|
|
|
|
if prm.stoken != nil {
|
|
|
|
cliPrm.WithinSession(*prm.stoken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.btoken != nil {
|
|
|
|
cliPrm.WithBearerToken(*prm.btoken)
|
|
|
|
}
|
|
|
|
|
2022-08-25 11:32:37 +00:00
|
|
|
if prm.key != nil {
|
|
|
|
cliPrm.UseKey(*prm.key)
|
|
|
|
}
|
|
|
|
|
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)
|
2022-07-12 18:55:59 +00:00
|
|
|
if err = c.handleError(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
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
var cliPrm sdkClient.PrmObjectSearch
|
|
|
|
|
|
|
|
cliPrm.InContainer(prm.cnrID)
|
|
|
|
cliPrm.SetFilters(prm.filters)
|
|
|
|
|
|
|
|
if prm.stoken != nil {
|
|
|
|
cliPrm.WithinSession(*prm.stoken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prm.btoken != nil {
|
|
|
|
cliPrm.WithBearerToken(*prm.btoken)
|
|
|
|
}
|
|
|
|
|
2022-08-23 18:56:55 +00:00
|
|
|
if prm.key != nil {
|
|
|
|
cliPrm.UseKey(*prm.key)
|
|
|
|
}
|
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
res, err := cl.ObjectSearchInit(ctx, cliPrm)
|
2022-07-12 18:55:59 +00:00
|
|
|
if err = c.handleError(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
|
|
|
|
}
|
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
var cliPrm sdkClient.PrmSessionCreate
|
|
|
|
cliPrm.SetExp(prm.exp)
|
2022-05-30 08:40:28 +00:00
|
|
|
cliPrm.UseKey(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()
|
|
|
|
}
|
|
|
|
if err = c.handleError(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 {
|
2022-07-12 18:55:59 +00:00
|
|
|
return c.healthy.Load()
|
|
|
|
}
|
|
|
|
|
2022-11-03 14:58:38 +00:00
|
|
|
func (c *clientStatusMonitor) setHealthy() {
|
|
|
|
c.healthy.Store(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientStatusMonitor) setUnhealthy() {
|
|
|
|
c.healthy.Store(false)
|
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()
|
|
|
|
|
|
|
|
if thresholdReached && c.logger != nil {
|
|
|
|
c.logger.Warn("error threshold reached",
|
|
|
|
zap.String("address", c.addr), zap.Uint32("threshold", c.errorThreshold))
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2022-07-13 06:57:04 +00:00
|
|
|
func (c *clientStatusMonitor) handleError(st apistatus.Status, err error) error {
|
2022-07-12 18:55:59 +00:00
|
|
|
if err != nil {
|
2022-07-13 09:51:43 +00:00
|
|
|
c.incErrorRate()
|
2022-07-12 18:55:59 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = apistatus.ErrFromStatus(st)
|
|
|
|
switch err.(type) {
|
2022-07-13 06:57:04 +00:00
|
|
|
case apistatus.ServerInternal, *apistatus.ServerInternal,
|
|
|
|
apistatus.WrongMagicNumber, *apistatus.WrongMagicNumber,
|
2022-09-15 05:35:27 +00:00
|
|
|
apistatus.SignatureVerification, *apistatus.SignatureVerification,
|
|
|
|
apistatus.NodeUnderMaintenance, *apistatus.NodeUnderMaintenance:
|
2022-07-13 09:51:43 +00:00
|
|
|
c.incErrorRate()
|
2022-07-12 18:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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)
|
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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress specifies address of the node.
|
|
|
|
func (x *NodeParam) SetAddress(address string) {
|
|
|
|
x.address = address
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWeight specifies weight of the node.
|
|
|
|
func (x *NodeParam) SetWeight(weight float64) {
|
|
|
|
x.weight = weight
|
2021-05-25 08:48:01 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-03-15 12:49:14 +00:00
|
|
|
timeout time.Duration
|
|
|
|
pollInterval time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTimeout specifies the time to wait for the operation to complete.
|
2022-03-23 07:50:34 +00:00
|
|
|
func (x *WaitParams) SetTimeout(timeout time.Duration) {
|
2022-03-15 12:49:14 +00:00
|
|
|
x.timeout = timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPollInterval specifies the interval, once it will check the completion of the operation.
|
2022-03-23 07:50:34 +00:00
|
|
|
func (x *WaitParams) SetPollInterval(tick time.Duration) {
|
2022-03-15 12:49:14 +00:00
|
|
|
x.pollInterval = tick
|
2021-06-22 09:15:21 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 08:06:23 +00:00
|
|
|
func (x *WaitParams) setDefaults() {
|
|
|
|
x.timeout = 120 * time.Second
|
|
|
|
x.pollInterval = 5 * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkForPositive panics if any of the wait params isn't positive.
|
|
|
|
func (x *WaitParams) checkForPositive() {
|
|
|
|
if x.timeout <= 0 || x.pollInterval <= 0 {
|
|
|
|
panic("all wait params must be positive")
|
2021-06-22 09:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
copiesNumber uint32
|
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) {
|
|
|
|
x.copiesNumber = copiesNumber
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-06-21 14:50:39 +00:00
|
|
|
prmClient sdkClient.PrmContainerPut
|
2022-03-28 08:06:23 +00:00
|
|
|
|
|
|
|
waitParams WaitParams
|
|
|
|
waitParamsSet bool
|
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.
|
|
|
|
//
|
2022-12-13 14:36:35 +00:00
|
|
|
// See github.com/TrueCloudLab/frostfs-sdk-go/client.PrmContainerPut.SetContainer.
|
2022-03-15 12:49:14 +00:00
|
|
|
func (x *PrmContainerPut) SetContainer(cnr container.Container) {
|
2022-06-21 14:50:39 +00:00
|
|
|
x.prmClient.SetContainer(cnr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithinSession specifies session to be used as a parameter of the base
|
|
|
|
// client's operation.
|
|
|
|
//
|
2022-12-13 14:36:35 +00:00
|
|
|
// See github.com/TrueCloudLab/frostfs-sdk-go/client.PrmContainerPut.WithinSession.
|
2022-06-21 14:50:39 +00:00
|
|
|
func (x *PrmContainerPut) WithinSession(s session.Container) {
|
|
|
|
x.prmClient.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.
|
|
|
|
func (x *PrmContainerPut) SetWaitParams(waitParams WaitParams) {
|
|
|
|
waitParams.checkForPositive()
|
|
|
|
x.waitParams = waitParams
|
|
|
|
x.waitParamsSet = true
|
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
// PrmContainerGet groups parameters of GetContainer operation.
|
|
|
|
type PrmContainerGet struct {
|
2022-03-15 12:49:14 +00:00
|
|
|
cnrID cid.ID
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainerID specifies identifier of the container to be read.
|
2022-03-15 12:49:14 +00:00
|
|
|
func (x *PrmContainerGet) SetContainerID(cnrID cid.ID) {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.cnrID = cnrID
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerList groups parameters of ListContainers operation.
|
|
|
|
type PrmContainerList struct {
|
2022-04-11 06:30:22 +00:00
|
|
|
ownerID user.ID
|
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.
|
2022-04-11 06:30:22 +00:00
|
|
|
func (x *PrmContainerList) SetOwnerID(ownerID user.ID) {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.ownerID = ownerID
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerDelete groups parameters of DeleteContainer operation.
|
|
|
|
type PrmContainerDelete struct {
|
2022-04-12 10:16:07 +00:00
|
|
|
cnrID cid.ID
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
stoken session.Container
|
2022-04-12 10:16:07 +00:00
|
|
|
stokenSet bool
|
2022-03-28 08:06:23 +00:00
|
|
|
|
|
|
|
waitParams WaitParams
|
|
|
|
waitParamsSet bool
|
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.
|
2022-03-15 12:49:14 +00:00
|
|
|
func (x *PrmContainerDelete) SetContainerID(cnrID cid.ID) {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.cnrID = cnrID
|
|
|
|
}
|
|
|
|
|
2022-03-17 09:53:04 +00:00
|
|
|
// SetSessionToken specifies session within which operation should be performed.
|
2022-04-07 16:09:15 +00:00
|
|
|
func (x *PrmContainerDelete) SetSessionToken(token session.Container) {
|
2022-03-17 09:53:04 +00:00
|
|
|
x.stoken = token
|
2022-04-12 10:16:07 +00:00
|
|
|
x.stokenSet = true
|
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.
|
|
|
|
func (x *PrmContainerDelete) SetWaitParams(waitParams WaitParams) {
|
|
|
|
waitParams.checkForPositive()
|
|
|
|
x.waitParams = waitParams
|
|
|
|
x.waitParamsSet = true
|
|
|
|
}
|
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
// PrmContainerEACL groups parameters of GetEACL operation.
|
|
|
|
type PrmContainerEACL struct {
|
2022-03-15 12:49:14 +00:00
|
|
|
cnrID cid.ID
|
2022-03-11 13:06:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// SetContainerID specifies identifier of the FrostFS container to read the eACL table.
|
2022-03-15 12:49:14 +00:00
|
|
|
func (x *PrmContainerEACL) SetContainerID(cnrID cid.ID) {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.cnrID = cnrID
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmContainerSetEACL groups parameters of SetEACL operation.
|
|
|
|
type PrmContainerSetEACL struct {
|
2022-03-15 12:49:14 +00:00
|
|
|
table eacl.Table
|
2022-03-28 08:06:23 +00:00
|
|
|
|
2022-06-21 14:50:39 +00:00
|
|
|
sessionSet bool
|
|
|
|
session session.Container
|
|
|
|
|
2022-03-28 08:06:23 +00:00
|
|
|
waitParams WaitParams
|
|
|
|
waitParamsSet bool
|
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.
|
|
|
|
//
|
2022-12-13 14:36:35 +00:00
|
|
|
// See github.com/TrueCloudLab/frostfs-sdk-go/client.PrmContainerSetEACL.SetTable.
|
2022-03-15 12:49:14 +00:00
|
|
|
func (x *PrmContainerSetEACL) SetTable(table eacl.Table) {
|
2022-03-11 13:06:08 +00:00
|
|
|
x.table = table
|
|
|
|
}
|
|
|
|
|
2022-06-21 14:50:39 +00:00
|
|
|
// WithinSession specifies session to be used as a parameter of the base
|
|
|
|
// client's operation.
|
|
|
|
//
|
2022-12-13 14:36:35 +00:00
|
|
|
// See github.com/TrueCloudLab/frostfs-sdk-go/client.PrmContainerSetEACL.WithinSession.
|
2022-06-21 14:50:39 +00:00
|
|
|
func (x *PrmContainerSetEACL) WithinSession(s session.Container) {
|
|
|
|
x.session = s
|
|
|
|
x.sessionSet = true
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (x *PrmContainerSetEACL) SetWaitParams(waitParams WaitParams) {
|
|
|
|
waitParams.checkForPositive()
|
|
|
|
x.waitParams = waitParams
|
|
|
|
x.waitParamsSet = true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
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
|
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
|
|
|
},
|
|
|
|
clientBuilder: options.clientBuilder,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-11-08 09:50:23 +00:00
|
|
|
err := initSessionForDuration(ctx, &st, clients[j], p.rebalanceParams.sessionExpirationDuration, *p.key)
|
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
|
|
|
|
2022-11-08 09:50:23 +00:00
|
|
|
_ = p.cache.Put(formCacheKey(addr, p.key), st)
|
|
|
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
switch level {
|
|
|
|
case zap.DebugLevel:
|
|
|
|
p.logger.Debug(msg, fields...)
|
|
|
|
case zap.WarnLevel:
|
|
|
|
p.logger.Warn(msg, fields...)
|
|
|
|
case zap.ErrorLevel:
|
|
|
|
p.logger.Error(msg, fields...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
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)
|
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]
|
|
|
|
go func(i int, innerPool *innerPool) {
|
|
|
|
defer wg.Done()
|
2022-03-11 14:55:02 +00:00
|
|
|
p.updateInnerNodesHealth(ctx, i, bufferWeights)
|
2021-11-16 13:50:33 +00:00
|
|
|
}(i, inner)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
func (p *Pool) updateInnerNodesHealth(ctx context.Context, i int, bufferWeights []float64) {
|
|
|
|
if i > len(p.innerPools)-1 {
|
2021-11-16 13:50:33 +00:00
|
|
|
return
|
2021-06-21 11:12:08 +00:00
|
|
|
}
|
2022-03-11 14:55:02 +00:00
|
|
|
pool := p.innerPools[i]
|
|
|
|
options := p.rebalanceParams
|
2021-11-16 13:50:33 +00:00
|
|
|
|
2022-07-12 18:55:59 +00:00
|
|
|
healthyChanged := atomic.NewBool(false)
|
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
|
|
|
|
2021-10-25 12:57:55 +00:00
|
|
|
func formCacheKey(address string, key *ecdsa.PrivateKey) string {
|
2021-12-10 13:56:04 +00:00
|
|
|
k := keys.PrivateKey{PrivateKey: *key}
|
|
|
|
return address + k.String()
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
func (p *Pool) checkSessionTokenErr(err error, address string) bool {
|
2021-10-25 12:57:55 +00:00
|
|
|
if err == nil {
|
2021-11-03 14:34:02 +00:00
|
|
|
return false
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-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
|
|
|
}
|
|
|
|
|
2022-05-30 08:40:28 +00:00
|
|
|
func initSessionForDuration(ctx context.Context, dst *session.Object, c client, dur uint64, ownerKey ecdsa.PrivateKey) 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
|
|
|
|
2022-03-28 09:17:56 +00:00
|
|
|
res, err := c.sessionCreate(ctx, prm)
|
2022-04-05 09:15:13 +00:00
|
|
|
if err != nil {
|
2022-04-26 10:09:13 +00:00
|
|
|
return err
|
2022-04-05 09:15:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
var id uuid.UUID
|
|
|
|
|
|
|
|
err = id.UnmarshalBinary(res.id)
|
|
|
|
if err != nil {
|
2022-04-26 10:09:13 +00:00
|
|
|
return fmt.Errorf("invalid session token ID: %w", err)
|
2022-04-07 16:09:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
var key frostfsecdsa.PublicKey
|
2022-04-07 16:09:15 +00:00
|
|
|
|
|
|
|
err = key.Decode(res.sessionKey)
|
|
|
|
if err != nil {
|
2022-04-26 10:09:13 +00:00
|
|
|
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-02-11 14:40:09 +00:00
|
|
|
// base context for RPC
|
|
|
|
context.Context
|
2022-02-07 20:27:56 +00:00
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
client client
|
2022-02-07 20:27:56 +00:00
|
|
|
|
|
|
|
// client endpoint
|
|
|
|
endpoint string
|
|
|
|
|
|
|
|
// request signer
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
// flag to open default session if session token is missing
|
|
|
|
sessionDefault bool
|
2022-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
|
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.
|
2022-03-09 09:40:23 +00:00
|
|
|
func (p *Pool) openDefaultSession(ctx *callContext) error {
|
2022-02-11 14:40:09 +00:00
|
|
|
cacheKey := formCacheKey(ctx.endpoint, ctx.key)
|
|
|
|
|
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
|
2022-05-30 08:40:28 +00:00
|
|
|
err := initSessionForDuration(ctx, &tok, ctx.client, p.stokenDuration, *ctx.key)
|
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
|
|
|
}
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
tok.ForVerb(ctx.sessionVerb)
|
2022-05-25 09:03:22 +00:00
|
|
|
tok.BindContainer(ctx.sessionCnr)
|
|
|
|
|
|
|
|
if ctx.sessionObjSet {
|
2022-11-11 10:17:26 +00:00
|
|
|
tok.LimitByObjects(ctx.sessionObjs...)
|
2022-05-25 09:03:22 +00:00
|
|
|
}
|
2022-02-11 14:40:09 +00:00
|
|
|
|
|
|
|
// sign the token
|
2022-04-07 16:09:15 +00:00
|
|
|
if err := tok.Sign(*ctx.key); err != nil {
|
2022-02-11 14:40:09 +00:00
|
|
|
return fmt.Errorf("sign token of the opened session: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
ctx.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.
|
|
|
|
func (p *Pool) call(ctx *callContext, f func() error) error {
|
2022-02-11 16:13:53 +00:00
|
|
|
var err error
|
2022-02-11 14:40:09 +00:00
|
|
|
|
2022-02-11 16:13:53 +00:00
|
|
|
if ctx.sessionDefault {
|
2022-03-24 14:19:30 +00:00
|
|
|
err = p.openDefaultSession(ctx)
|
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()
|
2022-03-24 14:19:30 +00:00
|
|
|
_ = p.checkSessionTokenErr(err, ctx.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
|
|
|
|
|
2022-02-11 14:40:09 +00:00
|
|
|
ctxCall.Context = ctx
|
2022-02-07 20:27:56 +00:00
|
|
|
|
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
|
2022-03-28 09:17:56 +00:00
|
|
|
if err := p.openDefaultSession(&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
|
|
|
|
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)
|
2022-08-08 09:15:10 +00:00
|
|
|
return id, fmt.Errorf("init writing on API client: %w", 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-02-15 05:36:47 +00:00
|
|
|
|
|
|
|
cc.Context = ctx
|
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
|
|
|
|
2022-03-24 14:19:30 +00:00
|
|
|
return p.call(&cc, func() error {
|
2022-03-28 09:17:56 +00:00
|
|
|
if err = cc.client.objectDelete(ctx, prm); err != nil {
|
2022-02-15 05:36:47 +00:00
|
|
|
return fmt.Errorf("remove object via client: %w", err)
|
|
|
|
}
|
2021-11-17 11:22:32 +00:00
|
|
|
|
2022-02-15 05:36:47 +00:00
|
|
|
return nil
|
|
|
|
})
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-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-03-21 11:29:29 +00:00
|
|
|
var prmCtx prmContext
|
|
|
|
prmCtx.useDefaultSession()
|
2022-04-07 16:09:15 +00:00
|
|
|
prmCtx.useVerb(session.VerbObjectGet)
|
|
|
|
prmCtx.useAddress(prm.addr)
|
2022-02-11 14:40:09 +00:00
|
|
|
|
2022-04-12 13:48:55 +00:00
|
|
|
p.fillAppropriateKey(&prm.prmCommon)
|
|
|
|
|
2022-03-24 14:19:30 +00:00
|
|
|
var cc callContext
|
2022-02-11 14:40:09 +00:00
|
|
|
cc.Context = ctx
|
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-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 {
|
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
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
return res, p.call(&cc, func() error {
|
2022-03-28 09:17:56 +00:00
|
|
|
res, err = cc.client.objectGet(ctx, prm)
|
2022-03-21 11:29:29 +00:00
|
|
|
return err
|
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-03-21 11:29:29 +00:00
|
|
|
var prmCtx prmContext
|
|
|
|
prmCtx.useDefaultSession()
|
2022-04-07 16:09:15 +00:00
|
|
|
prmCtx.useVerb(session.VerbObjectHead)
|
|
|
|
prmCtx.useAddress(prm.addr)
|
2022-02-11 16:13:53 +00:00
|
|
|
|
2022-04-12 13:48:55 +00:00
|
|
|
p.fillAppropriateKey(&prm.prmCommon)
|
|
|
|
|
2022-03-24 14:19:30 +00:00
|
|
|
var cc callContext
|
2022-02-11 16:13:53 +00:00
|
|
|
|
|
|
|
cc.Context = ctx
|
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-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 {
|
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
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
return obj, p.call(&cc, func() error {
|
2022-03-28 09:17:56 +00:00
|
|
|
obj, err = cc.client.objectHead(ctx, prm)
|
2022-03-21 11:29:29 +00:00
|
|
|
return err
|
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-03-21 11:29:29 +00:00
|
|
|
var prmCtx prmContext
|
|
|
|
prmCtx.useDefaultSession()
|
2022-04-07 16:09:15 +00:00
|
|
|
prmCtx.useVerb(session.VerbObjectRange)
|
|
|
|
prmCtx.useAddress(prm.addr)
|
2022-02-12 09:26:38 +00:00
|
|
|
|
2022-04-12 13:48:55 +00:00
|
|
|
p.fillAppropriateKey(&prm.prmCommon)
|
|
|
|
|
2022-03-24 14:19:30 +00:00
|
|
|
var cc callContext
|
2022-02-12 09:26:38 +00:00
|
|
|
cc.Context = ctx
|
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-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 {
|
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
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
return res, p.call(&cc, func() error {
|
2022-03-28 09:17:56 +00:00
|
|
|
res, err = cc.client.objectRange(ctx, prm)
|
2022-03-21 11:29:29 +00:00
|
|
|
return err
|
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-03-21 11:29:29 +00:00
|
|
|
var prmCtx prmContext
|
|
|
|
prmCtx.useDefaultSession()
|
2022-04-07 16:09:15 +00:00
|
|
|
prmCtx.useVerb(session.VerbObjectSearch)
|
2022-05-25 09:03:22 +00:00
|
|
|
prmCtx.useContainer(prm.cnrID)
|
2022-04-06 06:19:05 +00:00
|
|
|
|
2022-04-12 13:48:55 +00:00
|
|
|
p.fillAppropriateKey(&prm.prmCommon)
|
|
|
|
|
2022-03-24 14:19:30 +00:00
|
|
|
var cc callContext
|
2022-02-12 20:30:16 +00:00
|
|
|
|
|
|
|
cc.Context = ctx
|
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-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 {
|
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
|
|
|
|
2022-03-21 11:29:29 +00:00
|
|
|
return res, p.call(&cc, func() error {
|
2022-03-28 09:17:56 +00:00
|
|
|
res, err = cc.client.objectSearch(ctx, prm)
|
2022-03-21 11:29:29 +00:00
|
|
|
return err
|
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
|
|
|
|
2022-07-12 18:55:59 +00:00
|
|
|
return cp.containerPut(ctx, prm)
|
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
|
|
|
|
2022-07-12 18:55:59 +00:00
|
|
|
return cp.containerGet(ctx, prm)
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 08:54:15 +00:00
|
|
|
// ListContainers requests identifiers of the account-owned containers.
|
2022-03-11 13:06:08 +00:00
|
|
|
func (p *Pool) ListContainers(ctx context.Context, prm PrmContainerList) ([]cid.ID, error) {
|
2022-03-17 09:53:04 +00:00
|
|
|
cp, err := p.connection()
|
2021-10-25 12:57:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-11 17:17:31 +00:00
|
|
|
|
2022-07-12 18:55:59 +00:00
|
|
|
return cp.containerList(ctx, prm)
|
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
|
|
|
|
2022-07-12 18:55:59 +00:00
|
|
|
return cp.containerDelete(ctx, prm)
|
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
|
|
|
|
2022-07-12 18:55:59 +00:00
|
|
|
return cp.containerEACL(ctx, prm)
|
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
|
|
|
|
2022-07-12 18:55:59 +00:00
|
|
|
return cp.containerSetEACL(ctx, prm)
|
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
|
|
|
}
|
|
|
|
|
2022-07-12 18:55:59 +00:00
|
|
|
return cp.balanceGet(ctx, prm)
|
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 {
|
|
|
|
inner.lock.RLock()
|
|
|
|
for _, cl := range inner.clients {
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// waitForContainerPresence waits until the container is found on the FrostFS network.
|
2022-08-05 07:56:49 +00:00
|
|
|
func waitForContainerPresence(ctx context.Context, cli client, cnrID cid.ID, waitParams *WaitParams) error {
|
2022-03-23 07:50:34 +00:00
|
|
|
var prm PrmContainerGet
|
2022-08-05 07:56:49 +00:00
|
|
|
prm.SetContainerID(cnrID)
|
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.
|
2022-03-21 11:29:29 +00:00
|
|
|
func waitForEACLPresence(ctx context.Context, cli client, cnrID *cid.ID, table *eacl.Table, waitParams *WaitParams) error {
|
2022-03-23 07:50:34 +00:00
|
|
|
var prm PrmContainerEACL
|
|
|
|
if cnrID != nil {
|
|
|
|
prm.SetContainerID(*cnrID)
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2022-03-21 11:29:29 +00:00
|
|
|
func waitForContainerRemoved(ctx context.Context, cli client, cnrID *cid.ID, waitParams *WaitParams) error {
|
2022-03-24 12:42:49 +00:00
|
|
|
var prm PrmContainerGet
|
|
|
|
if cnrID != nil {
|
|
|
|
prm.SetContainerID(*cnrID)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
wctx, cancel := context.WithTimeout(ctx, params.timeout)
|
2021-06-22 09:15:21 +00:00
|
|
|
defer cancel()
|
2022-03-23 07:50:34 +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
|
|
|
|
}
|
2022-03-23 07:50:34 +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
|
|
|
}
|
|
|
|
|
2022-07-12 18:55:59 +00:00
|
|
|
return cp.networkInfo(ctx, prmNetworkInfo{})
|
2022-03-15 12:00:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 09:40:23 +00:00
|
|
|
// Close closes the Pool and releases all the associated resources.
|
|
|
|
func (p *Pool) Close() {
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
p.cancel()
|
|
|
|
<-p.closedCh
|
|
|
|
}
|
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
|
|
|
|
}
|