模拟 JVM 应用故障
Chaosd 可以通过 Byteman 模拟 JVM 应用故障,主要支持以下几种故障类型:
- 抛出自定义异常
- 触发垃圾回收
- 增加方法延迟
- 修改方法返回值
- 设置 Byteman 配置文件触发故障
- 增加 JVM 压力
同时,Chaosd 支持对常用的服务或其 Java 客户端注入上述的故障。比如,当 MySQL Java 客户端执行指定类型的 SQL 语句("select"
,"update"
,"insert"
,"replace"
或 "delete"
)时,你可以使用 Chaosd 在该客户端注入延迟或抛出异常。
本文主要介绍如何通过 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
mysql inject fault into MySQL client
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
抛出自定义异常相关配置说明
配置项 | 配置缩写 | 说明 | 值 |
---|---|---|---|
class | c | Java 类的名称 | string 类型,必须配置 |
exception | 无 | 抛出的自定义异常 | string 类型,必须配置 |
method | m | 方法名称 | string 类型,必须配置 |
pid | 无 | 需要注入故障的 Java 进程号 | int 类型,必须配置 |
port | 无 | 附加到 Java 进程 agent 的端口号,通过该端口号将故障注入到 Java 进程 | int 类型,默认为 9288 |
uid | 无 | 实验的编号 | string 类型,可以不配置,因为 Chaosd 会随机生成一个 |
抛出自定义异常示例
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
触发垃圾回收相关配置说明
配置项 | 配置缩写 | 说明 | 值 |
---|---|---|---|
pid | 无 | 需要注入故障的 Java 进程号 | int 类型,必须配置 |
port | 无 | 附加到 Java 进程 agent 的端口号,通过该端口号将故障注入到 Java 进程 | int 类型,默认为 9288 |
uid | 无 | 实验的编号 | string 类型,可以不配置,Chaosd 会随机生成一个 |
触发垃圾回收示例
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
触发垃圾回收为一次性操作,实验不需要恢复。