2022-05-23 16:26:27 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
2024-06-20 13:04:55 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
2022-05-23 16:26:27 +00:00
|
|
|
"fmt"
|
2024-06-20 13:04:55 +00:00
|
|
|
"time"
|
2022-05-23 16:26:27 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
rawclient "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control"
|
2024-06-20 13:04:55 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
2022-05-23 16:26:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
netmapStatusFlag = "status"
|
|
|
|
|
|
|
|
netmapStatusOnline = "online"
|
|
|
|
netmapStatusOffline = "offline"
|
|
|
|
netmapStatusMaintenance = "maintenance"
|
2024-06-20 13:04:55 +00:00
|
|
|
|
|
|
|
maxSetStatusMaxWaitTime = 30 * time.Minute
|
|
|
|
setStatusWaitTimeout = 30 * time.Second
|
2022-05-23 16:26:27 +00:00
|
|
|
)
|
|
|
|
|
2024-06-20 13:04:55 +00:00
|
|
|
var errNetmapStatusAwaitFailed = errors.New("netmap status hasn't changed for 30 minutes")
|
|
|
|
|
2022-05-23 16:26:27 +00:00
|
|
|
var setNetmapStatusCmd = &cobra.Command{
|
|
|
|
Use: "set-status",
|
2023-01-27 10:44:39 +00:00
|
|
|
Short: "Set status of the storage node in FrostFS network map",
|
|
|
|
Long: "Set status of the storage node in FrostFS network map",
|
2022-05-23 16:26:27 +00:00
|
|
|
Run: setNetmapStatus,
|
|
|
|
}
|
|
|
|
|
|
|
|
func initControlSetNetmapStatusCmd() {
|
2022-10-17 15:37:15 +00:00
|
|
|
initControlFlags(setNetmapStatusCmd)
|
2022-05-23 16:26:27 +00:00
|
|
|
|
|
|
|
flags := setNetmapStatusCmd.Flags()
|
|
|
|
flags.String(netmapStatusFlag, "",
|
2022-10-11 14:02:03 +00:00
|
|
|
fmt.Sprintf("New netmap status keyword ('%s', '%s', '%s')",
|
2022-05-23 16:26:27 +00:00
|
|
|
netmapStatusOnline,
|
|
|
|
netmapStatusOffline,
|
|
|
|
netmapStatusMaintenance,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
_ = setNetmapStatusCmd.MarkFlagRequired(netmapStatusFlag)
|
2022-10-18 15:42:14 +00:00
|
|
|
|
|
|
|
flags.BoolP(commonflags.ForceFlag, commonflags.ForceFlagShorthand, false,
|
|
|
|
"Force turning to local maintenance")
|
2024-06-20 13:04:55 +00:00
|
|
|
|
|
|
|
flags.Bool(commonflags.AwaitFlag, false, commonflags.AwaitFlagUsage)
|
2022-05-23 16:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setNetmapStatus(cmd *cobra.Command, _ []string) {
|
2022-05-24 08:22:23 +00:00
|
|
|
pk := key.Get(cmd)
|
2022-10-18 15:42:14 +00:00
|
|
|
body := new(control.SetNetmapStatusRequest_Body)
|
|
|
|
force, _ := cmd.Flags().GetBool(commonflags.ForceFlag)
|
2022-05-23 16:26:27 +00:00
|
|
|
|
2022-10-18 15:42:14 +00:00
|
|
|
printIgnoreForce := func(st control.NetmapStatus) {
|
|
|
|
if force {
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Ignore --%s flag for %s state.", commonflags.ForceFlag, st)
|
2022-10-18 15:42:14 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-23 16:26:27 +00:00
|
|
|
|
2024-06-20 13:04:55 +00:00
|
|
|
await, _ := cmd.Flags().GetBool(commonflags.AwaitFlag)
|
|
|
|
var targetStatus control.NetmapStatus
|
2022-05-23 16:26:27 +00:00
|
|
|
switch st, _ := cmd.Flags().GetString(netmapStatusFlag); st {
|
|
|
|
default:
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "", fmt.Errorf("unsupported status %s", st))
|
2022-05-23 16:26:27 +00:00
|
|
|
case netmapStatusOnline:
|
2022-10-18 15:42:14 +00:00
|
|
|
body.SetStatus(control.NetmapStatus_ONLINE)
|
|
|
|
printIgnoreForce(control.NetmapStatus_ONLINE)
|
2024-06-20 13:04:55 +00:00
|
|
|
targetStatus = control.NetmapStatus_ONLINE
|
2022-05-23 16:26:27 +00:00
|
|
|
case netmapStatusOffline:
|
2022-10-18 15:42:14 +00:00
|
|
|
body.SetStatus(control.NetmapStatus_OFFLINE)
|
|
|
|
printIgnoreForce(control.NetmapStatus_OFFLINE)
|
2024-06-20 13:04:55 +00:00
|
|
|
targetStatus = control.NetmapStatus_OFFLINE
|
2022-05-23 16:26:27 +00:00
|
|
|
case netmapStatusMaintenance:
|
2022-10-18 15:42:14 +00:00
|
|
|
body.SetStatus(control.NetmapStatus_MAINTENANCE)
|
2022-10-18 07:29:25 +00:00
|
|
|
|
2022-10-18 15:42:14 +00:00
|
|
|
if force {
|
|
|
|
body.SetForceMaintenance()
|
2022-12-27 09:36:30 +00:00
|
|
|
common.PrintVerbose(cmd, "Local maintenance will be forced.")
|
2022-10-18 07:29:25 +00:00
|
|
|
}
|
2024-06-20 13:04:55 +00:00
|
|
|
targetStatus = control.NetmapStatus_MAINTENANCE
|
2022-05-23 16:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req := new(control.SetNetmapStatusRequest)
|
|
|
|
req.SetBody(body)
|
|
|
|
|
|
|
|
signRequest(cmd, pk, req)
|
|
|
|
|
|
|
|
cli := getClient(cmd, pk)
|
|
|
|
|
|
|
|
var resp *control.SetNetmapStatusResponse
|
|
|
|
var err error
|
|
|
|
err = cli.ExecRaw(func(client *rawclient.Client) error {
|
|
|
|
resp, err = control.SetNetmapStatus(client, req)
|
|
|
|
return err
|
|
|
|
})
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "rpc error: %w", err)
|
2022-05-23 16:26:27 +00:00
|
|
|
|
|
|
|
verifyResponse(cmd, resp.GetSignature(), resp.GetBody())
|
|
|
|
|
|
|
|
cmd.Println("Network status update request successfully sent.")
|
2024-06-20 13:04:55 +00:00
|
|
|
|
|
|
|
if await {
|
|
|
|
awaitSetNetmapStatus(cmd, pk, cli, targetStatus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func awaitSetNetmapStatus(cmd *cobra.Command, pk *ecdsa.PrivateKey, cli *client.Client, targetStatus control.NetmapStatus) {
|
|
|
|
req := &control.GetNetmapStatusRequest{
|
|
|
|
Body: &control.GetNetmapStatusRequest_Body{},
|
|
|
|
}
|
|
|
|
signRequest(cmd, pk, req)
|
|
|
|
var epoch uint64
|
|
|
|
var status control.NetmapStatus
|
|
|
|
startTime := time.Now()
|
|
|
|
cmd.Println("Wait until epoch and netmap status change...")
|
|
|
|
for {
|
|
|
|
var resp *control.GetNetmapStatusResponse
|
|
|
|
var err error
|
|
|
|
err = cli.ExecRaw(func(client *rawclient.Client) error {
|
|
|
|
resp, err = control.GetNetmapStatus(client, req)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
commonCmd.ExitOnErr(cmd, "failed to get current netmap status: %w", err)
|
|
|
|
|
|
|
|
if epoch == 0 {
|
|
|
|
epoch = resp.GetBody().GetEpoch()
|
|
|
|
}
|
|
|
|
|
|
|
|
status = resp.GetBody().GetStatus()
|
|
|
|
if resp.GetBody().GetEpoch() > epoch {
|
|
|
|
epoch = resp.GetBody().GetEpoch()
|
|
|
|
cmd.Printf("Epoch changed to %d\n", resp.GetBody().GetEpoch())
|
|
|
|
}
|
|
|
|
|
|
|
|
if status == targetStatus {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Since(startTime) > maxSetStatusMaxWaitTime {
|
|
|
|
commonCmd.ExitOnErr(cmd, "failed to wait netmap status: %w", errNetmapStatusAwaitFailed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(setStatusWaitTimeout)
|
|
|
|
|
|
|
|
cmd.Printf("Current netmap status '%s', target status '%s'\n", status.String(), targetStatus.String())
|
|
|
|
}
|
|
|
|
cmd.Printf("Netmap status changed to '%s' successfully.\n", status.String())
|
2022-05-23 16:26:27 +00:00
|
|
|
}
|