Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] tools-v2: add bs status volume snapshot command #2745

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions tools-v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,38 @@ Output:
+------------+-----------+--------+--------+--------+---------+
```

##### status volume

###### status volume snapshot

get the snapshot status

Usage:

```bash
curve bs status volume snapshot --filename test --snapshotaddr 127.0.0.1:5550,127.0.0.1:5551,127.0.0.1:5552
```

Output:

```bash
+---------------+-------+
| STATUS | COUNT |
+---------------+-------+
| done | 10 |
+---------------+-------+
| in-progress | 4 |
+---------------+-------+
| deleting | 2 |
+---------------+-------+
| errorDeleting | 0 |
+---------------+-------+
| canceling | 0 |
+---------------+-------+
| error | 0 |
+---------------+-------+
```

#### delete

##### delete peer
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 @@ -509,6 +509,9 @@ var (
ErrTargetClient = func() *CmdError {
return NewInternalCmdError(83, "get client target error: %s")
}
ErrBsGetSnapshotStatus = func() *CmdError {
return NewInternalCmdError(84, "get snapshot status fail, err: %s")
}

// http error
ErrHttpUnreadableResult = func() *CmdError {
Expand Down
1 change: 1 addition & 0 deletions tools-v2/internal/utils/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
ROW_COPYSET = "copyset"
ROW_COPYSET_ID = "copysetId"
ROW_COPYSET_KEY = "copysetKey"
ROW_COUNT = "count"
ROW_CREATE_TIME = "createTime"
ROW_CREATED = "created"
ROW_CTIME = "ctime"
Expand Down
2 changes: 2 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/status/etcd"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/status/mds"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/status/snapshot"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/status/volume"
"github.com/spf13/cobra"
)

Expand All @@ -49,6 +50,7 @@ func (statusCmd *StatusCommand) AddSubCommands() {
chunkserver.NewChunkServerCommand(),
copyset.NewCopysetCommand(),
cluster.NewClusterCommand(),
volume.NewVolumeCommand(),
)
}

Expand Down
162 changes: 162 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/status/volume/snapshot/snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* 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-09-13
* Author: saicaca
*/

package snapshot

import (
"encoding/json"
"fmt"
"strconv"
"time"

cmderror "github.com/opencurve/curve/tools-v2/internal/error"
cobrautil "github.com/opencurve/curve/tools-v2/internal/utils"
snapshotutil "github.com/opencurve/curve/tools-v2/internal/utils/snapshot"
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/spf13/cobra"
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sort import libs as

import (
standard lib ...

external lib ...
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in the latest commit.


const (
LENGTH_EMPTY = 0
)

type SnapshotCommand struct {
basecmd.FinalCurveCmd

snapshotAddrs []string
timeout time.Duration
user string
filename string
Copy link
Contributor

@caoxianfei1 caoxianfei1 Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It means that we can query one volume snapshot status? can you give an exmaple in README.md.

And i think it may be error.

Copy link
Contributor Author

@saicaca saicaca Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This argument also exists in the old version tools.

def snapshot_status(args):
totalCount, records = curltool.get_snapshot_list_all(args.user, args.filename)

According to the implementation of the backend service, only the snapshots with the matching filename will be queried. It seems that the filename doesn't need to be unique so it can return multiple snapshots.

int SnapshotCloneMetaStoreEtcd::GetSnapshotList(const std::string &filename,
std::vector<SnapshotInfo> *v) {
ReadLockGuard guard(snapInfos_mutex);
for (auto it = snapInfos_.begin();
it != snapInfos_.end();
it++) {
if (filename == it->second.GetFileName()) {
v->push_back(it->second);
}
}
if (v->size() != 0) {
return 0;
}
return -1;
}

The filename is set when creating the snapshot. (#2772) (It is the same in the old version tools)

curve bs create volume snapshot --user root --filename test --snapshotname snap-test

So, I think this argument can be used like

curve bs status volume snapshot --filename test --snapshotaddr 127.0.0.1:5550,127.0.0.1:5551,127.0.0.1:5552

I will add it to the README.


errCode int
health cobrautil.ClUSTER_HEALTH_STATUS
}

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

func NewSnapshotCommand() *cobra.Command {
return NewVolumeSnapshotCommand().Cmd
}

func NewVolumeSnapshotCommand() *SnapshotCommand {
vsCmd := &SnapshotCommand{
FinalCurveCmd: basecmd.FinalCurveCmd{
Use: "snapshot",
Short: "show the snapshot status",
},
}
basecmd.NewFinalCurveCli(&vsCmd.FinalCurveCmd, vsCmd)
return vsCmd
}

func (sCmd *SnapshotCommand) Init(cmd *cobra.Command, args []string) error {
snapshotAddrs, err := config.GetBsSnapshotAddrSlice(sCmd.Cmd)
if err.TypeCode() != cmderror.CODE_SUCCESS || len(snapshotAddrs) == 0 {
return err.ToError()
}
sCmd.snapshotAddrs = snapshotAddrs
sCmd.filename = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_FILENAME)
sCmd.user = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_USER)
sCmd.timeout = config.GetFlagDuration(sCmd.Cmd, config.HTTPTIMEOUT)

header := []string{cobrautil.ROW_STATUS, cobrautil.ROW_COUNT}
sCmd.SetHeader(header)

return nil
}

func (sCmd *SnapshotCommand) RunCommand(cmd *cobra.Command, args []string) error {
params := map[string]any{
snapshotutil.QueryAction: snapshotutil.ActionGetFileSnapshotList,
snapshotutil.QueryUser: sCmd.user,
snapshotutil.QueryFile: sCmd.filename,
snapshotutil.QueryLimit: snapshotutil.Limit,
snapshotutil.QueryOffset: snapshotutil.Offset,
}
count := make(map[int]int)
for {
subUri := snapshotutil.NewQuerySubUri(params)

metric := basecmd.NewMetric(sCmd.snapshotAddrs, subUri, sCmd.timeout)
result, err := basecmd.QueryMetric(metric)
if err.TypeCode() != cmderror.CODE_SUCCESS {
sCmd.Error = cmderror.ErrBsGetSnapshotStatus()
sCmd.Error.Format(fmt.Sprintf("code: %d, msg: %s", err.TypeCode(), err.ToError().Error()))
return sCmd.Error.ToError()
}

var resp struct {
snapshotutil.Response
TotalCount int `json:"TotalCount"`
SnapShots []*snapshotutil.SnapshotInfo `json:"SnapShots"`
}
if err := json.Unmarshal([]byte(result), &resp); err != nil {
sCmd.Error = cmderror.ErrUnmarshalJson()
sCmd.Error.Format(err.Error())
return sCmd.Error.ToError()
}
if resp.Code != snapshotutil.ResultSuccess {
sCmd.Error = cmderror.ErrBsGetSnapshotStatus()
sCmd.Error.Format(fmt.Sprintf("code: %s, msg: %s", resp.Code, resp.Message))
return sCmd.Error.ToError()
}
if len(resp.SnapShots) == LENGTH_EMPTY {
break
}
for _, s := range resp.SnapShots {
count[s.Status] += 1
}

params[snapshotutil.QueryOffset] = params[snapshotutil.QueryOffset].(int) + params[snapshotutil.QueryLimit].(int)
}

rows := make([]map[string]string, 0)
for i, s := range snapshotutil.SnapshotStatus {
rows = append(rows, map[string]string{
cobrautil.ROW_STATUS: s,
cobrautil.ROW_COUNT: strconv.Itoa(count[i]),
})
}
list := cobrautil.ListMap2ListSortByKeys(rows, sCmd.Header, []string{})
sCmd.TableNew.AppendBulk(list)
sCmd.Result = rows
sCmd.Error = cmderror.ErrSuccess()

return nil
}

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

func (sCmd *SnapshotCommand) ResultPlainOutput() error {
return output.FinalCmdOutputPlain(&sCmd.FinalCurveCmd)
}

func (sCmd *SnapshotCommand) AddFlags() {
config.AddBsUserOptionFlag(sCmd.Cmd)
config.AddBsSnapshotFilenameFlag(sCmd.Cmd)
config.AddBsSnapshotCloneFlagOption(sCmd.Cmd)
config.AddHttpTimeoutFlag(sCmd.Cmd)
}
51 changes: 51 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/status/volume/volume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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-09-13
* Author: saicaca
*/

package volume

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

type VolumeCommand struct {
basecmd.MidCurveCmd
}

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

func (volumeCmd *VolumeCommand) AddSubCommands() {
volumeCmd.Cmd.AddCommand(
snapshot.NewSnapshotCommand(),
)
}

func NewVolumeCommand() *cobra.Command {
volumeCmd := &VolumeCommand{
basecmd.MidCurveCmd{
Use: "volume",
Short: "get the status of curvebs volume",
},
}
return basecmd.NewMidCurveCli(&volumeCmd.MidCurveCmd, volumeCmd)
}
4 changes: 4 additions & 0 deletions tools-v2/pkg/config/bs.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,10 @@ func AddBsTaskIDRequiredFlag(cmd *cobra.Command) {
AddBsStringRequiredFlag(cmd, CURVEBS_TASKID, "task id")
}

func AddBsSnapshotFilenameFlag(cmd *cobra.Command) {
AddBsStringOptionFlag(cmd, CURVEBS_FILENAME, "snapshot filename")
}

func AddBsSnapshotIDOptionFlag(cmd *cobra.Command) {
AddBsStringOptionFlag(cmd, CURVEBS_SNAPSHOT_ID, "snapshot seqId")
}
Expand Down