Skip to content

Commit

Permalink
support incremental formatting
Browse files Browse the repository at this point in the history
Signed-off-by: liuminjian <liuminjian@chinatelecom.cn>
  • Loading branch information
liuminjian committed Oct 10, 2023
1 parent 14c8166 commit c592f95
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 20 deletions.
28 changes: 21 additions & 7 deletions cli/command/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ import (

const (
FORMAT_EXAMPLE = `Examples:
$ curveadm format -f /path/to/format.yaml # Format chunkfile pool with specified configure file
$ curveadm format --status -f /path/to/format.yaml # Display formatting status
$ curveadm format --stop -f /path/to/format.yaml # Stop formatting progress
$ curveadm format --debug -f /path/to/format.yaml # Format chunkfile with debug mode
$ curveadm format --clean -f /path/to/format.yaml # clean the container left by debug mode`
$ curveadm format -f /path/to/format.yaml # Format chunkfile pool with specified configure file
$ curveadm format -f /path/to/format.yaml --increment # Incremental Format chunkfile pool with specified configure file
$ curveadm format --status -f /path/to/format.yaml # Display formatting status
$ curveadm format --stop -f /path/to/format.yaml # Stop formatting progress
$ curveadm format --debug -f /path/to/format.yaml # Format chunkfile with debug mode
$ curveadm format --clean -f /path/to/format.yaml # clean the container left by debug mode`
)

var (
Expand All @@ -70,6 +71,7 @@ type formatOptions struct {
stopFormat bool
debug bool
clean bool
increment bool
}

func checkFormatOptions(options formatOptions) error {
Expand Down Expand Up @@ -116,6 +118,7 @@ func NewFormatCommand(curveadm *cli.CurveAdm) *cobra.Command {
flags.BoolVar(&options.stopFormat, "stop", false, "Stop formatting progress")
flags.BoolVar(&options.debug, "debug", false, "Debug formatting progress")
flags.BoolVar(&options.clean, "clean", false, "Clean the Container")
flags.BoolVar(&options.increment, "increment", false, "Incremental formatting")

return cmd
}
Expand All @@ -131,6 +134,7 @@ func genFormatPlaybook(curveadm *cli.CurveAdm,
stopFormat := options.stopFormat
debug := options.debug
clean := options.clean
increment := options.increment

steps := FORMAT_PLAYBOOK_STEPS
if showStatus {
Expand All @@ -150,6 +154,7 @@ func genFormatPlaybook(curveadm *cli.CurveAdm,
if step == playbook.FORMAT_CHUNKFILE_POOL {
options[comm.DEBUG_MODE] = debug
}
options[comm.FORMAT_INCREMENTAL] = increment
pb.AddStep(&playbook.PlaybookStep{
Type: step,
Configs: fcs,
Expand Down Expand Up @@ -179,6 +184,7 @@ func runFormat(curveadm *cli.CurveAdm, options formatOptions) error {
var fcs []*configure.FormatConfig
diskRecords := curveadm.DiskRecords()
debug := options.debug
increment := options.increment
if debug {
curveadm.SetDebugLevel()
}
Expand Down Expand Up @@ -224,13 +230,21 @@ func runFormat(curveadm *cli.CurveAdm, options formatOptions) error {
return err
}

// 3) run playbook
// 3) confirm by user
if increment {
if pass := tuicomm.ConfirmYes(tuicomm.PromptIncrementFormat()); !pass {
curveadm.WriteOut(tuicomm.PromptCancelOpetation("increment format"))
return errno.ERR_CANCEL_OPERATION
}
}

// 4) run playbook
err = pb.Run()
if err != nil {
return err
}

// 4) print status or prompt
// 5) print status or prompt
if options.showStatus {
output := displayFormatStatus(curveadm)
curveadm.WriteOutln("")
Expand Down
1 change: 1 addition & 0 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const (

// format
KEY_ALL_FORMAT_STATUS = "ALL_FORMAT_STATUS"
FORMAT_INCREMENTAL = "FORMAT_INCREMENTAL"

// check
KEY_CHECK_WITH_WEAK = "CHECK_WITH_WEAK"
Expand Down
33 changes: 27 additions & 6 deletions internal/task/scripts/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,33 @@ percent=$2
chunkfile_size=$3
chunkfile_pool_dir=$4
chunkfile_pool_meta_path=$5
increment_format=$6
mkdir -p $chunkfile_pool_dir
$binary \
-allocatePercent=$percent \
-fileSize=$chunkfile_size \
-filePoolDir=$chunkfile_pool_dir \
-filePoolMetaPath=$chunkfile_pool_meta_path \
-fileSystemPath=$chunkfile_pool_dir
if [ $increment_format == "true" ]
then
rootdir=$(dirname $chunkfile_pool_dir)
used_percent=$(df $rootdir --output=pcent|tail -n 1|sed 's/%//'|xargs)
let minus=$percent-$used_percent
if [ $minus -gt 0 ]
then
$binary \
-allocatePercent=$minus \
-fileSize=$chunkfile_size \
-filePoolDir=$chunkfile_pool_dir \
-filePoolMetaPath=$chunkfile_pool_meta_path \
-fileSystemPath=$chunkfile_pool_dir
fi
else
$binary \
-allocatePercent=$percent \
-fileSize=$chunkfile_size \
-filePoolDir=$chunkfile_pool_dir \
-filePoolMetaPath=$chunkfile_pool_meta_path \
-fileSystemPath=$chunkfile_pool_dir
fi
`
34 changes: 34 additions & 0 deletions internal/task/step/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/opencurve/curveadm/internal/errno"
"github.com/opencurve/curveadm/internal/task/context"
"github.com/opencurve/curveadm/pkg/module"
"regexp"
)

type (
Expand Down Expand Up @@ -88,6 +89,13 @@ type (
module.ExecOptions
}

StopContainers struct {
Filter string
Time int
Out *string
module.ExecOptions
}

RestartContainer struct {
ContainerId string
Out *string
Expand Down Expand Up @@ -258,6 +266,32 @@ func (s *StopContainer) Execute(ctx *context.Context) error {
return PostHandle(nil, s.Out, out, err, errno.ERR_STOP_CONTAINER_FAILED.FD("(%s stop CONTAINER)", s.ExecWithEngine))
}

func (s *StopContainers) Execute(ctx *context.Context) error {
var output string
listStep := &ListContainers{
Format: "'{{.ID}} {{.Names}}'",
Out: &output,
ExecOptions: s.ExecOptions,
}
if err := listStep.Execute(ctx); err != nil {
return err
}
reg := regexp.MustCompile(`(\w+) ` + s.Filter)
containerIds := reg.FindAllStringSubmatch(output, -1)
for _, containerId := range containerIds {
if len(containerId) >= 1 {
step := &StopContainer{
ContainerId: containerId[1],
ExecOptions: s.ExecOptions,
}
if err := step.Execute(ctx); err != nil {
return err
}
}
}
return nil
}

func (s *RestartContainer) Execute(ctx *context.Context) error {
cli := ctx.Module().DockerCli().RestartContainer(s.ContainerId)
out, err := cli.Execute(s.ExecOptions)
Expand Down
23 changes: 16 additions & 7 deletions internal/task/task/bs/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ package bs

import (
"fmt"
comm "github.com/opencurve/curveadm/internal/common"
"regexp"
"strings"
"time"

"github.com/opencurve/curveadm/cli/cli"
comm "github.com/opencurve/curveadm/internal/common"
"github.com/opencurve/curveadm/internal/configure"
"github.com/opencurve/curveadm/internal/configure/disks"
os "github.com/opencurve/curveadm/internal/configure/os"
Expand Down Expand Up @@ -246,8 +246,9 @@ func NewFormatChunkfilePoolTask(curveadm *cli.CurveAdm, fc *configure.FormatConf
chunkfilePoolRootDir := layout.ChunkfilePoolRootDir
formatScript := scripts.SCRIPT_FORMAT
formatScriptPath := fmt.Sprintf("%s/format.sh", layout.ToolsBinDir)
formatCommand := fmt.Sprintf("%s %s %d %d %s %s", formatScriptPath, layout.FormatBinaryPath,
usagePercent, DEFAULT_CHUNKFILE_SIZE, layout.ChunkfilePoolDir, layout.ChunkfilePoolMetaPath)
increment := curveadm.MemStorage().Get(comm.FORMAT_INCREMENTAL).(bool)
formatCommand := fmt.Sprintf("%s %s %d %d %s %s %t", formatScriptPath, layout.FormatBinaryPath,
usagePercent, DEFAULT_CHUNKFILE_SIZE, layout.ChunkfilePoolDir, layout.ChunkfilePoolMetaPath, increment)
debug := curveadm.MemStorage().Get(comm.DEBUG_MODE).(bool)

// 1: skip if formating container exist
Expand Down Expand Up @@ -279,10 +280,18 @@ func NewFormatChunkfilePoolTask(curveadm *cli.CurveAdm, fc *configure.FormatConf
Paths: []string{mountPoint},
ExecOptions: curveadm.ExecOptions(),
})
t.AddStep(&step.CreateFilesystem{ // mkfs.ext4 MOUNT_POINT
Device: device,
ExecOptions: curveadm.ExecOptions(),
})
if !increment {
t.AddStep(&step.CreateFilesystem{ // mkfs.ext4 MOUNT_POINT
Device: device,
ExecOptions: curveadm.ExecOptions(),
})
} else {
// 先获取chunk server container id然后暂停chunk server
t.AddStep(&step.StopContainers{
Filter: "curvebs-chunkserver",
ExecOptions: curveadm.ExecOptions(),
})
}
t.AddStep(&step.MountFilesystem{
Source: device,
Directory: mountPoint,
Expand Down
6 changes: 6 additions & 0 deletions internal/tui/common/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ func PromptCollectService() string {
return prompt.Build()
}

func PromptIncrementFormat() string {
prompt := NewPrompt(color.YellowString(PROMPT_WARNING) + DEFAULT_CONFIRM_PROMPT)
prompt.data["warning"] = "WARNING: increment format will stop chunkserver service"
return prompt.Build()
}

func prettyClue(clue string) string {
items := strings.Split(clue, "\n")
for {
Expand Down

0 comments on commit c592f95

Please sign in to comment.