Skip to content

Commit

Permalink
[refactor] use new snapshot utils
Browse files Browse the repository at this point in the history
Signed-off-by: saicaca <[email protected]>
  • Loading branch information
saicaca committed Sep 24, 2023
1 parent 77e100d commit 449d662
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 109 deletions.
1 change: 1 addition & 0 deletions tools-v2/internal/utils/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
QueryOffset = "Offset"
QueryStatus = "Status"
QueryType = "Type"
QueryFile = "File"

ActionClone = "Clone"
ActionRecover = "Recover"
Expand Down
181 changes: 72 additions & 109 deletions tools-v2/pkg/cli/command/curvebs/status/volume/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,15 @@ import (
"github.com/opencurve/curve/tools-v2/pkg/config"
"github.com/opencurve/curve/tools-v2/pkg/output"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"net/url"
"strconv"
"strings"
"time"
)

type QueryResponse struct {
Code int32 `json:"Code"`
Code string `json:"Code"`
Message string `json:"Message"`
RequestId string `json:"RequestId"`
TotalCount int32 `json:"TotalCount"`
TotalCount int `json:"TotalCount"`
Snapshots []*SnapshotInfoData `json:"Snapshots,omitempty"`
}

Expand All @@ -68,15 +66,15 @@ const (
)

const (
STATUS_DONE = "done"
STATUS_IN_PROGRESS = "in-progress"
STATUS_DELETING = "deleting"
STATUS_ERROR_DELETING = "errorDeleting"
STATUS_CANCELING = "canceling"
STATUS_ERROR = "error"
STATUS_DONE = iota
STATUS_IN_PROGRESS
STATUS_DELETING
STATUS_ERROR_DELETING
STATUS_CANCELING
STATUS_ERROR
)

var statusName = []string{
var statusList = []int{
STATUS_DONE,
STATUS_IN_PROGRESS,
STATUS_DELETING,
Expand All @@ -85,8 +83,23 @@ var statusName = []string{
STATUS_ERROR,
}

var statusName = map[int]string{
STATUS_DONE: "done",
STATUS_IN_PROGRESS: "in-progress",
STATUS_DELETING: "deleting",
STATUS_ERROR_DELETING: "errorDeleting",
STATUS_CANCELING: "canceling",
STATUS_ERROR: "error",
}

type SnapshotCommand struct {
basecmd.FinalCurveCmd

snapshotAddrs []string
timeout time.Duration
user string
filename string

errCode int
health cobrautil.ClUSTER_HEALTH_STATUS
}
Expand All @@ -109,49 +122,70 @@ func NewVolumeSnapshotCommand() *SnapshotCommand {
}

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 {

filename := config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_FILENAME)
user := config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_USER)
addrs, addrErr := config.GetBsSnapshotAddrSlice(sCmd.Cmd)
if addrErr.TypeCode() != cmderror.CODE_SUCCESS {
sCmd.errCode = addrErr.Code
return fmt.Errorf(addrErr.Message)
params := map[string]any{
cobrautil.QueryAction: cobrautil.ActionGetFileSnapshotList,
cobrautil.QueryUser: sCmd.user,
cobrautil.QueryFile: sCmd.filename,
cobrautil.QueryLimit: 100,
cobrautil.QueryOffset: 0,
}
count := make(map[int]int)
for _, s := range statusList {
count[s] = 0
}
for {
subUri := cobrautil.NewSnapshotQuerySubUri(params)

metric := basecmd.NewMetric(sCmd.snapshotAddrs, subUri, sCmd.timeout)
result, err := basecmd.QueryMetric(metric)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}

_, records, err := getSnapshotListAll(addrs, user, filename, "", nil)
var resp QueryResponse
if err := json.Unmarshal([]byte(result), &resp); err != nil {
return err
}
if resp.Code != "0" {
return fmt.Errorf("get clone list fail, error code: %s", resp.Code)
}
if len(resp.Snapshots) == 0 {
break
}
for _, s := range resp.Snapshots {
count[int(s.Status)] += 1
}

retErr := cmderror.ErrBsGetSnapshotStatus()
retErr.Format(err.Message)
sCmd.Error = retErr
if err.TypeCode() == cmderror.CODE_SUCCESS {
sCmd.health = cobrautil.HEALTH_OK
} else {
sCmd.health = cobrautil.HEALTH_WARN
params[cobrautil.QueryOffset] = params[cobrautil.QueryOffset].(int) + params[cobrautil.QueryLimit].(int)
}

count := []int{0, 0, 0, 0, 0, 0}
for _, r := range records {
count[r.Status] = count[r.Status] + 1
}
rows := make([]map[string]string, 0)
for i, c := range count {
for _, s := range statusList {
rows = append(rows, map[string]string{
cobrautil.ROW_STATUS: statusName[i],
cobrautil.ROW_COUNT: strconv.Itoa(c),
cobrautil.ROW_STATUS: statusName[s],
cobrautil.ROW_COUNT: strconv.Itoa(count[s]),
})
}
list := cobrautil.ListMap2ListSortByKeys(rows, sCmd.Header, []string{
cobrautil.ROW_STATUS,
})
list := cobrautil.ListMap2ListSortByKeys(rows, sCmd.Header, []string{})
sCmd.TableNew.AppendBulk(list)
sCmd.Result = rows
sCmd.Error = cmderror.ErrSuccess()

return nil
}
Expand All @@ -168,76 +202,5 @@ func (sCmd *SnapshotCommand) AddFlags() {
config.AddBsUserOptionFlag(sCmd.Cmd)
config.AddBsSnapshotFilenameFlag(sCmd.Cmd)
config.AddBsSnapshotCloneFlagOption(sCmd.Cmd)
}

func getSnapshotListAll(
addrs []string,
user, filename, snapshotName string,
filter []string,
) (int, []*SnapshotInfoData, *cmderror.CmdError) {
limit := ENTRIES_PER_TIME
offset := 0
var receiveCount int
var receiveRecords []*SnapshotInfoData

for {
totalCount, records, err := getSnapshotList(addrs, user, filename, snapshotName, filter, limit, offset)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return -1, nil, err
}
if len(records) == 0 {
break
}
receiveCount += totalCount
receiveRecords = append(receiveRecords, records...)
offset += len(records)
}

return receiveCount, receiveRecords, nil
}

func getSnapshotList(
addrs []string,
user, filename, snapshotName string,
filter []string, limit, offset int,
) (int, []*SnapshotInfoData, *cmderror.CmdError) {
params := map[string]string{
"Action": ACTION_GET_FILE_SNAPSHOT_LIST,
"Limit": strconv.Itoa(limit),
"Offset": strconv.Itoa(offset),
"User": user,
"File": filename,
"Name": snapshotName,
"Filter": strings.Join(filter, ","),
}
paramsStr := encodeParam(params)
subUri := SNAPSHOT_SUBURI + "?" + paramsStr

metric := basecmd.NewMetric(addrs, subUri, viper.GetDuration(config.VIPER_GLOBALE_HTTPTIMEOUT))
result, err := basecmd.QueryMetric(metric)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return -1, nil, err
}
return parseResult(result)
}

func encodeParam(params map[string]string) string {
values := url.Values{}
for k, v := range params {
if v == "" {
continue
}
values.Add(k, v)
}
return values.Encode()
}

func parseResult(metricRet string) (int, []*SnapshotInfoData, *cmderror.CmdError) {
var data QueryResponse
if err := json.Unmarshal([]byte(metricRet), &data); err != nil {
retErr := cmderror.ErrParseMetric()
retErr.Format(err.Error())
return 0, nil, retErr
}
return int(data.TotalCount), data.Snapshots, cmderror.ErrSuccess()
config.AddHttpTimeoutFlag(sCmd.Cmd)
}

0 comments on commit 449d662

Please sign in to comment.