[#1231] Update new SDK Client interface

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2022-03-11 18:24:11 +03:00 committed by Alex Vanin
parent 697c12a5e9
commit b6720d5f97
14 changed files with 156 additions and 79 deletions

View file

@ -6,6 +6,7 @@ import (
"fmt"
"github.com/mr-tron/base58"
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/pkg/services/control"
ircontrol "github.com/nspcc-dev/neofs-node/pkg/services/control/ir"
ircontrolsrv "github.com/nspcc-dev/neofs-node/pkg/services/control/ir/server"
@ -223,7 +224,11 @@ func healthCheck(cmd *cobra.Command, _ []string) {
err = controlSvc.SignMessage(key, req)
exitOnErr(cmd, errf("could not sign message: %w", err))
resp, err := control.HealthCheck(cli.Raw(), req)
var resp *control.HealthCheckResponse
err = cli.ExecRaw(func(client *rawclient.Client) error {
resp, err = control.HealthCheck(client, req)
return err
})
exitOnErr(cmd, errf("rpc error: %w", err))
sign := resp.GetSignature()
@ -248,7 +253,11 @@ func healthCheckIR(cmd *cobra.Command, key *ecdsa.PrivateKey, c *client.Client)
err := ircontrolsrv.SignMessage(key, req)
exitOnErr(cmd, errf("could not sign request: %w", err))
resp, err := ircontrol.HealthCheck(c.Raw(), req)
var resp *ircontrol.HealthCheckResponse
err = c.ExecRaw(func(client *rawclient.Client) error {
resp, err = ircontrol.HealthCheck(client, req)
return err
})
exitOnErr(cmd, errf("rpc error: %w", err))
sign := resp.GetSignature()
@ -294,7 +303,11 @@ func setNetmapStatus(cmd *cobra.Command, _ []string) {
cli, err := getControlSDKClient(key)
exitOnErr(cmd, err)
resp, err := control.SetNetmapStatus(cli.Raw(), req)
var resp *control.SetNetmapStatusResponse
err = cli.ExecRaw(func(client *rawclient.Client) error {
resp, err = control.SetNetmapStatus(client, req)
return err
})
exitOnErr(cmd, errf("rpc error: %w", err))
sign := resp.GetSignature()
@ -351,7 +364,11 @@ var dropObjectsCmd = &cobra.Command{
cli, err := getControlSDKClient(key)
exitOnErr(cmd, err)
resp, err := control.DropObjects(cli.Raw(), req)
var resp *control.DropObjectsResponse
err = cli.ExecRaw(func(client *rawclient.Client) error {
resp, err = control.DropObjects(client, req)
return err
})
exitOnErr(cmd, errf("rpc error: %w", err))
sign := resp.GetSignature()
@ -385,7 +402,11 @@ var snapshotCmd = &cobra.Command{
cli, err := getControlSDKClient(key)
exitOnErr(cmd, err)
resp, err := control.NetmapSnapshot(cli.Raw(), req)
var resp *control.NetmapSnapshotResponse
err = cli.ExecRaw(func(client *rawclient.Client) error {
resp, err = control.NetmapSnapshot(client, req)
return err
})
exitOnErr(cmd, errf("rpc error: %w", err))
sign := resp.GetSignature()
@ -415,7 +436,11 @@ func listShards(cmd *cobra.Command, _ []string) {
cli, err := getControlSDKClient(key)
exitOnErr(cmd, err)
resp, err := control.ListShards(cli.Raw(), req)
var resp *control.ListShardsResponse
err = cli.ExecRaw(func(client *rawclient.Client) error {
resp, err = control.ListShards(client, req)
return err
})
exitOnErr(cmd, errf("rpc error: %w", err))
sign := resp.GetSignature()
@ -433,26 +458,27 @@ func listShards(cmd *cobra.Command, _ []string) {
// getControlSDKClient is the same getSDKClient but with
// another RPC endpoint flag.
func getControlSDKClient(key *ecdsa.PrivateKey) (*client.Client, error) {
var (
c client.Client
prmInit client.PrmInit
prmDial client.PrmDial
)
netAddr, err := getEndpointAddress(controlRPC)
if err != nil {
return nil, err
}
options := []client.Option{
client.WithAddress(netAddr.HostAddr()),
client.WithDefaultPrivateKey(key),
}
prmInit.SetDefaultPrivateKey(*key)
prmDial.SetServerURI(netAddr.HostAddr())
if netAddr.TLSEnabled() {
options = append(options, client.WithTLSConfig(&tls.Config{}))
prmDial.SetTLSConfig(&tls.Config{})
}
c, err := client.New(options...)
if err != nil {
return nil, fmt.Errorf("coult not init api client:%w", err)
}
c.Init(prmInit)
return c, err
return &c, c.Dial(prmDial)
}
func prettyPrintShards(cmd *cobra.Command, ii []*control.ShardInfo) {
@ -522,7 +548,11 @@ func setShardMode(cmd *cobra.Command, _ []string) {
cli, err := getControlSDKClient(key)
exitOnErr(cmd, err)
resp, err := control.SetShardMode(cli.Raw(), req)
var resp *control.SetShardModeResponse
err = cli.ExecRaw(func(client *rawclient.Client) error {
resp, err = control.SetShardMode(client, req)
return err
})
exitOnErr(cmd, errf("rpc error: %w", err))
sign := resp.GetSignature()

View file

@ -2,6 +2,7 @@ package cmd
import (
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/pkg/services/control"
controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server"
"github.com/nspcc-dev/neofs-sdk-go/util/signature"
@ -45,7 +46,11 @@ func dumpShard(cmd *cobra.Command, _ []string) {
cli, err := getControlSDKClient(key)
exitOnErr(cmd, err)
resp, err := control.DumpShard(cli.Raw(), req)
var resp *control.DumpShardResponse
err = cli.ExecRaw(func(client *client.Client) error {
resp, err = control.DumpShard(client, req)
return err
})
exitOnErr(cmd, errf("rpc error: %w", err))
sign := resp.GetSignature()

View file

@ -2,6 +2,7 @@ package cmd
import (
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/pkg/services/control"
controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server"
"github.com/nspcc-dev/neofs-sdk-go/util/signature"
@ -45,7 +46,11 @@ func restoreShard(cmd *cobra.Command, _ []string) {
cli, err := getControlSDKClient(key)
exitOnErr(cmd, err)
resp, err := control.RestoreShard(cli.Raw(), req)
var resp *control.RestoreShardResponse
err = cli.ExecRaw(func(client *client.Client) error {
resp, err = control.RestoreShard(client, req)
return err
})
exitOnErr(cmd, errf("rpc error: %w", err))
sign := resp.GetSignature()

View file

@ -339,27 +339,32 @@ func prepareBearerPrm(cmd *cobra.Command, prm bearerPrm) {
// getSDKClient returns default neofs-api-go sdk client. Consider using
// opts... to provide TTL or other global configuration flags.
func getSDKClient(key *ecdsa.PrivateKey) (*client.Client, error) {
var (
c client.Client
prmInit client.PrmInit
prmDial client.PrmDial
)
netAddr, err := getEndpointAddress(rpc)
if err != nil {
return nil, err
}
options := []client.Option{
client.WithAddress(netAddr.HostAddr()),
client.WithDefaultPrivateKey(key),
client.WithNeoFSErrorParsing(),
}
prmInit.SetDefaultPrivateKey(*key)
prmInit.ResolveNeoFSFailures()
prmDial.SetServerURI(netAddr.HostAddr())
if netAddr.TLSEnabled() {
options = append(options, client.WithTLSConfig(&tls.Config{}))
prmDial.SetTLSConfig(&tls.Config{})
}
c, err := client.New(options...)
c.Init(prmInit)
err = c.Dial(prmDial)
if err != nil {
return nil, fmt.Errorf("coult not init api client:%w", err)
}
return c, err
return &c, err
}
func getTTL() uint32 {

View file

@ -43,7 +43,6 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/util"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
"github.com/nspcc-dev/neofs-node/pkg/util/state"
apiclient "github.com/nspcc-dev/neofs-sdk-go/client"
"github.com/nspcc-dev/neofs-sdk-go/netmap"
"github.com/nspcc-dev/neofs-sdk-go/owner"
"github.com/nspcc-dev/neofs-sdk-go/version"
@ -302,10 +301,10 @@ func initCfg(path string) *cfg {
scriptHash: contractsconfig.Reputation(appCfg),
workerPool: reputationWorkerPool,
},
clientCache: cache.NewSDKClientCache(
apiclient.WithDialTimeout(apiclientconfig.DialTimeout(appCfg)),
apiclient.WithDefaultPrivateKey(&key.PrivateKey),
),
clientCache: cache.NewSDKClientCache(cache.ClientCacheOpts{
DialTimeout: apiclientconfig.DialTimeout(appCfg),
Key: &key.PrivateKey,
}),
persistate: persistate,
ownerIDFromKey: ownerIDFromKey,

2
go.mod
View file

@ -18,7 +18,7 @@ require (
github.com/nspcc-dev/neo-go v0.98.0
github.com/nspcc-dev/neofs-api-go/v2 v2.12.0
github.com/nspcc-dev/neofs-contract v0.14.2
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.2
github.com/nspcc-dev/neofs-sdk-go v1.0.0-rc.2.0.20220314100449-68b0440c626f
github.com/nspcc-dev/tzhash v1.5.2
github.com/panjf2000/ants/v2 v2.4.0
github.com/paulmach/orb v0.2.2

BIN
go.sum

Binary file not shown.

View file

@ -2,7 +2,6 @@ package client
import (
"context"
"io"
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/pkg/network"
@ -13,7 +12,6 @@ import (
// node's client.
type Client interface {
ContainerAnnounceUsedSpace(context.Context, client.PrmAnnounceSpace) (*client.ResAnnounceSpace, error)
ObjectPutInit(context.Context, client.PrmObjectPutInit) (*client.ObjectWriter, error)
ObjectDelete(context.Context, client.PrmObjectDelete) (*client.ResObjectDelete, error)
ObjectGetInit(context.Context, client.PrmObjectGet) (*client.ObjectReader, error)
@ -21,13 +19,10 @@ type Client interface {
ObjectSearchInit(context.Context, client.PrmObjectSearch) (*client.ObjectListReader, error)
ObjectRangeInit(context.Context, client.PrmObjectRange) (*client.ObjectRangeReader, error)
ObjectHash(context.Context, client.PrmObjectHash) (*client.ResObjectHash, error)
AnnounceLocalTrust(context.Context, client.PrmAnnounceLocalTrust) (*client.ResAnnounceLocalTrust, error)
AnnounceIntermediateTrust(context.Context, client.PrmAnnounceIntermediateTrust) (*client.ResAnnounceIntermediateTrust, error)
Raw() *rawclient.Client
Conn() io.Closer
ExecRaw(f func(client *rawclient.Client) error) error
Close() error
}
// MultiAddressClient is an interface of the
@ -37,7 +32,7 @@ type MultiAddressClient interface {
// RawForAddress must return rawclient.Client
// for the passed network.Address.
RawForAddress(network.Address) *rawclient.Client
RawForAddress(network.Address, func(cli *rawclient.Client) error) error
}
// NodeInfo groups information about NeoFS storage node needed for Client construction.

View file

@ -44,7 +44,7 @@ type (
func newClientCache(p *clientCacheParams) *ClientCache {
return &ClientCache{
log: p.Log,
cache: cache.NewSDKClientCache(),
cache: cache.NewSDKClientCache(cache.ClientCacheOpts{}),
key: p.Key,
sgTimeout: p.SGTimeout,
headTimeout: p.HeadTimeout,

View file

@ -1,8 +1,10 @@
package cache
import (
"crypto/ecdsa"
"encoding/hex"
"sync"
"time"
clientcore "github.com/nspcc-dev/neofs-node/pkg/core/client"
"github.com/nspcc-dev/neofs-node/pkg/network"
@ -15,13 +17,19 @@ type (
ClientCache struct {
mu *sync.RWMutex
clients map[string]clientcore.Client
opts []client.Option
opts ClientCacheOpts
}
ClientCacheOpts struct {
DialTimeout time.Duration
Key *ecdsa.PrivateKey
ResponseCallback func(client.ResponseMetaInfo) error
}
)
// NewSDKClientCache creates instance of client cache.
// `opts` are used for new client creation.
func NewSDKClientCache(opts ...client.Option) *ClientCache {
func NewSDKClientCache(opts ClientCacheOpts) *ClientCache {
return &ClientCache{
mu: new(sync.RWMutex),
clients: make(map[string]clientcore.Client),
@ -62,9 +70,9 @@ func (c *ClientCache) Get(info clientcore.NodeInfo) (clientcore.Client, error) {
return cli, nil
}
cli := newMultiClient(netAddr, append(c.opts,
client.WithResponseInfoHandler(clientcore.AssertKeyResponseCallback(info.PublicKey())),
))
newClientOpts := c.opts
newClientOpts.ResponseCallback = clientcore.AssertKeyResponseCallback(info.PublicKey())
cli := newMultiClient(netAddr, newClientOpts)
c.clients[cacheKey] = cli
@ -79,10 +87,7 @@ func (c *ClientCache) CloseAll() {
{
for _, cl := range c.clients {
con := cl.Conn()
if con != nil {
_ = con.Close()
}
_ = cl.Close()
}
}

View file

@ -4,7 +4,6 @@ import (
"context"
"crypto/tls"
"errors"
"io"
"sync"
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
@ -20,10 +19,10 @@ type multiClient struct {
addr network.AddressGroup
opts []client.Option
opts ClientCacheOpts
}
func newMultiClient(addr network.AddressGroup, opts []client.Option) *multiClient {
func newMultiClient(addr network.AddressGroup, opts ClientCacheOpts) *multiClient {
return &multiClient{
clients: make(map[string]clientcore.Client),
addr: addr,
@ -33,21 +32,39 @@ func newMultiClient(addr network.AddressGroup, opts []client.Option) *multiClien
// note: must be wrapped into mutex lock.
func (x *multiClient) createForAddress(addr network.Address) clientcore.Client {
opts := append(x.opts, client.WithAddress(addr.HostAddr()))
var (
c client.Client
prmInit client.PrmInit
prmDial client.PrmDial
)
prmDial.SetServerURI(addr.HostAddr())
if addr.TLSEnabled() {
opts = append(opts, client.WithTLSConfig(&tls.Config{}))
prmDial.SetTLSConfig(&tls.Config{})
}
c, err := client.New(opts...)
if x.opts.Key != nil {
prmInit.SetDefaultPrivateKey(*x.opts.Key)
}
if x.opts.DialTimeout > 0 {
prmDial.SetTimeout(x.opts.DialTimeout)
}
if x.opts.ResponseCallback != nil {
prmInit.SetResponseInfoCallback(x.opts.ResponseCallback)
}
c.Init(prmInit)
err := c.Dial(prmDial)
if err != nil {
// client never returns an error
panic(err)
}
x.clients[addr.String()] = c
x.clients[addr.String()] = &c
return c
return &c
}
func (x *multiClient) iterateClients(ctx context.Context, f func(clientcore.Client) error) error {
@ -169,12 +186,8 @@ func (x *multiClient) AnnounceIntermediateTrust(ctx context.Context, prm client.
return
}
func (x *multiClient) Raw() *rawclient.Client {
panic("multiClient.Raw() must not be called")
}
func (x *multiClient) Conn() io.Closer {
return x
func (x *multiClient) ExecRaw(f func(client *rawclient.Client) error) error {
panic("multiClient.ExecRaw() must not be called")
}
func (x *multiClient) Close() error {
@ -182,7 +195,7 @@ func (x *multiClient) Close() error {
{
for _, c := range x.clients {
_ = c.Conn().Close()
_ = c.Close()
}
}
@ -191,8 +204,8 @@ func (x *multiClient) Close() error {
return nil
}
func (x *multiClient) RawForAddress(addr network.Address) *rawclient.Client {
return x.client(addr).Raw()
func (x *multiClient) RawForAddress(addr network.Address, f func(client *rawclient.Client) error) error {
return x.client(addr).ExecRaw(f)
}
func (x *multiClient) client(addr network.Address) clientcore.Client {

View file

@ -77,7 +77,11 @@ func (s *Service) toPrm(req *objectV2.GetRequest, stream objectSvc.GetObjectStre
// perhaps it is worth highlighting the utility function in neofs-api-go
// open stream
stream, err := rpc.GetObject(c.RawForAddress(addr), req, rpcclient.WithContext(stream.Context()))
var getStream *rpc.GetResponseReader
err = c.RawForAddress(addr, func(cli *rpcclient.Client) error {
getStream, err = rpc.GetObject(cli, req, rpcclient.WithContext(stream.Context()))
return err
})
if err != nil {
return nil, fmt.Errorf("stream opening failed: %w", err)
}
@ -91,7 +95,7 @@ func (s *Service) toPrm(req *objectV2.GetRequest, stream objectSvc.GetObjectStre
for {
// receive message from server stream
err := stream.Read(resp)
err := getStream.Read(resp)
if err != nil {
if errors.Is(err, io.EOF) {
if !headWas {
@ -202,7 +206,11 @@ func (s *Service) toRangePrm(req *objectV2.GetRangeRequest, stream objectSvc.Get
// perhaps it is worth highlighting the utility function in neofs-api-go
// open stream
stream, err := rpc.GetObjectRange(c.RawForAddress(addr), req, rpcclient.WithContext(stream.Context()))
var rangeStream *rpc.ObjectRangeResponseReader
err = c.RawForAddress(addr, func(cli *rpcclient.Client) error {
rangeStream, err = rpc.GetObjectRange(cli, req, rpcclient.WithContext(stream.Context()))
return err
})
if err != nil {
return nil, fmt.Errorf("could not create Get payload range stream: %w", err)
}
@ -213,7 +221,7 @@ func (s *Service) toRangePrm(req *objectV2.GetRangeRequest, stream objectSvc.Get
for {
// receive message from server stream
err := stream.Read(resp)
err := rangeStream.Read(resp)
if err != nil {
if errors.Is(err, io.EOF) {
break
@ -362,18 +370,22 @@ func (s *Service) toHeadPrm(ctx context.Context, req *objectV2.HeadRequest, resp
// perhaps it is worth highlighting the utility function in neofs-api-go
// send Head request
resp, err := rpc.HeadObject(c.RawForAddress(addr), req, rpcclient.WithContext(ctx))
var headResp *objectV2.HeadResponse
err = c.RawForAddress(addr, func(cli *rpcclient.Client) error {
headResp, err = rpc.HeadObject(cli, req, rpcclient.WithContext(ctx))
return err
})
if err != nil {
return nil, fmt.Errorf("sending the request failed: %w", err)
}
// verify response key
if err = internal.VerifyResponseKeyV2(pubkey, resp); err != nil {
if err = internal.VerifyResponseKeyV2(pubkey, headResp); err != nil {
return nil, err
}
// verify response structure
if err := signature.VerifyServiceMessage(resp); err != nil {
if err := signature.VerifyServiceMessage(headResp); err != nil {
return nil, fmt.Errorf("response verification failed: %w", err)
}
@ -382,7 +394,7 @@ func (s *Service) toHeadPrm(ctx context.Context, req *objectV2.HeadRequest, resp
idSig *refs.Signature
)
switch v := resp.GetBody().GetHeaderPart().(type) {
switch v := headResp.GetBody().GetHeaderPart().(type) {
case nil:
return nil, fmt.Errorf("unexpected header type %T", v)
case *objectV2.ShortHeader:

View file

@ -5,6 +5,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/object"
"github.com/nspcc-dev/neofs-api-go/v2/rpc"
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
sessionV2 "github.com/nspcc-dev/neofs-api-go/v2/session"
"github.com/nspcc-dev/neofs-api-go/v2/signature"
"github.com/nspcc-dev/neofs-node/pkg/core/client"
@ -140,7 +141,10 @@ func (s *streamer) relayRequest(info client.NodeInfo, c client.MultiAddressClien
var stream *rpc.PutRequestWriter
stream, err = rpc.PutObject(c.RawForAddress(addr), resp)
err = c.RawForAddress(addr, func(cli *rawclient.Client) error {
stream, err = rpc.PutObject(cli, resp)
return err
})
if err != nil {
err = fmt.Errorf("stream opening failed: %w", err)
return

View file

@ -65,7 +65,11 @@ func (s *Service) toPrm(req *objectV2.SearchRequest, stream objectSvc.SearchStre
return nil, err
}
stream, err := rpc.SearchObjects(c.RawForAddress(addr), req, rpcclient.WithContext(stream.Context()))
var searchStream *rpc.SearchResponseReader
err = c.RawForAddress(addr, func(cli *rpcclient.Client) error {
searchStream, err = rpc.SearchObjects(cli, req, rpcclient.WithContext(stream.Context()))
return err
})
if err != nil {
return nil, err
}
@ -79,7 +83,7 @@ func (s *Service) toPrm(req *objectV2.SearchRequest, stream objectSvc.SearchStre
for {
// receive message from server stream
err := stream.Read(resp)
err := searchStream.Read(resp)
if err != nil {
if errors.Is(err, io.EOF) {
break