Skip to content

Commit

Permalink
seperate command
Browse files Browse the repository at this point in the history
Signed-off-by: pengpengSir <[email protected]>
  • Loading branch information
pengpengSir committed May 24, 2023
1 parent c607530 commit 8427f05
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 45 deletions.
12 changes: 6 additions & 6 deletions tools-v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1344,17 +1344,17 @@ Usage:

```shell
curve bs check server --serverid 1
curve bs check server --serverip 127.0.0.1 --port 8200
curve bs check server --ip 127.0.0.1 --port 8200
```

Output:

```shell
+--------+-----------+-------+-------------------------+-------------------------+
| SERVER | IP | TOTAL | UNHEALTHY COPYSET COUNT | UNHEALTHY COPYSET RATIO |
+--------+-----------+-------+-------------------------+-------------------------+
| 1 | 127.0.0.1 | 100 | 0 | 0 |
+--------+-----------+-------+-------------------------+-------------------------+
+--------+-----------+-------+------------------+
| SERVER | IP | TOTAL | UNHEALTHYCOPYSET |
+--------+-----------+-------+------------------+
| 1 | 127.0.0.1 | 100 | 0(0%) |
+--------+-----------+-------+------------------+
```


Expand Down
3 changes: 3 additions & 0 deletions tools-v2/internal/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ var (
ErrBsListChunkServer = func() *CmdError {
return NewInternalCmdError(54, "list chunkserver fail, err: %s")
}
ErrBsGetChunkServerInfos = func() *CmdError {
return NewInternalCmdError(55, "get chunkserver infos fail, err: %s")
}

// http error
ErrHttpUnreadableResult = func() *CmdError {
Expand Down
142 changes: 142 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/check/server/get_chunk_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* 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-05-07
* Author: pengpengSir
*/
package server

import (
"context"

cmderror "github.com/opencurve/curve/tools-v2/internal/error"
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/topology"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)

type GetChunkServerInfosRpc struct {
Info *basecmd.Rpc
Request *topology.ListChunkServerRequest
topologyClient topology.TopologyServiceClient
}

type GetChunkInfosCommand struct {
basecmd.FinalCurveCmd
Rpc *GetChunkServerInfosRpc
Response *topology.ListChunkServerResponse
}

var _ basecmd.FinalCurveCmdFunc = (*GetChunkInfosCommand)(nil)

func NewChunkInfosCommand() *cobra.Command {
return NewGetChunkServerInfosCommand().Cmd
}

func NewGetChunkServerInfosCommand() *GetChunkInfosCommand {
gCmd := &GetChunkInfosCommand{
FinalCurveCmd: basecmd.FinalCurveCmd{},
}
basecmd.NewFinalCurveCli(&gCmd.FinalCurveCmd, gCmd)
return gCmd
}

func (gcRpc *GetChunkServerInfosRpc) NewRpcClient(cc grpc.ClientConnInterface) {
gcRpc.topologyClient = topology.NewTopologyServiceClient(cc)
}

func (gcRpc *GetChunkServerInfosRpc) Stub_Func(ctx context.Context) (interface{}, error) {
return gcRpc.topologyClient.ListChunkServer(ctx, gcRpc.Request)
}

func (gCmd *GetChunkInfosCommand) AddFlags() {
config.AddBsMdsFlagOption(gCmd.Cmd)
config.AddRpcRetryTimesFlag(gCmd.Cmd)
config.AddRpcTimeoutFlag(gCmd.Cmd)
config.AddBsServerIdOptionFlag(gCmd.Cmd)
config.AddBsIpOptionFlag(gCmd.Cmd)
config.AddBsPortOptionFlag(gCmd.Cmd)
}

func (gCmd *GetChunkInfosCommand) Init(cmd *cobra.Command, args []string) error {
timeout := config.GetFlagDuration(gCmd.Cmd, config.RPCTIMEOUT)
retrytimes := config.GetFlagInt32(gCmd.Cmd, config.RPCRETRYTIMES)
mdsAddrs, _ := config.GetBsMdsAddrSlice(gCmd.Cmd)
ip := config.GetFlagString(gCmd.Cmd, config.CURVEBS_IP)
port := config.GetFlagUint32(gCmd.Cmd, config.CURVEBS_PORT)
serverid := config.GetFlagUint32(gCmd.Cmd, config.CURVEBS_SERVER_ID)

if ip != "" {
rpc := &GetChunkServerInfosRpc{
Request: &topology.ListChunkServerRequest{
Ip: &ip,
Port: &port,
},
Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "ListChunkServer"),
}
gCmd.Rpc = rpc
} else {
rpc := &GetChunkServerInfosRpc{
Request: &topology.ListChunkServerRequest{
ServerID: &serverid,
},
Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "ListChunkServer"),
}
gCmd.Rpc = rpc
}
return nil
}

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

func (gCmd *GetChunkInfosCommand) ResultPlainOutput() error {
return output.FinalCmdOutputPlain(&gCmd.FinalCurveCmd)
}

func (gCmd *GetChunkInfosCommand) RunCommand(cmd *cobra.Command, args []string) error {
result, err := basecmd.GetRpcResponse(gCmd.Rpc.Info, gCmd.Rpc)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}
gCmd.Response = result.(*topology.ListChunkServerResponse)
return nil
}

func GetChunkserverInfos(caller *cobra.Command) ([]*topology.ChunkServerInfo, *cmderror.CmdError) {
gCmd := NewGetChunkServerInfosCommand()
config.AlignFlagsValue(caller, gCmd.Cmd, []string{
config.CURVEBS_MDSADDR, config.RPCRETRYTIMES, config.RPCTIMEOUT, config.CURVEBS_SERVER_ID, config.CURVEBS_IP, config.CURVEBS_PORT,
})

gCmd.Cmd.SilenceErrors = true
gCmd.Cmd.SilenceUsage = true
gCmd.Cmd.SetArgs([]string{"--format", config.FORMAT_NOOUT})
err := gCmd.Cmd.Execute()
if err != nil {
retErr := cmderror.ErrBsGetChunkServerInfos()
retErr.Format(err.Error())
return nil, retErr
}

return gCmd.Response.ChunkServerInfos, cmderror.Success()
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (cCmd *CopysetidsCommand) ResultPlainOutput() error {
func (cCmd *CopysetidsCommand) RunCommand(cmd *cobra.Command, args []string) error {
result, err := basecmd.GetRpcResponse(cCmd.Rpc.Info, cCmd.Rpc)

if err.Message != "success" {
if err.TypeCode() != cmderror.CODE_SUCCESS {
fmt.Print(err.Message)
return err.ToError()
}
Expand Down
52 changes: 14 additions & 38 deletions tools-v2/pkg/cli/command/curvebs/check/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
cobrautil "github.com/opencurve/curve/tools-v2/internal/utils"
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"

listchunkserver "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/chunkserver"
copysetbs "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/status/copyset"
"github.com/opencurve/curve/tools-v2/pkg/config"
"github.com/opencurve/curve/tools-v2/pkg/output"
Expand Down Expand Up @@ -105,49 +104,23 @@ func (sCmd *ServerCommand) RunCommand(cmd *cobra.Command, args []string) error {
// 1 send RPCs to get all ChunkServerInfo on server
timeout := config.GetFlagDuration(sCmd.Cmd, config.RPCTIMEOUT)
retrytimes := config.GetFlagInt32(sCmd.Cmd, config.RPCRETRYTIMES)
mdsAddrs, err := config.GetBsMdsAddrSlice(sCmd.Cmd)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}
mdsAddr := config.GetBsFlagString(cmd, config.CURVEBS_MDSADDR)
copysetid2Status := make(map[uint32]*copyset.COPYSET_OP_STATUS)
ip_out := sCmd.ServerIP

var rpc *listchunkserver.ListChunkServerRpc
if sCmd.ServerIP != "" {
rpc = &listchunkserver.ListChunkServerRpc{
Request: &topology.ListChunkServerRequest{
Ip: &sCmd.ServerIP,
Port: &sCmd.Port,
},
Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "ListChunkServer"),
}
} else {
rpc = &listchunkserver.ListChunkServerRpc{
Request: &topology.ListChunkServerRequest{
ServerID: &sCmd.ServerID,
},
Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "ListChunkServer"),
}
}
total := 0
healthy := 0
unhelthy := 0

response, errCmd := basecmd.GetRpcResponse(rpc.Info, rpc)
if errCmd.TypeCode() != cmderror.CODE_SUCCESS {
sCmd.Error = errCmd
return errCmd.ToError()
chunkServerInfos, err := GetChunkserverInfos(sCmd.Cmd)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}

chunkServerInfos := response.(*topology.ListChunkServerResponse).ChunkServerInfos
ip_out := sCmd.ServerIP
if len(chunkServerInfos) != 0 {
ip_out = chunkServerInfos[0].GetHostIp()
}

mdsAddr := config.GetBsFlagString(cmd, config.CURVEBS_MDSADDR)

copysetid2Status := make(map[uint32]*copyset.COPYSET_OP_STATUS)

total := 0
healthy := 0
unhelthy := 0

for _, item := range chunkServerInfos {
err := sCmd.GetStatus(item, &timeout, uint32(retrytimes), mdsAddr, &copysetid2Status)
if err.TypeCode() != cmderror.CODE_SUCCESS {
Expand All @@ -169,11 +142,14 @@ func (sCmd *ServerCommand) RunCommand(cmd *cobra.Command, args []string) error {
row[cobrautil.ROW_SERVER] = fmt.Sprintf("%d", sCmd.ServerID)
row[cobrautil.ROW_TOTAL] = fmt.Sprintf("%d", total)
row[cobrautil.ROW_IP] = ip_out
row[cobrautil.ROW_UNHEALTHY_COPYSET] = fmt.Sprintf("%d(%v%%)", unhelthy, (unhelthy/total)*100)
if total == 0 {
row[cobrautil.ROW_UNHEALTHY_COPYSET] = fmt.Sprintf("%d(%d%%)", 0, 0)
} else {
row[cobrautil.ROW_UNHEALTHY_COPYSET] = fmt.Sprintf("%d(%v%%)", unhelthy, (unhelthy/total)*100)
}

list := cobrautil.Map2List(row, sCmd.Header)
sCmd.TableNew.Append(list)
config.AddFormatFlag(sCmd.Cmd)
return nil
}

Expand Down

0 comments on commit 8427f05

Please sign in to comment.