Skip to content

Commit

Permalink
[feat] tools-v2: add update leader-schedule
Browse files Browse the repository at this point in the history
Signed-off-by: montaguelhz <[email protected]>
  • Loading branch information
montaguelhz committed Jun 25, 2023
1 parent 83ee153 commit 25449e5
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 2 deletions.
23 changes: 22 additions & 1 deletion tools-v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ A tool for CurveFS & CurveBs.
- [update throttle](#update-throttle)
- [update scan-state](#update-scan-state)
- [update copyset availflag](#update-copyset-availflag)
- [update leader-schedule](#update-leader-schedule)
- [create](#create-1)
- [create file](#create-file)
- [create dir](#create-dir)
Expand Down Expand Up @@ -1579,6 +1580,25 @@ Output:
+--------+-----------+---------------+--------+
```

#### update leader-schedule

"rapidly transfer leader

Usage:
```bash
curve bs update leader-schedule --logicalpoolid 1
curve bs update leader-schedule --all
```

Output:
```
+---------+--------+
| RESULT | REASON |
+---------+--------+
| success | null |
+---------+--------+
```

### create

#### create file
Expand Down Expand Up @@ -1720,9 +1740,10 @@ Output:
| curve_ops_tool clean-recycle | curve bs clean-recycle |
| curve_ops_tool copysets-status | curve bs status copyset |
| 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 status | |
| curve_ops_tool check-consistency | |
| curve_ops_tool do-snapshot-all | |
| curve_ops_tool check-chunkserver | |
| curve_ops_tool check-server | |
| curve_ops_tool rapid-leader-schedule | |

30 changes: 30 additions & 0 deletions tools-v2/internal/proto/curvebs/schedule/statuscode.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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-06-14
* Author: montaguelhz
*/

syntax="proto2";
option go_package = "proto/schedule/statuscode";

enum ScheduleStatusCode {
ScheduleSuccess = 0;
InvalidLogicalPool = -1;
InvalidQueryChunkserverID = -2;
}
2 changes: 1 addition & 1 deletion tools-v2/internal/utils/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const (
ROW_S3CHUNKINFO_OFFSET = "s3Offset"
ROW_S3CHUNKINFO_SIZE = "s3Size"

// vale
// value
ROW_VALUE_ADD = "add"
ROW_VALUE_DEL = "del"
ROW_VALUE_DNE = "DNE"
Expand Down
2 changes: 2 additions & 0 deletions tools-v2/mk-proto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mkdir -p proto
# proto
protoc --go_out=proto --proto_path=internal/proto \
internal/proto/curvebs/topology/statuscode.proto
protoc --go_out=proto --proto_path=internal/proto \
internal/proto/curvebs/schedule/statuscode.proto
## curvebs
### proto/chunk.proto
protoc --go_out=proto --proto_path=.. \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* 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-06-14
* Author: montaguelhz
*/

package leader_schedule

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

type UpdateLeaderScheduleRpc struct {
Info *basecmd.Rpc
Request *schedule.RapidLeaderScheduleRequst
Client schedule.ScheduleServiceClient
}

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

func (uRpc *UpdateLeaderScheduleRpc) NewRpcClient(cc grpc.ClientConnInterface) {
uRpc.Client = schedule.NewScheduleServiceClient(cc)
}

func (uRpc *UpdateLeaderScheduleRpc) Stub_Func(ctx context.Context) (interface{}, error) {
return uRpc.Client.RapidLeaderSchedule(ctx, uRpc.Request)
}

type LeaderScheduleCommand struct {
basecmd.FinalCurveCmd

Rpc *UpdateLeaderScheduleRpc
}

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

const (
leaderScheduleExample = `$ curve bs update leader-schedule --logicalpoolid 1
$ curve bs update leader-schedule --all`
)

func NewLeaderScheduleCommand() *cobra.Command {
return NewUpdateLeaderScheduleCommand().Cmd
}

func (lCmd *LeaderScheduleCommand) AddFlags() {
config.AddBsMdsFlagOption(lCmd.Cmd)
config.AddRpcRetryTimesFlag(lCmd.Cmd)
config.AddRpcTimeoutFlag(lCmd.Cmd)

config.AddBsLogicalPoolIdOptionFlag(lCmd.Cmd)
config.AddBsAllOptionFlag(lCmd.Cmd)
}

func NewUpdateLeaderScheduleCommand() *LeaderScheduleCommand {
sCmd := &LeaderScheduleCommand{
FinalCurveCmd: basecmd.FinalCurveCmd{
Use: "leader-schedule",
Short: "rapidly transfer leader",
Example: leaderScheduleExample,
},
}

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

func (lCmd *LeaderScheduleCommand) Init(cmd *cobra.Command, args []string) error {
mdsAddrs, err := config.GetBsMdsAddrSlice(lCmd.Cmd)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}

header := []string{cobrautil.ROW_RESULT, cobrautil.ROW_REASON}
lCmd.SetHeader(header)

timeout := config.GetFlagDuration(lCmd.Cmd, config.RPCTIMEOUT)
retryTimes := config.GetFlagInt32(lCmd.Cmd, config.RPCRETRYTIMES)

// if flag all not changed, it must be false
all := config.GetBsFlagBool(lCmd.Cmd, config.CURVEBS_ALL)
islogicalPoolIDChanged := config.GetBsFlagChanged(lCmd.Cmd, config.CURVEBS_LOGIC_POOL_ID)
if !all && !islogicalPoolIDChanged {
return fmt.Errorf("all or logicalpoolid is required")
}

var logicalPoolID uint32
if all {
logicalPoolID = 0
} else {
logicalPoolID = config.GetBsFlagUint32(lCmd.Cmd, config.CURVEBS_LOGIC_POOL_ID)
if logicalPoolID == 0 {
return fmt.Errorf("please set flag all true instead of setting flag logicalpoolid 0")
}
}

lCmd.Rpc = &UpdateLeaderScheduleRpc{
Request: &schedule.RapidLeaderScheduleRequst{
LogicalPoolID: &logicalPoolID,
},
Info: basecmd.NewRpc(mdsAddrs, timeout, retryTimes, "RapidLeaderSchedule"),
}

return nil
}

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

func (lCmd *LeaderScheduleCommand) RunCommand(cmd *cobra.Command, args []string) error {
result, err := basecmd.GetRpcResponse(lCmd.Rpc.Info, lCmd.Rpc)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}
out := make(map[string]string)
if response, ok := result.(*schedule.RapidLeaderScheduleResponse); ok {
if response.GetStatusCode() != 0 {
out[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_FAILED
out[cobrautil.ROW_REASON] = statuscode.ScheduleStatusCode_name[*response.StatusCode]
} else {
out[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_SUCCESS
out[cobrautil.ROW_REASON] = cobrautil.ROW_VALUE_NULL
}
}

res := cobrautil.Map2List(out, lCmd.Header)
lCmd.TableNew.Append(res)

lCmd.Result = out
lCmd.Error = cmderror.ErrSuccess()
return nil
}

func (lCmd *LeaderScheduleCommand) ResultPlainOutput() error {
return output.FinalCmdOutputPlain(&lCmd.FinalCurveCmd)
}
2 changes: 2 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/file"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/leader"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/leader_schedule"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/peer"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/scan_state"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/throttle"
Expand All @@ -48,6 +49,7 @@ func (updateCmd *UpdateCommand) AddSubCommands() {
leader.NewleaderCommand(),
scan_state.NewScanStateCommand(),
copyset.NewCopysetCommand(),
leader_schedule.NewLeaderScheduleCommand(),
)
}

Expand Down
31 changes: 31 additions & 0 deletions tools-v2/pkg/config/bs.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ const (
CURVEBS_FIlTER = "filter"
VIPER_CURVEBS_FILTER = "curvebs.filter"
CURVEBS_DEFAULT_FILTER = false
CURVEBS_ALL = "all"
VIPER_CURVEBS_ALL = "curvebs.all"
CURVEBS_DEFAULT_ALL = false
)

var (
Expand Down Expand Up @@ -171,6 +174,7 @@ var (
CURVEBS_CHUNK_ID: VIPER_CURVEBS_CHUNK_ID,
CURVEBS_CHUNKSERVER_ADDRESS: VIPER_CURVEBS_CHUNKSERVER_ADDRESS,
CURVEBS_FIlTER: VIPER_CURVEBS_FILTER,
CURVEBS_ALL: VIPER_CURVEBS_ALL,
}

BSFLAG2DEFAULT = map[string]interface{}{
Expand All @@ -191,6 +195,7 @@ var (
CURVEBS_CHUNKSERVER_ID: CURVEBS_DEFAULT_CHUNKSERVER_ID,
CURVEBS_DRYRUN: CURVEBS_DEFAULT_DRYRUN,
CURVEBS_FIlTER: CURVEBS_DEFAULT_FILTER,
CURVEBS_ALL: CURVEBS_DEFAULT_ALL,
}
)

Expand Down Expand Up @@ -304,6 +309,18 @@ func AddBsDurationOptionFlag(cmd *cobra.Command, name string, usage string) {
}
}

func AddBsUint32OptionFlag(cmd *cobra.Command, name string, usage string) {
defaultValue := BSFLAG2DEFAULT[name]
if defaultValue == nil {
defaultValue = uint32(0)
}
cmd.Flags().Uint32(name, defaultValue.(uint32), usage)
err := viper.BindPFlag(BSFLAG2VIPER[name], cmd.Flags().Lookup(name))
if err != nil {
cobra.CheckErr(err)
}
}

func AddBsUint32SliceOptionFlag(cmd *cobra.Command, name string, usage string) {
defaultValue := BSFLAG2DEFAULT[name]
if defaultValue == nil {
Expand Down Expand Up @@ -462,6 +479,15 @@ func AddBsScanOptionFlag(cmd *cobra.Command) {
AddBsBoolOptionFlag(cmd, CURVEBS_SCAN, "enable/disable scan for logical pool")
}

// leader-schedule
func AddBsLogicalPoolIdOptionFlag(cmd *cobra.Command) {
AddBsUint32OptionFlag(cmd, CURVEBS_LOGIC_POOL_ID, "logical pool id")
}

func AddBsAllOptionFlag(cmd *cobra.Command) {
AddBsBoolOptionFlag(cmd, CURVEBS_ALL, "all")
}

// add flag required
// add path[required]
func AddBsPathRequiredFlag(cmd *cobra.Command) {
Expand Down Expand Up @@ -607,6 +633,11 @@ func GetBsFlagInt64(cmd *cobra.Command, flagName string) int64 {
return value
}

// determine whether the flag is changed
func GetBsFlagChanged(cmd *cobra.Command, flagName string) bool {
return cmd.Flag(flagName).Changed
}

// get mdsaddr
func GetBsAddrSlice(cmd *cobra.Command, addrType string) ([]string, *cmderror.CmdError) {
var addrsStr string
Expand Down

0 comments on commit 25449e5

Please sign in to comment.