[] reputation/router: Make route pkg independent

Make route package independent from
controller package. Add common interfaces
to `./common` directory.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-04-17 20:13:29 +03:00 committed by Pavel Karpy
parent 0a16aaacb1
commit ac8441b718
8 changed files with 87 additions and 75 deletions
cmd/neofs-node
pkg/services/reputation

View file

@ -20,6 +20,7 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/network/cache" "github.com/nspcc-dev/neofs-node/pkg/network/cache"
grpcreputation "github.com/nspcc-dev/neofs-node/pkg/network/transport/reputation/grpc" grpcreputation "github.com/nspcc-dev/neofs-node/pkg/network/transport/reputation/grpc"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation" "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" trustcontroller "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/controller"
reputationroute "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/route" reputationroute "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/route"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/route/managers" "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/route/managers"
@ -41,7 +42,7 @@ type localTrustStorage struct {
} }
type localTrustIterator struct { type localTrustIterator struct {
ctx trustcontroller.Context ctx reputationcommon.Context
storage *localTrustStorage storage *localTrustStorage
@ -55,7 +56,7 @@ type managerBuilder struct {
type remoteLocalTrustProvider struct { type remoteLocalTrustProvider struct {
localAddrSrc network.LocalAddressSource localAddrSrc network.LocalAddressSource
deadEndProvider trustcontroller.WriterProvider deadEndProvider reputationcommon.WriterProvider
key *ecdsa.PrivateKey key *ecdsa.PrivateKey
clientCache interface { clientCache interface {
@ -74,7 +75,7 @@ func (nopReputationWriter) Close() error {
} }
type remoteLocalTrustWriter struct { type remoteLocalTrustWriter struct {
ctx trustcontroller.Context ctx reputationcommon.Context
client apiClient.Client client apiClient.Client
key *ecdsa.PrivateKey key *ecdsa.PrivateKey
@ -116,7 +117,7 @@ type remoteLocalTrustWriterProvider struct {
} }
type localTrustLogger struct { type localTrustLogger struct {
ctx trustcontroller.Context ctx reputationcommon.Context
log *logger.Logger log *logger.Logger
} }
@ -135,7 +136,7 @@ func (*localTrustLogger) Close() error {
return nil return nil
} }
func (rtwp *remoteLocalTrustWriterProvider) InitWriter(ctx trustcontroller.Context) (trustcontroller.Writer, error) { func (rtwp *remoteLocalTrustWriterProvider) InitWriter(ctx reputationcommon.Context) (reputationcommon.Writer, error) {
return &remoteLocalTrustWriter{ return &remoteLocalTrustWriter{
ctx: ctx, ctx: ctx,
client: rtwp.client, client: rtwp.client,
@ -143,7 +144,7 @@ func (rtwp *remoteLocalTrustWriterProvider) InitWriter(ctx trustcontroller.Conte
}, nil }, nil
} }
func (rtp *remoteLocalTrustProvider) InitRemote(srv reputationroute.ServerInfo) (trustcontroller.WriterProvider, error) { func (rtp *remoteLocalTrustProvider) InitRemote(srv reputationroute.ServerInfo) (reputationcommon.WriterProvider, error) {
if srv == nil { if srv == nil {
return rtp.deadEndProvider, nil return rtp.deadEndProvider, nil
} }
@ -201,7 +202,7 @@ func (mb *managerBuilder) BuildManagers(epoch uint64, p reputation.PeerID) ([]re
return nil, nil 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()) epochStorage, err := s.storage.DataForEpoch(ctx.Epoch())
if err != nil && !errors.Is(err, truststorage.ErrNoPositiveTrust) { if err != nil && !errors.Is(err, truststorage.ErrNoPositiveTrust) {
return nil, err return nil, err
@ -214,7 +215,7 @@ func (s *localTrustStorage) InitIterator(ctx trustcontroller.Context) (trustcont
}, nil }, nil
} }
func (s *localTrustStorage) InitWriter(ctx trustcontroller.Context) (trustcontroller.Writer, error) { func (s *localTrustStorage) InitWriter(ctx reputationcommon.Context) (reputationcommon.Writer, error) {
return &localTrustLogger{ return &localTrustLogger{
ctx: ctx, ctx: ctx,
log: s.log, log: s.log,
@ -362,7 +363,7 @@ func initReputationService(c *cfg) {
type reputationServer struct { type reputationServer struct {
*cfg *cfg
log *logger.Logger log *logger.Logger
router trustcontroller.WriterProvider router reputationcommon.WriterProvider
routeBuilder reputationroute.Builder 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, 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) err := reputationroute.CheckRoute(s.routeBuilder, epoch, t, passedRoute)
if err != nil { if err != nil {
return errors.Wrap(err, "wrong route of reputation trust value") return errors.Wrap(err, "wrong route of reputation trust value")

View file

@ -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)
}

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation" "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/nspcc-dev/neofs-node/pkg/util/logger"
"github.com/pkg/errors" "github.com/pkg/errors"
"go.uber.org/zap" "go.uber.org/zap"
@ -47,7 +48,7 @@ type reportContext struct {
log *logger.Logger log *logger.Logger
ctx Context ctx common.Context
} }
type iteratorContext struct { type iteratorContext struct {

View file

@ -4,6 +4,8 @@ import (
"context" "context"
"fmt" "fmt"
"sync" "sync"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/common"
) )
// Prm groups the required parameters of the Controller's constructor. // Prm groups the required parameters of the Controller's constructor.
@ -22,7 +24,7 @@ type Prm struct {
// trust to other nodes. // trust to other nodes.
// //
// Must not be nil. // Must not be nil.
LocalTrustTarget WriterProvider LocalTrustTarget common.WriterProvider
} }
// Controller represents main handler for starting // Controller represents main handler for starting

View file

@ -1,59 +1,10 @@
package trustcontroller package trustcontroller
import ( import (
"context"
"io"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation" "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 // Iterator is a group of methods provided by entity
// which can iterate over a group of reputation.Trust values. // which can iterate over a group of reputation.Trust values.
type Iterator interface { type Iterator interface {
@ -79,5 +30,5 @@ type IteratorProvider interface {
// //
// Implementations can have different logic for different // Implementations can have different logic for different
// contexts, so specific ones may document their own behavior. // contexts, so specific ones may document their own behavior.
InitIterator(Context) (Iterator, error) InitIterator(common.Context) (Iterator, error)
} }

View file

@ -1,15 +1,17 @@
package trustcontroller package trustcontroller
import "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common"
type storageWrapper struct { type storageWrapper struct {
w Writer w common.Writer
i Iterator i Iterator
} }
func (s storageWrapper) InitIterator(Context) (Iterator, error) { func (s storageWrapper) InitIterator(common.Context) (Iterator, error) {
return s.i, nil return s.i, nil
} }
func (s storageWrapper) InitWriter(Context) (Writer, error) { func (s storageWrapper) InitWriter(common.Context) (common.Writer, error) {
return s.w, nil return s.w, nil
} }
@ -23,7 +25,7 @@ func SimpleIteratorProvider(i Iterator) IteratorProvider {
// SimpleWriterProvider returns WriterProvider that provides // SimpleWriterProvider returns WriterProvider that provides
// static context-independent Writer. // static context-independent Writer.
func SimpleWriterProvider(w Writer) WriterProvider { func SimpleWriterProvider(w common.Writer) common.WriterProvider {
return &storageWrapper{ return &storageWrapper{
w: w, w: w,
} }

View file

@ -4,18 +4,18 @@ import (
"sync" "sync"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation" "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" "go.uber.org/zap"
) )
type routeContext struct { type routeContext struct {
reputationcontroller.Context common.Context
passedRoute []ServerInfo passedRoute []ServerInfo
} }
// NewRouteContext wraps the main context of value passing with its traversal route and epoch. // 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{ return &routeContext{
Context: ctx, Context: ctx,
passedRoute: passed, passedRoute: passed,
@ -28,7 +28,7 @@ type trustWriter struct {
ctx *routeContext ctx *routeContext
routeMtx sync.RWMutex 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. // 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. // runtime and never returns an error.
// //
// Always returns nil 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 ( var (
routeCtx *routeContext routeCtx *routeContext
ok bool ok bool
@ -61,7 +61,7 @@ func (r *Router) InitWriter(ctx reputationcontroller.Context) (reputationcontrol
return &trustWriter{ return &trustWriter{
router: r, router: r,
ctx: routeCtx, ctx: routeCtx,
mServers: make(map[string]reputationcontroller.Writer), mServers: make(map[string]common.Writer),
}, nil }, nil
} }

View file

@ -2,7 +2,7 @@ package reputationroute
import ( import (
"github.com/nspcc-dev/neofs-node/pkg/services/reputation" "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 // ServerInfo describes a set of
@ -38,5 +38,5 @@ type RemoteWriterProvider interface {
// corresponding to info. // corresponding to info.
// //
// Nil info matches the end of the route. // Nil info matches the end of the route.
InitRemote(info ServerInfo) (reputationcontroller.WriterProvider, error) InitRemote(info ServerInfo) (common.WriterProvider, error)
} }