Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Experiment Messages #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 16 additions & 125 deletions experiments/cpu-stress/experiment/cpu-stress.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/litmuschaos/m-agent/pkg/cpu"
"github.com/litmuschaos/m-agent/pkg/probes"
stressng "github.com/litmuschaos/m-agent/pkg/stress-ng"
"github.com/pkg/errors"
)

var (
Expand Down Expand Up @@ -60,94 +61,44 @@ func CPUStress(w http.ResponseWriter, r *http.Request) {

action, reqID, payload, err := messages.ListenForClientMessage(conn)
if err != nil {

if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetClientMessageReadErrorPrefix()+err.Error()); err != nil {
clientMessageReadLogger.Printf("Error occured while sending error message to client, err: %v", err)
}

if err := conn.Close(); err != nil {
clientMessageReadLogger.Printf("Error occured while closing the connection, err: %v", err)
}

messages.HandleActionExecutionError(conn, reqID, errorcodes.GetClientMessageReadErrorPrefix(), err, clientMessageReadLogger)
return
}

switch action {

case "CHECK_STEADY_STATE":
if err := stressng.CheckStressNG(); err != nil {

if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetSteadyStateCheckErrorPrefix()+err.Error()); err != nil {
steadyStateCheckErrorLogger.Printf("Error occured while sending error message to client, err: %v", err)
}

if err := conn.Close(); err != nil {
steadyStateCheckErrorLogger.Printf("Error occured while closing the connection, err: %v", err)
}

messages.HandleActionExecutionError(conn, reqID, errorcodes.GetSteadyStateCheckErrorPrefix(), err, steadyStateCheckErrorLogger)
return
}

if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, nil); err != nil {

steadyStateCheckErrorLogger.Printf("Error occured while sending feedback message to client, err: %v", err)

if err := conn.Close(); err != nil {
steadyStateCheckErrorLogger.Printf("Error occured while closing the connection, err: %v", err)
}

messages.HandleFeedbackTransmissionError(conn, err, steadyStateCheckErrorLogger)
return
}

case "EXECUTE_EXPERIMENT":
cmd, err = cpu.StressCPU(payload, &stdout, &stderr)

if err != nil {

if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetExecuteExperimentErrorPrefix()+err.Error()); err != nil {
executeExperimentErrorLogger.Printf("Error occured while sending error message to client, err: %v", err)
}

if err := conn.Close(); err != nil {
executeExperimentErrorLogger.Printf("Error occured while closing the connection, err: %v", err)
}

messages.HandleActionExecutionError(conn, reqID, errorcodes.GetExecuteExperimentErrorPrefix(), err, executeExperimentErrorLogger)
return
}

if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, nil); err != nil {

executeExperimentErrorLogger.Printf("Error occured while sending feedback message to client, err: %v", err)

if err := conn.Close(); err != nil {
executeExperimentErrorLogger.Printf("Error occured while closing the connection, err: %v", err)
}

messages.HandleFeedbackTransmissionError(conn, err, executeExperimentErrorLogger)
return
}

case "CHECK_LIVENESS":
if err := stressng.CheckStressNGProcessLiveness(cmd.Process.Pid); err != nil {

if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetLivenessCheckErrorPrefix()+err.Error()); err != nil {
livenessCheckErrorLogger.Printf("Error occured while sending error message to client, err: %v", err)
}

if err := conn.Close(); err != nil {
livenessCheckErrorLogger.Printf("Error occured while closing the connection, err: %v", err)
}

messages.HandleActionExecutionError(conn, reqID, errorcodes.GetLivenessCheckErrorPrefix(), err, livenessCheckErrorLogger)
return
}

if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, nil); err != nil {

livenessCheckErrorLogger.Printf("Error occured while sending feedback message to client, err: %v", err)

if err := conn.Close(); err != nil {
livenessCheckErrorLogger.Printf("Error occured while closing the connection, err: %v", err)
}

messages.HandleFeedbackTransmissionError(conn, err, livenessCheckErrorLogger)
return
}

Expand All @@ -157,88 +108,42 @@ func CPUStress(w http.ResponseWriter, r *http.Request) {
if err != nil {

if err := messages.SendMessageToClient(conn, "PROBE_ERROR", reqID, errorcodes.GetCommandProbeExecutionErrorPrefix()+err.Error()); err != nil {

commandProbeExecutionErrorLogger.Printf("Error occured while sending error message to client, err: %v", err)

if err := conn.Close(); err != nil {
commandProbeExecutionErrorLogger.Printf("Error occured while closing the connection, err: %v", err)
}

messages.HandleFeedbackTransmissionError(conn, err, commandProbeExecutionErrorLogger)
return
}

} else {

if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, cmdProbeStdout); err != nil {

commandProbeExecutionErrorLogger.Printf("Error occured while sending feedback message to client, err: %v", err)

if err := conn.Close(); err != nil {
commandProbeExecutionErrorLogger.Printf("Error occured while closing the connection, err: %v", err)
}

messages.HandleFeedbackTransmissionError(conn, err, commandProbeExecutionErrorLogger)
return
}
}

case "REVERT_CHAOS":
if err := stressng.RevertStressNGProcess(cmd, &stderr); err != nil {

if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetChaosRevertErrorPrefix()+err.Error()); err != nil {
chaosRevertErrorLogger.Printf("Error occured while sending error message to client, err: %v", err)
}

if err := conn.Close(); err != nil {
chaosRevertErrorLogger.Printf("Error occured while closing the connection, err: %v", err)
}

messages.HandleActionExecutionError(conn, reqID, errorcodes.GetChaosRevertErrorPrefix(), err, chaosRevertErrorLogger)
return
}

if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, stdout.String()); err != nil {

chaosRevertErrorLogger.Printf("Error occured while sending feedback message to client, err: %v", err)

if err := conn.Close(); err != nil {
chaosRevertErrorLogger.Printf("Error occured while closing the connection, err: %v", err)
}

messages.HandleFeedbackTransmissionError(conn, err, chaosRevertErrorLogger)
return
}

case "ABORT_EXPERIMENT":
if err := stressng.AbortStressNGProcess(cmd); err != nil {

if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetChaosAbortErrorPrefix()+err.Error()); err != nil {
chaosAbortErrorLogger.Printf("Error occured while sending error message to client, err: %v", err)
}

if err := conn.Close(); err != nil {
chaosAbortErrorLogger.Printf("Error occured while closing the connection, err: %v", err)
}

messages.HandleActionExecutionError(conn, reqID, errorcodes.GetChaosAbortErrorPrefix(), err, chaosAbortErrorLogger)
return
}

if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, nil); err != nil {

chaosAbortErrorLogger.Printf("Error occured while sending feedback message to client, err: %v", err)

if err := conn.Close(); err != nil {
chaosAbortErrorLogger.Printf("Error occured while closing the connection, err: %v", err)
}

messages.HandleFeedbackTransmissionError(conn, err, chaosAbortErrorLogger)
return
}

if err := messages.SendMessageToClient(conn, "CLOSE_CONNECTION", reqID, nil); err != nil {

chaosAbortErrorLogger.Printf("Error occured while sending feedback message to client, err: %v", err)

if err := conn.Close(); err != nil {
chaosAbortErrorLogger.Printf("Error occured while closing the connection, err: %v", err)
}

messages.HandleFeedbackTransmissionError(conn, err, chaosAbortErrorLogger)
return
}

Expand All @@ -250,14 +155,7 @@ func CPUStress(w http.ResponseWriter, r *http.Request) {

case "CLOSE_CONNECTION":
if err := messages.SendMessageToClient(conn, "CLOSE_CONNECTION", reqID, nil); err != nil {

closeConnectionErrorLogger.Printf("Error occured while sending feedback message to client, err: %v", err)

if err := conn.Close(); err != nil {
closeConnectionErrorLogger.Printf("Error occured while closing the connection, err: %v", err)
}

return
}

if err := conn.Close(); err != nil {
Expand All @@ -267,14 +165,7 @@ func CPUStress(w http.ResponseWriter, r *http.Request) {
return

default:
if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetInvalidActionErrorPrefix()+"Invalid action: "+action); err != nil {
invalidActionErrorLogger.Printf("Error occured while sending error message to client, err: %v", err)
}

if err := conn.Close(); err != nil {
invalidActionErrorLogger.Printf("Error occured while closing the connection, err: %v", err)
}

messages.HandleActionExecutionError(conn, reqID, errorcodes.GetInvalidActionErrorPrefix(), errors.Errorf("Invalid action: "+action), invalidActionErrorLogger)
return
}
}
Expand Down
91 changes: 12 additions & 79 deletions experiments/process-kill/experiment/process-kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/litmuschaos/m-agent/internal/m-agent/upgrader"
"github.com/litmuschaos/m-agent/pkg/probes"
"github.com/litmuschaos/m-agent/pkg/process"
"github.com/pkg/errors"
)

// ProcessKill listens for the client actions and executes them as appropriate
Expand All @@ -49,121 +50,60 @@ func ProcessKill(w http.ResponseWriter, r *http.Request) {

action, reqID, payload, err := messages.ListenForClientMessage(conn)
if err != nil {

if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetClientMessageReadErrorPrefix()+err.Error()); err != nil {
clientMessageReadLogger.Printf("Error occurred while sending error message to client, %v", err)
}

if err := conn.Close(); err != nil {
clientMessageReadLogger.Printf("Error occurred while closing the connection, err: %v", err)
}

messages.HandleActionExecutionError(conn, reqID, errorcodes.GetClientMessageReadErrorPrefix(), err, clientMessageReadLogger)
return
}

switch action {

case "CHECK_STEADY_STATE":
if err := process.ProcessStateCheck(payload); err != nil {

if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetSteadyStateCheckErrorPrefix()+err.Error()); err != nil {
steadyStateCheckErrorLogger.Printf("Error occurred while sending error message to client, err: %v", err)
}

if err := conn.Close(); err != nil {
steadyStateCheckErrorLogger.Printf("Error occurred while closing the connection, err: %v", err)
}

messages.HandleActionExecutionError(conn, reqID, errorcodes.GetSteadyStateCheckErrorPrefix(), err, steadyStateCheckErrorLogger)
return
}

if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, nil); err != nil {

steadyStateCheckErrorLogger.Printf("Error occurred while sending feedback message to client, %v", err)

if err := conn.Close(); err != nil {
steadyStateCheckErrorLogger.Printf("Error occurred while closing the connection, err: %v", err)
}

messages.HandleFeedbackTransmissionError(conn, err, steadyStateCheckErrorLogger)
return
}

case "EXECUTE_EXPERIMENT":
if err := process.KillTargetProcesses(payload); err != nil {

if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetExecuteExperimentErrorPrefix()+err.Error()); err != nil {
executeExperimentErrorLogger.Printf("Error occurred while sending error message to client, err: %v", err)
}

if err := conn.Close(); err != nil {
executeExperimentErrorLogger.Printf("Error occurred while closing the connection, err: %v", err)
}

messages.HandleActionExecutionError(conn, reqID, errorcodes.GetExecuteExperimentErrorPrefix(), err, executeExperimentErrorLogger)
return
}

if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, nil); err != nil {

executeExperimentErrorLogger.Printf("Error occurred while sending feedback message to client, err: %v", err)

if err := conn.Close(); err != nil {
executeExperimentErrorLogger.Printf("Error occurred while closing the connection, err: %v", err)
}

messages.HandleFeedbackTransmissionError(conn, err, executeExperimentErrorLogger)
return
}

case "EXECUTE_COMMAND":
stdout, err := probes.ExecuteCmdProbeCommand(payload)
cmdProbeStdout, err := probes.ExecuteCmdProbeCommand(payload)

if err != nil {
if err := messages.SendMessageToClient(conn, "PROBE_ERROR", reqID, errorcodes.GetCommandProbeExecutionErrorPrefix()+err.Error()); err != nil {

commandProbeExecutionErrorLogger.Printf("Error occurred while sending error message to client, err: %v", err)

if err := conn.Close(); err != nil {
commandProbeExecutionErrorLogger.Printf("Error occurred while closing the connection, err: %v", err)
}

messages.HandleFeedbackTransmissionError(conn, err, commandProbeExecutionErrorLogger)
return
}

} else {

if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, stdout); err != nil {

commandProbeExecutionErrorLogger.Printf("Error occurred while sending feedback message to client, err: %v", err)

if err := conn.Close(); err != nil {
commandProbeExecutionErrorLogger.Printf("Error occurred while closing the connection, err: %v", err)
}

if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, cmdProbeStdout); err != nil {
messages.HandleFeedbackTransmissionError(conn, err, commandProbeExecutionErrorLogger)
return
}
}

case "CHECK_LIVENESS":
if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, nil); err != nil {

livenessCheckErrorLogger.Printf("Error occurred while sending feedback message to client, err: %v", err)

if err := conn.Close(); err != nil {
livenessCheckErrorLogger.Printf("Error occurred while closing the connection, err: %v", err)
}

messages.HandleFeedbackTransmissionError(conn, err, livenessCheckErrorLogger)
return
}

case "CLOSE_CONNECTION":
if err := messages.SendMessageToClient(conn, "CLOSE_CONNECTION", reqID, nil); err != nil {

closeConnectionErrorLogger.Printf("Error occurred while sending feedback message to client, err: %v", err)

if err := conn.Close(); err != nil {
closeConnectionErrorLogger.Printf("Error occurred while closing the connection, err: %v", err)
}

return
}

if err := conn.Close(); err != nil {
Expand All @@ -173,14 +113,7 @@ func ProcessKill(w http.ResponseWriter, r *http.Request) {
return

default:
if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetInvalidActionErrorPrefix()+"Invalid action: "+action); err != nil {
invalidActionErrorLogger.Printf("Error occurred while sending error message to client, err: %v", err)
}

if err := conn.Close(); err != nil {
invalidActionErrorLogger.Printf("Error occurred while closing the connection, err: %v", err)
}

messages.HandleActionExecutionError(conn, reqID, errorcodes.GetInvalidActionErrorPrefix(), errors.Errorf("Invalid action: "+action), invalidActionErrorLogger)
return
}
}
Expand Down
Loading