2023-06-06 06:29:23 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-07 09:57:53 +00:00
|
|
|
"crypto/tls"
|
2023-10-10 08:25:57 +00:00
|
|
|
"errors"
|
2023-06-06 06:29:23 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2024-11-07 11:29:55 +00:00
|
|
|
"time"
|
2023-06-06 06:29:23 +00:00
|
|
|
|
2024-11-07 11:29:55 +00:00
|
|
|
rpcapi "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc"
|
|
|
|
rpcclient "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/tree"
|
2023-06-06 06:29:23 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type treeClient struct {
|
2024-11-07 11:29:55 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
address string
|
|
|
|
opts []grpc.DialOption
|
|
|
|
client *rpcclient.Client
|
|
|
|
nodeDialTimeout time.Duration
|
|
|
|
streamTimeout time.Duration
|
|
|
|
|
2023-06-06 06:29:23 +00:00
|
|
|
healthy bool
|
|
|
|
}
|
|
|
|
|
2023-11-21 09:57:35 +00:00
|
|
|
// ErrUnhealthyEndpoint is returned when client in the pool considered unavailable.
|
|
|
|
var ErrUnhealthyEndpoint = errors.New("unhealthy endpoint")
|
2023-10-10 08:25:57 +00:00
|
|
|
|
2023-06-06 06:29:23 +00:00
|
|
|
// newTreeClient creates new tree client with auto dial.
|
2024-11-07 11:29:55 +00:00
|
|
|
func newTreeClient(addr string, opts []grpc.DialOption, dialTimeout time.Duration, streamTimeout time.Duration) *treeClient {
|
2023-06-06 06:29:23 +00:00
|
|
|
return &treeClient{
|
2024-11-07 11:29:55 +00:00
|
|
|
address: addr,
|
|
|
|
opts: opts,
|
|
|
|
nodeDialTimeout: dialTimeout,
|
|
|
|
streamTimeout: streamTimeout,
|
2023-06-06 06:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *treeClient) dial(ctx context.Context) error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
2024-11-07 11:29:55 +00:00
|
|
|
if c.client != nil {
|
2023-06-06 06:29:23 +00:00
|
|
|
return fmt.Errorf("couldn't dial '%s': connection already established", c.address)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2024-11-07 11:29:55 +00:00
|
|
|
c.client, err = c.createClient()
|
|
|
|
if err != nil {
|
2023-07-07 09:57:53 +00:00
|
|
|
return err
|
2023-06-06 06:29:23 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 11:29:55 +00:00
|
|
|
if _, err = rpcapi.Healthcheck(c.client, &tree.HealthcheckRequest{}, rpcclient.WithContext(ctx)); err != nil {
|
2023-06-06 06:29:23 +00:00
|
|
|
return fmt.Errorf("healthcheck tree service: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.healthy = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *treeClient) redialIfNecessary(ctx context.Context) (healthHasChanged bool, err error) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
2024-11-07 11:29:55 +00:00
|
|
|
if c.client == nil {
|
|
|
|
if c.client, err = c.createClient(); err != nil {
|
2023-07-07 09:57:53 +00:00
|
|
|
return false, err
|
2023-06-06 06:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wasHealthy := c.healthy
|
2024-11-07 11:29:55 +00:00
|
|
|
if _, err = rpcapi.Healthcheck(c.client, &tree.HealthcheckRequest{}, rpcclient.WithContext(ctx)); err != nil {
|
2023-06-06 06:29:23 +00:00
|
|
|
c.healthy = false
|
|
|
|
return wasHealthy, fmt.Errorf("healthcheck tree service: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-06-08 14:01:33 +00:00
|
|
|
c.healthy = true
|
|
|
|
|
2023-06-06 06:29:23 +00:00
|
|
|
return !wasHealthy, nil
|
|
|
|
}
|
|
|
|
|
2024-11-07 11:29:55 +00:00
|
|
|
func (c *treeClient) createClient() (*rpcclient.Client, error) {
|
|
|
|
cli := rpcclient.New(append(
|
|
|
|
rpcclient.WithNetworkURIAddress(c.address, &tls.Config{}),
|
|
|
|
rpcclient.WithDialTimeout(c.nodeDialTimeout),
|
|
|
|
rpcclient.WithRWTimeout(c.streamTimeout),
|
|
|
|
rpcclient.WithGRPCDialOptions(c.opts),
|
|
|
|
)...)
|
2023-07-07 09:57:53 +00:00
|
|
|
|
2024-11-07 11:29:55 +00:00
|
|
|
return cli, nil
|
2023-07-07 09:57:53 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 11:29:55 +00:00
|
|
|
func (c *treeClient) serviceClient() (*rpcclient.Client, error) {
|
2023-06-06 06:29:23 +00:00
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
|
2024-11-07 11:29:55 +00:00
|
|
|
if c.client == nil || !c.healthy {
|
2023-10-10 08:25:57 +00:00
|
|
|
return nil, fmt.Errorf("%w: '%s'", ErrUnhealthyEndpoint, c.address)
|
2023-06-06 06:29:23 +00:00
|
|
|
}
|
|
|
|
|
2024-11-07 11:29:55 +00:00
|
|
|
return c.client, nil
|
2023-06-06 06:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *treeClient) endpoint() string {
|
|
|
|
return c.address
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *treeClient) isHealthy() bool {
|
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
return c.healthy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *treeClient) setHealthy(val bool) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
c.healthy = val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *treeClient) close() error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
2024-11-07 11:29:55 +00:00
|
|
|
if c.client == nil || c.client.Conn() == nil {
|
2023-06-06 06:29:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-11-07 11:29:55 +00:00
|
|
|
return c.client.Conn().Close()
|
2023-06-06 06:29:23 +00:00
|
|
|
}
|