阿里 神器 Arthas 的骚操作,定位线上BUG,超给力!

共 32890字,需浏览 66分钟

 ·

2021-10-03 00:32

Arthas 是Alibaba开源的Java诊断工具。在线排查问题,无需重启;动态跟踪Java代码;实时监控JVM状态。对分秒必争的线上异常, Arthas可帮助我们快速诊断相关问题。

公司有个渠道系统,专门对接三方渠道使用,没有什么业务逻辑,主要是转换报文和参数校验之类的工作,起着一个承上启下的作用。

最近在优化接口的响应时间,优化了代码之后,但是时间还是达不到要求;有一个诡异的100ms左右的耗时问题,在接口中打印了请求处理时间后,和调用方的响应时间还有差了100ms左右。比如程序里记录150ms,但是调用方等待时间却为250ms左右。

下面记录下当时详细的定位&解决流程(其实解决很简单,关键在于怎么定位并找到解决问题的方法)

定位过程

分析代码

渠道系统是一个常见的spring-boot web工程,使用了集成的tomcat。分析了代码之后,发现并没有特殊的地方,没有特殊的过滤器或者拦截器,所以初步排除是业务代码问题

分析调用流程

出现这个问题之后,首先确认了下接口的调用流程。由于是内部测试,所以调用流程较少。

Nginx-反向代理->渠道系统

公司是云服务器,网络走的也是云的内网。由于不明确问题的原因,所以用排除法,首先确认服务器网络是否有问题。

先确认发送端到Nginx Host是否有问题:

  1. [jboss@VM_0_139_centos ~]$ ping 10.0.0.139

  2. PING 10.0.0.139(10.0.0.139) 56(84) bytes of data.

  3. 64 bytes from10.0.0.139: icmp_seq=1 ttl=64 time=0.029 ms

  4. 64 bytes from10.0.0.139: icmp_seq=2 ttl=64 time=0.041 ms

  5. 64 bytes from10.0.0.139: icmp_seq=3 ttl=64 time=0.040 ms

  6. 64 bytes from10.0.0.139: icmp_seq=4 ttl=64 time=0.040 ms

从ping结果上看,发送端到Nginx主机的延迟是无问题的,接下来查看Nginx到渠道系统的网络。

  1. # 由于日志是没问题的,这里直接复制上面日志了

  2. [jboss@VM_0_139_centos ~]$ ping 10.0.0.139

  3. PING 10.0.0.139(10.0.0.139) 56(84) bytes of data.

  4. 64 bytes from10.0.0.139: icmp_seq=1 ttl=64 time=0.029 ms

  5. 64 bytes from10.0.0.139: icmp_seq=2 ttl=64 time=0.041 ms

  6. 64 bytes from10.0.0.139: icmp_seq=3 ttl=64 time=0.040 ms

  7. 64 bytes from10.0.0.139: icmp_seq=4 ttl=64 time=0.040 ms

从ping结果上看,Nginx到渠道系统服务器网络延迟也是没问题的

既然网络看似没问题,那么可以继续排除法,砍掉Nginx,客户端直接再渠道系统的服务器上,通过回环地址(localhost)直连,避免经过网卡/dns,缩小问题范围看看能否复现(这个应用和地址是我后期模拟的,测试的是一个空接口):

  1. [jboss@VM_10_91_centos tmp]$ curl -w "@curl-time.txt" http://127.0.0.1:7744/send

  2. success

  3. http: 200

  4. dns: 0.001s

  5. redirect: 0.000s

  6. time_connect: 0.001s

  7. time_appconnect: 0.000s

  8. time_pretransfer: 0.001s

  9. time_starttransfer: 0.073s

  10. size_download: 7bytes

  11. speed_download: 95.000B/s

  12. ----------

  13. time_total: 0.073s请求总耗时

从curl日志上看,通过回环地址调用一个空接口耗时也有73ms。这就奇怪了,跳过了中间所有调用节点(包括过滤器&拦截器之类),直接请求应用一个空接口,都有73ms的耗时,再请求一次看看:

  1. [jboss@VM_10_91_centos tmp]$ curl -w "@curl-time.txt" http://127.0.0.1:7744/send

  2. success

  3. http: 200

  4. dns: 0.001s

  5. redirect: 0.000s

  6. time_connect: 0.001s

  7. time_appconnect: 0.000s

  8. time_pretransfer: 0.001s

  9. time_starttransfer: 0.003s

  10. size_download: 7bytes

  11. speed_download: 2611.000B/s

  12. ----------

  13. time_total: 0.003s

更奇怪的是,第二次请求耗时就正常了,变成了3ms。经查阅资料,linux curl是默认开启http keep-alive的(Keep-Alive的介绍可以参考我的另一篇文章)。就算不开启keep-alive,每次重新handshake,也不至于需要70ms。

经过不断分析测试发现,连续请求的话时间就会很短,每次请求只需要几毫秒,但是如果隔一段时间再请求,就会花费70ms以上。

从这个现象猜想,可能是某些缓存机制导致的,连续请求因为有缓存,所以速度快,时间长缓存失效后导致时间长。整编:微信公众号,搜云库技术团队,ID:souyunku

那么这个问题点到底在哪一层呢?tomcat层还是spring-webmvc呢?

光猜想定位不了问题,还是得实际测试一下,把渠道系统的代码放到本地ide里启动测试能否复现

但是导入本地Ide后,在Ide中启动后并不能复现问题,并没有70+ms的延迟问题。这下头疼了,本地无法复现,不能Debug,由于问题点不在业务代码,也不能通过加日志的方式来Debug

这时候可以祭出神器Arthas了

Arthas 是Alibaba开源的Java诊断工具。在线排查问题,无需重启;动态跟踪Java代码;实时监控JVM状态。对分秒必争的线上异常, Arthas可帮助我们快速诊断相关问题。

Arthas分析问题

Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱。

当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:

这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!是否有一个全局视角来查看系统的运行状况?有什么办法可以监控到JVM的实时运行状态?

上面是Arthas的官方简介,这次我只需要用他的一个小功能trace。动态计算方法调用路径和时间,这样我就可以定位时间在哪个地方被消耗了。

trace 方法内部调用路径,并输出方法路径上的每个节点上耗时

trace 命令能主动搜索 class-pattern/method-pattern 对应的方法调用路径,渲染和统计整个调用链路上的所有性能开销和追踪调用链路。

有了神器,那么该追踪什么方法呢?由于我对Tomcat源码不是很熟,所以只能从spring mvc下手,先来trace一下spring mvc的入口:

  1. [arthas@24851]$ trace org.springframework.web.servlet.DispatcherServlet*

  2. Press Q orCtrl+C to abort.

  3. Affect(class-cnt:1, method-cnt:44) cost in508 ms.

  4. `---ts=2019-09-14 21:07:44;thread_name=http-nio-7744-exec-2;id=11;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@7c136917

  5. `---[2.952142ms] org.springframework.web.servlet.DispatcherServlet:buildLocaleContext()


  6. `---ts=2019-09-14 21:07:44;thread_name=http-nio-7744-exec-2;id=11;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@7c136917

  7. `---[18.08903ms] org.springframework.web.servlet.DispatcherServlet:doService()

  8. +---[0.041346ms] org.apache.commons.logging.Log:isDebugEnabled() #889

  9. +---[0.022398ms] org.springframework.web.util.WebUtils:isIncludeRequest() #898

  10. +---[0.014904ms] org.springframework.web.servlet.DispatcherServlet:getWebApplicationContext() #910

  11. +---[1.071879ms] javax.servlet.http.HttpServletRequest:setAttribute() #910

  12. +---[0.020977ms] javax.servlet.http.HttpServletRequest:setAttribute() #911

  13. +---[0.017073ms] javax.servlet.http.HttpServletRequest:setAttribute() #912

  14. +---[0.218277ms] org.springframework.web.servlet.DispatcherServlet:getThemeSource() #913

  15. | `---[0.137568ms] org.springframework.web.servlet.DispatcherServlet:getThemeSource()

  16. | `---[min=0.00783ms,max=0.014251ms,total=0.022081ms,count=2] org.springframework.web.servlet.DispatcherServlet:getWebApplicationContext() #782

  17. +---[0.019363ms] javax.servlet.http.HttpServletRequest:setAttribute() #913

  18. +---[0.070694ms] org.springframework.web.servlet.FlashMapManager:retrieveAndUpdate() #916

  19. +---[0.01839ms] org.springframework.web.servlet.FlashMap:() #920

  20. +---[0.016943ms] javax.servlet.http.HttpServletRequest:setAttribute() #920

  21. +---[0.015268ms] javax.servlet.http.HttpServletRequest:setAttribute() #921

  22. +---[15.050124ms] org.springframework.web.servlet.DispatcherServlet:doDispatch() #925

  23. | `---[14.943477ms] org.springframework.web.servlet.DispatcherServlet:doDispatch()

  24. | +---[0.019135ms] org.springframework.web.context.request.async.WebAsyncUtils:getAsyncManager() #953

  25. | +---[2.108373ms] org.springframework.web.servlet.DispatcherServlet:checkMultipart() #960

  26. | | `---[2.004436ms] org.springframework.web.servlet.DispatcherServlet:checkMultipart()

  27. | | `---[1.890845ms] org.springframework.web.multipart.MultipartResolver:isMultipart() #1117

  28. | +---[2.054361ms] org.springframework.web.servlet.DispatcherServlet:getHandler() #964

  29. | | `---[1.961963ms] org.springframework.web.servlet.DispatcherServlet:getHandler()

  30. | | +---[0.02051ms] java.util.List:iterator() #1183

  31. | | +---[min=0.003805ms,max=0.009641ms,total=0.013446ms,count=2] java.util.Iterator:hasNext() #1183

  32. | | +---[min=0.003181ms,max=0.009751ms,total=0.012932ms,count=2] java.util.Iterator:next() #1183

  33. | | +---[min=0.005841ms,max=0.015308ms,total=0.021149ms,count=2] org.apache.commons.logging.Log:isTraceEnabled() #1184

  34. | | `---[min=0.474739ms,max=1.19145ms,total=1.666189ms,count=2] org.springframework.web.servlet.HandlerMapping:getHandler() #1188

  35. | +---[0.013071ms] org.springframework.web.servlet.HandlerExecutionChain:getHandler() #971

  36. | +---[0.372236ms] org.springframework.web.servlet.DispatcherServlet:getHandlerAdapter() #971

  37. | | `---[0.280073ms] org.springframework.web.servlet.DispatcherServlet:getHandlerAdapter()

  38. | | +---[0.004804ms] java.util.List:iterator() #1224

  39. | | +---[0.003668ms] java.util.Iterator:hasNext() #1224

  40. | | +---[0.003038ms] java.util.Iterator:next() #1224

  41. | | +---[0.006451ms] org.apache.commons.logging.Log:isTraceEnabled() #1225

  42. | | `---[0.012683ms] org.springframework.web.servlet.HandlerAdapter:supports() #1228

  43. | +---[0.012848ms] javax.servlet.http.HttpServletRequest:getMethod() #974

  44. | +---[0.013132ms] java.lang.String:equals() #975

  45. | +---[0.003025ms] org.springframework.web.servlet.HandlerExecutionChain:getHandler() #977

  46. | +---[0.008095ms] org.springframework.web.servlet.HandlerAdapter:getLastModified() #977

  47. | +---[0.006596ms] org.apache.commons.logging.Log:isDebugEnabled() #978

  48. | +---[0.018024ms] org.springframework.web.context.request.ServletWebRequest:() #981

  49. | +---[0.017869ms] org.springframework.web.context.request.ServletWebRequest:checkNotModified() #981

  50. | +---[0.038542ms] org.springframework.web.servlet.HandlerExecutionChain:applyPreHandle() #986

  51. | +---[0.00431ms] org.springframework.web.servlet.HandlerExecutionChain:getHandler() #991

  52. | +---[4.248493ms] org.springframework.web.servlet.HandlerAdapter:handle() #991

  53. | +---[0.014805ms] org.springframework.web.context.request.async.WebAsyncManager:isConcurrentHandlingStarted() #993

  54. | +---[1.444994ms] org.springframework.web.servlet.DispatcherServlet:applyDefaultViewName() #997

  55. | | `---[0.067631ms] org.springframework.web.servlet.DispatcherServlet:applyDefaultViewName()

  56. | +---[0.012027ms] org.springframework.web.servlet.HandlerExecutionChain:applyPostHandle() #998

  57. | +---[0.373997ms] org.springframework.web.servlet.DispatcherServlet:processDispatchResult() #1008

  58. | | `---[0.197004ms] org.springframework.web.servlet.DispatcherServlet:processDispatchResult()

  59. | | +---[0.007074ms] org.apache.commons.logging.Log:isDebugEnabled() #1075

  60. | | +---[0.005467ms] org.springframework.web.context.request.async.WebAsyncUtils:getAsyncManager() #1081

  61. | | +---[0.004054ms] org.springframework.web.context.request.async.WebAsyncManager:isConcurrentHandlingStarted() #1081

  62. | | `---[0.011988ms] org.springframework.web.servlet.HandlerExecutionChain:triggerAfterCompletion() #1087

  63. | `---[0.004015ms] org.springframework.web.context.request.async.WebAsyncManager:isConcurrentHandlingStarted() #1018

  64. +---[0.005055ms] org.springframework.web.context.request.async.WebAsyncUtils:getAsyncManager() #928

  65. `---[0.003422ms] org.springframework.web.context.request.async.WebAsyncManager:isConcurrentHandlingStarted() #928

  1. [jboss@VM_10_91_centos tmp]$ curl -w "@curl-time.txt" http://127.0.0.1:7744/send

  2. success

  3. http: 200

  4. dns: 0.001s

  5. redirect: 0.000s

  6. time_connect: 0.001s

  7. time_appconnect: 0.000s

  8. time_pretransfer: 0.001s

  9. time_starttransfer: 0.115s

  10. size_download: 7bytes

  11. speed_download: 60.000B/s

  12. ----------

  13. time_total: 0.115s

本次调用,调用端时间花费115ms,但是从arthas trace上看,spring mvc只消耗了18ms,那么剩下的97ms去哪了呢?

本地测试后已经可以排除spring mvc的问题了,最后也是唯一可能出问题的点就是tomcat

可是本人并不熟悉tomcat中的源码,就连请求入口都不清楚,tomcat里需要trace的类都不好找。。。

不过没关系,有神器Arthas,可以通过stack命令来反向查找调用路径,以 org.springframework.web.servlet.DispatcherServlet作为参数:

stack 输出当前方法被调用的调用路径

很多时候我们都知道一个方法被执行,但这个方法被执行的路径非常多,或者你根本就不知道这个方法是从那里被执行了,此时你需要的是 stack 命令。

  1. [arthas@24851]$ stack org.springframework.web.servlet.DispatcherServlet*

  2. Press Q orCtrl+C to abort.

  3. Affect(class-cnt:1, method-cnt:44) cost in495 ms.

  4. ts=2019-09-1421:15:19;thread_name=http-nio-7744-exec-5;id=14;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@7c136917

  5. @org.springframework.web.servlet.FrameworkServlet.processRequest()

  6. at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)

  7. at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)

  8. at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)

  9. at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)

  10. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

  11. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  12. at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

  13. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  14. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  15. at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)

  16. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  17. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  18. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  19. at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)

  20. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  21. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  22. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  23. at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)

  24. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  25. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  26. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  27. at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)

  28. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  29. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  30. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  31. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)

  32. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)

  33. at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)

  34. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)

  35. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)

  36. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)

  37. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)

  38. at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)

  39. at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)

  40. at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)

  41. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468)

  42. at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)

  43. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

  44. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

  45. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

  46. at java.lang.Thread.run(Thread.java:748)


  47. ts=2019-09-1421:15:19;thread_name=http-nio-7744-exec-5;id=14;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@7c136917

  48. @org.springframework.web.servlet.DispatcherServlet.doService()

  49. at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)

  50. at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)

  51. at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)

  52. at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)

  53. at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)

  54. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

  55. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  56. at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

  57. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  58. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  59. at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)

  60. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  61. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  62. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  63. at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)

  64. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  65. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  66. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  67. at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)

  68. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  69. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  70. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  71. at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)

  72. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  73. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

  74. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

  75. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)

  76. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)

  77. at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)

  78. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)

  79. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)

  80. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)

  81. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)

  82. at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)

  83. at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)

  84. at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)

  85. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468)

  86. at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)

  87. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

  88. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

  89. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

  90. at java.lang.Thread.run(Thread.java:748)

从stack日志上可以很直观的看出DispatchServlet的调用栈,那么这么长的路径,该trace哪个类呢(这里跳过spring mvc中的过滤器的trace过程,实际排查的时候也trace了一遍,但这诡异的时间消耗不是由这里过滤器产生的)?有一定经验的老司机从名字上大概也能猜出来从哪里下手比较好,那就是 org.apache.coyote.http11.Http11Processor.service,从名字上看,http1.1处理器,这可能是一个比较好的切入点。下面来trace一下:

  1. [arthas@24851]$ trace org.apache.coyote.http11.Http11Processor service

  2. Press Q orCtrl+C to abort.

  3. Affect(class-cnt:1, method-cnt:1) cost in269 ms.

  4. `---ts=2019-09-14 21:22:51;thread_name=http-nio-7744-exec-8;id=17;is_daemon=true;priority=5;TCCL=org.springframework.boot.loader.LaunchedURLClassLoader@20ad9418

  5. `---[131.650285ms] org.apache.coyote.http11.Http11Processor:service()

  6. +---[0.036851ms] org.apache.coyote.Request:getRequestProcessor() #667

  7. +---[0.009986ms] org.apache.coyote.RequestInfo:setStage() #668

  8. +---[0.008928ms] org.apache.coyote.http11.Http11Processor:setSocketWrapper() #671

  9. +---[0.013236ms] org.apache.coyote.http11.Http11InputBuffer:init() #672

  10. +---[0.00981ms] org.apache.coyote.http11.Http11OutputBuffer:init() #673

  11. +---[min=0.00213ms,max=0.007317ms,total=0.009447ms,count=2] org.apache.coyote.http11.Http11Processor:getErrorState() #683

  12. +---[min=0.002098ms,max=0.008888ms,total=0.010986ms,count=2] org.apache.coyote.ErrorState:isError() #683

  13. +---[min=0.002448ms,max=0.007149ms,total=0.009597ms,count=2] org.apache.coyote.http11.Http11Processor:isAsync() #683

  14. +---[min=0.002399ms,max=0.00852ms,total=0.010919ms,count=2] org.apache.tomcat.util.net.AbstractEndpoint:isPaused() #683

  15. +---[min=0.033587ms,max=0.11832ms,total=0.151907ms,count=2] org.apache.coyote.http11.Http11InputBuffer:parseRequestLine() #687

  16. +---[0.005384ms] org.apache.tomcat.util.net.AbstractEndpoint:isPaused() #695

  17. +---[0.007924ms] org.apache.coyote.Request:getMimeHeaders() #702

  18. +---[0.006744ms] org.apache.tomcat.util.net.AbstractEndpoint:getMaxHeaderCount() #702

  19. +---[0.012574ms] org.apache.tomcat.util.http.MimeHeaders:setLimit() #702

  20. +---[0.14319ms] org.apache.coyote.http11.Http11InputBuffer:parseHeaders() #703

  21. +---[0.003997ms] org.apache.coyote.Request:getMimeHeaders() #743

  22. +---[0.026561ms] org.apache.tomcat.util.http.MimeHeaders:values() #743

  23. +---[min=0.002869ms,max=0.01203ms,total=0.014899ms,count=2] java.util.Enumeration:hasMoreElements() #745

  24. +---[0.070114ms] java.util.Enumeration:nextElement() #746

  25. +---[0.010921ms] java.lang.String:toLowerCase() #746

  26. +---[0.008453ms] java.lang.String:contains() #746

  27. +---[0.002698ms] org.apache.coyote.http11.Http11Processor:getErrorState() #775

  28. +---[0.00307ms] org.apache.coyote.ErrorState:isError() #775

  29. +---[0.002708ms] org.apache.coyote.RequestInfo:setStage() #777

  30. +---[0.171139ms] org.apache.coyote.http11.Http11Processor:prepareRequest() #779

  31. +---[0.009349ms] org.apache.tomcat.util.net.SocketWrapperBase:decrementKeepAlive() #794

  32. +---[0.002574ms] org.apache.coyote.http11.Http11Processor:getErrorState() #800

  33. +---[0.002696ms] org.apache.coyote.ErrorState:isError() #800

  34. +---[0.002499ms] org.apache.coyote.RequestInfo:setStage() #802

  35. +---[0.005641ms] org.apache.coyote.http11.Http11Processor:getAdapter() #803

  36. +---[129.868916ms] org.apache.coyote.Adapter:service() #803

  37. +---[0.003859ms] org.apache.coyote.http11.Http11Processor:getErrorState() #809

  38. +---[0.002365ms] org.apache.coyote.ErrorState:isError() #809

  39. +---[0.003844ms] org.apache.coyote.http11.Http11Processor:isAsync() #809

  40. +---[0.002382ms] org.apache.coyote.Response:getStatus() #809

  41. +---[0.002476ms] org.apache.coyote.http11.Http11Processor:statusDropsConnection() #809

  42. +---[0.002284ms] org.apache.coyote.RequestInfo:setStage() #838

  43. +---[0.00222ms] org.apache.coyote.http11.Http11Processor:isAsync() #839

  44. +---[0.037873ms] org.apache.coyote.http11.Http11Processor:endRequest() #843

  45. +---[0.002188ms] org.apache.coyote.RequestInfo:setStage() #845

  46. +---[0.002112ms] org.apache.coyote.http11.Http11Processor:getErrorState() #849

  47. +---[0.002063ms] org.apache.coyote.ErrorState:isError() #849

  48. +---[0.002504ms] org.apache.coyote.http11.Http11Processor:isAsync() #853

  49. +---[0.009808ms] org.apache.coyote.Request:updateCounters() #854

  50. +---[0.002008ms] org.apache.coyote.http11.Http11Processor:getErrorState() #855

  51. +---[0.002192ms] org.apache.coyote.ErrorState:isIoAllowed() #855

  52. +---[0.01968ms] org.apache.coyote.http11.Http11InputBuffer:nextRequest() #856

  53. +---[0.010065ms] org.apache.coyote.http11.Http11OutputBuffer:nextRequest() #857

  54. +---[0.002576ms] org.apache.coyote.RequestInfo:setStage() #870

  55. +---[0.016599ms] org.apache.coyote.http11.Http11Processor:processSendfile() #872

  56. +---[0.008182ms] org.apache.coyote.http11.Http11InputBuffer:getParsingRequestLinePhase() #688

  57. +---[0.0075ms] org.apache.coyote.http11.Http11Processor:handleIncompleteRequestLineRead() #690

  58. +---[0.001979ms] org.apache.coyote.RequestInfo:setStage() #875

  59. +---[0.001981ms] org.apache.coyote.http11.Http11Processor:getErrorState() #877

  60. +---[0.001934ms] org.apache.coyote.ErrorState:isError() #877

  61. +---[0.001995ms] org.apache.tomcat.util.net.AbstractEndpoint:isPaused() #877

  62. +---[0.002403ms] org.apache.coyote.http11.Http11Processor:isAsync() #879

  63. `---[0.006176ms] org.apache.coyote.http11.Http11Processor:isUpgrade() #881

日志里有一个129ms的耗时点(时间比没开arthas的时候更长是因为arthas本身带来的性能消耗,所以生产环境小心使用),这个就是要找的问题点

打问题点找到了,那怎么定位是什么导致的问题呢,又如何解决呢?

继续trace吧,细化到具体的代码块或者内容。trace由于性能考虑,不会展示所有的调用路径,如果调用路径过深,只有手动深入trace,原则就是trace耗时长的那个方法:

  1. [arthas@24851]$ trace org.apache.coyote.Adapter service

  2. Press Q orCtrl+C to abort.

  3. Affect(class-cnt:1, method-cnt:1) cost in608 ms.

  4. `---ts=2019-09-14 21:34:33;thread_name=http-nio-7744-exec-1;id=10;is_daemon=true;priority=5;TCCL=org.springframework.boot.loader.LaunchedURLClassLoader@20ad9418

  5. `---[81.70999ms] org.apache.catalina.connector.CoyoteAdapter:service()

  6. +---[0.032546ms] org.apache.coyote.Request:getNote() #302

  7. +---[0.007148ms] org.apache.coyote.Response:getNote() #303

  8. +---[0.007475ms] org.apache.catalina.connector.Connector:getXpoweredBy() #324

  9. +---[0.00447ms] org.apache.coyote.Request:getRequestProcessor() #331

  10. +---[0.007902ms] java.lang.ThreadLocal:get() #331

  11. +---[0.006522ms] org.apache.coyote.RequestInfo:setWorkerThreadName() #331

  12. +---[73.793798ms] org.apache.catalina.connector.CoyoteAdapter:postParseRequest() #336

  13. +---[0.001536ms] org.apache.catalina.connector.Connector:getService() #339

  14. +---[0.004469ms] org.apache.catalina.Service:getContainer() #339

  15. +---[0.007074ms] org.apache.catalina.Engine:getPipeline() #339

  16. +---[0.004334ms] org.apache.catalina.Pipeline:isAsyncSupported() #339

  17. +---[0.002466ms] org.apache.catalina.connector.Request:setAsyncSupported() #339

  18. +---[6.01E-4ms] org.apache.catalina.connector.Connector:getService() #342

  19. +---[0.001859ms] org.apache.catalina.Service:getContainer() #342

  20. +---[9.65E-4ms] org.apache.catalina.Engine:getPipeline() #342

  21. +---[0.005231ms] org.apache.catalina.Pipeline:getFirst() #342

  22. +---[7.239154ms] org.apache.catalina.Valve:invoke() #342

  23. +---[0.006904ms] org.apache.catalina.connector.Request:isAsync() #345

  24. +---[0.00509ms] org.apache.catalina.connector.Request:finishRequest() #372

  25. +---[0.051461ms] org.apache.catalina.connector.Response:finishResponse() #373

  26. +---[0.007244ms] java.util.concurrent.atomic.AtomicBoolean:() #379

  27. +---[0.007314ms] org.apache.coyote.Response:action() #380

  28. +---[0.004518ms] org.apache.catalina.connector.Request:isAsyncCompleting() #382

  29. +---[0.001072ms] org.apache.catalina.connector.Request:getContext() #394

  30. +---[0.007166ms] java.lang.System:currentTimeMillis() #401

  31. +---[0.004367ms] org.apache.coyote.Request:getStartTime() #401

  32. +---[0.011483ms] org.apache.catalina.Context:logAccess() #401

  33. +---[0.0014ms] org.apache.coyote.Request:getRequestProcessor() #406

  34. +---[min=8.0E-4ms,max=9.22E-4ms,total=0.001722ms,count=2] java.lang.Integer:() #406

  35. +---[0.001082ms] java.lang.reflect.Method:invoke() #406

  36. +---[0.001851ms] org.apache.coyote.RequestInfo:setWorkerThreadName() #406

  37. +---[0.035805ms] org.apache.catalina.connector.Request:recycle() #410

  38. `---[0.007849ms] org.apache.catalina.connector.Response:recycle() #411


一段无聊的手动深入trace之后………………

  1. [arthas@24851]$ trace org.apache.catalina.webresources.AbstractArchiveResourceSet getArchiveEntries

  2. Press Q orCtrl+C to abort.

  3. Affect(class-cnt:4, method-cnt:2) cost in150 ms.

  4. `---ts=2019-09-14 21:36:26;thread_name=http-nio-7744-exec-3;id=12;is_daemon=true;priority=5;TCCL=org.springframework.boot.loader.LaunchedURLClassLoader@20ad9418

  5. `---[75.743681ms] org.apache.catalina.webresources.JarWarResourceSet:getArchiveEntries()

  6. +---[0.025731ms] java.util.HashMap:() #106

  7. +---[0.097729ms] org.apache.catalina.webresources.JarWarResourceSet:openJarFile() #109

  8. +---[0.091037ms] java.util.jar.JarFile:getJarEntry() #110

  9. +---[0.096325ms] java.util.jar.JarFile:getInputStream() #111

  10. +---[0.451916ms] org.apache.catalina.webresources.TomcatJarInputStream:() #113

  11. +---[min=0.001175ms,max=0.001176ms,total=0.002351ms,count=2] java.lang.Integer:() #114

  12. +---[0.00104ms] java.lang.reflect.Method:invoke() #114

  13. +---[0.045105ms] org.apache.catalina.webresources.TomcatJarInputStream:getNextJarEntry() #114

  14. +---[min=5.02E-4ms,max=0.008531ms,total=0.028864ms,count=31] java.util.jar.JarEntry:getName() #116

  15. +---[min=5.39E-4ms,max=0.022805ms,total=0.054647ms,count=31] java.util.HashMap:put() #116

  16. +---[min=0.004452ms,max=34.479307ms,total=74.206249ms,count=31] org.apache.catalina.webresources.TomcatJarInputStream:getNextJarEntry() #117

  17. +---[0.018358ms] org.apache.catalina.webresources.TomcatJarInputStream:getManifest() #119

  18. +---[0.006429ms] org.apache.catalina.webresources.JarWarResourceSet:setManifest() #120

  19. +---[0.010904ms] org.apache.tomcat.util.compat.JreCompat:isJre9Available() #121

  20. +---[0.003307ms] org.apache.catalina.webresources.TomcatJarInputStream:getMetaInfEntry() #133

  21. +---[5.5E-4ms] java.util.jar.JarEntry:getName() #135

  22. +---[6.42E-4ms] java.util.HashMap:put() #135

  23. +---[0.001981ms] org.apache.catalina.webresources.TomcatJarInputStream:getManifestEntry() #137

  24. +---[0.064484ms] org.apache.catalina.webresources.TomcatJarInputStream:close() #141

  25. +---[0.007961ms] org.apache.catalina.webresources.JarWarResourceSet:closeJarFile() #151

  26. `---[0.004643ms] java.io.InputStream:close() #155

发现了一个值得暂停思考的点:

  1. +---[min=0.004452ms,max=34.479307ms,total=74.206249ms,count=31]org.apache.catalina.webresources.TomcatJarInputStream:getNextJarEntry() #117

这行代码加载了31次,一共耗时74ms;从名字上看,应该是tomcat加载jar包时的耗时,那么是加载了31个jar包的耗时,还是加载了jar包内的某些资源31次耗时呢?

TomcatJarInputStream这个类源码的注释写到:

The purpose of this sub-class is to obtain references to the JarEntry objects for META-INF/ and META-INF/MANIFEST.MF that are otherwise swallowed by the JarInputStream implementation.

大概意思也就是,获取jar包内META-INF/,META-INF/MANIFEST的资源,这是一个子类,更多的功能在父类JarInputStream里。

其实看到这里大概也能猜到问题了,tomcat加载jar包内META-INF/,META-INF/MANIFEST的资源导致的耗时,至于为什么连续请求不会耗时,应该是tomcat的缓存机制(下面介绍源码分析)

不着急定位问题,试着通过Arthas最终定位问题细节,继续手动深入trace

  1. [arthas@24851]$ trace org.apache.catalina.webresources.TomcatJarInputStream*

  2. Press Q orCtrl+C to abort.

  3. Affect(class-cnt:1, method-cnt:4) cost in44 ms.

  4. `---ts=2019-09-14 21:37:47;thread_name=http-nio-7744-exec-5;id=14;is_daemon=true;priority=5;TCCL=org.springframework.boot.loader.LaunchedURLClassLoader@20ad9418

  5. `---[0.234952ms] org.apache.catalina.webresources.TomcatJarInputStream:createZipEntry()

  6. +---[0.039455ms] java.util.jar.JarInputStream:createZipEntry() #43

  7. `---[0.007827ms] java.lang.String:equals() #44


  8. `---ts=2019-09-1421:37:47;thread_name=http-nio-7744-exec-5;id=14;is_daemon=true;priority=5;TCCL=org.springframework.boot.loader.LaunchedURLClassLoader@20ad9418

  9. `---[0.050222ms] org.apache.catalina.webresources.TomcatJarInputStream:createZipEntry()

  10. +---[0.001889ms] java.util.jar.JarInputStream:createZipEntry() #43

  11. `---[0.001643ms] java.lang.String:equals() #46

  12. #这里一共31个trace日志,删减了剩下的

从方法名上看,还是加载资源之类的意思。都已经到jdk源码了,这时候来看一下 TomcatJarInputStream这个类的源码:

  1. /**

  2. * Creates a new JarEntry (ZipEntry) for the

  3. * specified JAR file entry name. The manifest attributes of

  4. * the specified JAR file entry name will be copied to the new

  5. * JarEntry.

  6. *

  7. * @param name the name of the JAR/ZIP file entry

  8. * @return the JarEntry object just created

  9. */

  10. protectedZipEntry createZipEntry(String name) {

  11. JarEntry e = newJarEntry(name);

  12. if(man != null) {

  13. e.attr = man.getAttributes(name);

  14. }

  15. return e;

  16. }

这个 createZipEntry有个name参数,从注释上看,是jar/zip文件名,如果能得到文件名这种关键信息,就可以直接定位问题了;还是通过Arthas,使用watch命令,动态监测方法调用数据

watch方法执行数据观测

让你能方便的观察到指定方法的调用情况。能观察到的范围为:返回值、抛出异常、入参,通过编写 OGNL 表达式进行对应变量的查看。

watch 该方法的入参

  1. [arthas@24851]$ watch org.apache.catalina.webresources.TomcatJarInputStream createZipEntry "{params[0]}"

  2. Press Q orCtrl+C to abort.

  3. Affect(class-cnt:1, method-cnt:1) cost in27 ms.

  4. ts=2019-09-1421:51:14; [cost=0.14547ms] result=@ArrayList[

  5. @String[META-INF/],

  6. ]

  7. ts=2019-09-1421:51:14; [cost=0.048028ms] result=@ArrayList[

  8. @String[META-INF/MANIFEST.MF],

  9. ]

  10. ts=2019-09-1421:51:14; [cost=0.046071ms] result=@ArrayList[

  11. @String[META-INF/resources/],

  12. ]

  13. ts=2019-09-1421:51:14; [cost=0.033855ms] result=@ArrayList[

  14. @String[META-INF/resources/swagger-ui.html],

  15. ]

  16. ts=2019-09-1421:51:14; [cost=0.039138ms] result=@ArrayList[

  17. @String[META-INF/resources/webjars/],

  18. ]

  19. ts=2019-09-1421:51:14; [cost=0.033701ms] result=@ArrayList[

  20. @String[META-INF/resources/webjars/springfox-swagger-ui/],

  21. ]

  22. ts=2019-09-1421:51:14; [cost=0.033644ms] result=@ArrayList[

  23. @String[META-INF/resources/webjars/springfox-swagger-ui/favicon-16x16.png],

  24. ]

  25. ts=2019-09-1421:51:14; [cost=0.033976ms] result=@ArrayList[

  26. @String[META-INF/resources/webjars/springfox-swagger-ui/springfox.css],

  27. ]

  28. ts=2019-09-1421:51:14; [cost=0.032818ms] result=@ArrayList[

  29. @String[META-INF/resources/webjars/springfox-swagger-ui/swagger-ui-standalone-preset.js.map],

  30. ]

  31. ts=2019-09-1421:51:14; [cost=0.04651ms] result=@ArrayList[

  32. @String[META-INF/resources/webjars/springfox-swagger-ui/swagger-ui.css],

  33. ]

  34. ts=2019-09-1421:51:14; [cost=0.034793ms] result=@ArrayList[

  35. @String[META-INF/resources/webjars/springfox-swagger-ui/swagger-ui.js.map],

这下直接看到了具体加载的资源名,这么熟悉的名字:swagger-ui,一个国外的rest接口文档工具,又有国内开发者基于swagger-ui做了一套spring mvc的集成工具,通过注解就可以自动生成swagger-ui需要的接口定义json文件,用起来还比较方便,就是侵入性较强。


删除swagger的jar包后问题,诡异的70+ms就消失了

pom中直接覆盖properties即可:

  1. 8.5.40

升级spring boot版本

springboot 2.1.0.RELEASE中的tomcat embed版本已经大于8.5.31了,所以直接将springboot升级至该版本及以上版本就可以解决此问题

浏览 46
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报