[#428] client: Hide client cache behind interface in dependent packages

Replace usage of `cache.ClientCache` type with interface with similar
signature. This will further allow overloading clients without affecting the
logic of dependent packages.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-03-23 21:40:36 +03:00 committed by Leonard Lyubich
parent f25253738a
commit 106884fc40
13 changed files with 65 additions and 49 deletions

View file

@ -195,7 +195,9 @@ type remoteLoadAnnounceProvider struct {
loadAddrSrc network.LocalAddressSource loadAddrSrc network.LocalAddressSource
clientCache *cache.ClientCache clientCache interface {
Get(string) (apiClient.Client, error)
}
deadEndProvider loadcontroller.WriterProvider deadEndProvider loadcontroller.WriterProvider
} }

View file

@ -230,7 +230,7 @@ func initObjectService(c *cfg) {
sPut := putsvc.NewService( sPut := putsvc.NewService(
putsvc.WithKeyStorage(keyStorage), putsvc.WithKeyStorage(keyStorage),
putsvc.WithClientCache(clientCache), putsvc.WithClientConstructor(clientCache),
putsvc.WithMaxSizeSource(c), putsvc.WithMaxSizeSource(c),
putsvc.WithLocalStorage(ls), putsvc.WithLocalStorage(ls),
putsvc.WithContainerSource(c.cfgObject.cnrStorage), putsvc.WithContainerSource(c.cfgObject.cnrStorage),
@ -251,7 +251,7 @@ func initObjectService(c *cfg) {
sSearch := searchsvc.New( sSearch := searchsvc.New(
searchsvc.WithLogger(c.log), searchsvc.WithLogger(c.log),
searchsvc.WithLocalStorageEngine(ls), searchsvc.WithLocalStorageEngine(ls),
searchsvc.WithClientCache(clientCache), searchsvc.WithClientConstructor(clientCache),
searchsvc.WithTraverserGenerator( searchsvc.WithTraverserGenerator(
traverseGen.WithTraverseOptions( traverseGen.WithTraverseOptions(
placement.WithoutSuccessTracking(), placement.WithoutSuccessTracking(),
@ -268,7 +268,7 @@ func initObjectService(c *cfg) {
sGet := getsvc.New( sGet := getsvc.New(
getsvc.WithLogger(c.log), getsvc.WithLogger(c.log),
getsvc.WithLocalStorageEngine(ls), getsvc.WithLocalStorageEngine(ls),
getsvc.WithClientCache(clientCache), getsvc.WithClientConstructor(clientCache),
getsvc.WithTraverserGenerator( getsvc.WithTraverserGenerator(
traverseGen.WithTraverseOptions( traverseGen.WithTraverseOptions(
placement.SuccessAfter(1), placement.SuccessAfter(1),

View file

@ -21,8 +21,10 @@ import (
type ( type (
ClientCache struct { ClientCache struct {
log *zap.Logger log *zap.Logger
cache *cache.ClientCache cache interface {
key *ecdsa.PrivateKey Get(string) (client.Client, error)
}
key *ecdsa.PrivateKey
sgTimeout, headTimeout, rangeTimeout time.Duration sgTimeout, headTimeout, rangeTimeout time.Duration
} }

View file

@ -1,11 +1,11 @@
package getsvc package getsvc
import ( import (
"github.com/nspcc-dev/neofs-api-go/pkg/client"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-node/pkg/core/netmap" "github.com/nspcc-dev/neofs-node/pkg/core/netmap"
"github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/core/object"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
"github.com/nspcc-dev/neofs-node/pkg/services/object/util" "github.com/nspcc-dev/neofs-node/pkg/services/object/util"
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement" "github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
"github.com/nspcc-dev/neofs-node/pkg/util/logger" "github.com/nspcc-dev/neofs-node/pkg/util/logger"
@ -91,8 +91,12 @@ func WithLocalStorageEngine(e *engine.StorageEngine) Option {
} }
} }
// WithClientCache returns option to set cache of remote node clients. type ClientConstructor interface {
func WithClientCache(v *cache.ClientCache) Option { Get(string) (client.Client, error)
}
// WithClientConstructor returns option to set constructor of remote node clients.
func WithClientConstructor(v ClientConstructor) Option {
return func(c *cfg) { return func(c *cfg) {
c.clientCache.(*clientCacheWrapper).cache = v c.clientCache.(*clientCacheWrapper).cache = v
} }

View file

@ -8,7 +8,6 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/core/netmap" "github.com/nspcc-dev/neofs-node/pkg/core/netmap"
"github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/core/object"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
) )
type SimpleObjectWriter struct { type SimpleObjectWriter struct {
@ -18,7 +17,7 @@ type SimpleObjectWriter struct {
} }
type clientCacheWrapper struct { type clientCacheWrapper struct {
cache *cache.ClientCache cache ClientConstructor
} }
type clientWrapper struct { type clientWrapper struct {

View file

@ -7,17 +7,20 @@ import (
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/core/object"
"github.com/nspcc-dev/neofs-node/pkg/network" "github.com/nspcc-dev/neofs-node/pkg/network"
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
"github.com/nspcc-dev/neofs-node/pkg/services/object/util" "github.com/nspcc-dev/neofs-node/pkg/services/object/util"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
type ClientConstructor interface {
Get(string) (client.Client, error)
}
// RemoteHeader represents utility for getting // RemoteHeader represents utility for getting
// the object header from a remote host. // the object header from a remote host.
type RemoteHeader struct { type RemoteHeader struct {
keyStorage *util.KeyStorage keyStorage *util.KeyStorage
clientCache *cache.ClientCache clientCache ClientConstructor
} }
// RemoteHeadPrm groups remote header operation parameters. // RemoteHeadPrm groups remote header operation parameters.
@ -30,7 +33,7 @@ type RemoteHeadPrm struct {
var ErrNotFound = errors.New("object header not found") var ErrNotFound = errors.New("object header not found")
// NewRemoteHeader creates, initializes and returns new RemoteHeader instance. // NewRemoteHeader creates, initializes and returns new RemoteHeader instance.
func NewRemoteHeader(keyStorage *util.KeyStorage, cache *cache.ClientCache) *RemoteHeader { func NewRemoteHeader(keyStorage *util.KeyStorage, cache ClientConstructor) *RemoteHeader {
return &RemoteHeader{ return &RemoteHeader{
keyStorage: keyStorage, keyStorage: keyStorage,
clientCache: cache, clientCache: cache,

View file

@ -6,7 +6,6 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg/client" "github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/core/object"
"github.com/nspcc-dev/neofs-node/pkg/network" "github.com/nspcc-dev/neofs-node/pkg/network"
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
"github.com/nspcc-dev/neofs-node/pkg/services/object/util" "github.com/nspcc-dev/neofs-node/pkg/services/object/util"
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/transformer" "github.com/nspcc-dev/neofs-node/pkg/services/object_manager/transformer"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -25,7 +24,7 @@ type remoteTarget struct {
obj *object.Object obj *object.Object
clientCache *cache.ClientCache clientConstructor ClientConstructor
} }
// RemoteSender represents utility for // RemoteSender represents utility for
@ -33,7 +32,7 @@ type remoteTarget struct {
type RemoteSender struct { type RemoteSender struct {
keyStorage *util.KeyStorage keyStorage *util.KeyStorage
clientCache *cache.ClientCache clientConstructor ClientConstructor
} }
// RemotePutPrm groups remote put operation parameters. // RemotePutPrm groups remote put operation parameters.
@ -60,7 +59,7 @@ func (t *remoteTarget) Close() (*transformer.AccessIdentifiers, error) {
return nil, err return nil, err
} }
c, err := t.clientCache.Get(addr) c, err := t.clientConstructor.Get(addr)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "(%T) could not create SDK client %s", t, addr) return nil, errors.Wrapf(err, "(%T) could not create SDK client %s", t, addr)
} }
@ -84,10 +83,10 @@ func (t *remoteTarget) Close() (*transformer.AccessIdentifiers, error) {
} }
// NewRemoteSender creates, initializes and returns new RemoteSender instance. // NewRemoteSender creates, initializes and returns new RemoteSender instance.
func NewRemoteSender(keyStorage *util.KeyStorage, cache *cache.ClientCache) *RemoteSender { func NewRemoteSender(keyStorage *util.KeyStorage, cons ClientConstructor) *RemoteSender {
return &RemoteSender{ return &RemoteSender{
keyStorage: keyStorage, keyStorage: keyStorage,
clientCache: cache, clientConstructor: cons,
} }
} }
@ -112,10 +111,10 @@ func (p *RemotePutPrm) WithObject(v *object.Object) *RemotePutPrm {
// PutObject sends object to remote node. // PutObject sends object to remote node.
func (s *RemoteSender) PutObject(ctx context.Context, p *RemotePutPrm) error { func (s *RemoteSender) PutObject(ctx context.Context, p *RemotePutPrm) error {
t := &remoteTarget{ t := &remoteTarget{
ctx: ctx, ctx: ctx,
keyStorage: s.keyStorage, keyStorage: s.keyStorage,
addr: p.node, addr: p.node,
clientCache: s.clientCache, clientConstructor: s.clientConstructor,
} }
if err := t.WriteHeader(object.NewRawFromObject(p.obj)); err != nil { if err := t.WriteHeader(object.NewRawFromObject(p.obj)); err != nil {

View file

@ -3,12 +3,12 @@ package putsvc
import ( import (
"context" "context"
"github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-node/pkg/core/container" "github.com/nspcc-dev/neofs-node/pkg/core/container"
"github.com/nspcc-dev/neofs-node/pkg/core/netmap" "github.com/nspcc-dev/neofs-node/pkg/core/netmap"
"github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/core/object"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
"github.com/nspcc-dev/neofs-node/pkg/network" "github.com/nspcc-dev/neofs-node/pkg/network"
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
objutil "github.com/nspcc-dev/neofs-node/pkg/services/object/util" objutil "github.com/nspcc-dev/neofs-node/pkg/services/object/util"
"github.com/nspcc-dev/neofs-node/pkg/util" "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/logger"
@ -29,6 +29,10 @@ type Service struct {
type Option func(*cfg) type Option func(*cfg)
type ClientConstructor interface {
Get(string) (client.Client, error)
}
type cfg struct { type cfg struct {
keyStorage *objutil.KeyStorage keyStorage *objutil.KeyStorage
@ -50,7 +54,7 @@ type cfg struct {
networkState netmap.State networkState netmap.State
clientCache *cache.ClientCache clientConstructor ClientConstructor
log *logger.Logger log *logger.Logger
} }
@ -138,9 +142,9 @@ func WithNetworkState(v netmap.State) Option {
} }
} }
func WithClientCache(v *cache.ClientCache) Option { func WithClientConstructor(v ClientConstructor) Option {
return func(c *cfg) { return func(c *cfg) {
c.clientCache = v c.clientConstructor = v
} }
} }

View file

@ -139,11 +139,11 @@ func (p *Streamer) newCommonTarget(prm *PutInitPrm) transformer.ObjectTarget {
} }
return &remoteTarget{ return &remoteTarget{
ctx: p.ctx, ctx: p.ctx,
keyStorage: p.keyStorage, keyStorage: p.keyStorage,
commonPrm: prm.common, commonPrm: prm.common,
addr: addr, addr: addr,
clientCache: p.clientCache, clientConstructor: p.clientConstructor,
} }
}, },
fmt: p.fmtValidator, fmt: p.fmtValidator,

View file

@ -147,7 +147,7 @@ func (exec execCtx) remoteClient(node *network.Address) (searchClient, bool) {
log.Debug("could not calculate node IP address") log.Debug("could not calculate node IP address")
case err == nil: case err == nil:
c, err := exec.svc.clientCache.get(ipAddr) c, err := exec.svc.clientConstructor.get(ipAddr)
switch { switch {
default: default:

View file

@ -257,7 +257,7 @@ func TestGetRemoteSmall(t *testing.T) {
curEpoch: b, curEpoch: b,
}, },
} }
svc.clientCache = c svc.clientConstructor = c
svc.currentEpochReceiver = testEpochReceiver(curEpoch) svc.currentEpochReceiver = testEpochReceiver(curEpoch)
return svc return svc
@ -376,7 +376,7 @@ func TestGetFromPastEpoch(t *testing.T) {
}, },
} }
svc.clientCache = &testClientCache{ svc.clientConstructor = &testClientCache{
clients: map[string]*testStorage{ clients: map[string]*testStorage{
as[0][0]: c11, as[0][0]: c11,
as[0][1]: c12, as[0][1]: c12,

View file

@ -1,11 +1,11 @@
package searchsvc package searchsvc
import ( import (
"github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-api-go/pkg/container" "github.com/nspcc-dev/neofs-api-go/pkg/container"
"github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-node/pkg/core/netmap" "github.com/nspcc-dev/neofs-node/pkg/core/netmap"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
"github.com/nspcc-dev/neofs-node/pkg/services/object/util" "github.com/nspcc-dev/neofs-node/pkg/services/object/util"
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement" "github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
"github.com/nspcc-dev/neofs-node/pkg/util/logger" "github.com/nspcc-dev/neofs-node/pkg/util/logger"
@ -25,6 +25,10 @@ type searchClient interface {
searchObjects(*execCtx) ([]*object.ID, error) searchObjects(*execCtx) ([]*object.ID, error)
} }
type ClientConstructor interface {
Get(string) (client.Client, error)
}
type cfg struct { type cfg struct {
log *logger.Logger log *logger.Logger
@ -32,7 +36,7 @@ type cfg struct {
search(*execCtx) ([]*object.ID, error) search(*execCtx) ([]*object.ID, error)
} }
clientCache interface { clientConstructor interface {
get(string) (searchClient, error) get(string) (searchClient, error)
} }
@ -47,8 +51,8 @@ type cfg struct {
func defaultCfg() *cfg { func defaultCfg() *cfg {
return &cfg{ return &cfg{
log: zap.L(), log: zap.L(),
clientCache: new(clientCacheWrapper), clientConstructor: new(clientConstructorWrapper),
} }
} }
@ -81,10 +85,10 @@ func WithLocalStorageEngine(e *engine.StorageEngine) Option {
} }
} }
// WithClientCache returns option to set cache of remote node clients. // WithClientConstructor returns option to set constructor of remote node clients.
func WithClientCache(v *cache.ClientCache) Option { func WithClientConstructor(v ClientConstructor) Option {
return func(c *cfg) { return func(c *cfg) {
c.clientCache.(*clientCacheWrapper).cache = v c.clientConstructor.(*clientConstructorWrapper).constructor = v
} }
} }

View file

@ -8,7 +8,6 @@ import (
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-node/pkg/core/netmap" "github.com/nspcc-dev/neofs-node/pkg/core/netmap"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
"github.com/nspcc-dev/neofs-node/pkg/services/object/util" "github.com/nspcc-dev/neofs-node/pkg/services/object/util"
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement" "github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
) )
@ -21,8 +20,8 @@ type uniqueIDWriter struct {
writer IDListWriter writer IDListWriter
} }
type clientCacheWrapper struct { type clientConstructorWrapper struct {
cache *cache.ClientCache constructor ClientConstructor
} }
type clientWrapper struct { type clientWrapper struct {
@ -68,8 +67,8 @@ func (w *uniqueIDWriter) WriteIDs(list []*objectSDK.ID) error {
return w.writer.WriteIDs(list) return w.writer.WriteIDs(list)
} }
func (c *clientCacheWrapper) get(addr string) (searchClient, error) { func (c *clientConstructorWrapper) get(addr string) (searchClient, error) {
clt, err := c.cache.Get(addr) clt, err := c.constructor.Get(addr)
return &clientWrapper{ return &clientWrapper{
client: clt, client: clt,