乐趣区

关于java:记一次CPU过高排查过程

存在的问题

上周忽然在部署一点很简略的新业务之后,上线没多久忽然 OOM,大部分接口拜访超时,甚至有的间接失败,刚开始认为是查问了什么了大数据导致的,后果看了下 CPU,300%。

排查思路

最开始我先看了下日志,如下:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
### The error may exist in file [/Users/bingfeng/Documents/mochuCode/service-ecook/target/classes/mybatis/mapper/CollectionSortMapper.xml]
### The error may involve cn.ecook.core.mapper.CollectionSortMapper.selectTopSortCollectionWithDays
### The error occurred while handling results
### SQL: select sortid as id ,count(*) as `number` from `collection_sort_collection`          WHERE  createtime between concat(?, '00:00:00') and concat(?, '23:59:59')          group by sortid         order by `number` DESC LIMIT ?
### Cause: java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:92)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
    at com.sun.proxy.$Proxy97.selectList(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:223)
    at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:93)
    at com.sun.proxy.$Proxy166.selectTopSortCollectionWithDays(Unknown Source)

日志过后只有这种报错,最开始我认为这就是简略的索引越界异样,没在意,把这块问题过掉了。

剖析 CPU 高的过程

到这里咱们就须要通过堆栈信息去进行剖析,看到底是哪里的呈现了问题,上面是具体的步骤:

  • 依据过程号查问查看过程中各个线程的资源使用率
top -Hp 2159

这里的 CPU 是复原后的,过后的后果 CPU 前三个都是 99%。

  • 将线程 id 转换为 16 进制

    printf "%x\n" 2205

  • 打印堆栈信息

    jstack 2159 | grep -10 89d

通过上面的状态,能够发现,呈现接口拜访慢的起因是因为所有的线程阻塞导致的,再往下能够发现导致这些问题的起因是查问 SQL 导致的。那么咱们就把最新提交的代码看看,哪里进行了 SQL 查问。

问题定位

通过排查发现,有这么一段代码,这是 SQL 查问后果的实体,就是因为应用了 @Builder 注解,没有显式的提供结构,才导致 CPU 始终飙升。

再应用 Builder 之后,肯定要显式的申明构造方法

反思

呈现这种问题我感觉就两个起因:

  • 1、自测不到位;
  • 2、标准不够(要是标准到位,实体类不可能呈现没有结构的状况);
退出移动版