2022-05-23 16:26:27 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
2022-06-23 14:21:43 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2022-05-23 16:26:27 +00:00
|
|
|
"fmt"
|
2023-09-18 07:05:17 +00:00
|
|
|
"sort"
|
2022-10-05 12:41:36 +00:00
|
|
|
"strings"
|
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/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"
|
2022-05-23 16:26:27 +00:00
|
|
|
"github.com/mr-tron/base58"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var listShardsCmd = &cobra.Command{
|
|
|
|
Use: "list",
|
|
|
|
Short: "List shards of the storage node",
|
|
|
|
Long: "List shards of the storage node",
|
|
|
|
Run: listShards,
|
|
|
|
}
|
|
|
|
|
|
|
|
func initControlShardsListCmd() {
|
2022-10-17 15:37:15 +00:00
|
|
|
initControlFlags(listShardsCmd)
|
2022-05-23 16:26:27 +00:00
|
|
|
|
|
|
|
flags := listShardsCmd.Flags()
|
2022-06-23 14:21:43 +00:00
|
|
|
flags.Bool(commonflags.JSON, false, "Print shard info as a JSON array")
|
2022-05-23 16:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func listShards(cmd *cobra.Command, _ []string) {
|
2022-05-24 08:22:23 +00:00
|
|
|
pk := key.Get(cmd)
|
2022-05-23 16:26:27 +00:00
|
|
|
|
|
|
|
req := new(control.ListShardsRequest)
|
|
|
|
req.SetBody(new(control.ListShardsRequest_Body))
|
|
|
|
|
|
|
|
signRequest(cmd, pk, req)
|
|
|
|
|
|
|
|
cli := getClient(cmd, pk)
|
|
|
|
|
|
|
|
var resp *control.ListShardsResponse
|
|
|
|
var err error
|
|
|
|
err = cli.ExecRaw(func(client *rawclient.Client) error {
|
|
|
|
resp, err = control.ListShards(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())
|
|
|
|
|
2023-09-18 07:05:17 +00:00
|
|
|
shards := resp.GetBody().GetShards()
|
|
|
|
sortShardsByID(shards)
|
|
|
|
|
2022-06-23 14:21:43 +00:00
|
|
|
isJSON, _ := cmd.Flags().GetBool(commonflags.JSON)
|
|
|
|
if isJSON {
|
2023-09-18 07:05:17 +00:00
|
|
|
prettyPrintShardsJSON(cmd, shards)
|
2022-06-23 14:21:43 +00:00
|
|
|
} else {
|
2023-09-18 07:05:17 +00:00
|
|
|
prettyPrintShards(cmd, shards)
|
2022-06-23 14:21:43 +00:00
|
|
|
}
|
2022-05-23 16:26:27 +00:00
|
|
|
}
|
|
|
|
|
2024-08-29 12:45:33 +00:00
|
|
|
func prettyPrintShardsJSON(cmd *cobra.Command, ii []control.ShardInfo) {
|
2023-02-21 11:42:45 +00:00
|
|
|
out := make([]map[string]any, 0, len(ii))
|
2022-05-23 16:26:27 +00:00
|
|
|
for _, i := range ii {
|
2023-02-21 11:42:45 +00:00
|
|
|
out = append(out, map[string]any{
|
2023-12-11 10:18:34 +00:00
|
|
|
"shard_id": base58.Encode(i.GetShard_ID()),
|
2022-06-23 14:21:43 +00:00
|
|
|
"mode": shardModeToString(i.GetMode()),
|
|
|
|
"metabase": i.GetMetabasePath(),
|
2022-10-05 12:41:36 +00:00
|
|
|
"blobstor": i.GetBlobstor(),
|
2022-06-23 14:21:43 +00:00
|
|
|
"writecache": i.GetWritecachePath(),
|
2024-02-19 12:53:38 +00:00
|
|
|
"pilorama": i.GetPiloramaPath(),
|
2022-06-23 14:21:43 +00:00
|
|
|
"error_count": i.GetErrorCount(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
enc := json.NewEncoder(buf)
|
|
|
|
enc.SetIndent("", " ")
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "cannot shard info to JSON: %w", enc.Encode(out))
|
2022-06-23 14:21:43 +00:00
|
|
|
|
2024-07-01 15:58:37 +00:00
|
|
|
cmd.Print(buf.String()) // pretty printer emits newline, so no need for Println
|
2022-06-23 14:21:43 +00:00
|
|
|
}
|
2022-05-23 16:26:27 +00:00
|
|
|
|
2024-08-29 12:45:33 +00:00
|
|
|
func prettyPrintShards(cmd *cobra.Command, ii []control.ShardInfo) {
|
2022-06-23 14:21:43 +00:00
|
|
|
for _, i := range ii {
|
2022-05-23 16:26:27 +00:00
|
|
|
pathPrinter := func(name, path string) string {
|
|
|
|
if path == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s: %s\n", name, path)
|
|
|
|
}
|
|
|
|
|
2022-10-05 12:41:36 +00:00
|
|
|
var sb strings.Builder
|
|
|
|
sb.WriteString("Blobstor:\n")
|
|
|
|
for j, info := range i.GetBlobstor() {
|
|
|
|
sb.WriteString(fmt.Sprintf("\tPath %d: %s\n\tType %d: %s\n",
|
|
|
|
j, info.GetPath(), j, info.GetType()))
|
|
|
|
}
|
|
|
|
|
2022-05-23 16:26:27 +00:00
|
|
|
cmd.Printf("Shard %s:\nMode: %s\n"+
|
|
|
|
pathPrinter("Metabase", i.GetMetabasePath())+
|
2022-10-05 12:41:36 +00:00
|
|
|
sb.String()+
|
2022-05-23 16:26:27 +00:00
|
|
|
pathPrinter("Write-cache", i.GetWritecachePath())+
|
2022-05-16 13:23:09 +00:00
|
|
|
pathPrinter("Pilorama", i.GetPiloramaPath())+
|
2022-05-23 16:26:27 +00:00
|
|
|
fmt.Sprintf("Error count: %d\n", i.GetErrorCount()),
|
2023-12-11 10:18:34 +00:00
|
|
|
base58.Encode(i.GetShard_ID()),
|
2022-06-23 14:21:43 +00:00
|
|
|
shardModeToString(i.GetMode()),
|
2022-05-23 16:26:27 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-06-23 14:21:43 +00:00
|
|
|
|
|
|
|
func shardModeToString(m control.ShardMode) string {
|
2022-10-15 07:57:06 +00:00
|
|
|
strMode, ok := lookUpShardModeString(m)
|
|
|
|
if ok {
|
|
|
|
return strMode
|
2022-06-23 14:21:43 +00:00
|
|
|
}
|
2022-10-14 08:36:54 +00:00
|
|
|
|
|
|
|
return "unknown"
|
2022-06-23 14:21:43 +00:00
|
|
|
}
|
2023-09-18 07:05:17 +00:00
|
|
|
|
2024-08-29 12:45:33 +00:00
|
|
|
func sortShardsByID(ii []control.ShardInfo) {
|
2023-09-18 07:05:17 +00:00
|
|
|
sort.Slice(ii, func(i, j int) bool {
|
2023-12-11 10:18:34 +00:00
|
|
|
return bytes.Compare(ii[i].GetShard_ID(), ii[j].GetShard_ID()) < 0
|
2023-09-18 07:05:17 +00:00
|
|
|
})
|
|
|
|
}
|