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"
|
2022-05-12 16:37:46 +00:00
|
|
|
"crypto/sha256"
|
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
|
|
|
|
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-09-28 04:46:10 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/client"
|
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"
|
2022-01-31 13:34:01 +00:00
|
|
|
cntClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/container"
|
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"
|
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"
|
2021-11-10 07:08:33 +00:00
|
|
|
apiClient "github.com/nspcc-dev/neofs-sdk-go/client"
|
|
|
|
containerSDK "github.com/nspcc-dev/neofs-sdk-go/container"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2022-05-17 13:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2021-02-01 11:21:24 +00:00
|
|
|
"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-09-09 14:13:35 +00:00
|
|
|
// container wrapper that tries to invoke notary
|
|
|
|
// requests if chain is configured so
|
2022-01-31 13:34:01 +00:00
|
|
|
wrap, err := cntClient.NewFromMorph(c.cfgMorph.client, c.cfgContainer.scriptHash, 0, cntClient.TryNotary())
|
2020-09-24 07:46:47 +00:00
|
|
|
fatalOnErr(err)
|
|
|
|
|
2021-09-09 14:13:35 +00:00
|
|
|
// container wrapper that always sends non-notary
|
|
|
|
// requests
|
2022-01-31 13:34:01 +00:00
|
|
|
wrapperNoNotary, err := cntClient.NewFromMorph(c.cfgMorph.client, c.cfgContainer.scriptHash, 0)
|
2021-09-09 14:13:35 +00:00
|
|
|
fatalOnErr(err)
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
cnrSrc := cntClient.AsContainerSource(wrap)
|
2021-05-19 16:32:25 +00:00
|
|
|
|
2021-08-30 11:16:41 +00:00
|
|
|
eACLFetcher := &morphEACLFetcher{
|
|
|
|
w: wrap,
|
|
|
|
}
|
|
|
|
|
|
|
|
cnrRdr := new(morphContainerReader)
|
2021-09-08 17:24:16 +00:00
|
|
|
|
|
|
|
cnrWrt := &morphContainerWriter{
|
|
|
|
neoClient: wrap,
|
|
|
|
}
|
2021-07-29 15:57:53 +00:00
|
|
|
|
2022-08-16 11:59:30 +00:00
|
|
|
if c.cfgMorph.cacheTTL <= 0 {
|
2021-08-30 11:16:41 +00:00
|
|
|
c.cfgObject.eaclSource = eACLFetcher
|
|
|
|
cnrRdr.eacl = eACLFetcher
|
|
|
|
c.cfgObject.cnrSource = cnrSrc
|
|
|
|
cnrRdr.get = cnrSrc
|
|
|
|
cnrRdr.lister = wrap
|
2021-07-29 15:57:53 +00:00
|
|
|
} else {
|
2021-08-30 11:16:41 +00:00
|
|
|
// use RPC node as source of Container contract items (with caching)
|
2022-08-16 11:59:30 +00:00
|
|
|
cachedContainerStorage := newCachedContainerStorage(cnrSrc, c.cfgMorph.cacheTTL)
|
|
|
|
cachedEACLStorage := newCachedEACLStorage(eACLFetcher, c.cfgMorph.cacheTTL)
|
|
|
|
cachedContainerLister := newCachedContainerLister(wrap, c.cfgMorph.cacheTTL)
|
2021-09-08 13:40:23 +00:00
|
|
|
|
2022-08-12 10:25:04 +00:00
|
|
|
subscribeToContainerCreation(c, func(e event.Event) {
|
|
|
|
ev := e.(containerEvent.PutSuccess)
|
|
|
|
|
|
|
|
// read owner of the created container in order to update the reading cache.
|
|
|
|
// TODO: use owner directly from the event after neofs-contract#256 will become resolved
|
|
|
|
// but don't forget about the profit of reading the new container and caching it:
|
|
|
|
// creation success are most commonly tracked by polling GET op.
|
|
|
|
cnr, err := cachedContainerStorage.Get(ev.ID)
|
|
|
|
if err == nil {
|
|
|
|
cachedContainerLister.update(cnr.Value.Owner(), ev.ID, true)
|
|
|
|
} else {
|
|
|
|
// unlike removal, we expect successful receive of the container
|
|
|
|
// after successful creation, so logging can be useful
|
|
|
|
c.log.Error("read newly created container after the notification",
|
|
|
|
zap.Stringer("id", ev.ID),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.log.Debug("container creation event's receipt",
|
|
|
|
zap.Stringer("id", ev.ID),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-08-11 14:48:16 +00:00
|
|
|
subscribeToContainerRemoval(c, func(e event.Event) {
|
|
|
|
ev := e.(containerEvent.DeleteSuccess)
|
|
|
|
|
2022-08-12 10:25:04 +00:00
|
|
|
// read owner of the removed container in order to update the listing cache.
|
|
|
|
// It's strange to read already removed container, but we can successfully hit
|
|
|
|
// the cache.
|
|
|
|
// TODO: use owner directly from the event after neofs-contract#256 will become resolved
|
|
|
|
cnr, err := cachedContainerStorage.Get(ev.ID)
|
|
|
|
if err == nil {
|
|
|
|
cachedContainerLister.update(cnr.Value.Owner(), ev.ID, false)
|
|
|
|
}
|
|
|
|
|
2022-08-11 14:48:16 +00:00
|
|
|
cachedContainerStorage.handleRemoval(ev.ID)
|
|
|
|
|
|
|
|
c.log.Debug("container removal event's receipt",
|
|
|
|
zap.Stringer("id", ev.ID),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-09-08 13:40:23 +00:00
|
|
|
c.cfgObject.eaclSource = cachedEACLStorage
|
|
|
|
c.cfgObject.cnrSource = cachedContainerStorage
|
|
|
|
|
|
|
|
cnrRdr.lister = cachedContainerLister
|
2021-08-30 11:16:41 +00:00
|
|
|
cnrRdr.eacl = c.cfgObject.eaclSource
|
|
|
|
cnrRdr.get = c.cfgObject.cnrSource
|
2021-09-08 13:40:23 +00:00
|
|
|
|
2021-09-08 17:24:16 +00:00
|
|
|
cnrWrt.cacheEnabled = true
|
|
|
|
cnrWrt.lists = cachedContainerLister
|
|
|
|
cnrWrt.eacls = cachedEACLStorage
|
2021-07-29 15:57:53 +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,
|
2021-09-09 14:13:35 +00:00
|
|
|
cnrMorphClient: wrapperNoNotary,
|
2021-02-01 12:41:00 +00:00
|
|
|
key: pubKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
loadAccumulator := loadstorage.New(loadstorage.Prm{})
|
|
|
|
|
|
|
|
loadPlacementBuilder := &loadPlacementBuilder{
|
|
|
|
log: c.log,
|
2022-03-29 13:52:12 +00:00
|
|
|
nmSrc: c.netMapSource,
|
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,
|
|
|
|
})
|
|
|
|
|
|
|
|
loadRouter := loadroute.New(
|
|
|
|
loadroute.Prm{
|
|
|
|
LocalServerInfo: c,
|
|
|
|
RemoteWriterProvider: &remoteLoadAnnounceProvider{
|
2021-05-31 08:55:38 +00:00
|
|
|
key: &c.key.PrivateKey,
|
2021-09-06 12:17:14 +00:00
|
|
|
netmapKeys: c,
|
2021-07-13 11:12:23 +00:00
|
|
|
clientCache: c.clientCache,
|
2021-02-01 12:41:00 +00:00
|
|
|
deadEndProvider: loadcontroller.SimpleWriterProvider(loadAccumulator),
|
|
|
|
},
|
|
|
|
Builder: routeBuilder,
|
|
|
|
},
|
|
|
|
loadroute.WithLogger(c.log),
|
|
|
|
)
|
|
|
|
|
|
|
|
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(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-06-22 17:25:18 +00:00
|
|
|
server := containerTransportGRPC.New(
|
|
|
|
containerService.NewSignService(
|
|
|
|
&c.key.PrivateKey,
|
|
|
|
containerService.NewResponseService(
|
|
|
|
&usedSpaceService{
|
2021-09-08 17:24:16 +00:00
|
|
|
Server: containerService.NewExecutionService(containerMorph.NewExecutor(cnrRdr, cnrWrt)),
|
2021-06-22 17:25:18 +00:00
|
|
|
loadWriterProvider: loadRouter,
|
|
|
|
loadPlacementBuilder: loadPlacementBuilder,
|
|
|
|
routeBuilder: routeBuilder,
|
|
|
|
cfg: c,
|
|
|
|
},
|
|
|
|
c.respSvc,
|
2020-08-24 14:07:08 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2021-06-22 17:25:18 +00:00
|
|
|
|
|
|
|
for _, srv := range c.cfgGRPC.servers {
|
|
|
|
containerGRPC.RegisterContainerServiceServer(srv, server)
|
|
|
|
}
|
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,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-11 13:24:26 +00:00
|
|
|
// stores already registered parsers of the notification events thrown by Container contract.
|
|
|
|
// MUST NOT be used concurrently.
|
|
|
|
var mRegisteredParsersContainer = make(map[string]struct{})
|
|
|
|
|
|
|
|
// registers event parser by name once. MUST NOT be called concurrently.
|
|
|
|
func registerEventParserOnceContainer(c *cfg, name string, p event.NotificationParser) {
|
|
|
|
if _, ok := mRegisteredParsersContainer[name]; !ok {
|
|
|
|
setContainerNotificationParser(c, name, p)
|
|
|
|
mRegisteredParsersContainer[name] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// subscribes to successful container creation. Provided handler is called asynchronously
|
|
|
|
// on corresponding routine pool. MUST NOT be called concurrently with itself and other
|
|
|
|
// similar functions.
|
|
|
|
func subscribeToContainerCreation(c *cfg, h event.Handler) {
|
|
|
|
const eventNameContainerCreated = "PutSuccess"
|
|
|
|
registerEventParserOnceContainer(c, eventNameContainerCreated, containerEvent.ParsePutSuccess)
|
|
|
|
addContainerAsyncNotificationHandler(c, eventNameContainerCreated, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// like subscribeToContainerCreation but for removal.
|
|
|
|
func subscribeToContainerRemoval(c *cfg, h event.Handler) {
|
|
|
|
const eventNameContainerRemoved = "DeleteSuccess"
|
|
|
|
registerEventParserOnceContainer(c, eventNameContainerRemoved, containerEvent.ParseDeleteSuccess)
|
|
|
|
addContainerAsyncNotificationHandler(c, eventNameContainerRemoved, h)
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:24:17 +00:00
|
|
|
func setContainerNotificationParser(c *cfg, sTyp string, p event.NotificationParser) {
|
2021-02-01 12:33:58 +00:00
|
|
|
typ := event.TypeFromString(sTyp)
|
|
|
|
|
|
|
|
if c.cfgContainer.parsers == nil {
|
2021-08-12 15:24:17 +00:00
|
|
|
c.cfgContainer.parsers = make(map[event.Type]event.NotificationParser, 1)
|
2021-02-01 12:33:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.cfgContainer.parsers[typ] = p
|
|
|
|
}
|
|
|
|
|
2021-02-01 11:21:24 +00:00
|
|
|
type morphLoadWriter struct {
|
|
|
|
log *logger.Logger
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
cnrMorphClient *cntClient.Client
|
2021-02-01 11:21:24 +00:00
|
|
|
|
|
|
|
key []byte
|
|
|
|
}
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
func (w *morphLoadWriter) Put(a containerSDK.SizeEstimation) error {
|
2021-02-01 11:21:24 +00:00
|
|
|
w.log.Debug("save used space announcement in contract",
|
|
|
|
zap.Uint64("epoch", a.Epoch()),
|
2022-06-28 07:01:05 +00:00
|
|
|
zap.Stringer("cid", a.Container()),
|
|
|
|
zap.Uint64("size", a.Value()),
|
2021-02-01 11:21:24 +00:00
|
|
|
)
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
prm := cntClient.AnnounceLoadPrm{}
|
2021-11-12 15:19:05 +00:00
|
|
|
|
|
|
|
prm.SetAnnouncement(a)
|
|
|
|
prm.SetReporter(w.key)
|
|
|
|
|
|
|
|
return w.cnrMorphClient.AnnounceLoad(prm)
|
2021-02-01 11:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*morphLoadWriter) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-01 11:59:35 +00:00
|
|
|
|
2021-10-05 11:53:18 +00:00
|
|
|
type nopLoadWriter struct{}
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
func (nopLoadWriter) Put(containerSDK.SizeEstimation) error {
|
2021-10-05 11:53:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nopLoadWriter) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:19:15 +00:00
|
|
|
type remoteLoadAnnounceProvider struct {
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
2021-09-06 12:17:14 +00:00
|
|
|
netmapKeys netmapCore.AnnouncedKeys
|
2021-02-01 12:19:15 +00:00
|
|
|
|
2021-03-23 18:40:36 +00:00
|
|
|
clientCache interface {
|
2022-01-13 15:01:50 +00:00
|
|
|
Get(client.NodeInfo) (client.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) {
|
2021-10-05 11:53:18 +00:00
|
|
|
if srv == nil {
|
2021-02-01 12:19:15 +00:00
|
|
|
return r.deadEndProvider, nil
|
|
|
|
}
|
|
|
|
|
2021-10-05 11:53:18 +00:00
|
|
|
if r.netmapKeys.IsLocalKey(srv.PublicKey()) {
|
|
|
|
// if local => return no-op writer
|
|
|
|
return loadcontroller.SimpleWriterProvider(new(nopLoadWriter)), nil
|
|
|
|
}
|
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
var info client.NodeInfo
|
2021-02-01 12:19:15 +00:00
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
err := client.NodeInfoFromRawNetmapElement(&info, srv)
|
2021-02-01 12:19:15 +00:00
|
|
|
if err != nil {
|
2021-09-28 05:32:30 +00:00
|
|
|
return nil, fmt.Errorf("parse client node info: %w", err)
|
2021-02-01 12:19:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 04:46:10 +00:00
|
|
|
c, err := r.clientCache.Get(info)
|
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,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-02-01 11:59:35 +00:00
|
|
|
type remoteLoadAnnounceWriterProvider struct {
|
2022-01-13 15:01:50 +00:00
|
|
|
client client.Client
|
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,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type remoteLoadAnnounceWriter struct {
|
|
|
|
ctx context.Context
|
|
|
|
|
2022-01-13 15:01:50 +00:00
|
|
|
client client.Client
|
2021-02-01 11:59:35 +00:00
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
buf []containerSDK.SizeEstimation
|
2021-02-01 11:59:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
func (r *remoteLoadAnnounceWriter) Put(a containerSDK.SizeEstimation) error {
|
2021-02-01 11:59:35 +00:00
|
|
|
r.buf = append(r.buf, a)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *remoteLoadAnnounceWriter) Close() error {
|
2022-02-25 09:20:49 +00:00
|
|
|
var cliPrm apiClient.PrmAnnounceSpace
|
2022-01-21 16:53:06 +00:00
|
|
|
|
|
|
|
cliPrm.SetValues(r.buf)
|
|
|
|
|
2022-02-25 09:20:49 +00:00
|
|
|
_, err := r.client.ContainerAnnounceUsedSpace(r.ctx, cliPrm)
|
2021-11-06 11:13:04 +00:00
|
|
|
return err
|
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
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func (l *loadPlacementBuilder) BuildPlacement(epoch uint64, cnr cid.ID) ([][]netmap.NodeInfo, error) {
|
2022-05-31 17:00:41 +00:00
|
|
|
cnrNodes, nm, err := l.buildPlacement(epoch, cnr)
|
2021-02-01 12:07:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const pivotPrefix = "load_announcement_"
|
|
|
|
|
|
|
|
pivot := []byte(
|
|
|
|
pivotPrefix + strconv.FormatUint(epoch, 10),
|
|
|
|
)
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
placement, err := nm.PlacementVectors(cnrNodes, pivot)
|
2021-02-01 12:07:07 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func (l *loadPlacementBuilder) buildPlacement(epoch uint64, idCnr cid.ID) ([][]netmap.NodeInfo, *netmap.NetMap, error) {
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr, err := l.cnrSrc.Get(idCnr)
|
2021-02-01 12:07:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
binCnr := make([]byte, sha256.Size)
|
|
|
|
idCnr.Encode(binCnr)
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
cnrNodes, err := nm.ContainerNodes(cnr.Value.PlacementPolicy(), binCnr)
|
2021-02-01 12:07:07 +00:00
|
|
|
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 {
|
2021-11-10 15:26:12 +00:00
|
|
|
idList, err := engine.ListContainers(d.engine)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("list containers on engine failure: %w", err)
|
|
|
|
}
|
2021-02-01 12:11:25 +00:00
|
|
|
|
|
|
|
for i := range idList {
|
2021-11-10 15:26:12 +00:00
|
|
|
sz, err := engine.ContainerSize(d.engine, idList[i])
|
|
|
|
if err != nil {
|
|
|
|
d.log.Debug("failed to calculate container size in storage engine",
|
|
|
|
zap.Stringer("cid", idList[i]),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2021-02-01 12:11:25 +00:00
|
|
|
|
|
|
|
d.log.Debug("container size in storage engine calculated successfully",
|
|
|
|
zap.Uint64("size", sz),
|
|
|
|
zap.Stringer("cid", idList[i]),
|
|
|
|
)
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
var a containerSDK.SizeEstimation
|
|
|
|
a.SetContainer(idList[i])
|
|
|
|
a.SetValue(sz)
|
2021-02-01 12:11:25 +00:00
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
if f != nil && !f(a) {
|
2021-02-01 12:11:25 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
if err := h(a); err != nil {
|
2021-02-01 12:11:25 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-06 12:17:14 +00:00
|
|
|
func (c *cfg) IsLocalKey(key []byte) bool {
|
|
|
|
return bytes.Equal(key, c.PublicKey())
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:00:00 +00:00
|
|
|
func (c *cfg) IterateAddresses(f func(string) bool) {
|
|
|
|
c.iterateNetworkAddresses(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cfg) NumberOfAddresses() int {
|
|
|
|
return c.addressNum()
|
2021-02-01 12:29:35 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 12:34:01 +00:00
|
|
|
func (c *cfg) ExternalAddresses() []string {
|
|
|
|
return c.cfgNodeInfo.localInfo.ExternalAddresses()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-06-22 16:00:00 +00:00
|
|
|
func (c *usedSpaceService) IterateAddresses(f func(string) bool) {
|
|
|
|
c.cfg.iterateNetworkAddresses(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *usedSpaceService) NumberOfAddresses() int {
|
|
|
|
return c.cfg.addressNum()
|
2021-02-01 12:29:35 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 12:34:01 +00:00
|
|
|
func (c *usedSpaceService) ExternalAddresses() []string {
|
|
|
|
return c.cfg.ExternalAddresses()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
var est containerSDK.SizeEstimation
|
|
|
|
|
2021-02-01 12:29:35 +00:00
|
|
|
for _, aV2 := range req.GetBody().GetAnnouncements() {
|
2022-06-28 07:01:05 +00:00
|
|
|
err = est.ReadFromV2(aV2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid size announcement: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.processLoadValue(ctx, est, passedRoute, w); err != nil {
|
2021-02-01 12:29:35 +00:00
|
|
|
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-06-22 16:00:00 +00:00
|
|
|
func (*containerOnlyKeyRemoteServerInfo) IterateAddresses(func(string) bool) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*containerOnlyKeyRemoteServerInfo) NumberOfAddresses() int {
|
|
|
|
return 0
|
2021-02-01 12:29:35 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 12:34:01 +00:00
|
|
|
func (*containerOnlyKeyRemoteServerInfo) ExternalAddresses() []string {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (l *loadPlacementBuilder) isNodeFromContainerKey(epoch uint64, cnr cid.ID, key []byte) (bool, error) {
|
|
|
|
cnrNodes, _, err := l.buildPlacement(epoch, cnr)
|
2021-02-01 12:29:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
for i := range cnrNodes {
|
|
|
|
for j := range cnrNodes[i] {
|
|
|
|
if bytes.Equal(cnrNodes[i][j].PublicKey(), key) {
|
2021-02-01 12:29:35 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
func (c *usedSpaceService) processLoadValue(_ context.Context, a containerSDK.SizeEstimation,
|
2021-02-01 12:29:35 +00:00
|
|
|
route []loadroute.ServerInfo, w loadcontroller.Writer) error {
|
2022-06-28 07:01:05 +00:00
|
|
|
fromCnr, err := c.loadPlacementBuilder.isNodeFromContainerKey(a.Epoch(), a.Container(), route[0].PublicKey())
|
2021-02-01 12:29:35 +00:00
|
|
|
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
|
|
|
|
}
|
2021-08-30 11:16:41 +00:00
|
|
|
|
|
|
|
// implements interface required by container service provided by morph executor.
|
|
|
|
type morphContainerReader struct {
|
2022-09-08 10:32:25 +00:00
|
|
|
eacl containerCore.EACLSource
|
2021-08-30 11:16:41 +00:00
|
|
|
|
|
|
|
get containerCore.Source
|
|
|
|
|
|
|
|
lister interface {
|
2022-05-31 17:00:41 +00:00
|
|
|
List(*user.ID) ([]cid.ID, error)
|
2021-08-30 11:16:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
func (x *morphContainerReader) Get(id cid.ID) (*containerCore.Container, error) {
|
2021-08-30 11:16:41 +00:00
|
|
|
return x.get.Get(id)
|
|
|
|
}
|
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
func (x *morphContainerReader) GetEACL(id cid.ID) (*containerCore.EACL, error) {
|
2021-08-30 11:16:41 +00:00
|
|
|
return x.eacl.GetEACL(id)
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (x *morphContainerReader) List(id *user.ID) ([]cid.ID, error) {
|
2021-08-30 11:16:41 +00:00
|
|
|
return x.lister.List(id)
|
|
|
|
}
|
2021-09-08 13:40:23 +00:00
|
|
|
|
2021-09-08 17:24:16 +00:00
|
|
|
type morphContainerWriter struct {
|
2022-01-31 13:34:01 +00:00
|
|
|
neoClient *cntClient.Client
|
2021-09-08 17:24:16 +00:00
|
|
|
|
|
|
|
cacheEnabled bool
|
|
|
|
eacls *ttlEACLStorage
|
|
|
|
lists *ttlContainerLister
|
|
|
|
}
|
2021-09-08 13:40:23 +00:00
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
func (m morphContainerWriter) Put(cnr containerCore.Container) (*cid.ID, error) {
|
2022-08-12 10:25:04 +00:00
|
|
|
return cntClient.Put(m.neoClient, cnr)
|
2021-09-08 13:40:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 17:24:16 +00:00
|
|
|
func (m morphContainerWriter) Delete(witness containerCore.RemovalWitness) error {
|
2022-04-15 05:52:24 +00:00
|
|
|
return cntClient.Delete(m.neoClient, witness)
|
2021-09-08 13:40:23 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
func (m morphContainerWriter) PutEACL(eaclInfo containerCore.EACL) error {
|
|
|
|
err := cntClient.PutEACL(m.neoClient, eaclInfo)
|
2021-09-08 17:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-09-08 13:40:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 17:24:16 +00:00
|
|
|
if m.cacheEnabled {
|
2022-06-22 10:55:31 +00:00
|
|
|
id, _ := eaclInfo.Value.CID()
|
2022-05-31 17:00:41 +00:00
|
|
|
m.eacls.InvalidateEACL(id)
|
2021-09-08 13:40:23 +00:00
|
|
|
}
|
2021-09-08 17:24:16 +00:00
|
|
|
|
|
|
|
return nil
|
2021-09-08 13:40:23 +00:00
|
|
|
}
|