laitimes

How can I use Arthas to improve my day-to-day development efficiency?

How can I use Arthas to improve my day-to-day development efficiency?

1. What are the functions of Arthas and how to use it, please see: Arthas User Manual

2. Arthas commands are more complicated, an IDEA plugin to help generate commands: arthas idea plugin uses documentation

3. A simple and easy-to-use hot deployment plug-in based on Arthas: ArthasHotSwap

I. Introduction

First of all, we should abandon the thinking of debug-oriented programming, debug is not necessarily the most efficient method, what to do if you can't debug when you encounter online problems. This topic describes how to use Arthas instead of debug to improve daily development efficiency.

Before you begin, you can browse through the articles recommended above, and the things used below are explained in detail in the above articles. You can also go through the following content first to make sure that what you describe can really improve development efficiency, and then you can read the above article.

First of all, we need to install two plugins, "arthas idea" and "ArthasHotSwap", IDEA can be installed and used directly. "arthas idea" is designed to generate Arthas commands efficiently, and "ArthasHotSwap" can be used for simple and fast hot deployment. Let's take a look at how to use Arthas to improve the efficiency of daily development through a few scenarios.

2. Solve a problem in 5 minutes

1 Problem appearance

The appearance of the problem is that the return parameter of the pre-issued interface is empty, I don't know why it is empty, and there is no log in the key place. What to do, debug? The debug port is not easy to apply, and it has been occupying the environment causing public outrage. Add logs to redeploy? It's taking too long to deploy, and you don't necessarily have to look at the logs to identify the problem. I'm going to use Arthas to troubleshoot the issue next.

How can I use Arthas to improve my day-to-day development efficiency?

2 How to find the problem

Step 1: Locate the relevant method

First locate the relevant code, the code is shown in the following figure, the method execution result should return a map, that is, the extraData of the above question, the return is empty, there must be a problem here, but I don't know the problem with that line of code. With Arthas, I can imagine that I can observe the input and output parameters of this method, and maybe I can find a problem. The Arthas watch command was used to observe the execution of the method.

How can I use Arthas to improve my day-to-day development efficiency?

Step 2: Generate a method observation command

After installing the "Arthas Idea" plugin, right-click on the method name and select Watch, as shown in the image below.

How can I use Arthas to improve my day-to-day development efficiency?

Step 3: Log in to the application server

Generally, applications are deployed on the cloud or other remote servers, and we need to log in to the server where the applications are deployed.

Step 4: Install Arthas

Arthas supports one-click installation on Linux/Unix/Mac and other platforms, please copy the following and paste it into the command line, and hit Enter to execute.

curl -L https://arthas.aliyun.com/install.sh | sh           

Step 5: Run Arthas

The above command will download the startup script file as.sh to the current directory, and execute the ./as.sh directly under the shell, and it will enter the interactive interface.

Step 6: Execute the Observation Method command

After the second step is executed, the command of the observation method is already on the pasteboard, and the paste execution is OK, the example command is as follows:

watch com.**.**.endpoint.BargainPlayApplyCycleEndPoint fetchPlayDetailRenderData '{params,returnObj,throwExp}' -n 5 -x 3 'params[0].getPlayToolDO().getId()==588'           

Command explanation: '{params,returnObj,throwExp}' indicates the observed object, where the input parameters, output parameters and exceptions are selected, -n 5 means that the observation stops after 5 times, -x 3 indicates that only 3 layers of nested structures are printed when the observed object is printed, and 'params[0].getPlayToolDO().getId()==588' is the conditional expression, and only those who meet the condition will be observed.

Step 7: Review the observations

After the frontend retriggers the execution, the observation result is as follows: from the input parameters, you can see that the targetId in playActivityApplyRequest is null, and the targetId in playActivityRecordDO should actually be taken. As you can see from the returned result, it is indeed because the product ID does not exist.

How can I use Arthas to improve my day-to-day development efficiency?

3 Take the problem a step further

So far, the problem has been discovered, and the average student is to modify the code and redeploy it, waiting for more than 10 minutes. We want to be an ordinary classmate, and then it's the turn of the ArthasHotSwap hot deployment plugin. Search for and install ArthasHotSwap in the IDEA plugin repository.

ArthasHotSwap 热部署

Modify the code -> maven compilation -> right-click and select swap this class -> The hot deployment command has been copied to the pasteboard -> Log in to the remote server and paste and execute the hot deployment command -> The hot deployment is successful

How can I use Arthas to improve my day-to-day development efficiency?

Problem solving

The hot deployment is successful, the problem is solved, the frontend re-requests, and the frontend display is normal.

How can I use Arthas to improve my day-to-day development efficiency?

Although the whole link seems to be very long, it is a very simple copy and paste operation, which is the actual problem I dealt with in the joint debugging process, and the whole process is about 5 minutes.

4 Some limitations

arthas refine has some limitations that lead to the same limitations for hot deployments. During hot deployment, you cannot modify the method name and attribute fields, but only the code in the method body.

The redefine command conflicts with commands such as jad/watch/trace/monitor/tt. After redefine, if you run the command mentioned above, the bytecode of redefine will be reset. In other words, after the hot deployment is executed, executing commands such as jad/watch/trace/monitor/tt will invalidate the hot deployment, so it is still necessary to redeploy it at the appropriate time. We can also use other ways to circumvent this, such as observing other classes when using watch, rather than the hot-deployed class.

Three methods of time tunneling

In the above scenario, we used the watch command to observe the execution parameters of the method, but watch is the case when the method is executed. What if we want to reproduce the last call.

In the day-to-day development process, environmental issues can greatly affect development efficiency, especially when it involves applications from other teams, and sometimes it is difficult to expect the upstream to trigger a call again. When you solve your problem with the above method, you want to debug it, and find that it is already late at night, and you are embarrassed to let the upstream application trigger another call, what should you do, and use the HSF console to call the parameter assembly parameters according to the method observed by watch? We need to be extraordinary students and use tools to liberate productivity.

1 Get the tt command

The TT command of Arthas can obtain the spatiotemporal tunnel of method execution data, record the input parameters and return information of each call of a specified method, and observe the calls at these different times.

After installing the arthas idea plug-in, right-click on the method that needs to be recorded, select TimeTunnel Tt, and the following selection box will appear, the first red box is the command to get the record method execution. The second red box is the operations that need to be performed on the record after the record method is executed, including viewing the record list, viewing the record execution, and reproducing the call.

How can I use Arthas to improve my day-to-day development efficiency?

2 Record method execution

Log in to the remote server, run the following two items to run arthas, and then execute the tt -t command copied from the idea plugin to record the method execution, as shown in the following figure. If you want to stop recording, you can enter q, and use tt -l to view the record.

curl -L https://arthas.aliyun.com/install.sh | sh
./as.sh           
How can I use Arthas to improve my day-to-day development efficiency?

3 Search method execution records

tt -l can view all method execution records, but if we want to be related to our execution records, then we can use the tt -s command to search for them. The tt -s command needs to be followed by a filter condition. Conditional expressions are written in OGNL, and a few of the usual filtered expressions are described below. First of all, we will introduce the following objects, params is the input parameter, params[0] is the first parameter, params[1] is the second parameter, and returnObj is the return object.

  • 根据入参过滤:tt -s 'params[2].getRecordId() == 110213603'
  • 根据返回结果过滤:tt -s 'returnObj.isSuccess() == false'
  • 根据入参和返回结果过滤:tt -s 'returnObj.isSuccess() == true && params[2].getRecordId() == 110213603'

4 Check how the method is performing

tt -w is equivalent to the watch command below tt, which can be used to view the execution of the method. It is more convenient to use the idea plugin to get the tt -w command.

5 Retrigger

If you need to retrigger a record, it is also possible, because the tt command records the call at that time, so you can initiate a call locally, and the tt -p -i 1000 command means to retrigger the record with idex=1000. TT -p --replay-times 5 --replay-interval 2000 -i 1000 indicates that it can be retriggered 5 times at an interval of 2 seconds.

When retriggering, we may also want to watch the execution of the method, what to do, open another page to log in to the remote server, run arthas, and execute the watch command.

6 Some Limitations

ThreadLocal 信息丢失

Many frameworks secretly stuff some environment variable information into the ThreadLocal of the initiating calling thread, and because the calling thread has changed, these ThreadLocal thread information cannot be saved through Arthas, so this information will be lost. Some common CASES are: Eagle Eye's TraceId, etc.

Referenced objects

It is important to note that the tt command saves a reference to an object in the current environment, but only one reference. If the input parameters are changed within the method, or if the returned object is processed later, the most accurate value at that time will not be seen when tt viewing. That's why the watch command exists.

Author: Murda

Source-WeChat public account: Alibaba Cloud Developer

Source: https://mp.weixin.qq.com/s/UAO5qHvO6VIhvyCSZnW--g

Read on