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

JVM: refine with byteman-helper #128

Merged
merged 14 commits into from
Feb 15, 2022
7 changes: 4 additions & 3 deletions .github/workflows/release_latest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@ jobs:
run: |

# download tools
curl -fsSL -o byteman.tar.gz https://mirrors.chaos-mesh.org/latest/byteman.tar.gz
BYTEMAN_DIR=byteman-chaos-mesh-download-v4.0.18-0.9
curl -fsSL -o ${BYTEMAN_DIR}.tar.gz https://mirrors.chaos-mesh.org/${BYTEMAN_DIR}.tar.gz
curl -fsSL -o stress-ng https://mirrors.chaos-mesh.org/latest/stress-ng
tar zxvf byteman.tar.gz
tar zxvf ${BYTEMAN_DIR}.tar.gz
chmod +x ./stress-ng

# prepare package
mkdir chaosd-latest-linux-amd64
mkdir chaosd-latest-linux-amd64/tools
mv bin/chaosd chaosd-latest-linux-amd64/
mv bin/PortOccupyTool chaosd-latest-linux-amd64/tools/
mv byteman chaosd-latest-linux-amd64/tools/
mv ${BYTEMAN_DIR} chaosd-latest-linux-amd64/tools/byteman
mv stress-ng chaosd-latest-linux-amd64/tools/

# upload package
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/release_tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,18 @@ jobs:
GIT_TAG=${GITHUB_REF##*/}

# download tools
curl -fsSL -o byteman.tar.gz https://mirrors.chaos-mesh.org/latest/byteman.tar.gz
BYTEMAN_DIR=byteman-chaos-mesh-download-v4.0.18-0.9
curl -fsSL -o ${BYTEMAN_DIR}.tar.gz https://mirrors.chaos-mesh.org/${BYTEMAN_DIR}.tar.gz
curl -fsSL -o stress-ng https://mirrors.chaos-mesh.org/latest/stress-ng
tar zxvf byteman.tar.gz
tar zxvf ${BYTEMAN_DIR}.tar.gz
chmod +x ./stress-ng

# prepare package
mkdir chaosd-${GIT_TAG}-linux-amd64
mkdir chaosd-${GIT_TAG}-linux-amd64/tools
mv bin/chaosd chaosd-${GIT_TAG}-linux-amd64/
mv bin/PortOccupyTool chaosd-${GIT_TAG}-linux-amd64/tools/
mv byteman chaosd-${GIT_TAG}-linux-amd64/tools/
mv ${BYTEMAN_DIR} chaosd-${GIT_TAG}-linux-amd64/tools/byteman
mv stress-ng chaosd-${GIT_TAG}-linux-amd64/tools/

# upload package
Expand Down
103 changes: 77 additions & 26 deletions pkg/core/jvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,64 @@ import (
)

const (
// jvm action
JVMLatencyAction = "latency"
JVMExceptionAction = "exception"
JVMReturnAction = "return"
JVMStressAction = "stress"
JVMGCAction = "gc"
JVMRuleFileAction = "rule-file"
JVMRuleDataAction = "rule-data"

// for action 'gc' and 'stress'
GCHelper = "org.chaos_mesh.byteman.helper.GCHelper"
StressHelper = "org.chaos_mesh.byteman.helper.StressHelper"

// the trigger point for 'gc' and 'stress'
TriggerClass = "org.chaos_mesh.chaos_agent.TriggerThread"
TriggerMethod = "triggerFunc"
)

// byteman rule template
const (
SimpleRuleTemplate = `
RULE {{.Name}}
CLASS {{.Class}}
METHOD {{.Method}}
AT ENTRY
IF true
DO
{{.Do}};
ENDRULE
`

CompleteRuleTemplate = `
RULE {{.Name}}
CLASS {{.Class}}
METHOD {{.Method}}
HELPER {{.Helper}}
AT ENTRY
BIND {{.Bind}};
IF {{.Condition}}
DO
{{.Do}};
ENDRULE
`
)

type JVMCommand struct {
CommonAttackConfig

// rule name, should be unique, and will generate by chaosd automatically
Name string `json:"name,omitempty"`
JVMCommonSpec

// Java class
Class string `json:"class,omitempty"`
JVMClassMethodSpec

// the method in Java class
Method string `json:"method,omitempty"`
JVMStressSpec

// rule name, should be unique, and will generate by chaosd automatically
Name string `json:"name,omitempty"`

// fault action, values can be latency, exception, return, stress
// fault action, values can be latency, exception, return, stress, gc, rule-file, rule-data
Action string `json:"action,omitempty"`

// the return value for action 'return'
Expand All @@ -56,35 +92,50 @@ type JVMCommand struct {
// the latency duration for action 'latency'
LatencyDuration int `json:"latency,omitempty"`

// the CPU core number, only set it when action is stress
CPUCount int `json:"cpu-count,omitempty"`

// the memory type to be located, only set it when action is stress, the value can be 'stack' or 'heap'
MemoryType string `json:"mem-type,omitempty"`
// btm rule file path for action 'rule-file'
RuleFile string `json:"rule-file,omitempty"`

// attach or agent
Type string
// RuleData used to save the rule file's data, will use it when recover, for action 'rule-data'
RuleData string `json:"rule-data,omitempty"`
}

type JVMCommonSpec struct {
// the port of agent server
Port int `json:"port,omitempty"`

// the pid of Java process which needs to attach
// the pid of Java process which need to attach
Pid int `json:"pid,omitempty"`
}

// btm rule file path
RuleFile string `json:"rule-file,omitempty"`

// RuleData used to save the rule file's data, will use it when recover
RuleData string `json:"rule-data,omitempty"`
type JVMClassMethodSpec struct {
// Java class
Class string `json:"class,omitempty"`

// below is only used for template
Do string `json:"-"`
// the method in Java class
Method string `json:"method,omitempty"`
}

StressType string `json:"-"`
type JVMStressSpec struct {
// the CPU core number need to use, only set it when action is stress
CPUCount int `json:"cpu-count,omitempty"`

StressValueName string `json:"-"`
// the memory type need to locate, only set it when action is stress, the value can be 'stack' or 'heap'
MemoryType string `json:"mem-type,omitempty"`
}

StressValue string `json:"-"`
type BytemanTemplateSpec struct {
Name string
Class string
Method string
Helper string
Bind string
Condition string
Do string

// below is only used for stress template
StressType string
StressValueName string
StressValue string
}

func (j *JVMCommand) Validate() error {
Expand All @@ -108,7 +159,7 @@ func (j *JVMCommand) Validate() error {
return errors.New("class not provided")
}

if len(j.Method) == 0 {
if len(j.JVMClassMethodSpec.Method) == 0 {
return errors.New("method not provided")
}
case JVMRuleFileAction:
Expand Down
56 changes: 40 additions & 16 deletions pkg/core/jvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,62 +32,86 @@ func TestJVMCommand(t *testing.T) {
},
{
&JVMCommand{
Pid: 1234,
JVMCommonSpec: JVMCommonSpec{
Pid: 1234,
},
},
"action not provided",
},
{
&JVMCommand{
Pid: 1234,
JVMCommonSpec: JVMCommonSpec{
Pid: 1234,
},
Action: "test",
},
"action test not supported",
},
{
&JVMCommand{
Pid: 1234,
JVMCommonSpec: JVMCommonSpec{
Pid: 1234,
},
Action: JVMLatencyAction,
},
"class not provided",
},
{
&JVMCommand{
Pid: 1234,
JVMCommonSpec: JVMCommonSpec{
Pid: 1234,
},
Action: JVMExceptionAction,
Class: "test",
JVMClassMethodSpec: JVMClassMethodSpec{
Class: "test",
},
},
"method not provided",
},
{
&JVMCommand{
Pid: 1234,
JVMCommonSpec: JVMCommonSpec{
Pid: 1234,
},
Action: JVMExceptionAction,
Class: "test",
Method: "test",
JVMClassMethodSpec: JVMClassMethodSpec{
Class: "test",
Method: "test",
},
},
"",
},
{
&JVMCommand{
Pid: 1234,
JVMCommonSpec: JVMCommonSpec{
Pid: 1234,
},
Action: JVMStressAction,
},
"must set one of cpu-count and mem-type",
},
{
&JVMCommand{
Pid: 1234,
Action: JVMStressAction,
CPUCount: 1,
MemoryType: "heap",
JVMCommonSpec: JVMCommonSpec{
Pid: 1234,
},
Action: JVMStressAction,
JVMStressSpec: JVMStressSpec{
CPUCount: 1,
MemoryType: "heap",
},
},
"inject stress on both CPU and memory is not support now",
},
{
&JVMCommand{
Pid: 1234,
Action: JVMStressAction,
CPUCount: 1,
JVMCommonSpec: JVMCommonSpec{
Pid: 1234,
},
Action: JVMStressAction,
JVMStressSpec: JVMStressSpec{
CPUCount: 1,
},
},
"",
},
Expand Down
Loading