[#1329] tree: Sync tree on startup

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
remotes/fyrchik/neofs-adm-fix-commands
Pavel Karpy 2022-10-06 23:18:46 +03:00 committed by fyrchik
parent f5f560d903
commit f4a3fa2977
3 changed files with 62 additions and 0 deletions

View File

@ -42,6 +42,7 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/writecache"
"github.com/nspcc-dev/neofs-node/pkg/metrics"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
containerClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/container"
nmClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
netmap2 "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
@ -305,6 +306,10 @@ type internals struct {
workers []worker
closers []func()
// onlineStateHandlers are executed in a separate
// goroutine on every !ONLINE -> ONLINE state transition
onlineStateHandlers []func(context.Context)
apiVersion version.Version
healthStatus *atomic.Int32
// is node under maintenance
@ -347,6 +352,8 @@ type shared struct {
netMap atomicstd.Value // type netmap.NetMap
netMapSource netmapCore.Source
cnrClient *containerClient.Client
respSvc *response.Service
replicator *replicator.Replicator
@ -831,6 +838,13 @@ func (c *cfg) LocalNodeInfo() (*netmapV2.NodeInfo, error) {
// Called with nil when storage node is outside the NeoFS network map
// (before entering the network and after leaving it).
func (c *cfg) handleLocalNodeInfo(ni *netmap.NodeInfo) {
if c.cfgNetmap.state.controlNetmapStatus() != control.NetmapStatus_ONLINE &&
ni != nil && ni.IsOnline() {
for _, h := range c.onlineStateHandlers {
go h(c.ctx)
}
}
c.cfgNetmap.state.setNodeInfo(ni)
}
@ -935,3 +949,7 @@ func (c *cfg) configWatcher(ctx context.Context) {
}
}
}
func (c *cfg) addOnlineStateHandler(h func(ctx context.Context)) {
c.onlineStateHandlers = append(c.onlineStateHandlers, h)
}

View File

@ -45,6 +45,8 @@ func initContainerService(c *cfg) {
wrap, err := cntClient.NewFromMorph(c.cfgMorph.client, c.cfgContainer.scriptHash, 0, cntClient.TryNotary())
fatalOnErr(err)
c.shared.cnrClient = wrap
// container wrapper that always sends non-notary
// requests
wrapperNoNotary, err := cntClient.NewFromMorph(c.cfgMorph.client, c.cfgContainer.scriptHash, 0)

View File

@ -6,9 +6,12 @@ import (
treeconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/tree"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/pilorama"
containerClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/container"
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
containerEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/container"
"github.com/nspcc-dev/neofs-node/pkg/services/control"
"github.com/nspcc-dev/neofs-node/pkg/services/tree"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
"go.uber.org/zap"
)
@ -38,6 +41,16 @@ func initTreeService(c *cfg) {
c.treeService.Start(ctx)
}))
syncTreeFunc := func(ctx context.Context) {
syncTrees(ctx, c.treeService, c.shared.cnrClient, c.log)
}
if c.cfgNetmap.state.controlNetmapStatus() == control.NetmapStatus_ONLINE {
c.workers = append(c.workers, newWorkerFromFunc(syncTreeFunc))
}
c.addOnlineStateHandler(syncTreeFunc)
subscribeToContainerRemoval(c, func(e event.Event) {
ev := e.(containerEvent.DeleteSuccess)
@ -53,3 +66,32 @@ func initTreeService(c *cfg) {
c.onShutdown(c.treeService.Shutdown)
}
func syncTrees(ctx context.Context, treeSvc *tree.Service, cnrCli *containerClient.Client, log *logger.Logger) {
log.Info("synchronizing trees...")
ids, err := cnrCli.List(nil)
if err != nil {
log.Error("trees are not synchronized", zap.Error(err))
return
}
// TODO: #1902 fetch all the trees via a new tree RPC
wellKnownTrees := [...]string{"version", "system"}
for _, id := range ids {
for _, tID := range wellKnownTrees {
err = treeSvc.Synchronize(ctx, id, tID)
if err != nil && !errors.Is(err, tree.ErrNotInContainer) {
log.Warn(
"tree synchronization failed",
zap.Stringer("cid", id),
zap.String("tree_id", tID),
zap.Error(err),
)
}
}
}
log.Info("trees have been synchronized")
}