[#131] client: refactor netmap dialer
All checks were successful
DCO / DCO (pull_request) Successful in 1m8s
Tests and linters / Tests (1.21) (pull_request) Successful in 1m21s
Tests and linters / Tests (1.20) (pull_request) Successful in 1m31s
Tests and linters / Lint (pull_request) Successful in 2m51s

Signed-off-by: olefirenque <egor.olefirenko892@gmail.com>
This commit is contained in:
Egor Olefirenko 2023-11-30 14:28:44 +03:00
parent dd8fb59efe
commit cf4677ecb9
2 changed files with 20 additions and 27 deletions

View file

@ -1,18 +1,19 @@
package client
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/tls"
"errors"
"fmt"
netmap "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap/grpc"
"net/url"
"time"
v2accounting "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/accounting"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
"google.golang.org/grpc"
)
@ -113,29 +114,23 @@ func (c *Client) Dial(ctx context.Context, prm PrmDial) error {
return nil
}
func (c *Client) netMapDialNode(ctx context.Context, node *netmap.NodeInfo) error {
// TODO: implement dialing options
addresses := node.ExternalAddresses()
return c.Dial(ctx, PrmDial{Endpoint: addresses[0]})
}
func (c *Client) NetMapDial(ctx context.Context, endpoint string) error {
u, err := url.Parse(endpoint)
if err != nil {
return err
}
isEqualSlices := func(a, b []byte) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
if u.Scheme == "frostfs" {
nodes := c.prm.NetMap.GetNodes()
nodes := c.prm.NetMap.Nodes()
for _, node := range nodes {
if isEqualSlices([]byte(u.Host), node.PublicKey) {
return c.Dial(ctx, PrmDial{Endpoint: node.Addresses[0]})
if bytes.Equal([]byte(u.Host), node.PublicKey()) {
return c.netMapDialNode(ctx, &node)
}
}
}
@ -173,7 +168,7 @@ type PrmInit struct {
ResponseInfoCallback func(ResponseMetaInfo) error
NetMap *netmap.Netmap
NetMap *netmap.NetMap
NetMagic uint64
}

View file

@ -6,10 +6,10 @@ import (
"crypto/elliptic"
"crypto/rand"
"fmt"
netmap "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap/grpc"
"testing"
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
"github.com/stretchr/testify/require"
)
@ -74,15 +74,13 @@ func TestClient_NetMapDialContext(t *testing.T) {
publicKey := "foo"
endpoint := "localhost:8080"
prmInit.NetMap = &netmap.Netmap{
Epoch: 0,
Nodes: []*netmap.NodeInfo{
{
PublicKey: []byte(publicKey),
Addresses: []string{endpoint},
},
},
}
node := netmap.NodeInfo{}
node.SetPublicKey([]byte(publicKey))
node.SetExternalAddresses(endpoint)
netMap := &netmap.NetMap{}
netMap.SetNodes([]netmap.NodeInfo{node})
prmInit.NetMap = netMap
c.Init(prmInit)