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
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
rawclient "github.com/TrueCloudLab/frostfs-api-go/v2/rpc/client"
|
|
|
|
clientcore "github.com/TrueCloudLab/frostfs-node/pkg/core/client"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/network"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/client"
|
2023-01-09 18:36:33 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/object"
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 15:46:45 +00:00
|
|
|
func (x *multiClient) createForAddress(addr network.Address) (clientcore.Client, error) {
|
2022-03-11 15:24:11 +00:00
|
|
|
var (
|
|
|
|
c client.Client
|
|
|
|
prmInit client.PrmInit
|
|
|
|
prmDial client.PrmDial
|
|
|
|
)
|
2021-06-21 14:56:19 +00:00
|
|
|
|
2022-05-04 12:34:26 +00:00
|
|
|
prmDial.SetServerURI(addr.URIAddr())
|
2021-06-21 14:56:19 +00:00
|
|
|
|
2022-03-11 15:24:11 +00:00
|
|
|
if x.opts.Key != nil {
|
|
|
|
prmInit.SetDefaultPrivateKey(*x.opts.Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
if x.opts.DialTimeout > 0 {
|
|
|
|
prmDial.SetTimeout(x.opts.DialTimeout)
|
|
|
|
}
|
|
|
|
|
2022-09-06 15:23:59 +00:00
|
|
|
if x.opts.StreamTimeout > 0 {
|
|
|
|
prmDial.SetStreamTimeout(x.opts.StreamTimeout)
|
|
|
|
}
|
|
|
|
|
2022-03-11 15:24:11 +00:00
|
|
|
if x.opts.ResponseCallback != nil {
|
|
|
|
prmInit.SetResponseInfoCallback(x.opts.ResponseCallback)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Init(prmInit)
|
|
|
|
err := c.Dial(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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2022-10-21 15:46:45 +00:00
|
|
|
c, err := x.client(addr)
|
|
|
|
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
|
|
|
|
var siErr *object.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
|
|
|
|
var siErr *object.SplitInfoError
|
|
|
|
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.lastAttempt = time.Now()
|
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
func (x *multiClient) AnnounceLocalTrust(ctx context.Context, prm client.PrmAnnounceLocalTrust) (res *client.ResAnnounceLocalTrust, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-01-21 16:53:06 +00:00
|
|
|
res, err = c.AnnounceLocalTrust(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) AnnounceIntermediateTrust(ctx context.Context, prm client.PrmAnnounceIntermediateTrust) (res *client.ResAnnounceIntermediateTrust, err error) {
|
2022-01-13 15:01:50 +00:00
|
|
|
err = x.iterateClients(ctx, func(c clientcore.Client) error {
|
2022-01-21 16:53:06 +00:00
|
|
|
res, err = c.AnnounceIntermediateTrust(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-03-11 15:24:11 +00:00
|
|
|
func (x *multiClient) ExecRaw(f func(client *rawclient.Client) error) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-11 15:24:11 +00:00
|
|
|
func (x *multiClient) RawForAddress(addr network.Address, f func(client *rawclient.Client) error) error {
|
2022-10-21 15:46:45 +00:00
|
|
|
c, err := x.client(addr)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-21 15:46:45 +00:00
|
|
|
func (x *multiClient) client(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
|
|
|
|
|
|
|
cl, err := x.createForAddress(addr)
|
|
|
|
if err != nil {
|
|
|
|
c.lastAttempt = time.Now()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.client = cl
|
|
|
|
return cl, nil
|
2021-06-21 14:56:19 +00:00
|
|
|
}
|