Skip to content

Commit

Permalink
[nspcc-dev#1329] tree: Sync tree on startup
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
  • Loading branch information
carpawell authored and aprasolova committed Oct 19, 2022
1 parent 6dab64a commit c23d720
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cmd/neofs-node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions cmd/neofs-node/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions cmd/neofs-node/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)

Expand All @@ -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")
}

0 comments on commit c23d720

Please sign in to comment.