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"
|
|
|
|
|
2023-07-07 09:57:53 +00:00
|
|
|
apiClient "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
|
2023-06-06 06:29:23 +00:00
|
|
|
grpcService "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool/tree/service"
|
|
|
|
"google.golang.org/grpc"
|
2023-07-07 09:57:53 +00:00
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2023-06-06 06:29:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type treeClient struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
address string
|
|
|
|
opts []grpc.DialOption
|
|
|
|
conn *grpc.ClientConn
|
|
|
|
service grpcService.TreeServiceClient
|
|
|
|
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.
|
|
|
|
func newTreeClient(addr string, opts ...grpc.DialOption) *treeClient {
|
|
|
|
return &treeClient{
|
|
|
|
address: addr,
|
|
|
|
opts: opts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *treeClient) dial(ctx context.Context) error {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
if c.conn != nil {
|
|
|
|
return fmt.Errorf("couldn't dial '%s': connection already established", c.address)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2023-07-07 09:57:53 +00:00
|
|
|
if c.conn, c.service, err = dialClient(ctx, c.address, c.opts...); err != nil {
|
|
|
|
return err
|
2023-06-06 06:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = c.service.Healthcheck(ctx, &grpcService.HealthcheckRequest{}); err != nil {
|
|
|
|
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()
|
|
|
|
|
|
|
|
if c.conn == nil {
|
2023-07-07 09:57:53 +00:00
|
|
|
if c.conn, c.service, err = dialClient(ctx, c.address, c.opts...); err != nil {
|
|
|
|
return false, err
|
2023-06-06 06:29:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wasHealthy := c.healthy
|
|
|
|
if _, err = c.service.Healthcheck(ctx, &grpcService.HealthcheckRequest{}); err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-07 09:57:53 +00:00
|
|
|
func dialClient(ctx context.Context, addr string, clientOptions ...grpc.DialOption) (*grpc.ClientConn, grpcService.TreeServiceClient, error) {
|
|
|
|
host, tlsEnable, err := apiClient.ParseURI(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("parse address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
creds := insecure.NewCredentials()
|
|
|
|
if tlsEnable {
|
|
|
|
creds = credentials.NewTLS(&tls.Config{})
|
|
|
|
}
|
|
|
|
|
|
|
|
options := []grpc.DialOption{grpc.WithTransportCredentials(creds)}
|
|
|
|
|
|
|
|
// the order is matter, we want client to be able to overwrite options.
|
|
|
|
opts := append(options, clientOptions...)
|
|
|
|
|
|
|
|
conn, err := grpc.DialContext(ctx, host, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("grpc dial node tree service: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, grpcService.NewTreeServiceClient(conn), nil
|
|
|
|
}
|
|
|
|
|
2023-06-06 06:29:23 +00:00
|
|
|
func (c *treeClient) serviceClient() (grpcService.TreeServiceClient, error) {
|
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
|
|
|
|
if c.conn == 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
|
|
|
}
|
|
|
|
|
|
|
|
return c.service, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
if c.conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|