Skip to content

Commit

Permalink
Add journal commands to CLI
Browse files Browse the repository at this point in the history
Add journal commands to CLI as part of Alluxio#17522
			pr-link: Alluxio#17569
			change-id: cid-5b23a8a36c129fc609d30fcfe5469c7ef9aefc51
  • Loading branch information
Xenorith authored and twalluxio committed Aug 14, 2023
1 parent f3d5ce5 commit 8e347b4
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 7 deletions.
70 changes: 70 additions & 0 deletions cli/src/alluxio.org/cli/cmd/journal/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/

package journal

import (
"github.com/spf13/cobra"

"alluxio.org/cli/env"
)

var Backup = &BackupCommand{
BaseJavaCommand: &env.BaseJavaCommand{
CommandName: "backup",
JavaClassName: "alluxio.cli.fsadmin.FileSystemAdminShell",
Parameters: []string{"backup"},
},
}

type BackupCommand struct {
*env.BaseJavaCommand

backupPath string
local bool
allowLeader bool
bypassDelegation bool
}

func (c *BackupCommand) Base() *env.BaseJavaCommand {
return c.BaseJavaCommand
}

func (c *BackupCommand) ToCommand() *cobra.Command {
cmd := c.Base().InitRunJavaClassCmd(&cobra.Command{
Use: Backup.CommandName,
Short: "Backup the local Alluxio master journal",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return c.Run(nil)
},
})
cmd.Flags().StringVar(&c.backupPath, "path", "", "Directory to write backup file to")
cmd.MarkFlagRequired("path")
cmd.Flags().BoolVar(&c.local, "local", false, "If true, writes the backup file to the local filesystem instead of root UFS")
cmd.Flags().BoolVar(&c.allowLeader, "allow-leader", false, "If true, allows the leader to take the backup when there are no standby masters")
cmd.Flags().BoolVar(&c.bypassDelegation, "bypass-delegation", false, "If true, takes the backup on the leader even if standby masters are available")
return cmd
}

func (c *BackupCommand) Run(_ []string) error {
javaArgs := []string{c.backupPath}
if c.local {
javaArgs = append(javaArgs, "--local")
}
if c.allowLeader {
javaArgs = append(javaArgs, "--allow-leader")
}
if c.bypassDelegation {
javaArgs = append(javaArgs, "--bypass-delegation")
}
return c.Base().Run(javaArgs)
}
46 changes: 46 additions & 0 deletions cli/src/alluxio.org/cli/cmd/journal/checkpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/

package journal

import (
"github.com/spf13/cobra"

"alluxio.org/cli/env"
)

var Checkpoint = &CheckpointCommand{
BaseJavaCommand: &env.BaseJavaCommand{
CommandName: "checkpoint",
JavaClassName: "alluxio.cli.fsadmin.FileSystemAdminShell",
Parameters: []string{"journal", "checkpoint"},
},
}

type CheckpointCommand struct {
*env.BaseJavaCommand
}

func (c *CheckpointCommand) Base() *env.BaseJavaCommand {
return c.BaseJavaCommand
}

func (c *CheckpointCommand) ToCommand() *cobra.Command {
cmd := c.Base().InitRunJavaClassCmd(&cobra.Command{
Use: Checkpoint.CommandName,
Short: "Create a checkpoint in the primary master journal system",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return c.Run(nil)
},
})
return cmd
}
9 changes: 5 additions & 4 deletions cli/src/alluxio.org/cli/cmd/journal/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import (

var Format = &FormatCommand{
BaseJavaCommand: &env.BaseJavaCommand{
CommandName: "format",
JavaClassName: "alluxio.cli.Format",
Parameters: []string{"master"},
ShellJavaOpts: fmt.Sprintf(env.JavaOptFormat, env.ConfAlluxioLoggerType, "Console"),
CommandName: "format",
JavaClassName: "alluxio.cli.Format",
Parameters: []string{"master"},
UseServerClasspath: true,
ShellJavaOpts: fmt.Sprintf(env.JavaOptFormat, env.ConfAlluxioLoggerType, "Console"),
},
}

Expand Down
3 changes: 3 additions & 0 deletions cli/src/alluxio.org/cli/cmd/journal/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ var Service = &env.Service{
Name: "journal",
Description: "Format, backup, and other journal related operations",
Commands: []env.Command{
Backup,
Checkpoint,
Format,
Read,
},
}
88 changes: 88 additions & 0 deletions cli/src/alluxio.org/cli/cmd/journal/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the "License"). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/

package journal

import (
"fmt"
"strconv"

"github.com/palantir/stacktrace"
"github.com/spf13/cobra"

"alluxio.org/cli/env"
)

var Read = &ReadCommand{
BaseJavaCommand: &env.BaseJavaCommand{
CommandName: "read",
JavaClassName: "alluxio.master.journal.tool.JournalTool",
UseServerClasspath: true,
ShellJavaOpts: fmt.Sprintf(env.JavaOptFormat, env.ConfAlluxioLoggerType, "Console"),
},
}

type ReadCommand struct {
*env.BaseJavaCommand

end int
inputDir string
master string
outputDir string
start int
}

func (c *ReadCommand) Base() *env.BaseJavaCommand {
return c.BaseJavaCommand
}

func (c *ReadCommand) ToCommand() *cobra.Command {
cmd := c.Base().InitRunJavaClassCmd(&cobra.Command{
Use: Read.CommandName,
Short: "Read an Alluxio journal file to a human-readable version",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return c.Run(nil)
},
})
cmd.Flags().IntVar(&c.end, "end", -1, "end log sequence number (exclusive)")
cmd.Flags().StringVar(&c.inputDir, "input-dir", "", "input directory on-disk to read the journal content from")
cmd.Flags().StringVar(&c.master, "master", "FileSystemMaster", "name of the master class")
cmd.Flags().StringVar(&c.outputDir, "output-dir", "", "output directory to write journal content to")
cmd.Flags().IntVar(&c.start, "start", 0, "start log sequence number (inclusive)")
return cmd
}

func (c *ReadCommand) Run(_ []string) error {
var javaArgs []string
if c.start < 0 {
return stacktrace.NewError("start log sequence number must be non-negative but was %v", c.start)
}
if c.end < -1 {
return stacktrace.NewError("end log sequence number must be non-negative but was %v", c.end)
}
if c.end != -1 {
javaArgs = append(javaArgs, "-end", strconv.Itoa(c.end))
}
if c.inputDir != "" {
javaArgs = append(javaArgs, "-inputDir", c.inputDir)
}
if c.master != "FileSystemMaster" {
javaArgs = append(javaArgs, "-master", c.master)
}
if c.outputDir != "" {
javaArgs = append(javaArgs, "-outputDir", c.outputDir)
}
if c.start > 0 {
javaArgs = append(javaArgs, "-start", strconv.Itoa(c.start))
}
return c.Base().Run(javaArgs)
}
12 changes: 9 additions & 3 deletions cli/src/alluxio.org/cli/env/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ type BaseJavaCommand struct {

DebugMode bool

InlineJavaOpts []string // java opts provided by the user as part of the inline command
ShellJavaOpts string // default java opts encoded as part of the specific command
UseServerClasspath bool // defaults to ALLUXIO_CLIENT_CLASSPATH, use ALLUXIO_SERVER_CLASSPATH if true
InlineJavaOpts []string // java opts provided by the user as part of the inline command
ShellJavaOpts string // default java opts encoded as part of the specific command
}

func (c *BaseJavaCommand) InitRunJavaClassCmd(cmd *cobra.Command) *cobra.Command {
Expand All @@ -58,7 +59,12 @@ func (c *BaseJavaCommand) RunJavaClassCmd(args []string) *exec.Cmd {
cmdArgs = append(cmdArgs, strings.Split(opts, " ")...)
}
}
cmdArgs = append(cmdArgs, "-cp", Env.EnvVar.GetString(EnvAlluxioClientClasspath))
classpath := EnvAlluxioClientClasspath
if c.UseServerClasspath {
classpath = EnvAlluxioServerClasspath
}
cmdArgs = append(cmdArgs, "-cp", Env.EnvVar.GetString(classpath))

if opts := Env.EnvVar.GetString(ConfAlluxioUserJavaOpts.EnvVar); opts != "" {
cmdArgs = append(cmdArgs, strings.Split(opts, " ")...)
}
Expand Down

0 comments on commit 8e347b4

Please sign in to comment.