Skip to content

Commit fafc3eb

Browse files
authored
add parameter recover-cmd for process kill (#122)
* add parameter recover-cmd for process kill Signed-off-by: FingerLeader <[email protected]> * add parameter recover-cmd for process kill Signed-off-by: FingerLeader <[email protected]> * resolve confilct Signed-off-by: FingerLeader <[email protected]> * add test for recover-cmd Signed-off-by: FingerLeader <[email protected]> * fix some detail Signed-off-by: FingerLeader <[email protected]> * modify details Signed-off-by: FingerLeader <[email protected]> * modify details Signed-off-by: FingerLeader <[email protected]> * modify details Signed-off-by: FingerLeader <[email protected]>
1 parent 22a8a7c commit fafc3eb

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

cmd/attack/process.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func NewProcessKillCommand(dep fx.Option, options *core.ProcessCommand) *cobra.C
6262

6363
cmd.Flags().StringVarP(&options.Process, "process", "p", "", "The process name or the process ID")
6464
cmd.Flags().IntVarP(&options.Signal, "signal", "s", 9, "The signal number to send")
65+
cmd.Flags().StringVarP(&options.RecoverCmd, "recover-cmd", "r", "", "The command to be executed when recovering experiment")
6566

6667
return cmd
6768
}

pkg/core/process.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ type ProcessCommand struct {
3030
CommonAttackConfig
3131

3232
// Process defines the process name or the process ID.
33-
Process string `json:"process,omitempty"`
34-
Signal int `json:"signal,omitempty"`
35-
PIDs []int
33+
Process string `json:"process,omitempty"`
34+
Signal int `json:"signal,omitempty"`
35+
PIDs []int
36+
RecoverCmd string `json:"recoverCmd,omitempty"`
3637
// TODO: support these feature
3738
// Newest bool
3839
// Oldest bool

pkg/server/chaosd/process.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ package chaosd
1515

1616
import (
1717
"fmt"
18+
"os/exec"
1819
"strconv"
1920
"syscall"
2021

2122
"github.com/mitchellh/go-ps"
2223
"github.com/pingcap/errors"
24+
"go.uber.org/zap"
25+
26+
"github.com/pingcap/log"
2327

2428
"github.com/chaos-mesh/chaosd/pkg/core"
2529
)
@@ -65,13 +69,23 @@ func (processAttack) Recover(exp core.Experiment, _ Environment) error {
6569
}
6670
pcmd := config.(*core.ProcessCommand)
6771
if pcmd.Signal != int(syscall.SIGSTOP) {
68-
return core.ErrNonRecoverableAttack.New("only SIGSTOP process attack is supported to recover")
69-
}
72+
if pcmd.RecoverCmd == "" {
73+
return core.ErrNonRecoverableAttack.New("only SIGSTOP process attack and process attack with the recover-cmd are supported to recover")
74+
}
7075

71-
for _, pid := range pcmd.PIDs {
72-
if err := syscall.Kill(pid, syscall.SIGCONT); err != nil {
76+
rcmd := exec.Command("bash", "-c", pcmd.RecoverCmd)
77+
if err := rcmd.Start(); err != nil {
7378
return errors.WithStack(err)
7479
}
80+
81+
log.Info("Execute recover-cmd successfully", zap.String("recover-cmd", pcmd.RecoverCmd))
82+
83+
} else {
84+
for _, pid := range pcmd.PIDs {
85+
if err := syscall.Kill(pid, syscall.SIGCONT); err != nil {
86+
return errors.WithStack(err)
87+
}
88+
}
7589
}
7690

7791
return nil

test/integration_test/process/run.sh

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,36 @@ if [[ ${stat} != "S" ]]; then
5858
exit 1
5959
fi
6060

61-
# test kill attack
62-
${bin_path}/chaosd attack process kill -p ${pid} > proc.out
61+
# test kill attack and recover-cmd
62+
${bin_path}/chaosd attack process kill -p ${pid} -r "echo \"recover process\" >> proc.out" > proc.out
6363

6464
sleep 1
6565

66+
uid=$(cat proc.out | grep "Attack process ${pid} successfully" | awk -F: '{print $2}')
6667
stat=$(ps axo pid | grep ${pid})
6768

69+
# test kill attack
6870
if [[ -n ${stat} ]]; then
6971
echo "target process is not killed by processed kill attack"
7072
rm dummy.out
7173
rm proc.out
74+
kill ${pid}
75+
exit 1
76+
fi
77+
78+
# test recover-cmd
79+
${bin_path}/chaosd recover ${uid}
80+
recover_result=$(cat proc.out | awk 'END {print}')
81+
82+
if [[ ${recover_result} != "recover process" ]]; then
83+
echo "target recover process is not executed"
84+
rm dummy.out
85+
rm proc.out
7286
exit 1
7387
fi
7488

89+
7590
rm dummy.out
7691
rm proc.out
7792

78-
kill ${pid}
79-
8093
exit 0

0 commit comments

Comments
 (0)