2022-05-28 09:27:55 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-09-13 08:59:17 +00:00
|
|
|
"crypto/ecdsa"
|
2022-09-07 07:25:03 +00:00
|
|
|
"errors"
|
2022-06-06 11:46:00 +00:00
|
|
|
"fmt"
|
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
|
|
|
|
2024-10-09 08:18:24 +00:00
|
|
|
internalNet "git.frostfs.info/TrueCloudLab/frostfs-node/internal/net"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/network"
|
2023-05-31 09:26:54 +00:00
|
|
|
metrics "git.frostfs.info/TrueCloudLab/frostfs-observability/metrics/grpc"
|
|
|
|
tracing "git.frostfs.info/TrueCloudLab/frostfs-observability/tracing/grpc"
|
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]
|
2024-09-13 08:59:17 +00:00
|
|
|
key *ecdsa.PrivateKey
|
2024-10-09 08:18:24 +00:00
|
|
|
ds *internalNet.DialerSource
|
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 (
|
2023-05-17 11:11:46 +00:00
|
|
|
defaultClientCacheSize = 32
|
2022-05-28 13:48:25 +00:00
|
|
|
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")
|
|
|
|
|
2024-10-09 08:18:24 +00:00
|
|
|
func (c *clientCache) init(pk *ecdsa.PrivateKey, ds *internalNet.DialerSource) {
|
2024-03-01 08:58:17 +00:00
|
|
|
l, _ := simplelru.NewLRU(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
|
2024-09-13 08:59:17 +00:00
|
|
|
c.key = pk
|
2024-10-09 08:18:24 +00:00
|
|
|
c.ds = ds
|
2022-05-28 09:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-13 08:59:17 +00:00
|
|
|
cc, err := c.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
|
|
|
|
}
|
|
|
|
|
2024-09-13 08:59:17 +00:00
|
|
|
func (c *clientCache) dialTreeService(ctx context.Context, netmapAddr string) (*grpc.ClientConn, error) {
|
2022-05-28 09:27:55 +00:00
|
|
|
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.WithChainUnaryInterceptor(
|
2023-05-31 09:26:54 +00:00
|
|
|
metrics.NewUnaryClientInterceptor(),
|
|
|
|
tracing.NewUnaryClientInteceptor(),
|
2023-03-13 11:01:43 +00:00
|
|
|
),
|
|
|
|
grpc.WithChainStreamInterceptor(
|
2023-05-31 09:26:54 +00:00
|
|
|
metrics.NewStreamClientInterceptor(),
|
|
|
|
tracing.NewStreamClientInterceptor(),
|
2023-03-13 11:01:43 +00:00
|
|
|
),
|
2024-10-09 08:18:24 +00:00
|
|
|
grpc.WithContextDialer(c.ds.GrpcContextDialer()),
|
2024-10-23 11:04:37 +00:00
|
|
|
grpc.WithDefaultCallOptions(grpc.WaitForReady(true)),
|
2023-03-13 11:01:43 +00:00
|
|
|
}
|
2022-05-30 17:33:54 +00:00
|
|
|
|
2023-07-05 14:07:52 +00:00
|
|
|
if !netAddr.IsTLSEnabled() {
|
2022-07-30 07:24:16 +00:00
|
|
|
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
2022-05-30 17:33:54 +00:00
|
|
|
}
|
|
|
|
|
2024-09-13 08:59:17 +00:00
|
|
|
req := &HealthcheckRequest{
|
|
|
|
Body: &HealthcheckRequest_Body{},
|
|
|
|
}
|
|
|
|
if err := SignMessage(req, c.key); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-28 13:48:25 +00:00
|
|
|
|
2024-09-13 08:59:17 +00:00
|
|
|
cc, err := grpc.NewClient(netAddr.URIAddr(), opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultClientConnectTimeout)
|
|
|
|
defer cancel()
|
|
|
|
// perform some request to check connection
|
|
|
|
if _, err := NewTreeServiceClient(cc).Healthcheck(ctx, req); err != nil {
|
|
|
|
_ = cc.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cc, nil
|
2022-05-28 09:27:55 +00:00
|
|
|
}
|