From ac8441b7187ab87a5f6ad8506d10887d346187cd Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Sat, 17 Apr 2021 20:13:29 +0300 Subject: [PATCH] [#482] reputation/router: Make route pkg independent Make route package independent from controller package. Add common interfaces to `./common` directory. Signed-off-by: Pavel Karpy --- cmd/neofs-node/reputation.go | 21 +++---- pkg/services/reputation/common/deps.go | 55 +++++++++++++++++++ .../reputation/local/controller/calls.go | 3 +- .../reputation/local/controller/controller.go | 4 +- .../reputation/local/controller/deps.go | 53 +----------------- .../reputation/local/controller/util.go | 10 ++-- pkg/services/reputation/local/route/calls.go | 12 ++-- pkg/services/reputation/local/route/deps.go | 4 +- 8 files changed, 87 insertions(+), 75 deletions(-) create mode 100644 pkg/services/reputation/common/deps.go diff --git a/cmd/neofs-node/reputation.go b/cmd/neofs-node/reputation.go index 25094e501..128a584a9 100644 --- a/cmd/neofs-node/reputation.go +++ b/cmd/neofs-node/reputation.go @@ -20,6 +20,7 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/network/cache" grpcreputation "github.com/nspcc-dev/neofs-node/pkg/network/transport/reputation/grpc" "github.com/nspcc-dev/neofs-node/pkg/services/reputation" + reputationcommon "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common" trustcontroller "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/controller" reputationroute "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/route" "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/route/managers" @@ -41,7 +42,7 @@ type localTrustStorage struct { } type localTrustIterator struct { - ctx trustcontroller.Context + ctx reputationcommon.Context storage *localTrustStorage @@ -55,7 +56,7 @@ type managerBuilder struct { type remoteLocalTrustProvider struct { localAddrSrc network.LocalAddressSource - deadEndProvider trustcontroller.WriterProvider + deadEndProvider reputationcommon.WriterProvider key *ecdsa.PrivateKey clientCache interface { @@ -74,7 +75,7 @@ func (nopReputationWriter) Close() error { } type remoteLocalTrustWriter struct { - ctx trustcontroller.Context + ctx reputationcommon.Context client apiClient.Client key *ecdsa.PrivateKey @@ -116,7 +117,7 @@ type remoteLocalTrustWriterProvider struct { } type localTrustLogger struct { - ctx trustcontroller.Context + ctx reputationcommon.Context log *logger.Logger } @@ -135,7 +136,7 @@ func (*localTrustLogger) Close() error { return nil } -func (rtwp *remoteLocalTrustWriterProvider) InitWriter(ctx trustcontroller.Context) (trustcontroller.Writer, error) { +func (rtwp *remoteLocalTrustWriterProvider) InitWriter(ctx reputationcommon.Context) (reputationcommon.Writer, error) { return &remoteLocalTrustWriter{ ctx: ctx, client: rtwp.client, @@ -143,7 +144,7 @@ func (rtwp *remoteLocalTrustWriterProvider) InitWriter(ctx trustcontroller.Conte }, nil } -func (rtp *remoteLocalTrustProvider) InitRemote(srv reputationroute.ServerInfo) (trustcontroller.WriterProvider, error) { +func (rtp *remoteLocalTrustProvider) InitRemote(srv reputationroute.ServerInfo) (reputationcommon.WriterProvider, error) { if srv == nil { return rtp.deadEndProvider, nil } @@ -201,7 +202,7 @@ func (mb *managerBuilder) BuildManagers(epoch uint64, p reputation.PeerID) ([]re return nil, nil } -func (s *localTrustStorage) InitIterator(ctx trustcontroller.Context) (trustcontroller.Iterator, error) { +func (s *localTrustStorage) InitIterator(ctx reputationcommon.Context) (trustcontroller.Iterator, error) { epochStorage, err := s.storage.DataForEpoch(ctx.Epoch()) if err != nil && !errors.Is(err, truststorage.ErrNoPositiveTrust) { return nil, err @@ -214,7 +215,7 @@ func (s *localTrustStorage) InitIterator(ctx trustcontroller.Context) (trustcont }, nil } -func (s *localTrustStorage) InitWriter(ctx trustcontroller.Context) (trustcontroller.Writer, error) { +func (s *localTrustStorage) InitWriter(ctx reputationcommon.Context) (reputationcommon.Writer, error) { return &localTrustLogger{ ctx: ctx, log: s.log, @@ -362,7 +363,7 @@ func initReputationService(c *cfg) { type reputationServer struct { *cfg log *logger.Logger - router trustcontroller.WriterProvider + router reputationcommon.WriterProvider routeBuilder reputationroute.Builder } @@ -447,7 +448,7 @@ func apiToLocalTrust(t *v2reputation.Trust, trustingPeer []byte) reputation.Trus } func (s *reputationServer) processTrust(epoch uint64, t reputation.Trust, - passedRoute []reputationroute.ServerInfo, w trustcontroller.Writer) error { + passedRoute []reputationroute.ServerInfo, w reputationcommon.Writer) error { err := reputationroute.CheckRoute(s.routeBuilder, epoch, t, passedRoute) if err != nil { return errors.Wrap(err, "wrong route of reputation trust value") diff --git a/pkg/services/reputation/common/deps.go b/pkg/services/reputation/common/deps.go new file mode 100644 index 000000000..9a1bcfecc --- /dev/null +++ b/pkg/services/reputation/common/deps.go @@ -0,0 +1,55 @@ +package common + +import ( + "context" + "io" + + "github.com/nspcc-dev/neofs-node/pkg/services/reputation" +) + +// Context wraps stdlib context +// with accompanying meta values. +type Context interface { + context.Context + + // Must return epoch number to select the values. + Epoch() uint64 +} + +// Writer describes the interface for storing reputation.Trust values. +// +// This interface is provided by both local storage +// of values and remote (wrappers over the RPC). +type Writer interface { + // Write performs a write operation of reputation.Trust value + // and returns any error encountered. + // + // All values after the Close call must be flushed to the + // physical target. Implementations can cache values before + // Close operation. + // + // Write must not be called after Close. + Write(reputation.Trust) error + + // Close exits with method-providing Writer. + // + // All cached values must be flushed before + // the Close's return. + // + // Methods must not be called after Close. + io.Closer +} + +// WriterProvider is a group of methods provided +// by entity which generates keepers of +// reputation.Trust values. +type WriterProvider interface { + // InitWriter should return an initialized Writer. + // + // Initialization problems are reported via error. + // If no error was returned, then the Writer must not be nil. + // + // Implementations can have different logic for different + // contexts, so specific ones may document their own behavior. + InitWriter(Context) (Writer, error) +} diff --git a/pkg/services/reputation/local/controller/calls.go b/pkg/services/reputation/local/controller/calls.go index 6ef7345ce..d4fc9e102 100644 --- a/pkg/services/reputation/local/controller/calls.go +++ b/pkg/services/reputation/local/controller/calls.go @@ -4,6 +4,7 @@ import ( "context" "github.com/nspcc-dev/neofs-node/pkg/services/reputation" + "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common" "github.com/nspcc-dev/neofs-node/pkg/util/logger" "github.com/pkg/errors" "go.uber.org/zap" @@ -47,7 +48,7 @@ type reportContext struct { log *logger.Logger - ctx Context + ctx common.Context } type iteratorContext struct { diff --git a/pkg/services/reputation/local/controller/controller.go b/pkg/services/reputation/local/controller/controller.go index 274848a40..c6e799d47 100644 --- a/pkg/services/reputation/local/controller/controller.go +++ b/pkg/services/reputation/local/controller/controller.go @@ -4,6 +4,8 @@ import ( "context" "fmt" "sync" + + "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common" ) // Prm groups the required parameters of the Controller's constructor. @@ -22,7 +24,7 @@ type Prm struct { // trust to other nodes. // // Must not be nil. - LocalTrustTarget WriterProvider + LocalTrustTarget common.WriterProvider } // Controller represents main handler for starting diff --git a/pkg/services/reputation/local/controller/deps.go b/pkg/services/reputation/local/controller/deps.go index 4976ccfb5..f6b5d3252 100644 --- a/pkg/services/reputation/local/controller/deps.go +++ b/pkg/services/reputation/local/controller/deps.go @@ -1,59 +1,10 @@ package trustcontroller import ( - "context" - "io" - "github.com/nspcc-dev/neofs-node/pkg/services/reputation" + "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common" ) -// Context wraps stdlib context -// with accompanying meta values. -type Context interface { - context.Context - - // Must return epoch number to select the values. - Epoch() uint64 -} - -// Writer describes the interface for storing reputation.Trust values. -// -// This interface is provided by both local storage -// of values and remote (wrappers over the RPC). -type Writer interface { - // Write performs a write operation of reputation.Trust value - // and returns any error encountered. - // - // All values after the Close call must be flushed to the - // physical target. Implementations can cache values before - // Close operation. - // - // Write must not be called after Close. - Write(reputation.Trust) error - - // Close exits with method-providing Writer. - // - // All cached values must be flushed before - // the Close's return. - // - // Methods must not be called after Close. - io.Closer -} - -// WriterProvider is a group of methods provided -// by entity which generates keepers of -// reputation.Trust values. -type WriterProvider interface { - // InitWriter should return an initialized Writer. - // - // Initialization problems are reported via error. - // If no error was returned, then the Writer must not be nil. - // - // Implementations can have different logic for different - // contexts, so specific ones may document their own behavior. - InitWriter(Context) (Writer, error) -} - // Iterator is a group of methods provided by entity // which can iterate over a group of reputation.Trust values. type Iterator interface { @@ -79,5 +30,5 @@ type IteratorProvider interface { // // Implementations can have different logic for different // contexts, so specific ones may document their own behavior. - InitIterator(Context) (Iterator, error) + InitIterator(common.Context) (Iterator, error) } diff --git a/pkg/services/reputation/local/controller/util.go b/pkg/services/reputation/local/controller/util.go index 38442d510..02ced8962 100644 --- a/pkg/services/reputation/local/controller/util.go +++ b/pkg/services/reputation/local/controller/util.go @@ -1,15 +1,17 @@ package trustcontroller +import "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common" + type storageWrapper struct { - w Writer + w common.Writer i Iterator } -func (s storageWrapper) InitIterator(Context) (Iterator, error) { +func (s storageWrapper) InitIterator(common.Context) (Iterator, error) { return s.i, nil } -func (s storageWrapper) InitWriter(Context) (Writer, error) { +func (s storageWrapper) InitWriter(common.Context) (common.Writer, error) { return s.w, nil } @@ -23,7 +25,7 @@ func SimpleIteratorProvider(i Iterator) IteratorProvider { // SimpleWriterProvider returns WriterProvider that provides // static context-independent Writer. -func SimpleWriterProvider(w Writer) WriterProvider { +func SimpleWriterProvider(w common.Writer) common.WriterProvider { return &storageWrapper{ w: w, } diff --git a/pkg/services/reputation/local/route/calls.go b/pkg/services/reputation/local/route/calls.go index f3d276eb0..702d0e7f8 100644 --- a/pkg/services/reputation/local/route/calls.go +++ b/pkg/services/reputation/local/route/calls.go @@ -4,18 +4,18 @@ import ( "sync" "github.com/nspcc-dev/neofs-node/pkg/services/reputation" - reputationcontroller "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/controller" + "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common" "go.uber.org/zap" ) type routeContext struct { - reputationcontroller.Context + common.Context passedRoute []ServerInfo } // NewRouteContext wraps the main context of value passing with its traversal route and epoch. -func NewRouteContext(ctx reputationcontroller.Context, passed []ServerInfo) reputationcontroller.Context { +func NewRouteContext(ctx common.Context, passed []ServerInfo) common.Context { return &routeContext{ Context: ctx, passedRoute: passed, @@ -28,7 +28,7 @@ type trustWriter struct { ctx *routeContext routeMtx sync.RWMutex - mServers map[string]reputationcontroller.Writer + mServers map[string]common.Writer } // InitWriter initializes and returns Writer that sends each value to its next route point. @@ -45,7 +45,7 @@ type trustWriter struct { // runtime and never returns an error. // // Always returns nil error. -func (r *Router) InitWriter(ctx reputationcontroller.Context) (reputationcontroller.Writer, error) { +func (r *Router) InitWriter(ctx common.Context) (common.Writer, error) { var ( routeCtx *routeContext ok bool @@ -61,7 +61,7 @@ func (r *Router) InitWriter(ctx reputationcontroller.Context) (reputationcontrol return &trustWriter{ router: r, ctx: routeCtx, - mServers: make(map[string]reputationcontroller.Writer), + mServers: make(map[string]common.Writer), }, nil } diff --git a/pkg/services/reputation/local/route/deps.go b/pkg/services/reputation/local/route/deps.go index e689b09f8..a6e2cd1e1 100644 --- a/pkg/services/reputation/local/route/deps.go +++ b/pkg/services/reputation/local/route/deps.go @@ -2,7 +2,7 @@ package reputationroute import ( "github.com/nspcc-dev/neofs-node/pkg/services/reputation" - reputationcontroller "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/controller" + "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common" ) // ServerInfo describes a set of @@ -38,5 +38,5 @@ type RemoteWriterProvider interface { // corresponding to info. // // Nil info matches the end of the route. - InitRemote(info ServerInfo) (reputationcontroller.WriterProvider, error) + InitRemote(info ServerInfo) (common.WriterProvider, error) }