Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [curve/toos-v2] add list chunkserver #2386

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion tools-v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ A tool for CurveFS & CurveBs.
- [list client](#list-client)
- [list dir](#list-dir)
- [list space](#list-space)
- [list chunkserver](#list-chunkserver)
- [clean-recycle](#clean-recycle)
- [query](#query-1)
- [query file](#query-file)
Expand Down Expand Up @@ -922,6 +923,28 @@ Output:
+----------+---------+---------+---------+------------+---------+
```

##### list chunkserver

list chunkserver information in curvebs

```bash
curve bs list chunkserver
Sindweller marked this conversation as resolved.
Show resolved Hide resolved
```

Output:

```bash
+----+------+-----------+------+-----------+------------+------------+-----------------------------------------------+--------------+-------------+------------------+-----------+
| ID | TYPE | IP | PORT | RWSTATUS | DISKSTATE | COPYSETNUM | MOUNTPOINT | DISKCAPACITY | DISKUSED | UNHEALTHYCOPYSET | EXTADDR |
+----+------+-----------+------+-----------+------------+------------+-----------------------------------------------+--------------+-------------+------------------+-----------+
| 1 | nvme | 127.0.0.1 | 8201 | READWRITE | DISKNORMAL | 100 | local:///curvebs/playground/chunkserver1/data | 39 GiB | 42140479488 | 0 % | 127.0.0.1 |
+----+ + +------+ + +------------+-----------------------------------------------+--------------+-------------+------------------+ +
| 2 | | | 8202 | | | 100 | local:///curvebs/playground/chunkserver2/data | 39 GiB | 42140479488 | 0 % | |
+----+ + +------+ + +------------+-----------------------------------------------+--------------+-------------+------------------+ +
| 3 | | | 8200 | | | 100 | local:///curvebs/playground/chunkserver0/data | 39 GiB | 42140479488 | 0 % | |
+----+------+-----------+------+-----------+------------+------------+-----------------------------------------------+--------------+-------------+------------------+-----------+
```

### clean-recycle

clean the recycle bin
Expand Down Expand Up @@ -1389,7 +1412,7 @@ Output:
| curve_ops_tool chunkserver-status | curve bs status chunkserver |
| curve_ops_tool status | |
| curve_ops_tool copysets-status | |
| curve_ops_tool chunkserver-list | |
| curve_ops_tool chunkserver-list | curve bs list chunkserver |
| curve_ops_tool clean-recycle | |
| curve_ops_tool check-consistency | |
| curve_ops_tool do-snapshot-all | |
Expand Down
3 changes: 3 additions & 0 deletions tools-v2/internal/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@ var (
ErrBsQueryChunkServerRecoverStatus = func() *CmdError {
return NewInternalCmdError(60, "query chunkserver recover status fail, err: %s")
}
ErrBsListChunkServer = func() *CmdError {
return NewInternalCmdError(54, "list chunkserver fail, err: %s")
}

// http error
ErrHttpUnreadableResult = func() *CmdError {
Expand Down
8 changes: 8 additions & 0 deletions tools-v2/internal/utils/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ const (
ROW_VERSION = "version"
ROW_ZONE = "zone"

ROW_RW_STATUS = "rwStatus"
ROW_DISK_STATE = "diskState"
ROW_COPYSET_NUM = "copysetNum"
ROW_DISK_CAPACITY = "diskCapacity"
ROW_DISK_USED = "diskUsed"
ROW_UNHEALTHY_COPYSET = "unhealthyCopyset"
ROW_EXT_ADDR = "extAddr"

// s3
ROW_S3CHUNKINFO_CHUNKID = "s3ChunkId"
ROW_S3CHUNKINFO_LENGTH = "s3Length"
Expand Down
269 changes: 269 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/list/chunkserver/chunkserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
/*
* Copyright (c) 2022 NetEase Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* Project: CurveCli
* Created Date: 2023-03-27
* Author: Sindweller
*/

package chunkserver

import (
"context"
"fmt"
"github.com/dustin/go-humanize"
cmderror "github.com/opencurve/curve/tools-v2/internal/error"
cobrautil "github.com/opencurve/curve/tools-v2/internal/utils"
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/server"
"github.com/opencurve/curve/tools-v2/pkg/config"
"github.com/opencurve/curve/tools-v2/pkg/output"
"github.com/opencurve/curve/tools-v2/proto/proto/topology"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"strconv"
)

const (
chunkServerExample = `$ curve bs list chunkserver`
)

type ListChunkServerRpc struct {
Info *basecmd.Rpc
Request *topology.ListChunkServerRequest
topologyServiceClient topology.TopologyServiceClient
}

var _ basecmd.RpcFunc = (*ListChunkServerRpc)(nil) // check interface

type ChunkServerCommand struct {
basecmd.FinalCurveCmd
Rpc []*ListChunkServerRpc
Response []*topology.ListChunkServerResponse
ChunkServerInfos []*topology.ChunkServerInfo
}

var _ basecmd.FinalCurveCmdFunc = (*ChunkServerCommand)(nil) // check interface

func (lRpc *ListChunkServerRpc) NewRpcClient(cc grpc.ClientConnInterface) {
lRpc.topologyServiceClient = topology.NewTopologyServiceClient(cc)
}

func (lRpc *ListChunkServerRpc) Stub_Func(ctx context.Context) (interface{}, error) {
return lRpc.topologyServiceClient.ListChunkServer(ctx, lRpc.Request)
}

func NewChunkServerCommand() *cobra.Command {
return NewListChunkServerCommand().Cmd
}

func NewListChunkServerCommand() *ChunkServerCommand {
lsCmd := &ChunkServerCommand{
FinalCurveCmd: basecmd.FinalCurveCmd{
Use: "chunkserver",
Short: "list chunk server in curvebs",
Example: chunkServerExample,
},
}

basecmd.NewFinalCurveCli(&lsCmd.FinalCurveCmd, lsCmd)
return lsCmd
}

// AddFlags implements basecmd.FinalCurveCmdFunc
func (pCmd *ChunkServerCommand) AddFlags() {
config.AddBsMdsFlagOption(pCmd.Cmd)
config.AddRpcRetryTimesFlag(pCmd.Cmd)
config.AddRpcTimeoutFlag(pCmd.Cmd)
config.AddBsUserOptionFlag(pCmd.Cmd)
config.AddBsPasswordOptionFlag(pCmd.Cmd)
config.AddBSCheckCSAliveOptionFlag(pCmd.Cmd)
config.AddBSCheckHealthOptionFlag(pCmd.Cmd)
config.AddBSCSOfflineOptionFlag(pCmd.Cmd)
config.AddBSCSUnhealthyOptionFlag(pCmd.Cmd)
}

// Init implements basecmd.FinalCurveCmdFunc
func (pCmd *ChunkServerCommand) Init(cmd *cobra.Command, args []string) error {
mdsAddrs, err := config.GetBsMdsAddrSlice(pCmd.Cmd)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}

timeout := config.GetFlagDuration(pCmd.Cmd, config.RPCTIMEOUT)
retrytimes := config.GetFlagInt32(pCmd.Cmd, config.RPCRETRYTIMES)
servers, err := server.ListServer(pCmd.Cmd)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}
for _, server := range servers {
id := server.GetServerID()
rpc := &ListChunkServerRpc{
Request: &topology.ListChunkServerRequest{
ServerID: &id,
},
Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "ListChunkServer"),
}
pCmd.Rpc = append(pCmd.Rpc, rpc)
}

header := []string{
cobrautil.ROW_ID,
cobrautil.ROW_TYPE,
cobrautil.ROW_IP,
cobrautil.ROW_PORT,
cobrautil.ROW_RW_STATUS,
cobrautil.ROW_DISK_STATE,
cobrautil.ROW_COPYSET_NUM,
cobrautil.ROW_MOUNTPOINT,
cobrautil.ROW_DISK_CAPACITY,
cobrautil.ROW_DISK_USED,
cobrautil.ROW_UNHEALTHY_COPYSET,
cobrautil.ROW_EXT_ADDR,
}
pCmd.SetHeader(header)
pCmd.TableNew.SetAutoMergeCellsByColumnIndex(cobrautil.GetIndexSlice(
pCmd.Header, []string{cobrautil.ROW_IP, cobrautil.ROW_ID, cobrautil.ROW_RW_STATUS, cobrautil.ROW_TYPE, cobrautil.ROW_DISK_STATE, cobrautil.ROW_EXT_ADDR},
))
return nil
}

// Print implements basecmd.FinalCurveCmdFunc
func (pCmd *ChunkServerCommand) Print(cmd *cobra.Command, args []string) error {
return output.FinalCmdOutput(&pCmd.FinalCurveCmd, pCmd)
}

// RunCommand implements basecmd.FinalCurveCmdFunc
func (pCmd *ChunkServerCommand) RunCommand(cmd *cobra.Command, args []string) error {
var rpcs []*basecmd.Rpc
var funcs []basecmd.RpcFunc
for _, rpc := range pCmd.Rpc {
rpcs = append(rpcs, rpc.Info)
funcs = append(funcs, rpc)
}
results, errs := basecmd.GetRpcListResponse(rpcs, funcs)
if len(errs) == len(rpcs) {
mergeErr := cmderror.MergeCmdErrorExceptSuccess(errs)
return mergeErr.ToError()
}
var errors []*cmderror.CmdError
rows := make([]map[string]string, 0)

// counts
total := 0
online := 0
offline := 0
unstable := 0
pendding := 0
retired := 0
penddingCopyset := 0

for _, res := range results {
if res == nil {
continue
}
infos := res.(*topology.ListChunkServerResponse).GetChunkServerInfos()
for _, info := range infos {
csID := info.GetChunkServerID()
// get copyset info
copysetCmd := NewGetCopySetsInCopySetCommand()
config.AlignFlagsValue(pCmd.Cmd, copysetCmd.Cmd, []string{
config.RPCRETRYTIMES, config.RPCTIMEOUT, config.CURVEBS_MDSADDR,
})
copysetCmd.Cmd.Flags().Set(config.CURVEBS_CHUNKSERVER_ID, strconv.FormatUint(uint64(csID), 10))
copysets, err := GetCopySetsInChunkServer(copysetCmd.Cmd)
if err.TypeCode() != cmderror.CODE_SUCCESS {
errors = append(errs, err)
continue
}
unhealthyRatio := 0.0

if info.GetOnlineState() != topology.OnlineState_ONLINE {
if info.GetOnlineState() == topology.OnlineState_OFFLINE {
offline++
}
if info.GetOnlineState() == topology.OnlineState_UNSTABLE {
unstable++
}
unhealthyRatio = 1.0
} else {
if config.GetFlagBool(pCmd.Cmd, config.CURVEBS_CS_OFFLINE) {
continue
}
online++
}

if info.GetStatus() == topology.ChunkServerStatus_PENDDING {
pendding++
penddingCopyset += len(copysets)
}
if info.GetStatus() == topology.ChunkServerStatus_RETIRED {
retired++
}
total++
unhealthyRatio *= 100.00
// generate row message
row := make(map[string]string)
row[cobrautil.ROW_ID] = fmt.Sprintf("%d", info.GetChunkServerID())
row[cobrautil.ROW_TYPE] = info.GetDiskType()
row[cobrautil.ROW_IP] = info.GetHostIp()
row[cobrautil.ROW_PORT] = fmt.Sprintf("%d", info.GetPort())
row[cobrautil.ROW_RW_STATUS] = fmt.Sprintf("%s", info.GetStatus())
row[cobrautil.ROW_DISK_STATE] = fmt.Sprintf("%s", info.GetDiskStatus())
row[cobrautil.ROW_COPYSET_NUM] = fmt.Sprintf("%d", len(copysets))
row[cobrautil.ROW_MOUNTPOINT] = info.GetMountPoint()
row[cobrautil.ROW_DISK_CAPACITY] = humanize.IBytes(info.GetDiskCapacity())
row[cobrautil.ROW_DISK_USED] = humanize.IBytes(info.GetDiskCapacity())
row[cobrautil.ROW_UNHEALTHY_COPYSET] = fmt.Sprintf("%.0f %%", unhealthyRatio)
row[cobrautil.ROW_EXT_ADDR] = info.GetExternalIp()
rows = append(rows, row)
pCmd.ChunkServerInfos = append(pCmd.ChunkServerInfos, info)
}
}

list := cobrautil.ListMap2ListSortByKeys(rows, pCmd.Header, []string{cobrautil.ROW_IP, cobrautil.ROW_ID, cobrautil.ROW_RW_STATUS, cobrautil.ROW_TYPE, cobrautil.ROW_DISK_STATE, cobrautil.ROW_EXT_ADDR})
pCmd.TableNew.AppendBulk(list)
if len(errors) != 0 {
mergeErr := cmderror.MergeCmdError(errors)
pCmd.Result, pCmd.Error = list, mergeErr
return mergeErr.ToError()
}
pCmd.Result, pCmd.Error = list, cmderror.Success()
return nil
}

// ResultPlainOutput implements basecmd.FinalCurveCmdFunc
func (pCmd *ChunkServerCommand) ResultPlainOutput() error {
return output.FinalCmdOutputPlain(&pCmd.FinalCurveCmd)
}

func ListChunkServerInfos(caller *cobra.Command) ([]*topology.ChunkServerInfo, *cmderror.CmdError) {
lCmd := NewListChunkServerCommand()
config.AlignFlagsValue(caller, lCmd.Cmd, []string{
config.RPCRETRYTIMES, config.RPCTIMEOUT, config.CURVEBS_MDSADDR,
})
lCmd.Cmd.SilenceErrors = true
lCmd.Cmd.SilenceUsage = true
lCmd.Cmd.SetArgs([]string{"--format", config.FORMAT_NOOUT})
err := lCmd.Cmd.Execute()
if err != nil {
retErr := cmderror.ErrBsListChunkServer()
retErr.Format(err.Error())
return nil, retErr
}
return lCmd.ChunkServerInfos, cmderror.ErrSuccess()
}
Loading