2021-04-17 19:17:30 +00:00
|
|
|
package router
|
2021-04-02 18:16:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
|
2021-04-17 17:13:29 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/reputation/common"
|
2021-04-02 18:16:09 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type routeContext struct {
|
2021-04-17 17:13:29 +00:00
|
|
|
common.Context
|
2021-04-02 18:16:09 +00:00
|
|
|
|
|
|
|
passedRoute []ServerInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRouteContext wraps the main context of value passing with its traversal route and epoch.
|
2021-04-17 17:13:29 +00:00
|
|
|
func NewRouteContext(ctx common.Context, passed []ServerInfo) common.Context {
|
2021-04-02 18:16:09 +00:00
|
|
|
return &routeContext{
|
|
|
|
Context: ctx,
|
|
|
|
passedRoute: passed,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type trustWriter struct {
|
|
|
|
router *Router
|
|
|
|
|
|
|
|
ctx *routeContext
|
|
|
|
|
|
|
|
routeMtx sync.RWMutex
|
2021-04-17 17:13:29 +00:00
|
|
|
mServers map[string]common.Writer
|
2021-04-02 18:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InitWriter initializes and returns Writer that sends each value to its next route point.
|
|
|
|
//
|
|
|
|
// If ctx was created by NewRouteContext, then the traversed route is taken into account,
|
|
|
|
// and the value will be sent to its continuation. Otherwise, the route will be laid
|
|
|
|
// from scratch and the value will be sent to its primary point.
|
|
|
|
//
|
|
|
|
// After building a list of remote points of the next leg of the route, the value is sent
|
|
|
|
// sequentially to all of them. If any transmissions (even all) fail, an error will not
|
|
|
|
// be returned.
|
|
|
|
//
|
|
|
|
// Close of the composed Writer calls Close method on each internal Writer generated in
|
|
|
|
// runtime and never returns an error.
|
|
|
|
//
|
|
|
|
// Always returns nil error.
|
2021-04-17 17:13:29 +00:00
|
|
|
func (r *Router) InitWriter(ctx common.Context) (common.Writer, error) {
|
2021-04-02 18:16:09 +00:00
|
|
|
var (
|
|
|
|
routeCtx *routeContext
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
|
|
|
|
if routeCtx, ok = ctx.(*routeContext); !ok {
|
|
|
|
routeCtx = &routeContext{
|
|
|
|
Context: ctx,
|
|
|
|
passedRoute: []ServerInfo{r.localSrvInfo},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &trustWriter{
|
|
|
|
router: r,
|
|
|
|
ctx: routeCtx,
|
2021-04-17 17:13:29 +00:00
|
|
|
mServers: make(map[string]common.Writer),
|
2021-04-02 18:16:09 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-04-21 04:56:38 +00:00
|
|
|
func (w *trustWriter) Write(ctx common.Context, t reputation.Trust) error {
|
2021-04-02 18:16:09 +00:00
|
|
|
w.routeMtx.Lock()
|
|
|
|
defer w.routeMtx.Unlock()
|
|
|
|
|
2021-04-17 16:15:38 +00:00
|
|
|
route, err := w.router.routeBuilder.NextStage(w.ctx.Epoch(), t, w.ctx.passedRoute)
|
2021-04-02 18:16:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(route) == 0 {
|
|
|
|
route = []ServerInfo{nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, remoteInfo := range route {
|
|
|
|
var endpoint string
|
|
|
|
|
|
|
|
if remoteInfo != nil {
|
|
|
|
endpoint = remoteInfo.Address()
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteWriter, ok := w.mServers[endpoint]
|
|
|
|
if !ok {
|
|
|
|
provider, err := w.router.remoteProvider.InitRemote(remoteInfo)
|
|
|
|
if err != nil {
|
|
|
|
w.router.log.Debug("could not initialize writer provider",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteWriter, err = provider.InitWriter(w.ctx)
|
|
|
|
if err != nil {
|
|
|
|
w.router.log.Debug("could not initialize writer",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
w.mServers[endpoint] = remoteWriter
|
|
|
|
}
|
|
|
|
|
2021-04-21 04:56:38 +00:00
|
|
|
err := remoteWriter.Write(ctx, t)
|
2021-04-02 18:16:09 +00:00
|
|
|
if err != nil {
|
|
|
|
w.router.log.Debug("could not write the value",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *trustWriter) Close() error {
|
|
|
|
for endpoint, wRemote := range w.mServers {
|
|
|
|
err := wRemote.Close()
|
|
|
|
if err != nil {
|
|
|
|
w.router.log.Debug("could not close remote server writer",
|
|
|
|
zap.String("endpoint", endpoint),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|