Skip to content

Commit

Permalink
feature/curve-bs-check-chunkserver
Browse files Browse the repository at this point in the history
Signed-off-by: lng2020 <[email protected]>
  • Loading branch information
lng2020 committed Jul 31, 2023
1 parent 7d10847 commit 9325b11
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 2 deletions.
23 changes: 21 additions & 2 deletions tools-v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ A tool for CurveFS & CurveBs.
- [create dir](#create-dir)
- [check](#check-1)
- [check copyset](#check-copyset-1)
- [check chunkserver](#check-chunkserver)
- [check server](#check-server)
- [snapshot](#snapshot)
- [snapshot copyset](#snapshot-copyset)
Expand Down Expand Up @@ -1659,6 +1660,25 @@ Output:
| 4294967297 | 1 | 1 | ok | 0 | |
+------------+-----------+--------+--------+--------+---------+
```
#### check chunkserver

check chunkserver health in curvebs

Usage:

```shell
curve bs check chunkserver --chunkserverid 1
```

Output:

```shell
+------------+-----------+--------+--------+--------+---------+
| CHUNKSERVERID | HELATHYCOUNT | UNHEALTHYCOUNT | UNHEALTHYRATIO |
+------------+-----------+--------+--------+--------+---------+
| 1 | 100 | 0 | 0.00% |
+------------+-----------+--------+--------+--------+---------+
```

#### check server

Expand All @@ -1681,7 +1701,6 @@ Output:
+--------+-----------+-------+------------------+
```


### snapshot

#### snapshot copyset
Expand Down Expand Up @@ -1782,8 +1801,8 @@ Output:
| curve_ops_tool list-may-broken-vol | curve bs list may-broken-vol |
| curve_ops_tool rapid-leader-schedule | curve bs update leader-schedule |
| curve_ops_tool do-snapshot-all | curve bs snapshot --all |
| curve_ops_tool check-chunkserver | curbe bs check chunkserver |
| curve_ops_tool status | |
| curve_ops_tool check-consistency | |
| curve_ops_tool check-chunkserver | |
| curve_ops_tool check-server | curve bs check server |

3 changes: 3 additions & 0 deletions tools-v2/internal/utils/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ const (
ROW_ZONE = "zone"
ROW_AVAILFLAG = "availFlag"
ROW_DRYRUN = "dryrun"
ROW_HEALTHY_COUNT = "healthyCount"
ROW_UNHEALTHY_COUNT = "unhealthyCount"
ROW_UNHEALTHY_RATIO = "unhealthyRatio"

ROW_RW_STATUS = "rwStatus"
ROW_DISK_STATE = "diskState"
Expand Down
2 changes: 2 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package check

import (
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/check/chunkserver"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/check/copyset"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/check/operator"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/check/server"
Expand All @@ -41,6 +42,7 @@ func (checkCmd *CheckCommand) AddSubCommands() {
copyset.NewCopysetCommand(),
operator.NewOperatorCommand(),
server.NewServerCommand(),
chunkserver.NewChunkserverCommand(),
)
}

Expand Down
147 changes: 147 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/check/chunkserver/chunkserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* 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: CurveCli
* Created Date: 2023-07-19
* Author: lng2020
*/

package chunkserver

import (
"fmt"
"strconv"
"strings"

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/check/copyset"
listchunkserver "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/chunkserver"
"github.com/opencurve/curve/tools-v2/pkg/config"
"github.com/opencurve/curve/tools-v2/pkg/output"
"github.com/spf13/cobra"
)

const (
chunkserverExample = `$ curve bs check chunkserver --chunkserverid=1`
)

type ChunkserverCommand struct {
basecmd.FinalCurveCmd
chunkserverid uint32
}

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

func NewChunkserverCommand() *cobra.Command {
return NewCheckChunkserverCommand().Cmd
}

func NewCheckChunkserverCommand() *ChunkserverCommand {
chunkserverCmd := &ChunkserverCommand{
FinalCurveCmd: basecmd.FinalCurveCmd{
Use: "chunkserver",
Short: "check chunkserver health in curvebs",
Example: chunkserverExample,
},
}
basecmd.NewFinalCurveCli(&chunkserverCmd.FinalCurveCmd, chunkserverCmd)
return chunkserverCmd
}

func (cCmd *ChunkserverCommand) AddFlags() {
config.AddBsMdsFlagOption(cCmd.Cmd)
config.AddRpcRetryTimesFlag(cCmd.Cmd)
config.AddRpcTimeoutFlag(cCmd.Cmd)
config.AddBsUserOptionFlag(cCmd.Cmd)
config.AddBsPasswordOptionFlag(cCmd.Cmd)

config.AddBsMarginOptionFlag(cCmd.Cmd)
config.AddBsChunkServerIdFlag(cCmd.Cmd)
}

func (cCmd *ChunkserverCommand) Init(cmd *cobra.Command, args []string) error {
strid, e := strconv.Atoi(config.GetBsFlagString(cCmd.Cmd, config.CURVEBS_CHUNKSERVER_ID))
if e != nil {
return e
}
cCmd.chunkserverid = uint32(strid)
header := []string{cobrautil.ROW_CHUNKSERVER, cobrautil.ROW_HEALTHY_COUNT, cobrautil.ROW_UNHEALTHY_COUNT, cobrautil.ROW_UNHEALTHY_RATIO}
cCmd.SetHeader(header)
return nil
}

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

func (cCmd *ChunkserverCommand) RunCommand(cmd *cobra.Command, args []string) error {
row := make(map[string]string, 0)
var err *cmderror.CmdError
healthyCount := 0
unhealthyCount := 0
// get copysetid and logicalpoolid

var copysetidList []string
var logicalpoolidList []string
resp, err := listchunkserver.GetCopySetsInChunkServer(cCmd.Cmd)
if err.TypeCode() != cmderror.CODE_SUCCESS {
cCmd.Error = err
cCmd.Result = cobrautil.ROW_VALUE_FAILED
return err.ToError()
}
for _, copyset := range resp {
copysetidList = append(copysetidList, strconv.FormatUint(uint64(copyset.GetCopysetId()), 10))
logicalpoolidList = append(logicalpoolidList, strconv.FormatUint(uint64(copyset.GetLogicalPoolId()), 10))
}

// check copyset
config.AddBsCopysetIdSliceRequiredFlag(cCmd.Cmd)
config.AddBsLogicalPoolIdSliceRequiredFlag(cCmd.Cmd)
cCmd.Cmd.ParseFlags([]string{
fmt.Sprintf("--%s", config.CURVEBS_COPYSET_ID), strings.Join(copysetidList, ","),
fmt.Sprintf("--%s", config.CURVEBS_LOGIC_POOL_ID), strings.Join(logicalpoolidList, ","),
})
response, err := copyset.CheckCopysets(cCmd.Cmd)
if err.TypeCode() != cmderror.CODE_SUCCESS {
cCmd.Error = err
cCmd.Result = cobrautil.ROW_VALUE_FAILED
return err.ToError()
}
for _, health := range response {
if health == cobrautil.HEALTH_OK {
healthyCount++
} else {
unhealthyCount++
}
}
row[cobrautil.ROW_CHUNKSERVER] = fmt.Sprint(cCmd.chunkserverid)
row[cobrautil.ROW_HEALTHY_COUNT] = fmt.Sprint(healthyCount)
row[cobrautil.ROW_UNHEALTHY_COUNT] = fmt.Sprint(unhealthyCount)
row[cobrautil.ROW_UNHEALTHY_RATIO] = fmt.Sprintf("%.2f%%", float64(unhealthyCount)/float64(healthyCount+unhealthyCount)*100)

cCmd.Result = row
cCmd.Error = cmderror.ErrSuccess()
list := cobrautil.Map2List(row, []string{cobrautil.ROW_CHUNKSERVER})
cCmd.TableNew.Append(list)
return nil
}

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

0 comments on commit 9325b11

Please sign in to comment.