【转载】记一次因 Redis 使用不当导致应用卡死 bug 的排查及解决!

本文主要是介绍【转载】记一次因 Redis 使用不当导致应用卡死 bug 的排查及解决!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

说明:此篇文章 作者分析问题的思路很好,值得学习记录,原文转载自公众号。

 

首先说下问题现象:内网sandbox环境API持续1周出现应用卡死,所有api无响应现象

刚开始当测试抱怨环境响应慢的时候 ,我们重启一下应用,应用恢复正常,于是没做处理。

但是后来问题出现频率越来越频繁,越来越多的同事开始抱怨,于是感觉代码可能有问题,开始排查。

首先发现开发的本地ide没有发现问题,应用卡死时候数据库,redis都正常,并且无特殊错误日志。开始怀疑是sandbox环境机器问题(测试环境本身就很脆!_!)

于是ssh上了服务器 执行以下命令

top

 

这时发现机器还算正常,于是打算看下jvm 堆栈信息

先看下问题应用比较耗资源的线程

执行 top -H -p 12798

 

找到前3个相对比较耗资源的线程

jstack 查看堆内存

jstack 12798 |grep 12799的16进制 31ff

 

没看出什么问题,上下10行也看看,于是执行

 

看到一些线程都是处于lock状态。但没有出现业务相关的代码,忽略了。这时候没有什么头绪。思考一番。决定放弃这次卡死状态的机器

为了保护事故现场 先 dump了问题进程所有堆内存,然后debug模式重启测试环境应用,打算问题再显时直接远程debug问题机器

第二天问题再现,于是通知运维nginx转发拿掉这台问题应用,自己远程debug tomcat。

自己随意找了一个接口,断点在接口入口地方,悲剧开始,什么也没有发生!API等待服务响应,没进断点。

这时候有点懵逼,冷静了一会,在入口之前的aop地方下了个断点,再debug一次,这次进了断点,f8 N次后发现在执行redis命令的时候卡主了。

继续跟,最后在到jedis的一个地方发现问题:

 1 /**
 2  * Returns a Jedis instance to be used as a Redis connection. The instance can be newly created or retrieved from a
 3  * pool.
 4  * 
 5  * @return Jedis instance ready for wrapping into a {@link RedisConnection}.
 6  */
 7 protected Jedis fetchJedisConnector() {
 8    try {
 9       if (usePool && pool != null) {
10          return pool.getResource();
11       }
12       Jedis jedis = new Jedis(getShardInfo());
13       // force initialization (see Jedis issue #82)
14       jedis.connect();
15       return jedis;
16    } catch (Exception ex) {
17       throw new RedisConnectionFailureException("Cannot get Jedis connection", ex);
18    }
19 }

上面pool.getResource()后线程开始wait

 1 public T getResource() {
 2   try {
 3     return internalPool.borrowObject();
 4   } catch (Exception e) {
 5     throw new JedisConnectionException("Could not get a resource from the pool", e);
 6   }
 7 }
 8 
 9 return internalPool.borrowObject(); 这个代码应该是一个租赁的代码,接着跟
10 
11 public T borrowObject(long borrowMaxWaitMillis) throws Exception {
12     this.assertOpen();
13     AbandonedConfig ac = this.abandonedConfig;
14     if (ac != null && ac.getRemoveAbandonedOnBorrow() && this.getNumIdle() < 2 && this.getNumActive() > this.getMaxTotal() - 3) {
15         this.removeAbandoned(ac);
16     }
17 
18     PooledObject p = null;
19     boolean blockWhenExhausted = this.getBlockWhenExhausted();
20     long waitTime = 0L;
21 
22     while(p == null) {
23         boolean create = false;
24         if (blockWhenExhausted) {
25             p = (PooledObject)this.idleObjects.pollFirst();
26             if (p == null) {
27                 create = true;
28                 p = this.create();
29             }
30 
31             if (p == null) {
32                 if (borrowMaxWaitMillis < 0L) {
33                     p = (PooledObject)this.idleObjects.takeFirst();
34                 } else {
35                     waitTime = System.currentTimeMillis();
36                     p = (PooledObject)this.idleObjects.pollFirst(borrowMaxWaitMillis, TimeUnit.MILLISECONDS);
37                     waitTime = System.currentTimeMillis() - waitTime;
38                 }
39             }
40 
41             if (p == null) {
42                 throw new NoSuchElementException("Timeout waiting for idle object");
43             }

其中有段代码

1 if (p == null) {
2     if (borrowMaxWaitMillis < 0L) {
3         p = (PooledObject)this.idleObjects.takeFirst();
4     } else {
5         waitTime = System.currentTimeMillis();
6         p = (PooledObject)this.idleObjects.pollFirst(borrowMaxWaitMillis, TimeUnit.MILLISECONDS);
7         waitTime = System.currentTimeMillis() - waitTime;
8     }
9 }

borrowMaxWaitMillis<0会一直执行,然后一直循环了 开始怀疑这个值没有配置

找到redis pool配置,发现确实没有配置MaxWaitMillis,配置后else代码也是一个Exception 并不能解决问题

继续F8

 1 public E takeFirst() throws InterruptedException {
 2     this.lock.lock();
 3 
 4     Object var2;
 5     try {
 6         Object x;
 7         while((x = this.unlinkFirst()) == null) {
 8             this.notEmpty.await();
 9         }
10 
11         var2 = x;
12     } finally {
13         this.lock.unlock();
14     }
15 
16     return var2;
17 }

到这边 发现lock字眼,开始怀疑所有请求api都被阻塞了

于是再次ssh 服务器 安装 arthas ,(Arthas 是Alibaba开源的Java诊断工具)

执行thread命令

 

发现大量http-nio的线程waiting状态,http-nio-8083-exec-这个线程其实就是出来http请求的tomcat线程

随意找一个线程查看堆内存

thread -428

 

这是能确认就是api一直转圈的问题,就是这个redis获取连接的代码导致的,

解读这段内存代码  所有线程都在等 @53e5504e这个对象释放锁。于是jstack 全局搜了一把53e5504e ,没有找到这个对象所在线程。

自此。问题原因能确定是 redis连接获取的问题。但是什么原因造成获取不到连接的还不能确定

再次执行 arthas 的thread -b (thread -b, 找出当前阻塞其他线程的线程)

 

没有结果。这边和想的不一样,应该是能找到一个阻塞线程的,于是看了下这个命令的文档,发现有下面的一句话

 

好吧,我们刚好是后者。。。。

再次整理下思路。这次修改redis pool 配置,将获取连接超时时间设置为2s,然后等问题再次复现时观察应用最后正常时干过什么。

添加一下配置

1 JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
2 .......
3 JedisPoolConfig config = new JedisPoolConfig();
4 config.setMaxWaitMillis(2000);
5 .......
6 jedisConnectionFactory.afterPropertiesSet();

重启服务,等待。。。。

又过一天,再次复现

ssh 服务器,检查tomcat accesslog ,发现大量api 请求出现500,

org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource fr
om the poolat org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:140)at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:229)at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:57)at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:128)at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:91)at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:78)at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:177)at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:152)at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:85)at org.springframework.data.redis.core.DefaultHashOperations.get(DefaultHashOperations.java:48)

 

找到源头第一次出现500地方,

发现以下代码

.......
Cursor c = stringRedisTemplate.getConnectionFactory().getConnection().scan(options);
while (c.hasNext()) {
.....,,}

分析这个代码,stringRedisTemplate.getConnectionFactory().getConnection()获取pool中的redisConnection后,并没有后续操作

也就是说此时redis 连接池中的链接被租赁后并没有释放或者退还到链接池中,虽然业务已处理完毕 redisConnection 已经空闲,但是pool中的redisConnection的状态还没有回到idle状态

 

正常应为

 

自此问题已经找到。

总结:spring stringRedisTemplate 对redis常规操作做了一些封装,但还不支持像 Scan SetNx等命令,这时需要拿到jedis Connection进行一些特殊的Commands

使用

stringRedisTemplate.getConnectionFactory().getConnection()

是不被推荐的

我们可以使用

1 stringRedisTemplate.execute(new RedisCallback() {
2 
3      @Override
4      public Cursor doInRedis(RedisConnection connection) throws DataAccessException {
5 
6        return connection.scan(options);
7      }
8    });

 

来执行,或者使用完connection后 ,用

RedisConnectionUtils.releaseConnection(conn, factory);

来释放connection.

同时,redis中也不建议使用keys命令,redis pool的配置应该合理配上,否则出现问题无错误日志,无报错,定位相当困难。

 

【转载说明】

作者:小木-_-

来源:https://my.oschina.net/xiaomu0082/blog/2990388

这篇关于【转载】记一次因 Redis 使用不当导致应用卡死 bug 的排查及解决!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Redis消息队列实现异步秒杀功能

《Redis消息队列实现异步秒杀功能》在高并发场景下,为了提高秒杀业务的性能,可将部分工作交给Redis处理,并通过异步方式执行,Redis提供了多种数据结构来实现消息队列,总结三种,本文详细介绍Re... 目录1 Redis消息队列1.1 List 结构1.2 Pub/Sub 模式1.3 Stream 结

SpringBoot内嵌Tomcat临时目录问题及解决

《SpringBoot内嵌Tomcat临时目录问题及解决》:本文主要介绍SpringBoot内嵌Tomcat临时目录问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录SprinjavascriptgBoot内嵌Tomcat临时目录问题1.背景2.方案3.代码中配置t

SpringBoot中配置Redis连接池的完整指南

《SpringBoot中配置Redis连接池的完整指南》这篇文章主要为大家详细介绍了SpringBoot中配置Redis连接池的完整指南,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以... 目录一、添加依赖二、配置 Redis 连接池三、测试 Redis 操作四、完整示例代码(一)pom.

Python结合PyWebView库打造跨平台桌面应用

《Python结合PyWebView库打造跨平台桌面应用》随着Web技术的发展,将HTML/CSS/JavaScript与Python结合构建桌面应用成为可能,本文将系统讲解如何使用PyWebView... 目录一、技术原理与优势分析1.1 架构原理1.2 核心优势二、开发环境搭建2.1 安装依赖2.2 验

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

SpringShell命令行之交互式Shell应用开发方式

《SpringShell命令行之交互式Shell应用开发方式》本文将深入探讨SpringShell的核心特性、实现方式及应用场景,帮助开发者掌握这一强大工具,具有很好的参考价值,希望对大家有所帮助,如... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定

SpringBoot应用中出现的Full GC问题的场景与解决

《SpringBoot应用中出现的FullGC问题的场景与解决》这篇文章主要为大家详细介绍了SpringBoot应用中出现的FullGC问题的场景与解决方法,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录Full GC的原理与触发条件原理触发条件对Spring Boot应用的影响示例代码优化建议结论F

MySQL 分区与分库分表策略应用小结

《MySQL分区与分库分表策略应用小结》在大数据量、复杂查询和高并发的应用场景下,单一数据库往往难以满足性能和扩展性的要求,本文将详细介绍这两种策略的基本概念、实现方法及优缺点,并通过实际案例展示如... 目录mysql 分区与分库分表策略1. 数据库水平拆分的背景2. MySQL 分区策略2.1 分区概念