[#488] cmd/reputation: Add DaughterStorage

Add `DaughterStorage` init in main pkg
and start write all received daughters'
trusts to it.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-04-21 07:56:38 +03:00 committed by Alex Vanin
parent 56b3e35779
commit f6783f4f81
8 changed files with 53 additions and 37 deletions

View file

@ -6,6 +6,7 @@ import (
v2reputation "github.com/nspcc-dev/neofs-api-go/v2/reputation" v2reputation "github.com/nspcc-dev/neofs-api-go/v2/reputation"
v2reputationgrpc "github.com/nspcc-dev/neofs-api-go/v2/reputation/grpc" v2reputationgrpc "github.com/nspcc-dev/neofs-api-go/v2/reputation/grpc"
crypto "github.com/nspcc-dev/neofs-crypto" crypto "github.com/nspcc-dev/neofs-crypto"
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/intermediate"
localreputation "github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/local" localreputation "github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/local"
"github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/nspcc-dev/neofs-node/pkg/morph/event"
"github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap" "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
@ -14,6 +15,7 @@ import (
"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" reputationcommon "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common"
reputationrouter "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common/router" reputationrouter "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common/router"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust/storage/daughters"
trustcontroller "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/controller" trustcontroller "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/controller"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/managers" "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/managers"
truststorage "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/storage" truststorage "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/storage"
@ -27,8 +29,15 @@ func initReputationService(c *cfg) {
// consider sharing this between application components // consider sharing this between application components
nmSrc := newCachedNetmapStorage(c.cfgNetmap.state, c.cfgNetmap.wrapper) nmSrc := newCachedNetmapStorage(c.cfgNetmap.state, c.cfgNetmap.wrapper)
// storing calculated trusts as a daughter
c.cfgReputation.localTrustStorage = truststorage.New(truststorage.Prm{}) c.cfgReputation.localTrustStorage = truststorage.New(truststorage.Prm{})
// storing received trusts as a manager
daughterStorage := &intermediate.DaughterStorage{
Log: c.log,
Storage: daughters.New(daughters.Prm{}),
}
trustStorage := &localreputation.TrustStorage{ trustStorage := &localreputation.TrustStorage{
Log: c.log, Log: c.log,
Storage: c.cfgReputation.localTrustStorage, Storage: c.cfgReputation.localTrustStorage,
@ -50,7 +59,7 @@ func initReputationService(c *cfg) {
remoteLocalTrustProvider := localreputation.NewRemoteTrustProvider( remoteLocalTrustProvider := localreputation.NewRemoteTrustProvider(
localreputation.RemoteProviderPrm{ localreputation.RemoteProviderPrm{
LocalAddrSrc: c, LocalAddrSrc: c,
DeadEndProvider: trustStorage, DeadEndProvider: daughterStorage,
ClientCache: cache.NewSDKClientCache(), ClientCache: cache.NewSDKClientCache(),
Key: c.key, Key: c.key,
}, },
@ -191,5 +200,5 @@ func (s *reputationServer) processTrust(epoch uint64, t reputation.Trust,
return errors.Wrap(err, "wrong route of reputation trust value") return errors.Wrap(err, "wrong route of reputation trust value")
} }
return w.Write(t) return w.Write(&localreputation.EpochContext{E: epoch}, t)
} }

View file

@ -0,0 +1,35 @@
package intermediate
import (
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/common"
reputationcommon "github.com/nspcc-dev/neofs-node/pkg/services/reputation/common"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust/storage/daughters"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
)
type DaughterStorage struct {
Log *logger.Logger
Storage *daughters.Storage
}
type DaughterTrustWriter struct {
log *logger.Logger
storage *daughters.Storage
}
func (w *DaughterTrustWriter) Write(ctx common.Context, t reputation.Trust) error {
w.storage.Put(ctx.Epoch(), t)
return nil
}
func (w *DaughterTrustWriter) Close() error {
return nil
}
func (s *DaughterStorage) InitWriter(_ reputationcommon.Context) (reputationcommon.Writer, error) {
return &DaughterTrustWriter{
log: s.Log,
storage: s.Storage,
}, nil
}

View file

@ -107,7 +107,7 @@ type RemoteTrustWriter struct {
buf []*reputationapi.Trust buf []*reputationapi.Trust
} }
func (rtp *RemoteTrustWriter) Write(t reputation.Trust) error { func (rtp *RemoteTrustWriter) Write(_ reputationcommon.Context, t reputation.Trust) error {
apiTrust := reputationapi.NewTrust() apiTrust := reputationapi.NewTrust()
apiPeer := reputationapi.NewPeerID() apiPeer := reputationapi.NewPeerID()

View file

@ -2,7 +2,6 @@ package local
import ( import (
"bytes" "bytes"
"encoding/hex"
netmapcore "github.com/nspcc-dev/neofs-node/pkg/core/netmap" netmapcore "github.com/nspcc-dev/neofs-node/pkg/core/netmap"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation" "github.com/nspcc-dev/neofs-node/pkg/services/reputation"
@ -11,7 +10,6 @@ import (
truststorage "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/storage" truststorage "github.com/nspcc-dev/neofs-node/pkg/services/reputation/local/storage"
"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"
) )
type TrustStorage struct { type TrustStorage struct {
@ -37,13 +35,6 @@ func (s *TrustStorage) InitIterator(ctx reputationcommon.Context) (trustcontroll
}, nil }, nil
} }
func (s *TrustStorage) InitWriter(ctx reputationcommon.Context) (reputationcommon.Writer, error) {
return &TrustLogger{
ctx: ctx,
log: s.Log,
}, nil
}
type TrustIterator struct { type TrustIterator struct {
ctx reputationcommon.Context ctx reputationcommon.Context
@ -98,23 +89,3 @@ func (it *TrustIterator) Iterate(h reputation.TrustHandler) error {
return nil return nil
} }
type TrustLogger struct {
ctx reputationcommon.Context
log *logger.Logger
}
func (l *TrustLogger) Write(t reputation.Trust) error {
l.log.Info("received local trust",
zap.Uint64("epoch", l.ctx.Epoch()),
zap.String("peer", hex.EncodeToString(t.Peer().Bytes())),
zap.Stringer("value", t.Value()),
)
return nil
}
func (*TrustLogger) Close() error {
return nil
}

View file

@ -3,6 +3,7 @@ package local
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/common"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation" "github.com/nspcc-dev/neofs-node/pkg/services/reputation"
) )
@ -18,7 +19,7 @@ func (ctx *EpochContext) Epoch() uint64 {
type NopReputationWriter struct{} type NopReputationWriter struct{}
func (NopReputationWriter) Write(reputation.Trust) error { func (NopReputationWriter) Write(common.Context, reputation.Trust) error {
return nil return nil
} }

View file

@ -29,7 +29,7 @@ type Writer interface {
// Close operation. // Close operation.
// //
// Write must not be called after Close. // Write must not be called after Close.
Write(reputation.Trust) error Write(Context, reputation.Trust) error
// Close exits with method-providing Writer. // Close exits with method-providing Writer.
// //

View file

@ -65,7 +65,7 @@ func (r *Router) InitWriter(ctx common.Context) (common.Writer, error) {
}, nil }, nil
} }
func (w *trustWriter) Write(t reputation.Trust) error { func (w *trustWriter) Write(ctx common.Context, t reputation.Trust) error {
w.routeMtx.Lock() w.routeMtx.Lock()
defer w.routeMtx.Unlock() defer w.routeMtx.Unlock()
@ -106,7 +106,7 @@ func (w *trustWriter) Write(t reputation.Trust) error {
w.mServers[endpoint] = remoteWriter w.mServers[endpoint] = remoteWriter
} }
err := remoteWriter.Write(t) err := remoteWriter.Write(ctx, t)
if err != nil { if err != nil {
w.router.log.Debug("could not write the value", w.router.log.Debug("could not write the value",
zap.String("error", err.Error()), zap.String("error", err.Error()),

View file

@ -126,7 +126,7 @@ func (c *reportContext) report() {
return err return err
} }
return targetWriter.Write(t) return targetWriter.Write(c.ctx, t)
}, },
) )
if err != nil && !errors.Is(err, context.Canceled) { if err != nil && !errors.Is(err, context.Canceled) {