2020-08-24 14:07:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-01 12:29:35 +00:00
|
|
|
"bytes"
|
2021-02-01 11:59:35 +00:00
|
|
|
"context"
|
2021-02-01 12:19:15 +00:00
|
|
|
"crypto/ecdsa"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-02-01 12:07:07 +00:00
|
|
|
"strconv"
|
2021-02-01 11:59:35 +00:00
|
|
|
|
|
|
|
apiClient "github.com/nspcc-dev/neofs-api-go/pkg/client"
|
2021-02-01 11:21:24 +00:00
|
|
|
containerSDK "github.com/nspcc-dev/neofs-api-go/pkg/container"
|
2021-05-31 11:03:17 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
2021-02-01 12:07:07 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2021-02-01 12:29:35 +00:00
|
|
|
containerV2 "github.com/nspcc-dev/neofs-api-go/v2/container"
|
2020-08-24 14:07:08 +00:00
|
|
|
containerGRPC "github.com/nspcc-dev/neofs-api-go/v2/container/grpc"
|
2021-02-01 12:07:07 +00:00
|
|
|
containerCore "github.com/nspcc-dev/neofs-node/pkg/core/container"
|
|
|
|
netmapCore "github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2021-02-01 12:11:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
|
2020-09-24 07:46:47 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper"
|
2021-02-01 12:33:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
2021-02-01 12:41:00 +00:00
|
|
|
containerEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/container"
|
2021-02-01 12:19:15 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
|
2020-08-24 14:07:08 +00:00
|
|
|
containerTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/container/grpc"
|
|
|
|
containerService "github.com/nspcc-dev/neofs-node/pkg/services/container"
|
2021-02-01 11:59:35 +00:00
|
|
|
loadcontroller "github.com/nspcc-dev/neofs-node/pkg/services/container/announcement/load/controller"
|
2021-02-01 12:19:15 +00:00
|
|
|
loadroute "github.com/nspcc-dev/neofs-node/pkg/services/container/announcement/load/route"
|
2021-02-01 12:41:00 +00:00
|
|
|
placementrouter "github.com/nspcc-dev/neofs-node/pkg/services/container/announcement/load/route/placement"
|
|
|
|
loadstorage "github.com/nspcc-dev/neofs-node/pkg/services/container/announcement/load/storage"
|
2020-08-24 14:07:08 +00:00
|
|
|
containerMorph "github.com/nspcc-dev/neofs-node/pkg/services/container/morph"
|
2021-02-01 11:21:24 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
|
|
|
"go.uber.org/zap"
|
2020-08-24 14:07:08 +00:00
|
|
|
)
|
|
|
|
|
2021-02-01 12:41:00 +00:00
|
|
|
const (
|
|
|
|
startEstimationNotifyEvent = "StartEstimation"
|
|
|
|
stopEstimationNotifyEvent = "StopEstimation"
|
|
|
|
)
|
|
|
|
|
2020-08-24 14:07:08 +00:00
|
|
|
func initContainerService(c *cfg) {
|
2021-05-21 11:02:46 +00:00
|
|
|
wrap, err := wrapper.NewFromMorph(c.cfgMorph.client, c.cfgContainer.scriptHash, 0)
|
2020-09-24 07:46:47 +00:00
|
|
|
fatalOnErr(err)
|
|
|
|
|
2021-05-19 16:32:25 +00:00
|
|
|
cnrSrc := wrapper.AsContainerSource(wrap)
|
|
|
|
|
|
|
|
c.cfgObject.cnrStorage = newCachedContainerStorage(cnrSrc) // use RPC node as source of containers (with caching)
|
2020-10-03 07:46:57 +00:00
|
|
|
c.cfgObject.cnrClient = wrap
|
2020-09-24 07:46:47 +00:00
|
|
|
|
2021-02-01 12:41:00 +00:00
|
|
|
localMetrics := &localStorageLoad{
|
|
|
|
log: c.log,
|
|
|
|
engine: c.cfgObject.cfgLocalStorage.localStorage,
|
|
|
|
}
|
|
|
|
|
2021-05-31 08:55:38 +00:00
|
|
|
pubKey := c.key.PublicKey().Bytes()
|
2021-02-01 12:41:00 +00:00
|
|
|
|
|
|
|
resultWriter := &morphLoadWriter{
|
|
|
|
log: c.log,
|
|
|
|
cnrMorphClient: wrap,
|
|
|
|
key: pubKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
loadAccumulator := loadstorage.New(loadstorage.Prm{})
|
|
|
|
|
|
|
|
loadPlacementBuilder := &loadPlacementBuilder{
|
|
|
|
log: c.log,
|
|
|
|
nmSrc: c.cfgNetmap.wrapper,
|
2021-05-19 16:32:25 +00:00
|
|
|
cnrSrc: cnrSrc,
|
2021-02-01 12:41:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
routeBuilder := placementrouter.New(placementrouter.Prm{
|
|
|
|
PlacementBuilder: loadPlacementBuilder,
|
|
|
|
})
|
|
|
|
|
2021-05-31 06:44:38 +00:00
|
|
|
clientCache := cache.NewSDKClientCache() // FIXME: use shared cache
|
|
|
|
|
2021-02-01 12:41:00 +00:00
|
|
|
loadRouter := loadroute.New(
|
|
|
|
loadroute.Prm{
|
|
|
|
LocalServerInfo: c,
|
|
|
|
RemoteWriterProvider: &remoteLoadAnnounceProvider{
|
2021-05-31 08:55:38 +00:00
|
|
|
key: &c.key.PrivateKey,
|
2021-02-01 12:41:00 +00:00
|
|
|
loadAddrSrc: c,
|
2021-05-31 06:44:38 +00:00
|
|
|
clientCache: clientCache,
|
2021-02-01 12:41:00 +00:00
|
|
|
deadEndProvider: loadcontroller.SimpleWriterProvider(loadAccumulator),
|
|
|
|
},
|
|
|
|
Builder: routeBuilder,
|
|
|
|
},
|
|
|
|
loadroute.WithLogger(c.log),
|
|
|
|
)
|
|
|
|
|
2021-05-31 06:44:38 +00:00
|
|
|
c.onShutdown(clientCache.CloseAll)
|
|
|
|
|
2021-02-01 12:41:00 +00:00
|
|
|
ctrl := loadcontroller.New(
|
|
|
|
loadcontroller.Prm{
|
|
|
|
LocalMetrics: loadcontroller.SimpleIteratorProvider(localMetrics),
|
|
|
|
AnnouncementAccumulator: loadcontroller.SimpleIteratorProvider(loadAccumulator),
|
|
|
|
LocalAnnouncementTarget: loadRouter,
|
|
|
|
ResultReceiver: loadcontroller.SimpleWriterProvider(resultWriter),
|
|
|
|
},
|
|
|
|
loadcontroller.WithLogger(c.log),
|
|
|
|
)
|
|
|
|
|
|
|
|
setContainerNotificationParser(c, startEstimationNotifyEvent, containerEvent.ParseStartEstimation)
|
2021-04-15 10:27:33 +00:00
|
|
|
addContainerAsyncNotificationHandler(c, startEstimationNotifyEvent, func(ev event.Event) {
|
2021-02-01 12:41:00 +00:00
|
|
|
ctrl.Start(loadcontroller.StartPrm{
|
|
|
|
Epoch: ev.(containerEvent.StartEstimation).Epoch(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
setContainerNotificationParser(c, stopEstimationNotifyEvent, containerEvent.ParseStopEstimation)
|
2021-04-15 10:27:33 +00:00
|
|
|
addContainerAsyncNotificationHandler(c, stopEstimationNotifyEvent, func(ev event.Event) {
|
2021-02-01 12:41:00 +00:00
|
|
|
ctrl.Stop(loadcontroller.StopPrm{
|
|
|
|
Epoch: ev.(containerEvent.StopEstimation).Epoch(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-24 14:07:08 +00:00
|
|
|
containerGRPC.RegisterContainerServiceServer(c.cfgGRPC.server,
|
|
|
|
containerTransportGRPC.New(
|
|
|
|
containerService.NewSignService(
|
2021-05-31 08:55:38 +00:00
|
|
|
&c.key.PrivateKey,
|
2020-10-22 11:07:06 +00:00
|
|
|
containerService.NewResponseService(
|
2021-02-01 12:43:09 +00:00
|
|
|
&usedSpaceService{
|
2021-05-21 11:02:46 +00:00
|
|
|
Server: containerService.NewExecutionService(containerMorph.NewExecutor(wrap)),
|
2021-02-01 12:43:09 +00:00
|
|
|
loadWriterProvider: loadRouter,
|
|
|
|
loadPlacementBuilder: loadPlacementBuilder,
|
|
|
|
routeBuilder: routeBuilder,
|
|
|
|
cfg: c,
|
|
|
|
},
|
2020-10-22 11:07:06 +00:00
|
|
|
c.respSvc,
|
2020-08-24 14:07:08 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2021-02-01 11:21:24 +00:00
|
|
|
|
2021-04-14 14:31:49 +00:00
|
|
|
// addContainerNotificationHandler adds handler that will be executed synchronously
|
2021-02-01 12:33:58 +00:00
|
|
|
func addContainerNotificationHandler(c *cfg, sTyp string, h event.Handler) {
|
|
|
|
typ := event.TypeFromString(sTyp)
|
|
|
|
|
|
|
|
if c.cfgContainer.subscribers == nil {
|
|
|
|
c.cfgContainer.subscribers = make(map[event.Type][]event.Handler, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.cfgContainer.subscribers[typ] = append(c.cfgContainer.subscribers[typ], h)
|
|
|
|
}
|
|
|
|
|
2021-04-14 14:31:49 +00:00
|
|
|
// addContainerAsyncNotificationHandler adds handler that will be executed asynchronously via container workerPool
|
|
|
|
func addContainerAsyncNotificationHandler(c *cfg, sTyp string, h event.Handler) {
|
|
|
|
addContainerNotificationHandler(
|
|
|
|
c,
|
|
|
|
sTyp,
|
|
|
|
event.WorkerPoolHandler(
|
|
|
|
c.cfgContainer.workerPool,
|
|
|
|
h,
|
|
|
|
c.log,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:33:58 +00:00
|
|
|
func setContainerNotificationParser(c *cfg, sTyp string, p event.Parser) {
|
|
|
|
typ := event.TypeFromString(sTyp)
|
|
|
|
|
|
|
|
if c.cfgContainer.parsers == nil {
|
|
|
|
c.cfgContainer.parsers = make(map[event.Type]event.Parser, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.cfgContainer.parsers[typ] = p
|
|
|
|
}
|
|
|
|
|
2021-02-01 11:21:24 +00:00
|
|
|
type morphLoadWriter struct {
|
|
|
|
log *logger.Logger
|
|
|
|
|
|
|
|
cnrMorphClient *wrapper.Wrapper
|
|
|
|
|
|
|
|
key []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *morphLoadWriter) Put(a containerSDK.UsedSpaceAnnouncement) error {
|
|
|
|
w.log.Debug("save used space announcement in contract",
|
|
|
|
zap.Uint64("epoch", a.Epoch()),
|
|
|
|
zap.Stringer("cid", a.ContainerID()),
|
|
|
|
zap.Uint64("size", a.UsedSpace()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return w.cnrMorphClient.AnnounceLoad(a, w.key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*morphLoadWriter) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-01 11:59:35 +00:00
|
|
|
|
2021-02-01 12:19:15 +00:00
|
|
|
type nopLoadWriter struct{}
|
|
|
|
|
|
|
|
func (nopLoadWriter) Put(containerSDK.UsedSpaceAnnouncement) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nopLoadWriter) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type remoteLoadAnnounceProvider struct {
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
loadAddrSrc network.LocalAddressSource
|
|
|
|
|
2021-03-23 18:40:36 +00:00
|
|
|
clientCache interface {
|
2021-06-21 16:59:05 +00:00
|
|
|
Get(network.AddressGroup) (apiClient.Client, error)
|
2021-03-23 18:40:36 +00:00
|
|
|
}
|
2021-02-01 12:19:15 +00:00
|
|
|
|
|
|
|
deadEndProvider loadcontroller.WriterProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *remoteLoadAnnounceProvider) InitRemote(srv loadroute.ServerInfo) (loadcontroller.WriterProvider, error) {
|
|
|
|
if srv == nil {
|
|
|
|
return r.deadEndProvider, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := srv.Address()
|
|
|
|
|
2021-06-18 06:00:21 +00:00
|
|
|
var netAddr network.Address
|
|
|
|
|
|
|
|
err := netAddr.FromString(addr)
|
2021-02-01 12:19:15 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not convert address to IP format: %w", err)
|
2021-02-01 12:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 06:00:21 +00:00
|
|
|
if network.IsLocalAddress(r.loadAddrSrc, netAddr) {
|
2021-06-16 12:03:11 +00:00
|
|
|
// if local => return no-op writer
|
|
|
|
return loadcontroller.SimpleWriterProvider(new(nopLoadWriter)), nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:59:05 +00:00
|
|
|
c, err := r.clientCache.Get(network.GroupFromAddress(netAddr))
|
2021-02-01 12:19:15 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not initialize API client: %w", err)
|
2021-02-01 12:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &remoteLoadAnnounceWriterProvider{
|
|
|
|
client: c,
|
2021-03-13 15:22:21 +00:00
|
|
|
key: r.key,
|
2021-02-01 12:19:15 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-02-01 11:59:35 +00:00
|
|
|
type remoteLoadAnnounceWriterProvider struct {
|
2021-03-17 11:34:59 +00:00
|
|
|
client apiClient.Client
|
2021-03-13 15:22:21 +00:00
|
|
|
key *ecdsa.PrivateKey
|
2021-02-01 11:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *remoteLoadAnnounceWriterProvider) InitWriter(ctx context.Context) (loadcontroller.Writer, error) {
|
|
|
|
return &remoteLoadAnnounceWriter{
|
|
|
|
ctx: ctx,
|
|
|
|
client: p.client,
|
2021-03-13 15:22:21 +00:00
|
|
|
key: p.key,
|
2021-02-01 11:59:35 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type remoteLoadAnnounceWriter struct {
|
|
|
|
ctx context.Context
|
|
|
|
|
2021-03-17 11:34:59 +00:00
|
|
|
client apiClient.Client
|
2021-03-13 15:22:21 +00:00
|
|
|
key *ecdsa.PrivateKey
|
2021-02-01 11:59:35 +00:00
|
|
|
|
|
|
|
buf []containerSDK.UsedSpaceAnnouncement
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *remoteLoadAnnounceWriter) Put(a containerSDK.UsedSpaceAnnouncement) error {
|
|
|
|
r.buf = append(r.buf, a)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *remoteLoadAnnounceWriter) Close() error {
|
2021-03-13 15:22:21 +00:00
|
|
|
return r.client.AnnounceContainerUsedSpace(r.ctx, r.buf, apiClient.WithKey(r.key))
|
2021-02-01 11:59:35 +00:00
|
|
|
}
|
2021-02-01 12:07:07 +00:00
|
|
|
|
|
|
|
type loadPlacementBuilder struct {
|
|
|
|
log *logger.Logger
|
|
|
|
|
|
|
|
nmSrc netmapCore.Source
|
|
|
|
|
|
|
|
cnrSrc containerCore.Source
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (l *loadPlacementBuilder) BuildPlacement(epoch uint64, cid *cid.ID) ([]netmap.Nodes, error) {
|
2021-02-01 12:07:07 +00:00
|
|
|
cnrNodes, nm, err := l.buildPlacement(epoch, cid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const pivotPrefix = "load_announcement_"
|
|
|
|
|
|
|
|
pivot := []byte(
|
|
|
|
pivotPrefix + strconv.FormatUint(epoch, 10),
|
|
|
|
)
|
|
|
|
|
|
|
|
placement, err := nm.GetPlacementVectors(cnrNodes, pivot)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not build placement vectors: %w", err)
|
2021-02-01 12:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return placement, nil
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (l *loadPlacementBuilder) buildPlacement(epoch uint64, cid *cid.ID) (netmap.ContainerNodes, *netmap.Netmap, error) {
|
2021-02-01 12:07:07 +00:00
|
|
|
cnr, err := l.cnrSrc.Get(cid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nm, err := l.nmSrc.GetNetMapByEpoch(epoch)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, nil, fmt.Errorf("could not get network map: %w", err)
|
2021-02-01 12:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cnrNodes, err := nm.GetContainerNodes(cnr.PlacementPolicy(), cid.ToV2().GetValue())
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, nil, fmt.Errorf("could not build container nodes: %w", err)
|
2021-02-01 12:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cnrNodes, nm, nil
|
|
|
|
}
|
2021-02-01 12:11:25 +00:00
|
|
|
|
|
|
|
type localStorageLoad struct {
|
|
|
|
log *logger.Logger
|
|
|
|
|
|
|
|
engine *engine.StorageEngine
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *localStorageLoad) Iterate(f loadcontroller.UsedSpaceFilter, h loadcontroller.UsedSpaceHandler) error {
|
|
|
|
idList := engine.ListContainers(d.engine)
|
|
|
|
|
|
|
|
for i := range idList {
|
|
|
|
sz := engine.ContainerSize(d.engine, idList[i])
|
|
|
|
|
|
|
|
d.log.Debug("container size in storage engine calculated successfully",
|
|
|
|
zap.Uint64("size", sz),
|
|
|
|
zap.Stringer("cid", idList[i]),
|
|
|
|
)
|
|
|
|
|
|
|
|
a := containerSDK.NewAnnouncement()
|
|
|
|
a.SetContainerID(idList[i])
|
|
|
|
a.SetUsedSpace(sz)
|
|
|
|
|
|
|
|
if f != nil && !f(*a) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h(*a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-01 12:29:35 +00:00
|
|
|
|
|
|
|
type usedSpaceService struct {
|
2021-03-15 10:53:08 +00:00
|
|
|
containerService.Server
|
2021-02-01 12:29:35 +00:00
|
|
|
|
|
|
|
loadWriterProvider loadcontroller.WriterProvider
|
|
|
|
|
|
|
|
loadPlacementBuilder *loadPlacementBuilder
|
|
|
|
|
|
|
|
routeBuilder loadroute.Builder
|
|
|
|
|
|
|
|
cfg *cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cfg) PublicKey() []byte {
|
2021-06-11 10:55:11 +00:00
|
|
|
return nodeKeyFromNetmap(c)
|
2021-02-01 12:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cfg) Address() string {
|
2021-06-11 10:55:11 +00:00
|
|
|
return nodeAddressFromNetmap(c)
|
2021-02-01 12:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *usedSpaceService) PublicKey() []byte {
|
2021-06-11 10:55:11 +00:00
|
|
|
return nodeKeyFromNetmap(c.cfg)
|
2021-02-01 12:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *usedSpaceService) Address() string {
|
2021-06-11 10:55:11 +00:00
|
|
|
return nodeAddressFromNetmap(c.cfg)
|
2021-02-01 12:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *usedSpaceService) AnnounceUsedSpace(ctx context.Context, req *containerV2.AnnounceUsedSpaceRequest) (*containerV2.AnnounceUsedSpaceResponse, error) {
|
|
|
|
var passedRoute []loadroute.ServerInfo
|
|
|
|
|
|
|
|
for hdr := req.GetVerificationHeader(); hdr != nil; hdr = hdr.GetOrigin() {
|
2021-04-06 17:15:09 +00:00
|
|
|
passedRoute = append(passedRoute, &containerOnlyKeyRemoteServerInfo{
|
2021-02-01 12:29:35 +00:00
|
|
|
key: hdr.GetBodySignature().GetKey(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for left, right := 0, len(passedRoute)-1; left < right; left, right = left+1, right-1 {
|
|
|
|
passedRoute[left], passedRoute[right] = passedRoute[right], passedRoute[left]
|
|
|
|
}
|
|
|
|
|
|
|
|
passedRoute = append(passedRoute, c)
|
|
|
|
|
|
|
|
w, err := c.loadWriterProvider.InitWriter(loadroute.NewRouteContext(ctx, passedRoute))
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not initialize container's used space writer: %w", err)
|
2021-02-01 12:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, aV2 := range req.GetBody().GetAnnouncements() {
|
|
|
|
if err := c.processLoadValue(ctx, *containerSDK.NewAnnouncementFromV2(aV2), passedRoute, w); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
respBody := new(containerV2.AnnounceUsedSpaceResponseBody)
|
|
|
|
|
|
|
|
resp := new(containerV2.AnnounceUsedSpaceResponse)
|
|
|
|
resp.SetBody(respBody)
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var errNodeOutsideContainer = errors.New("node outside the container")
|
|
|
|
|
2021-04-06 17:15:09 +00:00
|
|
|
type containerOnlyKeyRemoteServerInfo struct {
|
2021-02-01 12:29:35 +00:00
|
|
|
key []byte
|
|
|
|
}
|
|
|
|
|
2021-04-06 17:15:09 +00:00
|
|
|
func (i *containerOnlyKeyRemoteServerInfo) PublicKey() []byte {
|
2021-02-01 12:29:35 +00:00
|
|
|
return i.key
|
|
|
|
}
|
|
|
|
|
2021-04-06 17:15:09 +00:00
|
|
|
func (*containerOnlyKeyRemoteServerInfo) Address() string {
|
2021-02-01 12:29:35 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func (l *loadPlacementBuilder) isNodeFromContainerKey(epoch uint64, cid *cid.ID, key []byte) (bool, error) {
|
2021-02-01 12:29:35 +00:00
|
|
|
cnrNodes, _, err := l.buildPlacement(epoch, cid)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, vector := range cnrNodes.Replicas() {
|
|
|
|
for _, node := range vector {
|
|
|
|
if bytes.Equal(node.PublicKey(), key) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *usedSpaceService) processLoadValue(ctx context.Context, a containerSDK.UsedSpaceAnnouncement,
|
|
|
|
route []loadroute.ServerInfo, w loadcontroller.Writer) error {
|
|
|
|
fromCnr, err := c.loadPlacementBuilder.isNodeFromContainerKey(a.Epoch(), a.ContainerID(), route[0].PublicKey())
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not verify that the sender belongs to the container: %w", err)
|
2021-02-01 12:29:35 +00:00
|
|
|
} else if !fromCnr {
|
|
|
|
return errNodeOutsideContainer
|
|
|
|
}
|
|
|
|
|
|
|
|
err = loadroute.CheckRoute(c.routeBuilder, a, route)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("wrong route of container's used space value: %w", err)
|
2021-02-01 12:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = w.Put(a)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not write container's used space value: %w", err)
|
2021-02-01 12:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|