2021-06-21 14:56:19 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-03 11:21:04 +00:00
|
|
|
"errors"
|
2022-10-21 15:46:45 +00:00
|
|
|
"fmt"
|
2021-06-21 14:56:19 +00:00
|
|
|
"sync"
|
2022-12-19 14:47:28 +00:00
|
|
|
"time"
|
2021-06-21 14:56:19 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
rawclient "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
|
|
|
|
clientcore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/network"
|
2023-05-31 09:30:46 +00:00
|
|
|
metrics "git.frostfs.info/TrueCloudLab/frostfs-observability/metrics/grpc"
|
|
|
|
tracing "git.frostfs.info/TrueCloudLab/frostfs-observability/tracing/grpc"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-05-31 09:30:46 +00:00
|
|
|
"google.golang.org/grpc"
|
2023-02-15 09:46:01 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2021-06-21 14:56:19 +00:00
|
|
|
)
|
|
|
|
|
2022-12-19 14:47:28 +00:00
|
|
|
type singleClient struct {
|
|
|
|
sync.RWMutex
|
|
|
|
client clientcore.Client
|
|
|
|
lastAttempt time.Time
|
|
|
|
}
|
|
|
|
|
2021-06-21 14:56:19 +00:00
|
|
|
type multiClient struct {
|
|
|
|
mtx sync.RWMutex
|
|
|
|
|
2022-12-19 14:47:28 +00:00
|
|
|
clients map[string]*singleClient
|
2021-06-21 14:56:19 +00:00
|
|
|
|
2022-12-13 13:10:45 +00:00
|
|
|
// addrMtx protects addr field. Should not be taken before the mtx.
|
|
|
|
addrMtx sync.RWMutex
|
|
|
|
addr network.AddressGroup
|
2021-06-21 14:56:19 +00:00
|
|
|
|
2022-03-11 15:24:11 +00:00
|
|
|
opts ClientCacheOpts
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 14:47:28 +00:00
|
|
|
const defaultReconnectInterval = time.Second * 30
|
|
|
|
|
2022-03-11 15:24:11 +00:00
|
|
|
func newMultiClient(addr network.AddressGroup, opts ClientCacheOpts) *multiClient {
|
2022-12-19 15:03:48 +00:00
|
|
|
if opts.ReconnectTimeout <= 0 {
|
|
|
|
opts.ReconnectTimeout = defaultReconnectInterval
|
|
|
|
}
|
2021-06-21 14:56:19 +00:00
|
|
|
return &multiClient{
|
2023-01-16 12:47:17 +00:00
|
|
|
clients: make(map[string]*singleClient),
|
|
|
|
addr: addr,
|
|
|
|
opts: opts,
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-18 09:04:59 +00:00
|
|
|
func (x *multiClient) createForAddress(ctx context.Context, addr network.Address) (clientcore.Client, error) {
|
2023-11-15 12:28:45 +00:00
|
|
|
var c client.Client
|
2021-06-21 14:56:19 +00:00
|
|
|
|
2023-11-15 12:28:45 +00:00
|
|
|
prmInit := client.PrmInit{
|
|
|
|
DisableFrostFSErrorResolution: true,
|
|
|
|
}
|
2022-03-11 15:24:11 +00:00
|
|
|
if x.opts.Key != nil {
|
2023-11-15 12:28:45 +00:00
|
|
|
prmInit.Key = *x.opts.Key
|
2022-03-11 15:24:11 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 12:28:45 +00:00
|
|
|
prmDial := client.PrmDial{
|
|
|
|
Endpoint: addr.URIAddr(),
|
|
|
|
GRPCDialOptions: []grpc.DialOption{
|
|
|
|
grpc.WithChainUnaryInterceptor(
|
|
|
|
metrics.NewUnaryClientInterceptor(),
|
|
|
|
tracing.NewUnaryClientInteceptor(),
|
|
|
|
),
|
|
|
|
grpc.WithChainStreamInterceptor(
|
|
|
|
metrics.NewStreamClientInterceptor(),
|
|
|
|
tracing.NewStreamClientInterceptor(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
2022-03-11 15:24:11 +00:00
|
|
|
if x.opts.DialTimeout > 0 {
|
2023-11-15 12:28:45 +00:00
|
|
|
prmDial.DialTimeout = x.opts.DialTimeout
|
2022-03-11 15:24:11 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 15:23:59 +00:00
|
|
|
if x.opts.StreamTimeout > 0 {
|
2023-11-15 12:28:45 +00:00
|
|
|
prmDial.StreamTimeout = x.opts.StreamTimeout
|
2022-09-06 15:23:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 15:24:11 +00:00
|
|
|
if x.opts.ResponseCallback != nil {
|
2023-11-15 12:28:45 +00:00
|
|
|
prmInit.ResponseInfoCallback = x.opts.ResponseCallback
|
2022-03-11 15:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Init(prmInit)
|
2023-04-18 09:04:59 +00:00
|
|
|
err := c.Dial(ctx, prmDial)
|
2021-06-21 14:56:19 +00:00
|
|
|
if err != nil {
|
2022-10-21 15:46:45 +00:00
|
|
|
return nil, fmt.Errorf("can't init SDK client: %w", err)
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 15:46:45 +00:00
|
|
|
return &c, nil
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 12:02:27 +00:00
|
|
|
// updateGroup replaces current multiClient addresses with a new group.
|
|
|
|
// Old addresses not present in group are removed.
|
|
|
|
func (x *multiClient) updateGroup(group network.AddressGroup) {
|
|
|
|
// Firstly, remove old clients.
|
|
|
|
cache := make([]string, 0, group.Len())
|
|
|
|
group.IterateAddresses(func(a network.Address) bool {
|
|
|
|
cache = append(cache, a.String())
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2022-12-13 13:10:45 +00:00
|
|
|
x.addrMtx.RLock()
|
|
|
|
oldGroup := x.addr
|
|
|
|
x.addrMtx.RUnlock()
|
|
|
|
if len(oldGroup) == len(cache) {
|
|
|
|
needUpdate := false
|
|
|
|
for i := range oldGroup {
|
|
|
|
if cache[i] != oldGroup[i].String() {
|
|
|
|
needUpdate = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !needUpdate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 12:02:27 +00:00
|
|
|
x.mtx.Lock()
|
|
|
|
defer x.mtx.Unlock()
|
|
|
|
loop:
|
|
|
|
for a := range x.clients {
|
|
|
|
for i := range cache {
|
|
|
|
if cache[i] == a {
|
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
}
|
2023-09-04 12:13:49 +00:00
|
|
|
x.clients[a].invalidate()
|
2022-03-21 12:02:27 +00:00
|
|
|
delete(x.clients, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then add new clients.
|
2022-12-13 13:10:45 +00:00
|
|
|
x.addrMtx.Lock()
|
2022-03-21 12:02:27 +00:00
|
|
|
x.addr = group
|
2022-12-13 13:10:45 +00:00
|
|
|
x.addrMtx.Unlock()
|
2022-03-21 12:02:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 14:47:28 +00:00
|
|
|
var errRecentlyFailed = errors.New("client has recently failed, skipping")
|
|
|
|
|
2022-01-13 15:01:50 +00:00
|
|
|
func (x *multiClient) iterateClients(ctx context.Context, f func(clientcore.Client) error) error {
|
2021-06-21 14:56:19 +00:00
|
|
|
var firstErr error
|
|
|
|
|
2022-12-13 13:10:45 +00:00
|
|
|
x.addrMtx.RLock()
|
|
|
|
group := x.addr
|
|
|
|
x.addrMtx.RUnlock()
|
|
|
|
|
|
|
|
group.IterateAddresses(func(addr network.Address) bool {
|
2021-08-03 11:21:04 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
firstErr = context.Canceled
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-06-21 14:56:19 +00:00
|
|
|
var err error
|
|
|
|
|
2023-04-18 09:04:59 +00:00
|
|
|
c, err := x.client(ctx, addr)
|
2022-10-21 15:46:45 +00:00
|
|
|
if err == nil {
|
|
|
|
err = f(c)
|
|
|
|
}
|
2021-06-21 14:56:19 +00:00
|
|
|
|
2023-01-09 18:36:33 +00:00
|
|
|
// non-status logic error that could be returned
|
|
|
|
// from the SDK client; should not be considered
|
|
|
|
// as a connection error
|
2023-07-06 12:36:41 +00:00
|
|
|
var siErr *objectSDK.SplitInfoError
|
2021-06-21 14:56:19 +00:00
|
|
|
|
2023-01-09 18:36:33 +00:00
|
|
|
success := err == nil || errors.Is(err, context.Canceled) || errors.As(err, &siErr)
|
2022-12-19 14:47:28 +00:00
|
|
|
if success || firstErr == nil || errors.Is(firstErr, errRecentlyFailed) {
|
2021-06-21 14:56:19 +00:00
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
|
2022-12-19 14:47:28 +00:00
|
|
|
if err != nil {
|
|
|
|
x.ReportError(err)
|
|
|
|
}
|
|
|
|
|
2021-06-21 14:56:19 +00:00
|
|
|
return success
|
|
|
|
})
|
|
|
|
|
|
|
|
return firstErr
|
|
|
|
}
|
|
|
|
|
2022-12-19 14:47:28 +00:00
|
|
|
func (x *multiClient) ReportError(err error) {
|
|
|
|
if errors.Is(err, errRecentlyFailed) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-15 09:46:01 +00:00
|
|
|
if status.Code(err) == codes.Canceled || errors.Is(err, context.Canceled) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-09 18:36:33 +00:00
|
|
|
// non-status logic error that could be returned
|
|
|
|
// from the SDK client; should not be considered
|
|
|
|
// as a connection error
|
2023-07-06 12:36:41 +00:00
|
|
|
var siErr *objectSDK.SplitInfoError
|
2023-01-09 18:36:33 +00:00
|
|
|
if errors.As(err, &siErr) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-19 14:47:28 +00:00
|
|
|
// Dropping all clients here is not necessary, we do this
|
|
|
|
// because `multiClient` doesn't yet provide convenient interface
|
|
|
|
// for reporting individual errors for streaming operations.
|
|
|
|
x.mtx.RLock()
|
|
|
|
for _, sc := range x.clients {
|
|
|
|
sc.invalidate()
|
|
|
|
}
|
|
|
|
x.mtx.RUnlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *singleClient) invalidate() {
|
|
|
|
s.Lock()
|
|
|
|
if s.client != nil {
|
|
|
|
_ = s.client.Close()
|
|
|
|
}
|
|
|
|
s.client = nil
|
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-06-29 06:44:11 +00:00
|
|
|
func (x *multiClient) ObjectPutInit(ctx context.Context, p client.PrmObjectPutInit) (res client.ObjectWriter, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-02-25 09:20:49 +00:00
|
|
|
res, err = c.ObjectPutInit(ctx, p)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 14:47:42 +00:00
|
|
|
func (x *multiClient) ObjectPutSingle(ctx context.Context, p client.PrmObjectPutSingle) (res *client.ResObjectPutSingle, err error) {
|
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
|
|
|
res, err = c.ObjectPutSingle(ctx, p)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) ContainerAnnounceUsedSpace(ctx context.Context, prm client.PrmAnnounceSpace) (res *client.ResAnnounceSpace, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-02-25 09:20:49 +00:00
|
|
|
res, err = c.ContainerAnnounceUsedSpace(ctx, prm)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) ObjectDelete(ctx context.Context, p client.PrmObjectDelete) (res *client.ResObjectDelete, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-02-25 09:20:49 +00:00
|
|
|
res, err = c.ObjectDelete(ctx, p)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) ObjectGetInit(ctx context.Context, p client.PrmObjectGet) (res *client.ObjectReader, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-02-25 09:20:49 +00:00
|
|
|
res, err = c.ObjectGetInit(ctx, p)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) ObjectRangeInit(ctx context.Context, p client.PrmObjectRange) (res *client.ObjectRangeReader, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-02-25 09:20:49 +00:00
|
|
|
res, err = c.ObjectRangeInit(ctx, p)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) ObjectHead(ctx context.Context, p client.PrmObjectHead) (res *client.ResObjectHead, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-02-25 09:20:49 +00:00
|
|
|
res, err = c.ObjectHead(ctx, p)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) ObjectHash(ctx context.Context, p client.PrmObjectHash) (res *client.ResObjectHash, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-02-25 09:20:49 +00:00
|
|
|
res, err = c.ObjectHash(ctx, p)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) ObjectSearchInit(ctx context.Context, p client.PrmObjectSearch) (res *client.ObjectListReader, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-02-25 09:20:49 +00:00
|
|
|
res, err = c.ObjectSearchInit(ctx, p)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
2021-06-21 14:56:19 +00:00
|
|
|
})
|
|
|
|
|
2021-11-06 11:13:04 +00:00
|
|
|
return
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 08:24:40 +00:00
|
|
|
func (x *multiClient) ExecRaw(func(client *rawclient.Client) error) error {
|
2022-03-11 15:24:11 +00:00
|
|
|
panic("multiClient.ExecRaw() must not be called")
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x *multiClient) Close() error {
|
|
|
|
x.mtx.RLock()
|
|
|
|
|
|
|
|
{
|
|
|
|
for _, c := range x.clients {
|
2022-12-19 14:47:28 +00:00
|
|
|
if c.client != nil {
|
|
|
|
_ = c.client.Close()
|
|
|
|
}
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x.mtx.RUnlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-18 09:04:59 +00:00
|
|
|
func (x *multiClient) RawForAddress(ctx context.Context, addr network.Address, f func(client *rawclient.Client) error) error {
|
|
|
|
c, err := x.client(ctx, addr)
|
2022-10-21 15:46:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-19 14:47:28 +00:00
|
|
|
|
|
|
|
err = c.ExecRaw(f)
|
|
|
|
if err != nil {
|
|
|
|
x.ReportError(err)
|
|
|
|
}
|
|
|
|
return err
|
2021-07-13 11:58:26 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 09:04:59 +00:00
|
|
|
func (x *multiClient) client(ctx context.Context, addr network.Address) (clientcore.Client, error) {
|
2021-07-13 11:02:24 +00:00
|
|
|
strAddr := addr.String()
|
|
|
|
|
2022-03-21 12:07:55 +00:00
|
|
|
x.mtx.RLock()
|
2021-07-13 11:02:24 +00:00
|
|
|
c, cached := x.clients[strAddr]
|
2022-03-21 12:07:55 +00:00
|
|
|
x.mtx.RUnlock()
|
|
|
|
|
2022-10-21 15:46:45 +00:00
|
|
|
if cached {
|
2022-12-19 14:47:28 +00:00
|
|
|
c.RLock()
|
|
|
|
if c.client != nil {
|
|
|
|
cl := c.client
|
|
|
|
c.RUnlock()
|
|
|
|
return cl, nil
|
|
|
|
}
|
2023-01-16 12:47:17 +00:00
|
|
|
if x.opts.ReconnectTimeout != 0 && time.Since(c.lastAttempt) < x.opts.ReconnectTimeout {
|
2022-12-19 14:47:28 +00:00
|
|
|
c.RUnlock()
|
|
|
|
return nil, errRecentlyFailed
|
|
|
|
}
|
|
|
|
c.RUnlock()
|
|
|
|
} else {
|
|
|
|
var ok bool
|
|
|
|
x.mtx.Lock()
|
|
|
|
c, ok = x.clients[strAddr]
|
|
|
|
if !ok {
|
|
|
|
c = new(singleClient)
|
|
|
|
x.clients[strAddr] = c
|
|
|
|
}
|
|
|
|
x.mtx.Unlock()
|
2022-10-21 15:46:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 14:47:28 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2021-06-21 14:56:19 +00:00
|
|
|
|
2022-12-19 14:47:28 +00:00
|
|
|
if c.client != nil {
|
|
|
|
return c.client, nil
|
|
|
|
}
|
|
|
|
|
2023-01-16 12:47:17 +00:00
|
|
|
if x.opts.ReconnectTimeout != 0 && time.Since(c.lastAttempt) < x.opts.ReconnectTimeout {
|
2022-12-19 14:47:28 +00:00
|
|
|
return nil, errRecentlyFailed
|
2022-03-21 12:07:55 +00:00
|
|
|
}
|
2022-12-19 14:47:28 +00:00
|
|
|
|
2023-04-18 09:04:59 +00:00
|
|
|
cl, err := x.createForAddress(ctx, addr)
|
2022-12-19 14:47:28 +00:00
|
|
|
if err != nil {
|
|
|
|
c.lastAttempt = time.Now()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.client = cl
|
|
|
|
return cl, nil
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|