2022-05-28 09:27:55 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-07 07:25:03 +00:00
|
|
|
"errors"
|
2022-06-06 11:46:00 +00:00
|
|
|
"fmt"
|
2022-05-30 17:33:54 +00:00
|
|
|
"strings"
|
2022-05-28 09:27:55 +00:00
|
|
|
"sync"
|
2022-05-28 13:48:25 +00:00
|
|
|
"time"
|
2022-05-28 09:27:55 +00:00
|
|
|
|
2023-03-13 11:01:43 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/pkg/tracing"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/network"
|
2022-12-30 09:53:41 +00:00
|
|
|
"github.com/hashicorp/golang-lru/v2/simplelru"
|
2022-05-28 09:27:55 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/connectivity"
|
2022-07-30 07:24:16 +00:00
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2022-05-28 09:27:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type clientCache struct {
|
|
|
|
sync.Mutex
|
2022-12-30 09:53:41 +00:00
|
|
|
simplelru.LRU[string, cacheItem]
|
2022-05-28 09:27:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 11:46:00 +00:00
|
|
|
type cacheItem struct {
|
|
|
|
cc *grpc.ClientConn
|
|
|
|
lastTry time.Time
|
|
|
|
}
|
|
|
|
|
2022-05-28 13:48:25 +00:00
|
|
|
const (
|
|
|
|
defaultClientCacheSize = 10
|
|
|
|
defaultClientConnectTimeout = time.Second * 2
|
2022-06-06 11:46:00 +00:00
|
|
|
defaultReconnectInterval = time.Second * 15
|
2022-05-28 13:48:25 +00:00
|
|
|
)
|
2022-05-28 09:27:55 +00:00
|
|
|
|
2022-09-07 07:25:03 +00:00
|
|
|
var errRecentlyFailed = errors.New("client has recently failed")
|
|
|
|
|
2022-05-28 09:27:55 +00:00
|
|
|
func (c *clientCache) init() {
|
2022-12-30 09:53:41 +00:00
|
|
|
l, _ := simplelru.NewLRU[string, cacheItem](defaultClientCacheSize, func(_ string, value cacheItem) {
|
2023-05-04 16:44:49 +00:00
|
|
|
if conn := value.cc; conn != nil {
|
|
|
|
_ = conn.Close()
|
|
|
|
}
|
2022-05-28 09:27:55 +00:00
|
|
|
})
|
|
|
|
c.LRU = *l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientCache) get(ctx context.Context, netmapAddr string) (TreeServiceClient, error) {
|
|
|
|
c.Lock()
|
|
|
|
ccInt, ok := c.LRU.Get(netmapAddr)
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
if ok {
|
2022-12-30 09:53:41 +00:00
|
|
|
item := ccInt
|
2022-06-06 11:46:00 +00:00
|
|
|
if item.cc == nil {
|
|
|
|
if d := time.Since(item.lastTry); d < defaultReconnectInterval {
|
2022-09-07 07:25:03 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s till the next reconnection to %s",
|
|
|
|
errRecentlyFailed, d, netmapAddr)
|
2022-06-06 11:46:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if s := item.cc.GetState(); s == connectivity.Idle || s == connectivity.Ready {
|
|
|
|
return NewTreeServiceClient(item.cc), nil
|
|
|
|
}
|
|
|
|
_ = item.cc.Close()
|
2022-05-28 09:27:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cc, err := dialTreeService(ctx, netmapAddr)
|
2022-06-06 11:46:00 +00:00
|
|
|
lastTry := time.Now()
|
2022-05-28 09:27:55 +00:00
|
|
|
|
|
|
|
c.Lock()
|
2022-06-06 11:46:00 +00:00
|
|
|
if err != nil {
|
|
|
|
c.LRU.Add(netmapAddr, cacheItem{cc: nil, lastTry: lastTry})
|
|
|
|
} else {
|
|
|
|
c.LRU.Add(netmapAddr, cacheItem{cc: cc, lastTry: lastTry})
|
|
|
|
}
|
2022-05-28 09:27:55 +00:00
|
|
|
c.Unlock()
|
|
|
|
|
2022-06-06 11:46:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-28 09:27:55 +00:00
|
|
|
return NewTreeServiceClient(cc), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func dialTreeService(ctx context.Context, netmapAddr string) (*grpc.ClientConn, error) {
|
|
|
|
var netAddr network.Address
|
|
|
|
if err := netAddr.FromString(netmapAddr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-13 11:01:43 +00:00
|
|
|
opts := []grpc.DialOption{
|
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.WithChainUnaryInterceptor(
|
|
|
|
tracing.NewGRPCUnaryClientInteceptor(),
|
|
|
|
),
|
|
|
|
grpc.WithChainStreamInterceptor(
|
|
|
|
tracing.NewGRPCStreamClientInterceptor(),
|
|
|
|
),
|
|
|
|
}
|
2022-05-30 17:33:54 +00:00
|
|
|
|
|
|
|
// FIXME(@fyrchik): ugly hack #1322
|
|
|
|
if !strings.HasPrefix(netAddr.URIAddr(), "grpcs:") {
|
2022-07-30 07:24:16 +00:00
|
|
|
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
2022-05-30 17:33:54 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 13:48:25 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultClientConnectTimeout)
|
2022-05-30 17:33:54 +00:00
|
|
|
cc, err := grpc.DialContext(ctx, netAddr.URIAddr(), opts...)
|
2022-05-28 13:48:25 +00:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
return cc, err
|
2022-05-28 09:27:55 +00:00
|
|
|
}
|