Skip to content

Commit

Permalink
Use ionice and nice with Restic CMD (#716)
Browse files Browse the repository at this point in the history
- [x] merge and revendor kmodules/offshoot-api#13
  • Loading branch information
Dipta Das authored and tamalsaha committed Apr 3, 2019
1 parent ccdd429 commit 1ede024
Show file tree
Hide file tree
Showing 39 changed files with 1,136 additions and 2,886 deletions.
11 changes: 5 additions & 6 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions pkg/restic/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (

const (
ResticCMD = "/bin/restic_0.9.4"
IONiceCMD = "/bin/ionice"
NiceCMD = "/bin/nice"
)

type Snapshot struct {
Expand Down Expand Up @@ -272,6 +274,10 @@ func (w *ResticWrapper) run(commands ...Command) ([]byte, error) {
w.sh.Stderr = io.MultiWriter(os.Stderr, errBuff)

for _, cmd := range commands {
if cmd.Name == ResticCMD {
// first apply NiceSettings, then apply IONiceSettings
cmd = w.applyIONiceSettings(w.applyNiceSettings(cmd))
}
w.sh.Command(cmd.Name, cmd.Args...)
}
out, err := w.sh.Output()
Expand All @@ -290,3 +296,42 @@ func formatError(err error, stdErr string) error {
}
return err
}

func (w *ResticWrapper) applyIONiceSettings(oldCommand Command) Command {
if w.config.IONice == nil {
return oldCommand
}
newCommand := Command{
Name: IONiceCMD,
}
if w.config.IONice.Class != nil {
newCommand.Args = append(newCommand.Args, "-c", fmt.Sprint(*w.config.IONice.Class))
}
if w.config.IONice.ClassData != nil {
newCommand.Args = append(newCommand.Args, "-n", fmt.Sprint(*w.config.IONice.Class))
}
// TODO: should we use "-t" option with ionice ?
// newCommand.Args = append(newCommand.Args, "-t")

// append oldCommand as args of newCommand
newCommand.Args = append(newCommand.Args, oldCommand.Name)
newCommand.Args = append(newCommand.Args, oldCommand.Args...)
return newCommand
}

func (w *ResticWrapper) applyNiceSettings(oldCommand Command) Command {
if w.config.Nice == nil {
return oldCommand
}
newCommand := Command{
Name: NiceCMD,
}
if w.config.Nice.Adjustment != nil {
newCommand.Args = append(newCommand.Args, "-n", fmt.Sprint(*w.config.Nice.Adjustment))
}

// append oldCommand as args of newCommand
newCommand.Args = append(newCommand.Args, oldCommand.Name)
newCommand.Args = append(newCommand.Args, oldCommand.Args...)
return newCommand
}
3 changes: 3 additions & 0 deletions pkg/restic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package restic
import (
"github.com/appscode/stash/apis/stash/v1alpha1"
shell "github.com/codeskyblue/go-sh"
ofst "kmodules.xyz/offshoot-api/api/v1"
)

const (
Expand Down Expand Up @@ -54,6 +55,8 @@ type SetupOptions struct {
CacertFile string
ScratchDir string
EnableCache bool
Nice *ofst.NiceSettings
IONice *ofst.IONiceSettings
}

type MetricsOptions struct {
Expand Down
77 changes: 77 additions & 0 deletions pkg/restic/restic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"path/filepath"
"testing"

"github.com/appscode/go/types"
"github.com/stretchr/testify/assert"
ofst "kmodules.xyz/offshoot-api/api/v1"
)

var (
Expand Down Expand Up @@ -135,3 +137,78 @@ func TestBackupRestoreStdin(t *testing.T) {
}
fmt.Println("dump output:", dumpOut)
}

func TestBackupRestoreWithScheduling(t *testing.T) {
w := setupTest()
defer cleanup()

w.config.IONice = &ofst.IONiceSettings{
Class: types.Int32P(2),
ClassData: types.Int32P(3),
}
w.config.Nice = &ofst.NiceSettings{
Adjustment: types.Int32P(12),
}

backupOpt := BackupOptions{
BackupDirs: []string{targetDir},
}
backupOut, err := w.RunBackup(backupOpt)
if err != nil {
log.Fatal(err)
}
fmt.Println(backupOut)

// delete target then restore
if err = os.RemoveAll(targetDir); err != nil {
log.Fatal(err)
}
restoreOpt := RestoreOptions{
RestoreDirs: []string{targetDir},
}
restoreOut, err := w.RunRestore(restoreOpt)
if err != nil {
log.Fatal(err)
}
fmt.Println(restoreOut)

// check file
fileContentByte, err := ioutil.ReadFile(filepath.Join(targetDir, fileName))
if err != nil {
log.Fatal(err)
}
assert.Equal(t, fileContent, string(fileContentByte))
}

func TestBackupRestoreStdinWithScheduling(t *testing.T) {
w := setupTest()
defer cleanup()

w.config.IONice = &ofst.IONiceSettings{
Class: types.Int32P(2),
ClassData: types.Int32P(3),
}
w.config.Nice = &ofst.NiceSettings{
Adjustment: types.Int32P(12),
}

backupOpt := BackupOptions{
StdinPipeCommand: stdinPipeCommand,
StdinFileName: fileName,
}
backupOut, err := w.RunBackup(backupOpt)
if err != nil {
t.Fatal(err)
}
fmt.Println("backup output:", backupOut)

dumpOpt := DumpOptions{
FileName: fileName,
StdoutPipeCommand: stdoutPipeCommand,
}
dumpOut, err := w.Dump(dumpOpt)
if err != nil {
t.Fatal(err)
}
fmt.Println("dump output:", dumpOut)
}
Loading

0 comments on commit 1ede024

Please sign in to comment.