[#1793] node: Serve NetmapService.NetmapSnapshot RPC

There is no more need to serve the same request on Control API.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2022-09-17 16:37:01 +04:00 committed by fyrchik
parent 59de20fbba
commit 485a5418d2
33 changed files with 653 additions and 1238 deletions

View file

@ -30,7 +30,6 @@ func init() {
healthCheckCmd,
setNetmapStatusCmd,
dropObjectsCmd,
snapshotCmd,
shardsCmd,
synchronizeTreeCmd,
)
@ -38,7 +37,6 @@ func init() {
initControlHealthCheckCmd()
initControlSetNetmapStatusCmd()
initControlDropObjectsCmd()
initControlSnapshotCmd()
initControlShardsCmd()
initControlSynchronizeTreeCmd()
}

View file

@ -1,74 +0,0 @@
package control
import (
"github.com/mr-tron/base58"
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-node/pkg/services/control"
"github.com/spf13/cobra"
)
const (
netmapSnapshotJSONFlag = commonflags.JSON
)
var snapshotCmd = &cobra.Command{
Use: "netmap-snapshot",
Short: "Get network map snapshot",
Long: "Get network map snapshot",
Run: func(cmd *cobra.Command, args []string) {
pk := key.Get(cmd)
req := new(control.NetmapSnapshotRequest)
req.SetBody(new(control.NetmapSnapshotRequest_Body))
signRequest(cmd, pk, req)
cli := getClient(cmd, pk)
var resp *control.NetmapSnapshotResponse
var err error
err = cli.ExecRaw(func(client *rawclient.Client) error {
resp, err = control.NetmapSnapshot(client, req)
return err
})
common.ExitOnErr(cmd, "rpc error: %w", err)
verifyResponse(cmd, resp.GetSignature(), resp.GetBody())
isJSON, _ := cmd.Flags().GetBool(netmapSnapshotJSONFlag)
prettyPrintNetmap(cmd, resp.GetBody().GetNetmap(), isJSON)
},
}
func initControlSnapshotCmd() {
commonflags.InitWithoutRPC(snapshotCmd)
flags := snapshotCmd.Flags()
flags.String(controlRPC, controlRPCDefault, controlRPCUsage)
flags.Bool(netmapSnapshotJSONFlag, false, "print netmap structure in JSON format")
}
func prettyPrintNetmap(cmd *cobra.Command, nm *control.Netmap, jsonEncoding bool) {
if jsonEncoding {
common.PrettyPrintJSON(cmd, nm, "netmap")
return
}
cmd.Println("Epoch:", nm.GetEpoch())
for i, node := range nm.GetNodes() {
cmd.Printf("Node %d: %s %s %v\n", i+1,
base58.Encode(node.GetPublicKey()),
node.GetState(),
node.GetAddresses(),
)
for _, attr := range node.GetAttributes() {
cmd.Printf("\t%s: %s\n", attr.GetKey(), attr.GetValue())
}
}
}

View file

@ -22,9 +22,11 @@ func init() {
getEpochCmd,
nodeInfoCmd,
netInfoCmd,
snapshotCmd,
)
initGetEpochCmd()
initNetInfoCmd()
initNodeInfoCmd()
initSnapshotCmd()
}

View file

@ -0,0 +1,63 @@
package netmap
import (
"encoding/hex"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-sdk-go/netmap"
"github.com/spf13/cobra"
)
var snapshotCmd = &cobra.Command{
Use: "snapshot",
Short: "Request current local snapshot of the network map",
Long: `Request current local snapshot of the network map`,
Run: func(cmd *cobra.Command, args []string) {
p := key.GetOrGenerate(cmd)
cli := internalclient.GetSDKClientByFlag(cmd, p, commonflags.RPC)
var prm internalclient.NetMapSnapshotPrm
prm.SetClient(cli)
res, err := internalclient.NetMapSnapshot(prm)
common.ExitOnErr(cmd, "rpc error: %w", err)
prettyPrintNetMap(cmd, res.NetMap())
},
}
func initSnapshotCmd() {
commonflags.Init(snapshotCmd)
commonflags.InitAPI(snapshotCmd)
}
func prettyPrintNetMap(cmd *cobra.Command, nm netmap.NetMap) {
cmd.Println("Epoch:", nm.Epoch())
nodes := nm.Nodes()
for i := range nodes {
var strState string
switch {
case nodes[i].IsOnline():
strState = "ONLINE"
case nodes[i].IsOffline():
strState = "OFFLINE"
}
cmd.Printf("Node %d: %s %s ", i+1, hex.EncodeToString(nodes[i].PublicKey()), strState)
netmap.IterateNetworkEndpoints(nodes[i], func(endpoint string) {
cmd.Printf("%s ", endpoint)
})
cmd.Println()
nodes[i].IterateAttributes(func(key, value string) {
cmd.Printf("\t%s: %s\n", key, value)
})
}
}