Skip to content

Commit

Permalink
[feat] add bs update throttle
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 wuhongsong committed Apr 26, 2023
1 parent cf7911f commit b9b6bfd
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 12 deletions.
14 changes: 14 additions & 0 deletions tools-v2/internal/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,9 @@ var (
ErrBsListLogicalPoolInfo = func() *CmdError {
return NewInternalCmdError(49, "list logical pool info fail, the error is: %s")
}
ErrBsUnknownThrottleType = func() *CmdError {
return NewInternalCmdError(50, "unknown throttle type[%s], only support: iops_total|iops_read|iops_write|bps_total|bps_read|bps_write")
}

// http error
ErrHttpUnreadableResult = func() *CmdError {
Expand Down Expand Up @@ -751,4 +754,15 @@ var (
}
return NewRpcReultCmdError(code, message)
}
ErrUpdateFileThrottle = func(statusCode nameserver2.StatusCode, path string) *CmdError {
var message string
code := int(statusCode)
switch statusCode {
case nameserver2.StatusCode_kOK:
message = "successfully update the file throttle"
default:
message = fmt.Sprintf("failed to update file[%s] throttle, err: %s", path, statusCode.String())
}
return NewRpcReultCmdError(code, message)
}
)
23 changes: 22 additions & 1 deletion tools-v2/internal/utils/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func Topology2Map(topo *topology.ListTopologyResponse) (map[string]interface{},
}

const (
TYPE_DIR = "dir"
TYPE_DIR = "dir"
TYPE_FILE = "file"
)

Expand All @@ -211,3 +211,24 @@ func TranslateFileType(fileType string) (nameserver2.FileType, *cmderror.CmdErro
retErr.Format(fileType)
return nameserver2.FileType_INODE_DIRECTORY, retErr
}

const (
IOPS_TOTAL = "iops_total"
IOPS_READ = "iops_read"
IOPS_WRITE = "iops_write"
BPS_TOTAL = "bps_total"
BPS_READ = "bps_read"
BPS_WRITE = "bps_write"
)

func ParseThrottleType(typeStr string) (nameserver2.ThrottleType, *cmderror.CmdError) {
throttleType := nameserver2.ThrottleType_value[strings.ToUpper(typeStr)]
var retErr *cmderror.CmdError
if throttleType == 0 {
retErr = cmderror.ErrBsUnknownThrottleType()
retErr.Format(typeStr)
} else {
retErr = cmderror.ErrSuccess()
}
return nameserver2.ThrottleType(throttleType), retErr
}
8 changes: 4 additions & 4 deletions tools-v2/pkg/cli/command/curvebs/list/dir/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
)

const (
dirExample = `$ curve bs list dir --dir /`
dirExample = `$ curve bs list dir --path /`
)

type ListDirRpc struct {
Expand Down Expand Up @@ -90,7 +90,7 @@ func (pCmd *DirCommand) AddFlags() {
config.AddBsMdsFlagOption(pCmd.Cmd)
config.AddRpcRetryTimesFlag(pCmd.Cmd)
config.AddRpcTimeoutFlag(pCmd.Cmd)
config.AddBsDirOptionFlag(pCmd.Cmd)
config.AddBsPathOptionFlag(pCmd.Cmd)
config.AddBsUserOptionFlag(pCmd.Cmd)
config.AddBsPasswordOptionFlag(pCmd.Cmd)
}
Expand All @@ -104,7 +104,7 @@ func (pCmd *DirCommand) Init(cmd *cobra.Command, args []string) error {

timeout := config.GetFlagDuration(pCmd.Cmd, config.RPCTIMEOUT)
retrytimes := config.GetFlagInt32(pCmd.Cmd, config.RPCRETRYTIMES)
fileName := config.GetBsFlagString(pCmd.Cmd, config.CURVEBS_DIR)
fileName := config.GetBsFlagString(pCmd.Cmd, config.CURVEBS_PATH)
owner := config.GetBsFlagString(pCmd.Cmd, config.CURVEBS_USER)
date, errDat := cobrautil.GetTimeofDayUs()
if errDat.TypeCode() != cmderror.CODE_SUCCESS {
Expand Down Expand Up @@ -168,7 +168,7 @@ func (pCmd *DirCommand) RunCommand(cmd *cobra.Command, args []string) error {
infos := res.(*nameserver2.ListDirResponse).GetFileInfo()
for _, info := range infos {
row := make(map[string]string)
dirName := config.GetBsFlagString(pCmd.Cmd, config.CURVEBS_DIR)
dirName := config.GetBsFlagString(pCmd.Cmd, config.CURVEBS_PATH)
var fileName string
if dirName == "/" {
fileName = dirName + info.GetFileName()
Expand Down
168 changes: 168 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/update/throttle/throttle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* 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-24
* Author: chengyi01
*/

package throttle

import (
"context"

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/nameserver2"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
)

const (
throttleExample = `$ curve bs update throttle --path /test --throttleType iops_total|iops_read|iops_write|bps_total|bps_read|bps_write --limit 20000 [--burst 30000] [--burstlength 10]`
)

type UpdateFileThrottleRpc struct {
Info *basecmd.Rpc
Request *nameserver2.UpdateFileThrottleParamsRequest
mdsClient nameserver2.CurveFSServiceClient
}

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

func (uRpc *UpdateFileThrottleRpc) NewRpcClient(cc grpc.ClientConnInterface) {
uRpc.mdsClient = nameserver2.NewCurveFSServiceClient(cc)
}

func (uRpc *UpdateFileThrottleRpc) Stub_Func(ctx context.Context) (interface{}, error) {
return uRpc.mdsClient.UpdateFileThrottleParams(ctx, uRpc.Request)
}

type ThrottleCommand struct {
basecmd.FinalCurveCmd
Rpc *UpdateFileThrottleRpc
Response *nameserver2.UpdateFileThrottleParamsResponse
}

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

func NewUpdateThrottleCommand() *ThrottleCommand {
throttleCmd := &ThrottleCommand{
FinalCurveCmd: basecmd.FinalCurveCmd{
Use: "throttle",
Short: "update file throttle params",
Example: throttleExample,
},
}
basecmd.NewFinalCurveCli(&throttleCmd.FinalCurveCmd, throttleCmd)
return throttleCmd
}

func NewThrottleCommand() *cobra.Command {
return NewUpdateThrottleCommand().Cmd
}

func (tCmd *ThrottleCommand) AddFlags() {
config.AddRpcRetryTimesFlag(tCmd.Cmd)
config.AddRpcTimeoutFlag(tCmd.Cmd)
config.AddBsMdsFlagOption(tCmd.Cmd)
config.AddBsPathRequiredFlag(tCmd.Cmd)
config.AddBsThrottleTypeRequiredFlag(tCmd.Cmd)
config.AddBsUserOptionFlag(tCmd.Cmd)
config.AddBsPasswordOptionFlag(tCmd.Cmd)
config.AddBsLimitRequiredFlag(tCmd.Cmd)
config.AddBsBurstOptionFlag(tCmd.Cmd)
config.AddBsBurstLengthOptionFlag(tCmd.Cmd)
}

func (tCmd *ThrottleCommand) Init(cmd *cobra.Command, args []string) error {
path := config.GetBsFlagString(cmd, config.CURVEBS_PATH)
throttleTypeStr := config.GetBsFlagString(cmd, config.CURVEBS_TYPE)
throttleType, err := cobrautil.ParseThrottleType(throttleTypeStr)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}
limit := config.GetBsFlagUint64(cmd, config.CURVEBS_LIMIT)
params := &nameserver2.ThrottleParams{
Type: &throttleType,
Limit: &limit,
}
if config.GetFlagChanged(cmd, config.CURVEBS_BURST) {
burst := config.GetBsFlagUint64(cmd, config.CURVEBS_BURST)
burstLength := config.GetBsFlagUint64(cmd, config.CURVEBS_BURST_LENGTH)
if burstLength == 0 {
burstLength = 1
}
params.Burst = &burst
params.BurstLength = &burstLength
}
date, errDat := cobrautil.GetTimeofDayUs()
owner := config.GetBsFlagString(tCmd.Cmd, config.CURVEBS_USER)
if errDat.TypeCode() != cmderror.CODE_SUCCESS {
return errDat.ToError()
}
request := &nameserver2.UpdateFileThrottleParamsRequest{
FileName: &path,
Owner: &owner,
Date: &date,
ThrottleParams: params,
}
password := config.GetBsFlagString(tCmd.Cmd, config.CURVEBS_PASSWORD)
if owner == viper.GetString(config.VIPER_CURVEBS_USER) && len(password) != 0 {
strSig := cobrautil.GetString2Signature(date, owner)
sig := cobrautil.CalcString2Signature(strSig, password)
request.Signature = &sig
}
mdsAddrs, errMds := config.GetBsMdsAddrSlice(tCmd.Cmd)
if errMds.TypeCode() != cmderror.CODE_SUCCESS {
return errMds.ToError()
}
timeout := config.GetFlagDuration(tCmd.Cmd, config.RPCTIMEOUT)
retrytimes := config.GetFlagInt32(tCmd.Cmd, config.RPCRETRYTIMES)
tCmd.Rpc = &UpdateFileThrottleRpc{
Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "UpdateFileThrottle"),
Request: request,
}
tCmd.SetHeader([]string{cobrautil.ROW_RESULT})
return nil
}

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

func (tCmd *ThrottleCommand) RunCommand(cmd *cobra.Command, args []string) error {
result, err := basecmd.GetRpcResponse(tCmd.Rpc.Info, tCmd.Rpc)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}
tCmd.Response = result.(*nameserver2.UpdateFileThrottleParamsResponse)
tCmd.Result = tCmd.Response
tCmd.Error = cmderror.ErrUpdateFileThrottle(tCmd.Response.GetStatusCode(), tCmd.Rpc.Request.GetFileName())
tCmd.TableNew.Append([]string{tCmd.Error.Message})
if tCmd.Error.TypeCode() != cmderror.CODE_SUCCESS {
return tCmd.Error.ToError()
}
return nil
}

func (tCmd *ThrottleCommand) ResultPlainOutput() error {
return output.FinalCmdOutputPlain(&tCmd.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 @@ -28,6 +28,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/peer"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/throttle"
)

type UpdateCommand struct {
Expand All @@ -40,6 +41,7 @@ func (updateCmd *UpdateCommand) AddSubCommands() {
updateCmd.Cmd.AddCommand(
peer.NewPeerCommand(),
file.NewFileCommand(),
throttle.NewThrottleCommand(),
)
}

Expand Down
Loading

0 comments on commit b9b6bfd

Please sign in to comment.