Skip to content

Commit

Permalink
code for clean-recycle
Browse files Browse the repository at this point in the history
Signed-off-by: lng2020 <nanguanlin6@gmail.com>
  • Loading branch information
lng2020 authored and Cyber-SiKu committed Apr 26, 2023
1 parent 48872a0 commit 61c07b3
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 118 deletions.
21 changes: 21 additions & 0 deletions tools-v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ A tool for CurveFS & CurveBs.
- [create](#create-1)
- [create file](#create-file)
- [create dir](#create-dir)
- [clean-recycle](#clean-recycle)
- [Comparison of old and new commands](#comparison-of-old-and-new-commands)
- [curve fs](#curve-fs)
- [curve bs](#curve-bs)
Expand Down Expand Up @@ -936,6 +937,26 @@ Output:
+------+------+----------------+-------+--------+---------+--------+-----+---------------------+--------------+---------+-----------------+----------+
```

### clean-recycle

clean the recycle bin

Usage:

```bash
curve bs clean-recycle --recycleprefix=/test --expiredtime=1h
```

Output:

```bash
+---------+
| RESULT |
+---------+
| success |
+---------+
```

##### query chunk

query the location of the chunk corresponding to the offset
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 @@ -411,6 +411,9 @@ var (
ErrBsUnknownThrottleType = func() *CmdError {
return NewInternalCmdError(50, "unknown throttle type[%s], only support: iops_total|iops_read|iops_write|bps_total|bps_read|bps_write")
}
ErrBsListDir = func() *CmdError {
return NewInternalCmdError(51, "list directory fail, err: %s")
}

// http error
ErrHttpUnreadableResult = func() *CmdError {
Expand Down
1 change: 1 addition & 0 deletions tools-v2/internal/utils/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
IP_PORT_REGEX = "((\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5]):([0-9]|[1-9]\\d{1,3}|[1-5]\\d{4}|6[0-4]\\d{4}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5]))|(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])"
PATH_REGEX = `^(/[^/ ]*)+/?$`
FS_NAME_REGEX = "^([a-z0-9]+\\-?)+$"
K_STRING_TRUE = "true"

ROOT_PATH = "/"
RECYCLEBIN_PATH = "/RecycleBin"
Expand Down
2 changes: 2 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/bs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/spf13/cobra"

basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/clean_recycle"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/create"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/delete"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list"
Expand All @@ -48,6 +49,7 @@ func (bsCmd *CurveBsCommand) AddSubCommands() {
delete.NewDeleteCommand(),
create.NewCreateCmd(),
update.NewUpdateCommand(),
clean_recycle.NewCleanRecycleCommand(),
)
}

Expand Down
134 changes: 134 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/clean_recycle/clean_recycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Project: tools-v2
* Created Date: 2023-4-6
* Author: nanguanlin6@gmail.com
*/

package clean_recycle

import (
"strings"
"time"

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/cli/command/curvebs/delete/file"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/dir"
"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"
)

const (
cleanRecycleBinExample = `$ curve bs clean-recycle --expiredtime=1h --recycleprefix=/test`
RECYCLEBINDIR = "/RecycleBin"
)

// CleanRecycleCommand
type CleanRecycleCommand struct {
basecmd.FinalCurveCmd
recyclePrefix string
expireTime time.Duration
}

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

// new CleanRecycleCommand function
func NewCleanRecycleCommand() *cobra.Command {
crCmd := &CleanRecycleCommand{
FinalCurveCmd: basecmd.FinalCurveCmd{
Use: "clean-recycle",
Short: "clean recycle bin",
Example: cleanRecycleBinExample,
},
}
return basecmd.NewFinalCurveCli(&crCmd.FinalCurveCmd, crCmd)
}

// method of CleanRecycleCommand struct
func (crCmd *CleanRecycleCommand) Init(cmd *cobra.Command, args []string) error {
crCmd.recyclePrefix = config.GetBsRecyclePrefix(crCmd.Cmd)
crCmd.expireTime = config.GetBsExpireTime(crCmd.Cmd)
header := []string{cobrautil.ROW_RESULT}
crCmd.SetHeader(header)
crCmd.TableNew.SetAutoMergeCellsByColumnIndex(cobrautil.GetIndexSlice(
crCmd.Header, header,
))
return nil
}

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

func (crCmd *CleanRecycleCommand) RunCommand(cmd *cobra.Command, args []string) error {
// Get the file infos in recycle bin
crCmd.Cmd.Flags().Set(config.CURVEBS_PATH, RECYCLEBINDIR)
resp, err := dir.ListDir(crCmd.Cmd)
if err.TypeCode() != cmderror.CODE_SUCCESS {
crCmd.Error = err
crCmd.Result = cobrautil.ROW_VALUE_FAILED
return err.ToError()
}

// Define the needDelete function
needDelete := func(fileInfo *nameserver2.FileInfo, now time.Time, expireTime time.Duration) bool {
createTime := time.Unix(int64(fileInfo.GetCtime()/1000000), 0)
return createTime.Add(expireTime).Before(now)
}

// Iterate through files and delete if necessary
now := time.Now()

var errs []*cmderror.CmdError
infos := resp.GetFileInfo()
for _, fileInfo := range infos {
originPath := fileInfo.GetOriginalFullPathName()
if !strings.HasPrefix(originPath, crCmd.recyclePrefix) || !needDelete(fileInfo, now, crCmd.expireTime) {
continue
}

filename := RECYCLEBINDIR + "/" + fileInfo.GetFileName()
crCmd.Cmd.Flags().Set(config.CURVEBS_PATH, filename)
crCmd.Cmd.Flags().Set(config.CURVEBS_FORCE, cobrautil.K_STRING_TRUE)
deleteResult, err := file.DeleteFile(crCmd.Cmd)
if deleteResult.GetStatusCode() != nameserver2.StatusCode_kOK {
errs = append(errs, err)
continue
}
}

if len(errs) != 0 {
crCmd.Result = cobrautil.ROW_VALUE_FAILED
crCmd.Error = cmderror.MergeCmdError(errs)
return crCmd.Error.ToError()
}

out := make(map[string]string)
out[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_SUCCESS
list := cobrautil.Map2List(out, []string{cobrautil.ROW_RESULT})
crCmd.TableNew.Append(list)

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

func (crCmd *CleanRecycleCommand) ResultPlainOutput() error {
return output.FinalCmdOutputPlain(&crCmd.FinalCurveCmd)
}

func (crCmd *CleanRecycleCommand) AddFlags() {
config.AddBsMdsFlagOption(crCmd.Cmd)
config.AddRpcRetryTimesFlag(crCmd.Cmd)
config.AddRpcTimeoutFlag(crCmd.Cmd)
config.AddBsUserOptionFlag(crCmd.Cmd)
config.AddBsPasswordOptionFlag(crCmd.Cmd)

config.AddBsForceDeleteOptionFlag(crCmd.Cmd)
config.AddBsPathOptionFlag(crCmd.Cmd)
config.AddBsRecyclePrefixOptionFlag(crCmd.Cmd)
config.AddBsExpireTimeOptionFlag(crCmd.Cmd)
}
2 changes: 1 addition & 1 deletion tools-v2/pkg/cli/command/curvebs/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var _ basecmd.MidCurveCmdFunc = (*DeleteCommand)(nil) // check interface

func (dCmd *DeleteCommand) AddSubCommands() {
dCmd.Cmd.AddCommand(
file.NewCommand(),
file.NewFileCommand(),
peer.NewCommand(),
)
}
Expand Down
66 changes: 45 additions & 21 deletions tools-v2/pkg/cli/command/curvebs/delete/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

const (
deleteCliExample = `curve bs delete file --filename /curvebs-file-name --username username [--password password] [--forcedelete true]`
deleteCliExample = `curve bs delete file --path /curvebs-file-path --user username [--password password] [--force true]`
)

type DeleteCertainFileRpc struct {
Expand Down Expand Up @@ -51,16 +51,16 @@ func (deleteCommand *DeleteCommand) Init(cmd *cobra.Command, args []string) erro
//get the default timeout and retrytimes
timeout := config.GetFlagDuration(deleteCommand.Cmd, config.RPCTIMEOUT)
retrytimes := config.GetFlagInt32(deleteCommand.Cmd, config.RPCRETRYTIMES)
filename := config.GetBsFlagString(deleteCommand.Cmd, config.CURVEBS_FILENAME)
path := config.GetBsFlagString(deleteCommand.Cmd, config.CURVEBS_PATH)
username := config.GetBsFlagString(deleteCommand.Cmd, config.CURVEBS_USER)
password := config.GetBsFlagString(deleteCommand.Cmd, config.CURVEBS_PASSWORD)
forcedelete := config.GetFlagBool(deleteCommand.Cmd, config.CURVEBS_FORCEDELETE)
forcedelete := config.GetBsFlagBool(deleteCommand.Cmd, config.CURVEBS_FORCE)
date, errDat := cobrautil.GetTimeofDayUs()
if errDat.TypeCode() != cmderror.CODE_SUCCESS {
return errDat.ToError()
}
deleteRequest := nameserver2.DeleteFileRequest{
FileName: &filename,
FileName: &path,
Owner: &username,
Date: &date,
ForceDelete: &forcedelete,
Expand All @@ -74,7 +74,7 @@ func (deleteCommand *DeleteCommand) Init(cmd *cobra.Command, args []string) erro
Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "DeleteFile"),
Request: &deleteRequest,
}
header := []string{cobrautil.ROW_RESULT, cobrautil.ROW_REASON}
header := []string{cobrautil.ROW_RESULT}
deleteCommand.SetHeader(header)
deleteCommand.TableNew.SetAutoMergeCellsByColumnIndex(cobrautil.GetIndexSlice(
deleteCommand.Header, header,
Expand All @@ -83,24 +83,24 @@ func (deleteCommand *DeleteCommand) Init(cmd *cobra.Command, args []string) erro
}

func (deleteCommand *DeleteCommand) RunCommand(cmd *cobra.Command, args []string) error {
out := make(map[string]string)
result, err := basecmd.GetRpcResponse(deleteCommand.Rpc.Info, deleteCommand.Rpc)
if err.TypeCode() != cmderror.CODE_SUCCESS {
out[cobrautil.ROW_RESULT] = "failed"
out[cobrautil.ROW_REASON] = err.Message
return nil
deleteCommand.Error = err
deleteCommand.Result = result
return err.ToError()
}
deleteCommand.Response = result.(*nameserver2.DeleteFileResponse)
if deleteCommand.Response.GetStatusCode() != nameserver2.StatusCode_kOK {
err = cmderror.ErrBsDeleteFile()
out[cobrautil.ROW_RESULT] = "failed"
out[cobrautil.ROW_REASON] = err.Message
return nil
deleteCommand.Error = cmderror.ErrBsDeleteFile()
deleteCommand.Result = result
return deleteCommand.Error.ToError()
}
out[cobrautil.ROW_RESULT] = "success"
out[cobrautil.ROW_REASON] = ""
list := cobrautil.Map2List(out, []string{cobrautil.ROW_RESULT, cobrautil.ROW_REASON})
out := make(map[string]string)
out[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_SUCCESS
list := cobrautil.Map2List(out, []string{cobrautil.ROW_RESULT})
deleteCommand.TableNew.Append(list)

deleteCommand.Result, deleteCommand.Error = result, cmderror.Success()
return nil
}

Expand All @@ -113,18 +113,18 @@ func (deleteCommand *DeleteCommand) ResultPlainOutput() error {
}

func (deleteCommand *DeleteCommand) AddFlags() {
config.AddFsMdsAddrFlag(deleteCommand.Cmd)
config.AddBsMdsFlagOption(deleteCommand.Cmd)
config.AddRpcTimeoutFlag(deleteCommand.Cmd)
config.AddRpcRetryTimesFlag(deleteCommand.Cmd)

config.AddBsFilenameRequiredFlag(deleteCommand.Cmd)
config.AddBsUsernameRequiredFlag(deleteCommand.Cmd)
config.AddBsPathRequiredFlag(deleteCommand.Cmd)
config.AddBsUserOptionFlag(deleteCommand.Cmd)
config.AddBsPasswordOptionFlag(deleteCommand.Cmd)
config.AddBsForceDeleteOptionFlag(deleteCommand.Cmd)
}

// NewCommand return the mid cli
func NewCommand() *cobra.Command {
func NewDeleteFileCommand() *DeleteCommand {
deleteCommand := &DeleteCommand{
FinalCurveCmd: basecmd.FinalCurveCmd{
Use: "file",
Expand All @@ -133,5 +133,29 @@ func NewCommand() *cobra.Command {
},
}
basecmd.NewFinalCurveCli(&deleteCommand.FinalCurveCmd, deleteCommand)
return basecmd.NewFinalCurveCli(&deleteCommand.FinalCurveCmd, deleteCommand)
return deleteCommand
}

func NewFileCommand() *cobra.Command {
return NewDeleteFileCommand().Cmd
}

// DeleteFile function wraps the DeleteCertainFile rpc
func DeleteFile(caller *cobra.Command) (*nameserver2.DeleteFileResponse, *cmderror.CmdError) {
delCmd := NewDeleteFileCommand()
config.AlignFlagsValue(caller, delCmd.Cmd, []string{
config.RPCRETRYTIMES, config.RPCTIMEOUT, config.CURVEBS_MDSADDR,
config.CURVEBS_PATH, config.CURVEBS_USER, config.CURVEBS_PASSWORD,
config.CURVEBS_FORCE,
})
delCmd.Cmd.SilenceErrors = true
delCmd.Cmd.SilenceUsage = true
delCmd.Cmd.SetArgs([]string{"--format", config.FORMAT_NOOUT})
err := delCmd.Cmd.Execute()
if err != nil {
retErr := cmderror.ErrBsDeleteFile()
retErr.Format(err.Error())
return delCmd.Response, retErr
}
return delCmd.Response, cmderror.Success()
}
Loading

0 comments on commit 61c07b3

Please sign in to comment.