[#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 <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

View file

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

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"
"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 {

View file

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

View file

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

View file

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

View file

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

View file

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