2021-04-17 19:17:30 +00:00
|
|
|
package router
|
2021-04-02 18:16:09 +00:00
|
|
|
|
|
|
|
import (
|
2023-04-05 07:44:40 +00:00
|
|
|
"context"
|
2021-06-22 15:40:39 +00:00
|
|
|
"encoding/hex"
|
2021-04-02 18:16:09 +00:00
|
|
|
"sync"
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/reputation"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/reputation/common"
|
2021-04-02 18:16:09 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
// RouteInfo wraps epoch provider with additional passed
|
2021-05-04 12:31:20 +00:00
|
|
|
// route data. It is only used inside Router and is
|
|
|
|
// not passed in any external methods.
|
2023-04-05 07:44:40 +00:00
|
|
|
type RouteInfo struct {
|
|
|
|
common.EpochProvider
|
2021-04-02 18:16:09 +00:00
|
|
|
|
2021-04-23 14:54:55 +00:00
|
|
|
passedRoute []common.ServerInfo
|
2021-04-02 18:16:09 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
// NewRouteInfo wraps the main context of value passing with its traversal route and epoch.
|
|
|
|
func NewRouteInfo(ep common.EpochProvider, passed []common.ServerInfo) *RouteInfo {
|
|
|
|
return &RouteInfo{
|
|
|
|
EpochProvider: ep,
|
|
|
|
passedRoute: passed,
|
2021-04-02 18:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type trustWriter struct {
|
|
|
|
router *Router
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
routeInfo *RouteInfo
|
2021-04-02 18:16:09 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
//
|
2023-04-05 07:44:40 +00:00
|
|
|
// If ep was created by NewRouteInfo, then the traversed route is taken into account,
|
2021-04-02 18:16:09 +00:00
|
|
|
// 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.
|
2023-04-05 07:44:40 +00:00
|
|
|
func (r *Router) InitWriter(ep common.EpochProvider) (common.Writer, error) {
|
2021-04-02 18:16:09 +00:00
|
|
|
var (
|
2023-04-05 07:44:40 +00:00
|
|
|
routeInfo *RouteInfo
|
|
|
|
ok bool
|
2021-04-02 18:16:09 +00:00
|
|
|
)
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
if routeInfo, ok = ep.(*RouteInfo); !ok {
|
|
|
|
routeInfo = &RouteInfo{
|
|
|
|
EpochProvider: ep,
|
|
|
|
passedRoute: []common.ServerInfo{r.localSrvInfo},
|
2021-04-02 18:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &trustWriter{
|
2023-04-05 07:44:40 +00:00
|
|
|
router: r,
|
|
|
|
routeInfo: routeInfo,
|
|
|
|
mServers: make(map[string]common.Writer),
|
2021-04-02 18:16:09 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
func (w *trustWriter) Write(ctx context.Context, t reputation.Trust) error {
|
2021-04-02 18:16:09 +00:00
|
|
|
w.routeMtx.Lock()
|
|
|
|
defer w.routeMtx.Unlock()
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
route, err := w.router.routeBuilder.NextStage(w.routeInfo.Epoch(), t, w.routeInfo.passedRoute)
|
2021-04-02 18:16:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(route) == 0 {
|
2021-04-23 14:54:55 +00:00
|
|
|
route = []common.ServerInfo{nil}
|
2021-04-02 18:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, remoteInfo := range route {
|
2021-06-22 15:40:39 +00:00
|
|
|
var key string
|
2021-04-02 18:16:09 +00:00
|
|
|
|
|
|
|
if remoteInfo != nil {
|
2021-06-22 15:40:39 +00:00
|
|
|
key = hex.EncodeToString(remoteInfo.PublicKey())
|
2021-04-02 18:16:09 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 15:40:39 +00:00
|
|
|
remoteWriter, ok := w.mServers[key]
|
2021-04-02 18:16:09 +00:00
|
|
|
if !ok {
|
|
|
|
provider, err := w.router.remoteProvider.InitRemote(remoteInfo)
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
w.router.log.Debug(logs.RouterCouldNotInitializeWriterProvider,
|
2021-04-02 18:16:09 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-04 12:31:20 +00:00
|
|
|
// init writer with original context wrapped in routeContext
|
2023-04-05 07:44:40 +00:00
|
|
|
remoteWriter, err = provider.InitWriter(w.routeInfo.EpochProvider)
|
2021-04-02 18:16:09 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
w.router.log.Debug(logs.RouterCouldNotInitializeWriter,
|
2021-04-02 18:16:09 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-06-22 15:40:39 +00:00
|
|
|
w.mServers[key] = remoteWriter
|
2021-04-02 18:16:09 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
err := remoteWriter.Write(ctx, t)
|
2021-04-02 18:16:09 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
w.router.log.Debug(logs.RouterCouldNotWriteTheValue,
|
2021-04-02 18:16:09 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
func (w *trustWriter) Close(ctx context.Context) error {
|
2021-06-22 15:40:39 +00:00
|
|
|
for key, wRemote := range w.mServers {
|
2023-04-05 07:44:40 +00:00
|
|
|
err := wRemote.Close(ctx)
|
2021-04-02 18:16:09 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
w.router.log.Debug(logs.RouterCouldNotCloseRemoteServerWriter,
|
2021-06-22 15:40:39 +00:00
|
|
|
zap.String("key", key),
|
2021-04-02 18:16:09 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|