Arthas Async Jobs tutorial (#1354)

pull/1367/head
Hollow Man 5 years ago committed by GitHub
parent 86a45e9ce2
commit e30eaae715
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,9 @@
Arthas异步调用
Arthas后台异步任务
===
arthas中的异步调用使用了仿linux系统任务相关的命令。[linux任务相关介绍](https://ehlxr.me/2017/01/18/Linux-%E4%B8%AD-fg%E3%80%81bg%E3%80%81jobs%E3%80%81-%E6%8C%87%E4%BB%A4/)。
[`后台异步任务`在线教程](https://alibaba.github.io/arthas/arthas-tutorials?language=cn&id=case-async-jobs)
arthas中的后台异步任务使用了仿linux系统任务相关的命令。[linux任务相关介绍](https://ehlxr.me/2017/01/18/Linux-%E4%B8%AD-fg%E3%80%81bg%E3%80%81jobs%E3%80%81-%E6%8C%87%E4%BB%A4/)。
## 1. 使用&在后台执行任务
@ -41,7 +44,7 @@ $ jobs
* 非当前session创建的job只能由当前session fg到前台执行
## 5. 任务输出重定向
可通过`>`或者`>>`将任务输出结果输出到指定的文件中,可以和`&`一起使用实现arthas命令的异步调用。比如:
可通过`>`或者`>>`将任务输出结果输出到指定的文件中,可以和`&`一起使用实现arthas命令的后台异步任务。比如:
```bash
$ trace Test t >> test.out &

@ -70,7 +70,7 @@ Arthas provides `pipe` to process the result returned from commands further, e.g
* plaintext - remove the ANSI color
* wc - count lines
## async in background
## async jobs
[async](async.md) can be handy when a problem is hardly to reproduce in the production environment, e.g. one `watch` condition may happen only once in one single day.

@ -1,6 +1,8 @@
Arthas Async Jobs
===
[`Async Jobs` online tutorial](https://alibaba.github.io/arthas/arthas-tutorials?language=en&id=case-async-jobs)
Asynchronous jobs in arthas. The idea is borrowed from [linux jobs](http://man7.org/linux/man-pages/man1/jobs.1p.html).

@ -0,0 +1,16 @@
在新的`Terminal 2`里,下载`arthas-boot.jar`,再用`java -jar`命令启动:
`wget https://alibaba.github.io/arthas/arthas-boot.jar
java -jar arthas-boot.jar`{{execute T2}}
`arthas-boot`是`Arthas`的启动程序它启动后会列出所有的Java进程用户可以选择需要诊断的目标进程。
选择第一个进程,输入 `1`{{execute T2}} ,再`Enter/回车`
Attach成功之后会打印Arthas LOGO。输入 `help`{{execute T2}} 可以获取到更多的帮助信息。
![Arthas Boot](/arthas/scenarios/common-resources/assets/arthas-boot.png)

@ -0,0 +1,10 @@
下载`arthas-demo.jar`,再用`java -jar`命令启动:
`wget https://alibaba.github.io/arthas/arthas-demo.jar
java -jar arthas-demo.jar`{{execute T1}}
`arthas-demo`是一个很简单的程序,它随机生成整数,再执行因式分解,把结果打印出来。如果生成的随机数是负数,则会打印提示信息。

@ -0,0 +1,2 @@
arthas中的后台异步任务使用了仿linux系统任务相关的命令。[linux任务相关介绍](https://ehlxr.me/2017/01/18/Linux-%E4%B8%AD-fg%E3%80%81bg%E3%80%81jobs%E3%80%81-%E6%8C%87%E4%BB%A4/)。

@ -0,0 +1,10 @@
通过本教程基本掌握了Arthas后台异步任务。更多高级特性可以在下面的进阶指南里继续了解。
* [Arthas进阶](https://alibaba.github.io/arthas/arthas-tutorials?language=cn&id=arthas-advanced)
* [Arthas Github](https://github.com/alibaba/arthas)
* [Arthas 文档](https://alibaba.github.io/arthas/)
欢迎关注公众号获取Arthas项目的信息源码分析案例实践。
![Arthas公众号](/arthas/scenarios/common-resources/assets/qrcode_gongzhonghao.jpg)

@ -0,0 +1,63 @@
{
"title": "Arthas 后台异步任务 案例",
"description": "Arthas 后台异步任务 案例",
"difficulty": "高级使用者",
"time": "10分钟",
"details": {
"steps": [
{
"title": "Arthas demo",
"text": "arthas-demo.md"
},
{
"title": "Start arthas-boot",
"text": "arthas-boot.md"
},
{
"title": "后台异步任务",
"text": "async-jobs.md"
},
{
"title": "使用&在后台执行任务",
"text": "run-cmd-in-backgd.md"
},
{
"title": "通过jobs查看任务",
"text": "list-backgd-jobs.md"
},
{
"title": "任务暂停和取消",
"text": "suspend-cancel-job.md"
},
{
"title": "fg、bg命令将命令转到前台、后台继续执行",
"text": "switch-foregd-backgd.md"
},
{
"title": "任务输出重定向",
"text": "redirect-output.md"
},
{
"title": "停止命令",
"text": "stop-job.md"
},
{
"title": "其他",
"text": "others.md"
}
],
"intro": {
"text": "intro.md"
},
"finish": {
"text": "finish.md"
}
},
"environment": {
"uilayout": "terminal"
},
"backend": {
"imageid": "java",
"environmentsprotocol": "http"
}
}

@ -0,0 +1,23 @@
![Arthas](https://alibaba.github.io/arthas/_images/arthas.png)
`Arthas` 是Alibaba开源的Java诊断工具深受开发者喜爱。在线排查问题无需重启动态跟踪Java代码实时监控JVM状态。
`Arthas` 支持JDK 6+支持Linux/Mac/Windows采用命令行交互模式同时提供丰富的 `Tab` 自动补全功能,进一步方便进行问题的定位和诊断。
当你遇到以下类似问题而束手无策时Arthas可以帮助你解决
- 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception
- 我改的代码为什么没有执行到?难道是我没 commit分支搞错了
- 遇到问题无法在线上 debug难道只能通过加日志再重新发布吗
- 线上遇到某个用户的数据处理有问题,但线上同样无法 debug线下无法重现
- 是否有一个全局视角来查看系统的运行状况?
- 有什么办法可以监控到JVM的实时运行状态
- 怎么快速定位应用的热点,生成火焰图?
本教程会以一个简单的应用为例,演示后台异步任务。
* Github: https://github.com/alibaba/arthas
* 文档: https://alibaba.github.io/arthas/

@ -0,0 +1,24 @@
如果希望查看当前有哪些arthas任务在执行可以执行jobs命令执行结果如下
`jobs`{{execute T2}}
```bash
$ jobs
[1]*
Running trace demo.MathGame primeFactors &
execution count : 49
start time : Wed Jul 22 05:47:52 GMT 2020
timeout date : Thu Jul 23 05:47:52 GMT 2020
session : aa75753d-74f1-4929-a829-7ff965345183 (current)
```
可以看到目前有一个后台任务在执行。
job id是10, `*` 表示此job是当前session创建
状态是Stopped
execution count是执行次数从启动开始已经执行了19次
timeout date是超时的时间到这个时间任务将会自动超时退出

@ -0,0 +1,6 @@
* 最多同时支持8个命令使用重定向将结果写日志
* 请勿同时开启过多的后台异步命令以免对目标JVM性能造成影响
* 如果不想停止arthas继续执行后台任务可以执行 `quit` 退出arthas控制台`stop` 会停止arthas 服务)

@ -0,0 +1,20 @@
可通过>或者>>将任务输出结果输出到指定的文件中,可以和&一起使用实现arthas命令的后台异步任务。比如
`trace demo.MathGame primeFactors >> test.out &`{{execute T2}}
这时trace命令会在后台执行并且把结果输出到`~/logs/arthas-cache/test.out`。可继续执行其他命令。并可查看文件中的命令执行结果。
`cat test.out`{{execute T2}}
当连接到远程的arthas server时可能无法查看远程机器的文件arthas同时支持了自动重定向到本地缓存路径。使用方法如下
`trace demo.MathGame primeFactors >> &`{{execute T2}}
```bash
$ trace Test t >> &
job id : 2
cache location : /Users/gehui/logs/arthas-cache/28198/2
```
可以看到并没有指定重定向文件位置arthas自动重定向到缓存中了执行命令后会输出`job id`和`cache location`。`cache location`就是重定向文件的路径在系统logs目录下路径包括`pid`和`job id`,避免和其他任务冲突。命令输出结果到`/Users/gehui/logs/arthas-cache/28198/2`中,`job id`为2。

@ -0,0 +1,6 @@
比如希望执行后台执行trace命令那么调用下面命令
`trace demo.MathGame primeFactors &`{{execute T2}}
这时命令在后台执行可以在console中继续执行其他命令。

@ -0,0 +1,2 @@
异步执行的命令,如果希望停止,可执行`kill`

@ -0,0 +1,8 @@
当任务正在前台执行,比如直接调用命令`trace Test t`或者调用后台执行命令`trace Test t &`后又通过fg命令将任务转到前台。这时console中无法继续执行命令但是可以接收并处理以下事件
`ctrl + z`将任务暂停。通过jbos查看任务状态将会变为Stopped通过`bg <job-id>`或者`fg <job-id>`可让任务重新开始执行
`ctrl + c`:停止任务
`ctrl + d`按照linux语义应当是退出终端目前arthas中是空实现不处理

@ -0,0 +1,6 @@
* When a job is executed in the background or in suspended status (use `ctrl + z` to suspend job), `fg <job-id>` can transfer the job to the foreground to continue to run.
* When a job is in suspended status (use `ctrl + z` to suspend job), `bg <job-id>` can put the job to the background to continue to run.
* A job created by other session can only be put to the foreground to run by using `fg` in the current session.

@ -0,0 +1,16 @@
In the new `Terminal 2`, download `arthas-boot.jar` and start with the `java -jar` command:
`wget https://alibaba.github.io/arthas/arthas-boot.jar
java -jar arthas-boot.jar`{{execute T2}}
`arthas-boot` is the launcher for `Arthas`. It lists all the Java processes, and the user can select the target process to be diagnosed.
Select the first process, type `1`{{execute T2}} then type `Enter`
After the Attach is successful, Arthas LOGO is printed. Enter `help`{{execute T2}} for more help.
![Arthas Boot](/arthas/scenarios/common-resources/assets/arthas-boot.png)

@ -0,0 +1,11 @@
Download `arthas-demo.jar` and start with the `java -jar` command:
`wget https://alibaba.github.io/arthas/arthas-demo.jar
java -jar arthas-demo.jar`{{execute T1}}
`arthas-demo` is a very simple program that randomly generates integers, performs factorization, and prints the results.
If the generated random number is negative, a error message will be printed.

@ -0,0 +1,2 @@
Asynchronous jobs in arthas. The idea is borrowed from [linux jobs](http://man7.org/linux/man-pages/man1/jobs.1p.html).

@ -0,0 +1,6 @@
Through this tutorial, you can know Arthas Async Jobs. More advanced features can be found in the Advanced Guide below.
* [Arthas Advanced](https://alibaba.github.io/arthas/arthas-tutorials?language=en&id=arthas-advanced)
* [Arthas Github](https://github.com/alibaba/arthas)
* [Arthas Documentation](https://alibaba.github.io/arthas/en)

@ -0,0 +1,63 @@
{
"title": "Arthas Async Jobs",
"description": "Arthas Async Jobs",
"difficulty": "expert",
"time": "10 minutes",
"details": {
"steps": [
{
"title": "Arthas demo",
"text": "arthas-demo.md"
},
{
"title": "Start arthas-boot",
"text": "arthas-boot.md"
},
{
"title": "Async Jobs",
"text": "async-jobs.md"
},
{
"title": "Use & to run the command in the background",
"text": "run-cmd-in-backgd.md"
},
{
"title": "List background jobs",
"text": "list-backgd-jobs.md"
},
{
"title": "Suspend and cancel job",
"text": "suspend-cancel-job.md"
},
{
"title": "fg/bg, switch the job from the foreground to the background, and vise verse",
"text": "switch-foregd-backgd.md"
},
{
"title": "Redirect the output",
"text": "redirect-output.md"
},
{
"title": "Stop job",
"text": "stop-job.md"
},
{
"title": "Others",
"text": "others.md"
}
],
"intro": {
"text": "intro.md"
},
"finish": {
"text": "finish.md"
}
},
"environment": {
"uilayout": "terminal"
},
"backend": {
"imageid": "java",
"environmentsprotocol": "http"
}
}

@ -0,0 +1,40 @@
![Arthas](https://alibaba.github.io/arthas/_images/arthas.png)
`Arthas` is a Java diagnostic tool open-sourced by Alibaba middleware team. Arthas helps developers in trouble-shooting issues in production environment for Java based applications without modifying code or restarting servers.
`Arthas` supports JDK 6+, supports Linux/Mac/Windows.
## Background
Oftentimes the production system network is inaccessible from local development environment. If issues are encountered in production systems, it is impossible to use IDE to debug the application remotely. Whats even worse, debugging in production environment is unacceptable, as it will suspend all the threads, leading to services downtime.
Developers could always try to reproduce the same issue on the test/staging environment. However, this is tricky as some issues cannot be reproduced easily in a different environment, or even disappear once restarted.
And if youre thinking of adding some logs to your code to help trouble-shoot the issue, you will have to go through the following lifecycle: test, staging, and then to production. Time is money! This approach is inefficient! Worse still, the issue may not be fixed since it might be irreproducible once the JVM is restarted, as described above.
Arthas is built to solve these issues. A developer can troubleshoot production issues on the fly. No JVM restart, no additional code changes. Arthas works as an observer, that is, it will never suspend your running threads.
## Key features
- Check whether a class is loaded? Or where the class is loaded from? (Useful for trouble-shooting jar file conflicts)
- Decompile a class to ensure the code is running as expected.
- Check classloader statistics, e.g. the number of classloaders, the number of classes loaded per classloader, the classloader hierarchy, possible classloader leaks, etc.
- Check the method invocation details, e.g. method parameter, returned values, exceptions and etc.
- Check the stack trace of specified method invocation. This is useful when a developer wants to know the caller of the method.
- Trace the method invocation to find slow sub-invocations.
- Monitor method invocation statistics, e.g. QPS (Query Per Second), RT (Return Time), success rate and etc.
- Monitor system metrics, thread states and CPU usage, GC statistics and etc.
- Supports command line interactive mode, with auto-complete feature enabled.
- Supports telnet and WebSocket, which enables both local and remote diagnostics with command line and browsers.
- Supports profiler/Flame Graph
- Supports JDK 6+
- Supports Linux/Mac/Windows
This tutorial takes a simple application as an example to demonstrate the the use case of Async Jobs.
* Github: https://github.com/alibaba/arthas
* Docs: https://alibaba.github.io/arthas/

@ -0,0 +1,24 @@
If you want to list all background jobs, you can execute the jobs command and the results are as follows:
`jobs`{{execute T2}}
```bash
$ jobs
[1]*
Running trace demo.MathGame primeFactors &
execution count : 49
start time : Wed Jul 22 05:47:52 GMT 2020
timeout date : Thu Jul 23 05:47:52 GMT 2020
session : aa75753d-74f1-4929-a829-7ff965345183 (current)
```
You can see that there is currently a background job executing:
job id is 10, `*` indicates that this job is created by the current session.
status is Stopped
execution count is the number of executions, which have been executed 19 times since the start.
timeout date: timeout timestamp, when the time exceeds this timestamp, the job will be automatically timeout and exit.

@ -0,0 +1,6 @@
* Support up to 8 commands at the same time to redirect the output to the log files.
* Do not open too many background jobs at the same time to avoid negative performance effect to the target JVM.
* If you do not want to stop the Arthas service and continue to perform background tasks, you can exit the Arthas console by executing `quit` command (`stop` command will stop the Arthas service)

@ -0,0 +1,20 @@
The job output can be redirect to the specified file by > or >>, and can be used together with &. By doing this, you can achieve running commands asynchronously, for example:
`trace demo.MathGame primeFactors >> test.out &`{{execute T2}}
The trace command will be running in the background and the output will be redirect to `~/logs/arthas-cache/test.out`. You can continue to execute other commands in the console, at the same time, you can also examine the execution result from the output file.
`cat test.out`{{execute T2}}
When connected to a remote Arthas server, you may not be able to view the output file on the remote machine. In this case, Arthas also supports automatically redirecting the output to the local cache file. Examples are as follows:
`trace demo.MathGame primeFactors >> &`{{execute T2}}
```bash
$ trace Test t >> &
job id : 2
cache location : /Users/gehui/logs/arthas-cache/28198/2
```
If output path is not given, Arthas will automatically redirect the output to the local cache. `Job id` and `cache location` will be shown on the console. `Cache location` is a directory where the output files are put. For one given job, the path of its output file contains `PID` and `job id` in order to avoid potential conflict with other jobs. In the above example, `pid` is 28198 and `job id` is 2.

@ -0,0 +1,6 @@
For example, execute the trace command in the background:
`trace demo.MathGame primeFactors &`{{execute T2}}
By doing this, the current command is put to the background to run, you can continue to execute other commands in the console.

@ -0,0 +1,2 @@
If you want to stop background job, just `kill <job-id>`.

@ -0,0 +1,8 @@
When the job is executing in the foreground, for example, directly executing the command` trace Test t`, or executing the background job command `trace Test t &`, then putting the job back to the foreground via fg command, the console cannot continue to execute other command, but can receive and process the following keyboard events:
`ctrl + z`: Suspends the job, the job status will change to Stopped, and the job can be restarted by `bg <job-id>` or `fg <job-id>`
`ctrl + c`: Stops the job
`ctrl + d`: According to linux semantics this should lead to exit the terminal, right now Arthas has not implemented this yet, therefore simply ignore this keystroke.

@ -0,0 +1,6 @@
* 任务在后台执行或者暂停状态(`ctrl + z`暂停任务)时,执行`fg <job-id>`将可以把对应的任务转到前台继续执行。在前台执行时无法在console中执行其他命令
* 当任务处于暂停状态时(`ctrl + z`暂停任务),执行`bg <job-id>`将可以把对应的任务在后台继续执行
* 非当前session创建的job只能由当前session `fg`到前台执行
Loading…
Cancel
Save