模拟 JVM 应用故障
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
Chaosd 通过 Byteman 模拟 JVM 应用故障,支持以下故障类型:
-
抛出自定义异常
-
触发垃圾回收
-
增加方法延迟
-
修改方法返回值
-
通过 Byteman 配置文件触发故障
-
增加 JVM 压力
本文介绍如何使用 Chaosd 创建上述类型的 JVM 实验。
使用命令行模式创建实验
本节介绍如何通过命令行模式创建 JVM 应用故障实验。
创建实验前,可运行以下命令查看 Chaosd 支持的 JVM 应用故障类型:
chaosd attack jvm -h
结果如下:
JVM attack related commands
Usage:
chaosd attack jvm [command]
Available Commands:
exception throw specified exception for specified method
gc trigger GC for JVM
latency inject latency to specified method
return return specified value for specified method
rule-file inject fault with configured byteman rule file
stress inject stress to JVM
Flags:
-h, --help help for jvm
--pid int the pid of Java process which needs to attach
--port int the port of agent server (default 9288)
Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--uid string the experiment ID
Use "chaosd attack jvm [command] --help" for more information about a command.
使用命令行模式抛出自定义异常
抛出异常命令
查看抛出自定义异常命令的用法及配置项:
chaosd attack jvm exception --help
结果如下:
throw specified exception for specified method
Usage:
chaosd attack jvm exception [options] [flags]
Flags:
-c, --class string Java class name
--exception string the exception which needs to throw for action 'exception'
-h, --help help for exception
-m, --method string the method name in Java class
Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--pid int the pid of Java process which needs to attach
--port int the port of agent server (default 9288)
--uid string the experiment ID
抛出异常配置说明
| Configuration item | Abbreviation | Description | Value |
|---|---|---|---|
class | c | The name of the Java class | string type, required |
exception | None | The thrown custom exception | string type, required |
method | m | The name of the method | string type, required to be configured |
pid | None | The Java process ID where the fault is to be injected | int type, required |
port | None | The port number attached to the Java process agent. The fault is injected into the Java process through this port number. | int type. The default value is 9288. |
uid | None | The experiment ID | string type. This item is not required to be configured, because Chaosd randomly creates one. |
抛出异常示例
chaosd attack jvm exception -c Main -m sayhello --exception 'java.io.IOException("BOOM")' --pid 30045
结果如下:
[2021/08/05 02:39:39.106 +00:00] [INFO] [jvm.go:208] ["byteman rule"] [rule="\nRULE Main-sayhello-exception-q6nd0\nCLASS Main\nMETHOD sayhello\nAT ENTRY\nIF true\nDO \n\tthrow new java.io.IOException(\"BOOM\");\nENDRULE\n"] [file=/tmp/rule.btm296930759]
Attack jvm successfully, uid: 26a45ae2-d395-46f5-a126-2b2c6c85ae9d
使用命令行模式触发垃圾回收
垃圾回收命令
查看触发垃圾回收命令的用法及配置项:
chaosd attack jvm gc --help
trigger GC for JVM
Usage:
chaosd attack jvm gc [flags]
Flags:
-h, --help help for gc
Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--pid int the pid of Java process which needs to attach
--port int the port of agent server (default 9288)
--uid string the experiment ID
垃圾回收配置说明
| Configuration item | Abbreviation | Description | Value |
|---|---|---|---|
pid | None | The Java process ID where the fault is to be injected | int type, required |
port | None | The port number attached to the Java process agent. The fault is injected into the Java process through this port number. | int type. The default value is 9288. |
uid | None | The experiment ID | string type. This item is not required to be configured, because Chaosd randomly creates one. |
垃圾回收示例
chaosd attack jvm gc --pid 89345
结果如下:
[2021/08/05 02:49:47.850 +00:00] [INFO] [jvm.go:208] ["byteman rule"] [rule="\nRULE --gc-u0mlf\nGC\nENDRULE\n"] [file=/tmp/rule.btm012481052]
Attack jvm successfully, uid: f360e70a-5359-49b6-8526-d7e0a3c6f696
垃圾回收为一次性操作,实验无需恢复。
使用命令行模式增加方法延迟
增加延迟命令
查看增加方法延迟命令的用法及配置项:
chaosd attack jvm latency --help
结果如下:
inject latency to specified method
Usage:
chaosd attack jvm latency [options] [flags]
Flags:
-c, --class string Java class name
-h, --help help for latency
--latency int the latency duration, unit ms
-m, --method string the method name in Java class
Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--pid int the pid of Java process which needs to attach
--port int the port of agent server (default 9288)
--uid string the experiment ID
增加延迟配置说明
| Configuration item | Abbreviation | Description | Value |
|---|---|---|---|
class | c | The name of the Java class | string type, required |
latency | None | The duration of increasing method latency | int type, required. The unit is millisecond. |
method | m | The name of the method | string type, required |
pid | None | The Java process ID where the fault is to be injected | int type, required |
port | None | The port number attached to the Java process agent. The fault is injected into the Java process through this port number. | int type. The default value is 9288. |
uid | None | The experiment ID | string type. This item is not required to be configured, because Chaosd randomly creates one. |
增加延迟示例
chaosd attack jvm latency --class Main --method sayhello --latency 5000 --pid 100840
结果如下:
[2021/08/05 03:08:50.716 +00:00] [INFO] [jvm.go:208] ["byteman rule"] [rule="\nRULE Main-sayhello-latency-hlib2\nCLASS Main\nMETHOD sayhello\nAT ENTRY\nIF true\nDO \n\tThread.sleep(5000);\nENDRULE\n"] [file=/tmp/rule.btm359997255]
[2021/08/05 03:08:51.155 +00:00] [INFO] [jvm.go:94] ["submit rules"] [output="install rule Main-sayhello-latency-hlib2\n\n"]
Attack jvm successfully, uid: bbe00c57-ac9d-4113-bf0c-2a6f184be261
使用命令行模式修改方法返回值
修改返回值命令
查看修改方法返回值命令的用法及配置项:
chaosd attack jvm return --help
return specified value for specified method
Usage:
chaosd attack jvm return [options] [flags]
Flags:
-c, --class string Java class name
-h, --help help for return
-m, --method string the method name in Java class
--value string the return value for action 'return'. Only supports number and string types.
Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--pid int the pid of Java process which needs to attach
--port int the port of agent server (default 9288)
--uid string the experiment ID
修改返回值配置说明
| Configuration item | Abbreviation | Description | Value |
|---|---|---|---|
| class | c | The name of the Java class | string type, required to be configured |
| method | m | The name of the method | string type, required to be configured |
| value | None | Specifies the return value of the method | string type, required to be configured. Currently, the item can be numeric and string types. If the item (return value) is string, double quotes are required, like "chaos". |
| pid | None | The Java process ID where the fault is needed to be injected | int type, required to be configured |
| port | None | The port number attached to the Java process agent. The faults is injected into the Java process through this port number. | int type. The default value is 9288. |
uid | None | The experiment ID | string type. This item is not required to be configured, because Chaosd randomly creates one. |
修改返回值场景示例
chaosd attack jvm return --class Main --method getnum --value 999 --pid 112694
结果如下:
[2021/08/05 03:35:10.603 +00:00] [INFO] [jvm.go:208] ["byteman rule"] [rule="\nRULE Main-getnum-return-i6gb7\nCLASS Main\nMETHOD getnum\nAT ENTRY\nIF true\nDO \n\treturn 999;\nENDRULE\n"] [file=/tmp/rule.btm051982059]
[2021/08/05 03:35:10.820 +00:00] [INFO] [jvm.go:94] ["submit rules"] [output="install rule Main-getnum-return-i6gb7\n\n"]
Attack jvm successfully, uid: e2f204f6-4bed-4d92-aade-2b4a47b02e5d
使用命令行模式通过 Byteman 配置文件触发故障
您可在 Byteman 规则配置文件中设置故障规则,然后通过 Chaosd 指定配置文件路径注入故障。Byteman 规则配置请参考 byteman-rule-language。
配置文件触发命令
查看通过 Byteman 配置文件触发故障命令的用法及配置项:
chaosd attack jvm rule-file --help
结果如下:
inject fault with configured byteman rule file
Usage:
chaosd attack jvm rule-file [options] [flags]
Flags:
-h, --help help for rule-file
-p, --path string the path of configured byteman rule file
Global Flags:
--log-level string the log level of chaosd, the value can be 'debug', 'info', 'warn' and 'error'
--pid int the pid of Java process which needs to attach
--port int the port of agent server (default 9288)
--uid string the experiment ID
配置文件触发配置说明
| Configuration item | Abbreviation | Description | Value |
|---|---|---|---|
path | None | Specifies the path of the Byteman configuration file | string type, required |
pid | None | The Java process ID where the fault is to be injected | int type, required |
port | None | The port number attached to the Java process agent. The fault is injected into the Java process through this port number. | int type. The default value is 9288. |
uid | None | The experiment ID | string type. This item is not required to be configured, because Chaosd randomly creates one. |
配置文件触发示例
首先,根据具体 Java 程序并参考 Byteman 规则语言编写规则配置文件。例如:
RULE modify return value
CLASS Main
METHOD getnum
AT ENTRY
IF true
DO
return 9999
ENDRULE
将配置文件保存为 return.btm 后,运行以下命令注入故障:
chaosd attack jvm rule-file -p ./return.btm --pid 112694
结果如下:
[2021/08/05 03:45:40.757 +00:00] [INFO] [jvm.go:152] ["rule file data:RULE modify return value\nCLASS Main\nMETHOD getnum\nAT ENTRY\nIF true\nDO\n return 9999\nENDRULE\n"]
[2021/08/05 03:45:41.011 +00:00] [INFO] [jvm.go:94] ["submit rules"] [output="install rule modify return value\n\n"]
Attack jvm successfully, uid: 5ca2e06d-a7c6-421d-bb67-0c9908bac17a
通过命令行模式增加 JVM 压力
增加 JVM 压力的命令
查看增加 JVM 压力命令的用法及配置项:
chaosd attack jvm stress --help
结果如下:
inject stress to JVM
Usage:
chaosd attack jvm stress [options] [flags]
Flags:
--cpu-count int the CPU core number
-h, --help help for stress
--mem-type int the memory type to be allocated. The value can be 'stack' or 'heap'.
Global Flags:
--log-level string the log level of chaosd. The value can be 'debug', 'info', 'warn' and 'error'
--pid int the pid of Java process which needs to attach
--port int the port of agent server (default 9288)
--uid string the experiment ID
增加 JVM 压力的配置说明
| Configuration item | Abbreviation | Description | Value |
|---|---|---|---|
cpu-count | None | The number of CPU cores used for increasing JVM stress | int type. You must configure one item between cpu-count and mem-type. |
mem-type | None | The type of OOM | string type. Currently, both 'stack' and 'heap' OOM types are supported. You must configure one item between cpu-count and mem-type. |
pid | None | The Java process ID where the fault is to be injected | int type, required |
port | None | The port number attached to the Java process agent. The fault is injected into the Java process through this port number. | int type. The default value is 9288. |
uid | None | The experiment ID | string type. This item is not required to be configured, because Chaosd randomly creates one. |
增加 JVM 压力的示例
chaosd attack jvm stress --cpu-count 2 --pid 123546
结果如下:
[2021/08/05 03:59:51.256 +00:00] [INFO] [jvm.go:208] ["byteman rule"] [rule="\nRULE --stress-jfeiu\nSTRESS CPU\nCPUCOUNT 2\nENDRULE\n"] [file=/tmp/rule.btm773062009]
[2021/08/05 03:59:51.613 +00:00] [INFO] [jvm.go:94] ["submit rules"] [output="install rule --stress-jfeiu\n\n"]
Attack jvm successfully, uid: b9b997b5-0a0d-4f1f-9081-d52a32318b84
通过服务模式创建实验
请按以下步骤通过服务模式创建实验:
-
在服务模式下运行 Chaosd:
chaosd server --port 31767 -
向 Chaosd 服务的
/api/attack/{uid}路径发送 HTTP POST 请求:curl -X POST 172.16.112.130:31767/api/attack/jvm -H "Content-Type:application/json" -d '{fault-configuration}'上述命令中的
fault-configuration部分需根据故障类型配置。具体参数请参考后续各故障类型的参数说明及示例。
执行实验时请务必保存实验的 UID 信息。当需要结束对应 UID 的实验时,需向 Chaosd 服务的 /api/attack/{uid} 路径发送 HTTP DELETE 请求。
通过服务模式抛出自定义异常
抛出自定义异常的参数
| Parameter | Description | Value |
|---|---|---|
action | The action of the experiment | Set to "exception" |
class | The name of the Java class | string type, required |
exception | The thrown custom exception | string type, required |
method | The name of the method | string type, required |
pid | The Java process ID where the fault is to be injected | int type, required |
port | The port number attached to the Java process agent. The faults is injected into the Java process through this port number. | int type. The default value is 9288. |
uid | The experiment ID | string type. This item is not required to be configured, because Chaosd randomly creates one. |
通过服务模式抛出自定义异常的示例
curl -X POST 172.16.112.130:31767/api/attack/jvm -H "Content-Type:application/json" -d '{"action":"exception","class":"Main","method":"sayhello","exception":"java.io.IOException(\"BOOM\")","pid":1828622}'
结果如下:
{"status":200,"message":"attack successfully","uid":"c3c519bf-819a-4a7b-97fb-e3d0814481fa"}
通过服务模式触发垃圾回收
触发垃圾回收的参数
| Parameter | Description | Value |
|---|---|---|
action | The action of the experiment | Set to "gc" |
pid | The Java process ID where the fault is to be injected | int type, required |
port | The port number attached to the Java process agent. The fault is injected into the Java process through this port number. | int type. The default value is 9288. |
uid | The experiment ID | string type. This item is not required to be configured, because Chaosd randomly creates one. |
通过服务模式触发垃圾回收的示例
curl -X POST 172.16.112.130:31767/api/attack/jvm -H "Content-Type:application/json" -d '{"action":"gc","pid":1828622}'
结果如下:
{"status":200,"message":"attack successfully","uid":"c3c519bf-819a-4a7b-97fb-e3d0814481fa"}
触发垃圾回收是一次性操作,该实验无需恢复。
通过服务模式增加方法延迟
增加方法延迟的参数
| Parameter | Description | Value |
|---|---|---|
action | The action of the experiment | Set to "latency" |
class | The name of the Java class | string type, required |
latency | The duration of increasing method latency | int type, required. The unit is millisecond. |
method | The name of the method | string type, required |
pid | The Java process ID where the fault is to be injected | int type, required |
port | The Java process ID where the fault is needed to be injected | int type, required |
uid | The experiment ID | string type. This item is not required to be configured, because Chaosd randomly creates one. |
通过服务模式增加方法延迟的示例
curl -X POST 172.16.112.130:31767/api/attack/jvm -H "Content-Type:application/json" -d '{"action":"latency","class":"Main","method":"sayhello","latency":5000,"pid":1828622}'
结果如下:
{"status":200,"message":"attack successfully","uid":"a551206c-960d-4ac5-9056-518e512d4d0d"}
通过服务模式修改方法返回值
修改方法返回值的参数
| Parameter | Description | Value |
|---|---|---|
action | The action of the experiment | Set to "return" |
class | The name of the Java class | string type, required |
method | The name of the method | string type, required |
value | Specifies the return value of the method | string type, required. Currently, the item can be numeric and string types. If the item (return value) is string, double quotes are required, like "chaos". |
pid | The Java process ID where the fault is to be injected | int type, required |
port | The port number attached to the Java process agent. The fault is injected into the Java process through this port number. | int type. The default value is 9288. |
uid | The experiment ID | string type. This item is not required to be configured, because Chaosd randomly creates one. |
通过服务模式修改方法返回值的示例
curl -X POST 172.16.112.130:31767/api/attack/jvm -H "Content-Type:application/json" -d '{"action":"return","class":"Main","method":"getnum","value":"999","pid":1828622}'
结果如下:
{"status":200,"message":"attack successfully","uid":"a551206c-960d-4ac5-9056-518e512d4d0d"}
通过服务模式设置 Byteman 配置文件触发故障
您可根据 Byteman 规则配置设置故障规则。关于 Byteman 规则配置的详细信息,请参阅 byteman-rule-language。
设置 Byteman 配置文件触发故障的参数
| Parameter | Description | Value |
|---|---|---|
action | The action of the experiment | Set to "rule-data" |
rule-data | Specifies the Byteman configuration data | string type, required |
pid | The Java process ID where the fault is to be injected | int type, required |
port | The port number attached to the Java process agent. The fault is injected into the Java process through this port number. | int type. The default value is 9288. |
uid | The experiment ID | string type. This item is not required to be configured, because Chaosd randomly creates one. |
通过服务模式设置 Byteman 配置文件触发故障的示例
首先,根据具体 Java 程序并参考 Byteman 规则语言编写规则配置文件。例如:
RULE modify return value
CLASS Main
METHOD getnum
AT ENTRY
IF true
DO
return 9999
ENDRULE
接着将配置文件中的换行符转义为换行字符 "\n",并将转义后的文本作为 "rule-data" 的值。运行以下命令:
curl -X POST 127.0.0.1:31767/api/attack/jvm -H "Content-Type:application/json" -d '{"action":"rule-data","pid":30045,"rule-data":"\nRULE modify return value\nCLASS Main\nMETHOD getnum\nAT ENTRY\nIF true\nDO return 9999\nENDRULE\n"}'
结果如下:
{"status":200,"message":"attack successfully","uid":"a551206c-960d-4ac5-9056-518e512d4d0d"}
通过服务模式增加 JVM 压力
增加 JVM 压力的参数
| 参数 | 描述 | 值 | --- |
|---|---|---|---|
action | 实验操作类型 | 固定为 "stress" | |
cpu-count | 用于增加 CPU 压力的 CPU 核心数 | int 类型。必须配置 cpu-count 和 mem-type 其中之一 | |
mem-type | OOM 类型 | string 类型。目前支持 'stack'(栈)和 'heap'(堆)两种 OOM 类型。必须配置 cpu-count 和 mem-type 其中之一 | |
pid | None | 要注入故障的 Java 进程 ID | int 类型,必填 |
port | None | 附加到 Java 进程代理的端口号,故障通过此端口注入 Java 进程 | int 类型,默认值 9288 |
uid | None | 实验 ID | string 类型。此项无需配置,Chaosd 会自动生成随机 ID |
通过服务模式增加 JVM 压力的示例
curl -X POST 172.16.112.130:31767/api/attack/jvm -H "Content-Type:application/json" -d '{"action":"stress","cpu-count":1,"pid":1828622}'
结果如下:
{"status":200,"message":"attack successfully","uid":"a551206c-960d-4ac5-9056-518e512d4d0d"}