Skip to content

Commit

Permalink
[feat]tools-v2: add check copyset
Browse files Browse the repository at this point in the history
Signed-off-by: baytan0720 <baytan2@hotmail.com>
  • Loading branch information
baytan0720 authored and Cyber-SiKu committed May 5, 2023
1 parent f442aae commit 202b270
Show file tree
Hide file tree
Showing 9 changed files with 671 additions and 3 deletions.
26 changes: 25 additions & 1 deletion tools-v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ A tool for CurveFS & CurveBs.
- [update throttle](#update-throttle)
- [create dir](#create-dir)
- [clean-recycle](#clean-recycle)
- [check](#check-1)
- [check copyset](#check-copyset)
- [Comparison of old and new commands](#comparison-of-old-and-new-commands)
- [curve fs](#curve-fs)
- [curve bs](#curve-bs)
Expand Down Expand Up @@ -1173,6 +1175,28 @@ Output:
+---------+
```

### check

#### check copyset

check copysets health in curvebs

Usage:

```shell
curve bs check copyset --copysetid 1 --logicalpoolid 1
```

Output:

```shell
+------------+-----------+--------+--------+--------+---------+
| COPYSETKEY | COPYSETID | POOLID | STATUS | LOGGAP | EXPLAIN |
+------------+-----------+--------+--------+--------+---------+
| 4294967297 | 1 | 1 | ok | 0 | |
+------------+-----------+--------+--------+--------+---------+
```

## Comparison of old and new commands

### curve fs
Expand Down Expand Up @@ -1220,6 +1244,7 @@ Output:
| reset-peer | curve bs update peer |
| space | curve bs list space |
| update-throttle | curve bs update throttle |
| check-copyset | curve bs check copyset |
| status | |
| chunkserver-status | |
| client-status | |
Expand All @@ -1233,7 +1258,6 @@ Output:
| do-snapshot | |
| do-snapshot-all | |
| check-chunkserver | |
| check-copyset | |
| check-server | |
| check-operator | |
| list-may-broken-vol | |
Expand Down
4 changes: 3 additions & 1 deletion tools-v2/internal/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,9 @@ var (
ErrBsListDir = func() *CmdError {
return NewInternalCmdError(51, "list directory fail, err: %s")
}

ErrBsGetCopysetStatus = func() *CmdError {
return NewInternalCmdError(52, "get copyset status fail, err: %s")
}
// http error
ErrHttpUnreadableResult = func() *CmdError {
return NewHttpResultCmdError(1, "http response is unreadable, the uri is: %s, the error is: %s")
Expand Down
46 changes: 46 additions & 0 deletions tools-v2/internal/utils/copyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,20 @@ import (
cmderror "github.com/opencurve/curve/tools-v2/internal/error"
"github.com/opencurve/curve/tools-v2/proto/curvefs/proto/copyset"
"github.com/opencurve/curve/tools-v2/proto/curvefs/proto/heartbeat"
bscopyset "github.com/opencurve/curve/tools-v2/proto/proto/copyset"
bsheartbeat "github.com/opencurve/curve/tools-v2/proto/proto/heartbeat"
)

type CopysetInfoStatus struct {
Info *heartbeat.CopySetInfo `json:"info,omitempty"`
Peer2Status map[string]*copyset.CopysetStatusResponse `json:"peer status,omitempty"`
}

type BsCopysetInfoStatus struct {
Info *bsheartbeat.CopySetInfo `json:"info,omitempty"`
Peer2Status map[string]*bscopyset.CopysetStatusResponse `json:"peer status,omitempty"`
}

type COPYSET_HEALTH_STATUS int32

const (
Expand Down Expand Up @@ -153,6 +160,45 @@ func CheckCopySetHealth(copysetIS *CopysetInfoStatus) (COPYSET_HEALTH_STATUS, []
}
}

func CheckBsCopySetHealth(copysetIS *BsCopysetInfoStatus) (COPYSET_HEALTH_STATUS, []*cmderror.CmdError) {
peers := copysetIS.Info.GetPeers()
peer2Status := copysetIS.Peer2Status
avalibalePeerNum := 0
var errs []*cmderror.CmdError
for addr, status := range peer2Status {
if status == nil {
// peer is offline
err := cmderror.ErrOfflineCopysetPeer()
err.Format(addr)
errs = append(errs, err)
continue
}
opStatus := status.GetStatus()
state := status.GetState()
peer := status.GetPeer()
if opStatus == bscopyset.COPYSET_OP_STATUS_COPYSET_OP_STATUS_SUCCESS && CopysetState_Avaliable[state] {
avalibalePeerNum++
} else if opStatus != bscopyset.COPYSET_OP_STATUS_COPYSET_OP_STATUS_SUCCESS {
err := cmderror.ErrBsCopysetOpStatus(opStatus, addr)
errs = append(errs, err)
} else {
err := cmderror.ErrStateCopysetPeer()
err.Format(peer.String(), CopysetState_name[state])
errs = append(errs, err)
}
}

n := len(peers)
switch {
case avalibalePeerNum == n:
return COPYSET_OK, errs
case avalibalePeerNum >= n/2+1:
return COPYSET_WARN, errs
default:
return COPYSET_ERROR, errs
}
}

func GetCopysetKey(poolid uint64, copysetid uint64) uint64 {
return (poolid << 32) | copysetid
}
Expand Down
12 changes: 12 additions & 0 deletions tools-v2/internal/utils/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,15 @@ func StringList2Uint64List(strList []string) ([]uint64, error) {
}
return retList, nil
}

func StringList2Uint32List(strList []string) ([]uint32, error) {
retList := make([]uint32, 0)
for _, str := range strList {
v, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return nil, err
}
retList = append(retList, uint32(v))
}
return retList, nil
}
2 changes: 2 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/bs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/spf13/cobra"

basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/check"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/clean_recycle"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/create"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/delete"
Expand All @@ -50,6 +51,7 @@ func (bsCmd *CurveBsCommand) AddSubCommands() {
create.NewCreateCmd(),
update.NewUpdateCommand(),
clean_recycle.NewCleanRecycleCommand(),
check.NewCheckCommand(),
)
}

Expand Down
51 changes: 51 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/check/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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-04-24
* Author: baytan0720
*/

package check

import (
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/check/copyset"
"github.com/spf13/cobra"
)

type CheckCommand struct {
basecmd.MidCurveCmd
}

var _ basecmd.MidCurveCmdFunc = (*CheckCommand)(nil) // check interface

func (checkCmd *CheckCommand) AddSubCommands() {
checkCmd.Cmd.AddCommand(
copyset.NewCopysetCommand(),
)
}

func NewCheckCommand() *cobra.Command {
checkCmd := &CheckCommand{
basecmd.MidCurveCmd{
Use: "check",
Short: "checkout the health of resources in curvebs",
},
}
return basecmd.NewMidCurveCli(&checkCmd.MidCurveCmd, checkCmd)
}
Loading

0 comments on commit 202b270

Please sign in to comment.