Springboot shutdown 耗时太长的分析使用btrace

2023-11-04 08:08

本文主要是介绍Springboot shutdown 耗时太长的分析使用btrace,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

背景

从本文你可以学到如何分析jvm无法正常关闭的问题? 知道why and how.

没怎么用过springboot, 但是还是咬牙上了. 在这篇使用springboottest和h2来构建数据库测试的采坑记录中就发现我们的应用在测试用例跑完了无法自动关闭. 而且还总是等了2分钟就自动关闭了. 然后最开始以为是test case才有问题 结果发现是应用本身运行的时候正常关闭也有问题.
如下图:(测试已经完了,springboot开始shutdown 但是进程本身没有退出)
在这里插入图片描述

先google

发现都是说的如何gracefully shutdown的… 并没有立即shutdown的… 开始以为是springboot的问题, 写了个简单demo发现可以正常快速关闭…

初步诊断

一个简单办法是后台应用额外启动一个线程, 不断打印线程堆栈, 看看有哪些非daemon的线程,

        Thread th = new Thread(new Runnable() {@Overridepublic void run() {while(true) {try {Thread.sleep(1000 * 5);}catch (InterruptedException e) {e.printStackTrace();}Thread.getAllStackTraces().forEach((th, els) -> {System.out.println("-----------------");if (!th.isDaemon()) {System.out.println("non daemon:" + th);for (StackTraceElement e : els) {System.out.println("\t\t" + e);}} else {System.out.println("Daemon thread:" + th);}System.out.println("-----------------");});}}});th.setName("PrintThread");th.setDaemon(true);th.start();

我发现了这个:

Daemon thread:Thread[pool-8-thread-1,5,main]
-----------------
-----------------
non daemon:Thread[nioEventLoopGroup-2-4,10,main]sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:198)sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:117)sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)io.netty.channel.nio.SelectedSelectionKeySetSelector.select(SelectedSelectionKeySetSelector.java:62)io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:753)io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:408)io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:897)io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)java.lang.Thread.run(Thread.java:748)
-----------------
-----------------
Daemon thread:Thread[Attach Listener,9,system]
-----------------
-----------------
Daemon thread:Thread[BTrace Command Queue Processor,5,main]
-----------------
-----------------
Daemon thread:Thread[RMI TCP Accept-0,5,system]
-----------------
-----------------
Daemon thread:Thread[Abandoned connection cleanup thread,5,main]
-----------------
-----------------
non daemon:Thread[pool-3-thread-1,5,main]sun.misc.Unsafe.park(Native Method)java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)java.lang.Thread.run(Thread.java:748)
-----------------
-----------------
Daemon thread:Thread[RMI TCP Connection(3)-127.0.0.1,5,RMI Runtime]
-----------------
-----------------
Daemon thread:Thread[PrintThread,5,main]
-----------------
-----------------
non daemon:Thread[pool-6-thread-1,5,main]sun.misc.Unsafe.park(Native Method)java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)java.lang.Thread.run(Thread.java:748)
-----------------
-----------------
Daemon thread:Thread[Monitor Ctrl-Break,5,main]
-----------------
-----------------
non daemon:Thread[nioEventLoopGroup-2-3,10,main]sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:198)sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:117)sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)io.netty.channel.nio.SelectedSelectionKeySetSelector.select(SelectedSelectionKeySetSelector.java:62)io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:753)io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:408)io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:897)io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)java.lang.Thread.run(Thread.java:748)
-----------------
-----------------
non daemon:Thread[nioEventLoopGroup-2-5,10,main]sun.nio.ch.KQueueArrayWrapper.kevent0(Native Method)sun.nio.ch.KQueueArrayWrapper.poll(KQueueArrayWrapper.java:198)sun.nio.ch.KQueueSelectorImpl.doSelect(KQueueSelectorImpl.java:117)sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)io.netty.channel.nio.SelectedSelectionKeySetSelector.select(SelectedSelectionKeySetSelector.java:62)io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:753)io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:408)io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:897)io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)java.lang.Thread.run(Thread.java:748)
-----------------
-----------------
Daemon thread:Thread[COThread-kb,5,main]

有很多netty的线程没有关闭. 那么问题来了 : 如何知道是谁创建的这些线程呢? 在一个复杂项目中

大杀器 BTrace

我的另外一篇博客: 记录一次TCP连接异常问题-使用btrace
完整的代码参考github的md: btrace_usage.md 里面的0.1 Add an example of how to run 部分.
以前也有用过btrace, 发现btrace从 com.sun开源出来了… 给oracle点赞… 所以才有了更新后的文档.

回归正题

在这里插入图片描述可以看到是我们引用的一个外部组件初始化的netty. 想办法加入springboot shutdownhook中就可以了. ps结果还发现了项目中其他多个地方非daemon线程. 统一修改后就可以了. 比如用guava的ThreadFactoryBuilder修饰一下就可以了

Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("cleanup-expirecode").build()).scheduleAtFixedRate(() 

思考问题

  1. 前面我有说到, 在自己的应用启动了一个额外的进程来打印堆栈, 实际上这个可以通过btrace实现.就留给大家思考啦.
  2. springboot的DelayedShutdownHook 解决完自身的非daemon后发现还剩一个这个:
non daemon:Thread[DelayedShutdownHook-for-java.util.concurrent.ThreadPoolExecutor@2c47a053[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0],5,main]sun.misc.Unsafe.park(Native Method)java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)java.util.concurrent.ThreadPoolExecutor.awaitTermination(ThreadPoolExecutor.java:1475)com.google.common.util.concurrent.MoreExecutors$Application$1.run(MoreExecutors.java:203)java.lang.Thread.run(Thread.java:748)

如何通过btrace找到这个线程池是谁创建的呢? (ps: 跟前面监控线程创建类似类似)
结果发现是guava的线程池封装:

我们的代码:// private final ExecutorService _executor = Executors.newSingleThreadExecutor();private final ExecutorService _executor = MoreExecutors.getExitingExecutorService((ThreadPoolExecutor)Executors.newFixedThreadPool(1));
guava的代码:
com.google.common.util.concurrent.MoreExecutors.Application#getExitingExecutorService(java.util.concurrent.ThreadPoolExecutor)final ExecutorService getExitingExecutorService(ThreadPoolExecutor executor) {return getExitingExecutorService(executor, 120, TimeUnit.SECONDS);}

是的没错, 就是2分钟!!! 问题到此解决了.

这篇关于Springboot shutdown 耗时太长的分析使用btrace的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/344408

相关文章

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

MySQL 内存使用率常用分析语句

《MySQL内存使用率常用分析语句》用户整理了MySQL内存占用过高的分析方法,涵盖操作系统层确认及数据库层bufferpool、内存模块差值、线程状态、performance_schema性能数据... 目录一、 OS层二、 DB层1. 全局情况2. 内存占js用详情最近连续遇到mysql内存占用过高导致

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd