服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Redis - redis使用不当导致应用卡死bug的过程解析

redis使用不当导致应用卡死bug的过程解析

2021-08-15 21:52小木-_- Redis

本文主要记一次找因redis使用不当导致应用卡死bug的过程,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧

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

刚开始当测试抱怨环境响应慢的时候 ,我们重启一下应用,应用恢复正常,于是没做处理。但是后来问题出现频率越来越频繁,越来越多的同事开始抱怨,于是感觉代码可能有问题,开始排查。

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

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

top

redis使用不当导致应用卡死bug的过程解析

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

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

执行 top -h -p 12798

redis使用不当导致应用卡死bug的过程解析

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

jstack 查看堆内存

jstack 12798 |grep 12799的16进制 31ff

redis使用不当导致应用卡死bug的过程解析

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

redis使用不当导致应用卡死bug的过程解析

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

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

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

自己随意找了一个接口,断点在接口入口地方,悲剧开始,什么也没有发生!api等待服务响应,没进断点。这时候有点懵逼,冷静了一会,在入口之前的aop地方下了个断点,再debug一次,这次进了断点,f8 n次后发现在执行redis命令的时候卡主了。继续跟,最后在到jedis的一个地方发现问题:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * returns a jedis instance to be used as a redis connection. the instance can be newly created or retrieved from a
 * pool.
 *
 * @return jedis instance ready for wrapping into a {@link redisconnection}.
 */
protected jedis fetchjedisconnector() {
   try {
      if (usepool && pool != null) {
         return pool.getresource();
      }
      jedis jedis = new jedis(getshardinfo());
      // force initialization (see jedis issue #82)
      jedis.connect();
      return jedis;
   } catch (exception ex) {
      throw new redisconnectionfailureexception("cannot get jedis connection", ex);
   }
}

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

?
1
2
3
4
5
6
7
public t getresource() {
  try {
    return internalpool.borrowobject();
  } catch (exception e) {
    throw new jedisconnectionexception("could not get a resource from the pool", e);
  }
}

return internalpool.borrowobject(); 这个代码应该是一个租赁的代码 接着跟

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public t borrowobject(long borrowmaxwaitmillis) throws exception {
    this.assertopen();
    abandonedconfig ac = this.abandonedconfig;
    if (ac != null && ac.getremoveabandonedonborrow() && this.getnumidle() < 2 && this.getnumactive() > this.getmaxtotal() - 3) {
        this.removeabandoned(ac);
    }
 
    pooledobject<t> p = null;
    boolean blockwhenexhausted = this.getblockwhenexhausted();
    long waittime = 0l;
 
    while(p == null) {
        boolean create = false;
        if (blockwhenexhausted) {
            p = (pooledobject)this.idleobjects.pollfirst();
            if (p == null) {
                create = true;
                p = this.create();
            }
 
            if (p == null) {
                if (borrowmaxwaitmillis < 0l) {
                    p = (pooledobject)this.idleobjects.takefirst();
                } else {
                    waittime = system.currenttimemillis();
                    p = (pooledobject)this.idleobjects.pollfirst(borrowmaxwaitmillis, timeunit.milliseconds);
                    waittime = system.currenttimemillis() - waittime;
                }
            }
 
            if (p == null) {
                throw new nosuchelementexception("timeout waiting for idle object");
            }

其中有段代码

?
1
2
3
4
5
6
7
8
9
if (p == null) {
    if (borrowmaxwaitmillis < 0l) {
        p = (pooledobject)this.idleobjects.takefirst();
    } else {
        waittime = system.currenttimemillis();
        p = (pooledobject)this.idleobjects.pollfirst(borrowmaxwaitmillis, timeunit.milliseconds);
        waittime = system.currenttimemillis() - waittime;
    }
}

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

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

继续f8 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public e takefirst() throws interruptedexception {
    this.lock.lock();
 
    object var2;
    try {
        object x;
        while((x = this.unlinkfirst()) == null) {
            this.notempty.await();
        }
 
        var2 = x;
    } finally {
        this.lock.unlock();
    }
 
    return var2;
}

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

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

执行thread命令 

redis使用不当导致应用卡死bug的过程解析

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

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

?
1
thread -428

redis使用不当导致应用卡死bug的过程解析

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

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

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

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

redis使用不当导致应用卡死bug的过程解析

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

redis使用不当导致应用卡死bug的过程解析

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

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

添加一下配置

?
1
2
3
4
5
6
jedisconnectionfactory jedisconnectionfactory = new jedisconnectionfactory();
.......
jedispoolconfig config = new jedispoolconfig();
config.setmaxwaitmillis(2000);
.......
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 pool
    at 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地方,

发现以下代码

?
1
2
3
4
5
.......
cursor c = stringredistemplate.getconnectionfactory().getconnection().scan(options);
while (c.hasnext()) {
.....,,
   }

分析这个代码,stringredistemplate.getconnectionfactory().getconnection()获取pool中的redisconnection后,并没有后续操作,也就是说此时redis 连接池中的链接被租赁后并没有释放或者退还到链接池中,虽然业务已处理完毕 redisconnection 已经空闲,但是pool中的redisconnection的状态还没有回到idle状态

redis使用不当导致应用卡死bug的过程解析

正常应为

redis使用不当导致应用卡死bug的过程解析

自此问题已经找到。

总结:spring stringredistemplate 对redis常规操作做了一些封装,但还不支持像 scan setnx等命令,这时需要拿到jedis connection进行一些特殊的commands

使用 stringredistemplate.getconnectionfactory().getconnection() 是不被推荐的

我们可以使用

?
1
2
3
4
5
6
7
8
stringredistemplate.execute(new rediscallback<cursor>() {
 
     @override
     public cursor doinredis(redisconnection connection) throws dataaccessexception {
         
       return connection.scan(options);
     }
   });

来执行,

或者使用完connection后 ,用

?
1
redisconnectionutils.releaseconnection(conn, factory);

来释放connection.

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

 到此这篇关于redis使用不当导致应用卡死bug的过程解析的文章就介绍到这了,更多相关redis导致应用卡死内容请搜索萬仟网以前的文章或继续浏览下面的相关文章希望大家以后多多支持萬仟网!

原文链接:https://my.oschina.net/xiaomu0082/blog/2990388

延伸 · 阅读

精彩推荐
  • Redisredis启动,停止,及端口占用处理方法

    redis启动,停止,及端口占用处理方法

    今天小编就为大家分享一篇redis启动,停止,及端口占用处理方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    澄海单挑狂5152019-11-14
  • Redis就这?Redis持久化策略——AOF

    就这?Redis持久化策略——AOF

    今天为大家介绍Redis的另一种持久化策略——AOF。注意:AOF文件只会记录Redis的写操作命令,因为读命令对数据的恢复没有任何意义...

    头发茂密的刘叔4052021-12-14
  • RedisRedis分布式锁升级版RedLock及SpringBoot实现方法

    Redis分布式锁升级版RedLock及SpringBoot实现方法

    这篇文章主要介绍了Redis分布式锁升级版RedLock及SpringBoot实现,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以...

    等不到的口琴7802021-07-25
  • RedisRedis存取序列化与反序列化性能问题详解

    Redis存取序列化与反序列化性能问题详解

    这篇文章主要给大家介绍了关于Redis存取序列化与反序列化性能问题的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参...

    这名字已经存在9742021-02-24
  • Redis在ssm项目中使用redis缓存查询数据的方法

    在ssm项目中使用redis缓存查询数据的方法

    本文主要简单的使用Java代码进行redis缓存,即在查询的时候先在service层从redis缓存中获取数据。如果大家对在ssm项目中使用redis缓存查询数据的相关知识感...

    caychen8962019-11-12
  • RedisLinux Redis 的安装步骤详解

    Linux Redis 的安装步骤详解

    这篇文章主要介绍了 Linux Redis 的安装步骤详解的相关资料,希望大家通过本文能掌握如何安装Redis,需要的朋友可以参考下 ...

    carl-zhao3822019-11-08
  • Redis聊一聊Redis与MySQL双写一致性如何保证

    聊一聊Redis与MySQL双写一致性如何保证

    一致性就是数据保持一致,在分布式系统中,可以理解为多个节点中数据的值是一致的。本文给大家分享Redis与MySQL双写一致性该如何保证,感兴趣的朋友一...

    mind_programmonkey6432021-08-12
  • RedisRedis数据结构之链表与字典的使用

    Redis数据结构之链表与字典的使用

    这篇文章主要介绍了Redis数据结构之链表与字典的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

    白泽来了4052021-08-03