跳至主内容
版本:下一版本

模拟 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 itemAbbreviationDescriptionValue
classcThe name of the Java classstring type, required
exceptionNoneThe thrown custom exceptionstring type, required
methodmThe name of the methodstring type, required to be configured
pidNoneThe Java process ID where the fault is to be injectedint type, required
portNoneThe 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.
uidNoneThe experiment IDstring 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 itemAbbreviationDescriptionValue
pidNoneThe Java process ID where the fault is to be injectedint type, required
portNoneThe 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.
uidNoneThe experiment IDstring 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 itemAbbreviationDescriptionValue
classcThe name of the Java classstring type, required
latencyNoneThe duration of increasing method latencyint type, required. The unit is millisecond.
methodmThe name of the methodstring type, required
pidNoneThe Java process ID where the fault is to be injectedint type, required
portNoneThe 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.
uidNoneThe experiment IDstring 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 itemAbbreviationDescriptionValue
classcThe name of the Java classstring type, required to be configured
methodmThe name of the methodstring type, required to be configured
valueNoneSpecifies the return value of the methodstring 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".
pidNoneThe Java process ID where the fault is needed to be injectedint type, required to be configured
portNoneThe 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.
uidNoneThe experiment IDstring 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 itemAbbreviationDescriptionValue
pathNoneSpecifies the path of the Byteman configuration filestring type, required
pidNoneThe Java process ID where the fault is to be injectedint type, required
portNoneThe 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.
uidNoneThe experiment IDstring 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 itemAbbreviationDescriptionValue
cpu-countNoneThe number of CPU cores used for increasing JVM stressint type. You must configure one item between cpu-count and mem-type.
mem-typeNoneThe type of OOMstring type. Currently, both 'stack' and 'heap' OOM types are supported. You must configure one item between cpu-count and mem-type.
pidNoneThe Java process ID where the fault is to be injectedint type, required
portNoneThe 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.
uidNoneThe experiment IDstring 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

通过服务模式创建实验

请按以下步骤通过服务模式创建实验:

  1. 在服务模式下运行 Chaosd:

    chaosd server --port 31767
  2. 向 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 请求。

通过服务模式抛出自定义异常

抛出自定义异常的参数

ParameterDescriptionValue
actionThe action of the experimentSet to "exception"
classThe name of the Java classstring type, required
exceptionThe thrown custom exceptionstring type, required
methodThe name of the methodstring type, required
pidThe Java process ID where the fault is to be injectedint type, required
portThe 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.
uidThe experiment IDstring 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"}

通过服务模式触发垃圾回收

触发垃圾回收的参数

ParameterDescriptionValue
actionThe action of the experimentSet to "gc"
pidThe Java process ID where the fault is to be injectedint type, required
portThe 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.
uidThe experiment IDstring 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"}

触发垃圾回收是一次性操作,该实验无需恢复。

通过服务模式增加方法延迟

增加方法延迟的参数

ParameterDescriptionValue
actionThe action of the experimentSet to "latency"
classThe name of the Java classstring type, required
latencyThe duration of increasing method latencyint type, required. The unit is millisecond.
methodThe name of the methodstring type, required
pidThe Java process ID where the fault is to be injectedint type, required
portThe Java process ID where the fault is needed to be injectedint type, required
uidThe experiment IDstring 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"}

通过服务模式修改方法返回值

修改方法返回值的参数

ParameterDescriptionValue
actionThe action of the experimentSet to "return"
classThe name of the Java classstring type, required
methodThe name of the methodstring type, required
valueSpecifies the return value of the methodstring type, required. Currently, the item can be numeric and string types. If the item (return value) is string, double quotes are required, like "chaos".
pidThe Java process ID where the fault is to be injectedint type, required
portThe 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.
uidThe experiment IDstring 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 配置文件触发故障的参数

ParameterDescriptionValue
actionThe action of the experimentSet to "rule-data"
rule-dataSpecifies the Byteman configuration datastring type, required
pidThe Java process ID where the fault is to be injectedint type, required
portThe 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.
uidThe experiment IDstring 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-countmem-type 其中之一
mem-typeOOM 类型string 类型。目前支持 'stack'(栈)和 'heap'(堆)两种 OOM 类型。必须配置 cpu-countmem-type 其中之一
pidNone要注入故障的 Java 进程 IDint 类型,必填
portNone附加到 Java 进程代理的端口号,故障通过此端口注入 Java 进程int 类型,默认值 9288
uidNone实验 IDstring 类型。此项无需配置,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"}