Skip to content

Commit

Permalink
Merge pull request #6459 from blackpiglet/6001_fix
Browse files Browse the repository at this point in the history
Add exit code log and possible memory shortage warning log for Restic command failure
  • Loading branch information
Lyndon-Li authored Jul 7, 2023
2 parents 2ea24f6 + d7f1ea4 commit 7deae4c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/6459-blackpiglet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add exit code log and possible memory shortage warning log for Restic command failure.
2 changes: 1 addition & 1 deletion pkg/repository/restic/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (r *RepositoryService) exec(cmd *restic.Command, bsl *velerov1api.BackupSto
cmd.ExtraFlags = append(cmd.ExtraFlags, skipTLSRet)
}

stdout, stderr, err := veleroexec.RunCommand(cmd.Cmd())
stdout, stderr, err := veleroexec.RunCommandWithLog(cmd.Cmd(), r.log)
r.log.WithFields(logrus.Fields{
"repository": cmd.RepoName(),
"command": cmd.String(),
Expand Down
4 changes: 3 additions & 1 deletion pkg/restic/exec_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func RunBackup(backupCmd *Command, log logrus.FieldLogger, updater uploader.Prog

err := cmd.Start()
if err != nil {
exec.LogErrorAsExitCode(err, log)
return stdoutBuf.String(), stderrBuf.String(), err
}

Expand Down Expand Up @@ -119,6 +120,7 @@ func RunBackup(backupCmd *Command, log logrus.FieldLogger, updater uploader.Prog

err = cmd.Wait()
if err != nil {
exec.LogErrorAsExitCode(err, log)
return stdoutBuf.String(), stderrBuf.String(), err
}
quit <- struct{}{}
Expand Down Expand Up @@ -229,7 +231,7 @@ func RunRestore(restoreCmd *Command, log logrus.FieldLogger, updater uploader.Pr
}
}()

stdout, stderr, err := exec.RunCommand(restoreCmd.Cmd())
stdout, stderr, err := exec.RunCommandWithLog(restoreCmd.Cmd(), log)
quit <- struct{}{}

// update progress to 100%
Expand Down
23 changes: 23 additions & 0 deletions pkg/util/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os/exec"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// RunCommand runs a command and returns its stdout, stderr, and its returned
Expand Down Expand Up @@ -52,3 +53,25 @@ func RunCommand(cmd *exec.Cmd) (string, string, error) {

return stdout, stderr, runErr
}

func RunCommandWithLog(cmd *exec.Cmd, log logrus.FieldLogger) (string, string, error) {
stdout, stderr, err := RunCommand(cmd)
LogErrorAsExitCode(err, log)
return stdout, stderr, err
}

func LogErrorAsExitCode(err error, log logrus.FieldLogger) {
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
log.Errorf("Restic command fail with ExitCode: %d. Process ID is %d, Exit error is: %s", exitError.ExitCode(), exitError.Pid(), exitError.String())
// Golang's os.exec -1 ExitCode means signal kill. Usually this is caused
// by CGroup's OOM. Log a warning to notice user.
// https://github.com/golang/go/blob/master/src/os/exec_posix.go#L128-L136
if exitError.ExitCode() == -1 {
log.Warnf("The ExitCode is -1, which means the process is terminated by signal. Usually this is caused by CGroup kill due to out of memory. Please check whether there is such information in the work nodes' dmesg log.")
}
} else {
log.WithError(err).Info("Error cannot be convert to ExitError format.")
}
}
}

0 comments on commit 7deae4c

Please sign in to comment.