Skip to content

Commit

Permalink
[feat]tools-v2: add bs scan status
Browse files Browse the repository at this point in the history
Signed-off-by: Cyber-SiKu <[email protected]>
  • Loading branch information
Cyber-SiKu authored and SeanHai committed Apr 28, 2023
1 parent 5b36e44 commit a713551
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 25 deletions.
22 changes: 19 additions & 3 deletions tools-v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ A tool for CurveFS & CurveBs.
- [update](#update)
- [update peer](#update-peer)
- [update file](#update-file)
- [create](#create-1)
- [create file](#create-file)
- [update throttle](#update-throttle)
- [create dir](#create-dir)
- [clean-recycle](#clean-recycle)
- [Comparison of old and new commands](#comparison-of-old-and-new-commands)
Expand Down Expand Up @@ -1119,6 +1118,23 @@ Output:
+---------+
```

#### update throttle

update file throttle params

Usage:
```bash
curve bs update throttle --path /test1 --type=bps_total --limit 20000

Output:
```
+---------+
| RESULT |
+---------+
| success |
+---------+
```
### create
#### create file
Expand Down Expand Up @@ -1203,6 +1219,7 @@ Output:
| remove-peer | curve bs delete peer |
| reset-peer | curve bs update peer |
| space | curve bs list space |
| update-throttle | curve bs update throttle |
| status | |
| chunkserver-status | |
| client-status | |
Expand All @@ -1221,7 +1238,6 @@ Output:
| check-operator | |
| list-may-broken-vol | |
| set-copyset-availflag | |
| update-throttle | |
| rapid-leader-schedule | |
| set-scan-state | |
| scan-status | |
11 changes: 11 additions & 0 deletions tools-v2/internal/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,4 +768,15 @@ var (
}
return NewRpcReultCmdError(code, message)
}
ErrBsGetCopyset = func(statusCode statuscode.TopoStatusCode, logicalpoolid, copysetid uint32) *CmdError {
var message string
code := int(statusCode)
switch statusCode {
case statuscode.TopoStatusCode_Success:
message = "success"
default:
message = fmt.Sprintf("get copyset(id: %d,logicalPoolid: %d) info fail, err: %s", copysetid, logicalpoolid, statusCode.String())
}
return NewRpcReultCmdError(code, message)
}
)
12 changes: 12 additions & 0 deletions tools-v2/internal/utils/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,15 @@ func Addr2IpPort(addr string) (string, uint32, *cmderror.CmdError) {
}
return ipPort[0], uint32(u64Port), cmderror.Success()
}

func StringList2Uint64List(strList []string) ([]uint64, error) {
retList := make([]uint64, 0)
for _, str := range strList {
v, err := strconv.ParseUint(str, 10, 64)
if err != nil {
return nil, err
}
retList = append(retList, v)
}
return retList, nil
}
2 changes: 1 addition & 1 deletion tools-v2/pkg/cli/command/curvebs/query/chunk/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (cCmd *ChunkCommand) Init(cmd *cobra.Command, args []string) error {
return err.ToError()
}
config.AddBSLogicalPoolIdRequiredFlag(cCmd.Cmd)
config.AddBSCopysetIdSliceRequiredFlag(cCmd.Cmd)
config.AddBsCopysetIdSliceRequiredFlag(cCmd.Cmd)
cCmd.Cmd.ParseFlags([]string{
fmt.Sprintf("--%s", config.CURVEBS_LOGIC_POOL_ID),
fmt.Sprintf("%d", cCmd.LogicalpoolId),
Expand Down
32 changes: 13 additions & 19 deletions tools-v2/pkg/cli/command/curvebs/query/chunk/chunkserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ package chunk
import (
"context"
"fmt"
"strconv"

cmderror "github.com/opencurve/curve/tools-v2/internal/error"
cobrautil "github.com/opencurve/curve/tools-v2/internal/utils"
Expand Down Expand Up @@ -79,8 +78,8 @@ func (cCmd *ChunkServerListInCoysetCommand) AddFlags() {
config.AddBsMdsFlagOption(cCmd.Cmd)
config.AddRpcRetryTimesFlag(cCmd.Cmd)
config.AddRpcTimeoutFlag(cCmd.Cmd)
config.AddBSLogicalPoolIdSliceRequiredFlag(cCmd.Cmd)
config.AddBSCopysetIdSliceRequiredFlag(cCmd.Cmd)
config.AddBsLogicalPoolIdSliceRequiredFlag(cCmd.Cmd)
config.AddBsCopysetIdSliceRequiredFlag(cCmd.Cmd)
}

func (cCmd *ChunkServerListInCoysetCommand) Init(cmd *cobra.Command, args []string) error {
Expand All @@ -95,24 +94,19 @@ func (cCmd *ChunkServerListInCoysetCommand) Init(cmd *cobra.Command, args []stri
if len(logicalpoolidList) != len(copysetidList) {
return fmt.Errorf("logicalpoolidList and copysetidList length not equal")
}

logicalpoolIds := make([]uint32, len(logicalpoolidList))
copysetIds := make([]uint32, len(copysetidList))
logicalpoolIds, errParse := cobrautil.StringList2Uint64List(logicalpoolidList)
if errParse != nil {
return fmt.Errorf("Parse logicalpoolid", logicalpoolidList, " fail!")
}
copysetIds, errParse := cobrautil.StringList2Uint64List(copysetidList)
if errParse != nil {
return fmt.Errorf("Parse copysetid", copysetidList, " fail!")
}
logicalpool2copysets := make(map[uint32][]uint32)
for i := 0; i < len(logicalpoolidList); i++ {
id, err := strconv.ParseUint(logicalpoolidList[i], 10, 32)
if err != nil {
return fmt.Errorf("converting %s to uint32 err: %s", logicalpoolidList[i], err.Error())
}
logicalpoolIds[i] = uint32(id)

id, err = strconv.ParseUint(copysetidList[i], 10, 32)
if err != nil {
return fmt.Errorf("converting %s to uint32 err: %s", copysetidList[i], err.Error())
}
copysetIds[i] = uint32(id)

logicalpool2copysets[logicalpoolIds[i]] = append(logicalpool2copysets[logicalpoolIds[i]], copysetIds[i])
lpid := logicalpoolIds[i]
cpid := copysetIds[i]
logicalpool2copysets[uint32(lpid)] = append(logicalpool2copysets[uint32(lpid)], uint32(cpid))
}

for logicalpoolId, copysetIds := range logicalpool2copysets {
Expand Down
174 changes: 174 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/query/copyset/copyset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright (c) 2023 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: curve
* Created Date: 2023-04-26
* Author: chengyi01
*/

package copyset

import (
"context"
"fmt"

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/config"
"github.com/opencurve/curve/tools-v2/pkg/output"
"github.com/opencurve/curve/tools-v2/proto/proto/common"
"github.com/opencurve/curve/tools-v2/proto/proto/topology"
"github.com/opencurve/curve/tools-v2/proto/proto/topology/statuscode"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)

type GetCoysetRpc struct {
Info *basecmd.Rpc
Request *topology.GetCopysetRequest
mdsClient topology.TopologyServiceClient
}

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

func (gRpc *GetCoysetRpc) NewRpcClient(cc grpc.ClientConnInterface) {
gRpc.mdsClient = topology.NewTopologyServiceClient(cc)
}

func (gRpc *GetCoysetRpc) Stub_Func(ctx context.Context) (interface{}, error) {
return gRpc.mdsClient.GetCopyset(ctx, gRpc.Request)
}

type CopysetCommand struct {
CopysetInfoList []*common.CopysetInfo
Rpc []*GetCoysetRpc
basecmd.FinalCurveCmd
}

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

func NewQueryCopysetCommand() *CopysetCommand {
copysetCmd := &CopysetCommand{
FinalCurveCmd: basecmd.FinalCurveCmd{},
}

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

func NewCopysetCommand() *cobra.Command {
return NewQueryCopysetCommand().Cmd
}

func (cCmd *CopysetCommand) AddFlags() {
config.AddRpcTimeoutFlag(cCmd.Cmd)
config.AddRpcRetryTimesFlag(cCmd.Cmd)
config.AddBsMdsFlagOption(cCmd.Cmd)
config.AddBsCopysetIdSliceRequiredFlag(cCmd.Cmd)
config.AddBsLogicalPoolIdSliceRequiredFlag(cCmd.Cmd)
}

func (cCmd *CopysetCommand) Init(cmd *cobra.Command, args []string) error {
logicalpoolidList := config.GetBsFlagStringSlice(cCmd.Cmd, config.CURVEBS_LOGIC_POOL_ID)
copysetidList := config.GetBsFlagStringSlice(cCmd.Cmd, config.CURVEBS_COPYSET_ID)
if len(logicalpoolidList) != len(copysetidList) {
return fmt.Errorf("logicalpoolidList and copysetidList length not equal")
}
logicalpoolIds, errParse := cobrautil.StringList2Uint64List(logicalpoolidList)
if errParse != nil {
return fmt.Errorf("parse logicalpoolid%v fail", logicalpoolidList)
}
copysetIds, errParse := cobrautil.StringList2Uint64List(copysetidList)
if errParse != nil {
return fmt.Errorf("parse copysetid%v fail", copysetidList)
}
mdsAddrs, err := config.GetBsMdsAddrSlice(cCmd.Cmd)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}
timeout := config.GetFlagDuration(cCmd.Cmd, config.RPCTIMEOUT)
retrytimes := config.GetFlagInt32(cCmd.Cmd, config.RPCRETRYTIMES)
for i, logicalPool := range logicalpoolIds {
lpId := uint32(logicalPool)
cpId := uint32(copysetIds[i])
cCmd.Rpc = append(cCmd.Rpc, &GetCoysetRpc{
Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "GetCopyset"),
Request: &topology.GetCopysetRequest{
LogicalPoolId: &lpId,
CopysetId: &cpId,
},
})
}

return nil
}

func (cCmd *CopysetCommand) Print(cmd *cobra.Command, args []string) error {
return output.FinalCmdOutput(&cCmd.FinalCurveCmd, cCmd)
}

func (cCmd *CopysetCommand) RunCommand(cmd *cobra.Command, args []string) error {
var infos []*basecmd.Rpc
var funcs []basecmd.RpcFunc
for _, rpc := range cCmd.Rpc {
infos = append(infos, rpc.Info)
funcs = append(funcs, rpc)
}
results, errs := basecmd.GetRpcListResponse(infos, funcs)
if len(errs) == len(infos) {
mergeErr := cmderror.MergeCmdErrorExceptSuccess(errs)
return mergeErr.ToError()
}
for i, result := range results {
if result != nil {
continue
}
res := result.(*topology.GetCopysetResponse)
request := cCmd.Rpc[i].Request
err := cmderror.ErrBsGetCopyset(statuscode.TopoStatusCode(res.GetStatusCode()), request.GetLogicalPoolId(), request.GetCopysetId())
if err.TypeCode() != cmderror.CODE_SUCCESS {
errs = append(errs, err)
} else {
cCmd.CopysetInfoList = append(cCmd.CopysetInfoList, res.GetCopysetInfo())
}
}
cCmd.Result = cCmd.CopysetInfoList
cCmd.Error = cmderror.MergeCmdErrorExceptSuccess(errs)
return nil
}

func (cCmd *CopysetCommand) ResultPlainOutput() error {
return output.FinalCmdOutputPlain(&cCmd.FinalCurveCmd)
}

func GetCopyset(caller *cobra.Command) ([]*common.CopysetInfo, *cmderror.CmdError) {
getCmd := NewQueryCopysetCommand()
config.AlignFlagsValue(caller, getCmd.Cmd, []string{
config.RPCRETRYTIMES, config.RPCTIMEOUT, config.CURVEBS_MDSADDR,
config.CURVEBS_LOGIC_POOL_ID, config.CURVEBS_COPYSET_ID,
})
getCmd.Cmd.SilenceErrors = true
getCmd.Cmd.SilenceUsage = true
getCmd.Cmd.SetArgs([]string{"--format", config.FORMAT_NOOUT})
err := getCmd.Cmd.Execute()
if err != nil {
retErr := cmderror.ErrBsCreateFileOrDirectoryType()
retErr.Format(err.Error())
return getCmd.CopysetInfoList, retErr
}
return getCmd.CopysetInfoList, cmderror.Success()
}
4 changes: 2 additions & 2 deletions tools-v2/pkg/config/bs.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,11 @@ func AddBSCopysetIdRequiredFlag(cmd *cobra.Command) {
AddBsUint32RequiredFlag(cmd, CURVEBS_COPYSET_ID, "copyset id")
}

func AddBSCopysetIdSliceRequiredFlag(cmd *cobra.Command) {
func AddBsCopysetIdSliceRequiredFlag(cmd *cobra.Command) {
AddBsStringSliceRequiredFlag(cmd, CURVEBS_COPYSET_ID, "copyset ids")
}

func AddBSLogicalPoolIdSliceRequiredFlag(cmd *cobra.Command) {
func AddBsLogicalPoolIdSliceRequiredFlag(cmd *cobra.Command) {
AddBsStringSliceRequiredFlag(cmd, CURVEBS_LOGIC_POOL_ID, "logical pool id")
}

Expand Down

0 comments on commit a713551

Please sign in to comment.