最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

redis使用不當(dāng)導(dǎo)致應(yīng)用卡死bug的過程解析

 更新時(shí)間:2021年07月01日 15:32:50   作者:小木-_-  
本文主要記一次找因redis使用不當(dāng)導(dǎo)致應(yīng)用卡死bug的過程,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

首先說下問題現(xiàn)象:內(nèi)網(wǎng)sandbox環(huán)境API持續(xù)1周出現(xiàn)應(yīng)用卡死,所有api無響應(yīng)現(xiàn)象

剛開始當(dāng)測(cè)試抱怨環(huán)境響應(yīng)慢的時(shí)候 ,我們重啟一下應(yīng)用,應(yīng)用恢復(fù)正常,于是沒做處理。但是后來問題出現(xiàn)頻率越來越頻繁,越來越多的同事開始抱怨,于是感覺代碼可能有問題,開始排查。

首先發(fā)現(xiàn)開發(fā)的本地ide沒有發(fā)現(xiàn)問題,應(yīng)用卡死時(shí)候數(shù)據(jù)庫,redis都正常,并且無特殊錯(cuò)誤日志。開始懷疑是sandbox環(huán)境機(jī)器問題(測(cè)試環(huán)境本身就很脆!_!)

于是ssh上了服務(wù)器 執(zhí)行以下命令

top

這時(shí)發(fā)現(xiàn) 機(jī)器還算正常,但是內(nèi)心還是😖,于是打算看下jvm 堆棧信息

先看下問題應(yīng)用比較耗資源的線程

執(zhí)行 top -H -p 12798

找到前3個(gè)相對(duì)比較耗資源的線程

jstack 查看堆內(nèi)存

jstack 12798 |grep 12799的16進(jìn)制 31ff

沒看出什么問題,上下10行也看看 于是執(zhí)行

看到一些線程都是處于lock狀態(tài)。但沒有出現(xiàn)業(yè)務(wù)相關(guān)的代碼,忽略了。這時(shí)候沒有什么頭緒。思考一番。決定放棄這次卡死狀態(tài)的機(jī)器

為了保護(hù)事故現(xiàn)場(chǎng) 先 dump了問題進(jìn)程所有堆內(nèi)存,然后debug模式重啟測(cè)試環(huán)境應(yīng)用,打算問題再顯時(shí)直接遠(yuǎn)程debug問題機(jī)器

第二天問題再現(xiàn),于是通知運(yùn)維nginx轉(zhuǎn)發(fā)拿掉這臺(tái)問題應(yīng)用,自己遠(yuǎn)程debug tomcat。

自己隨意找了一個(gè)接口,斷點(diǎn)在接口入口地方,悲劇開始,什么也沒有發(fā)生!API等待服務(wù)響應(yīng),沒進(jìn)斷點(diǎn)。這時(shí)候有點(diǎn)懵逼,冷靜了一會(huì),在入口之前的aop地方下了個(gè)斷點(diǎn),再debug一次,這次進(jìn)了斷點(diǎn),f8 N次后發(fā)現(xiàn)在執(zhí)行redis命令的時(shí)候卡主了。繼續(xù)跟,最后在到j(luò)edis的一個(gè)地方發(fā)現(xiàn)問題:

/**
 * 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

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(); 這個(gè)代碼應(yīng)該是一個(gè)租賃的代碼 接著跟

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");
            }

其中有段代碼

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會(huì)一直執(zhí)行,然后一直循環(huán)了 開始懷疑這個(gè)值沒有配置

找到redis pool配置,發(fā)現(xiàn)確實(shí)沒有配置MaxWaitMillis,配置后else代碼也是一個(gè)Exception 并不能解決問題

繼續(xù)F8 

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;
}

到這邊 發(fā)現(xiàn)lock字眼,開始懷疑所有請(qǐng)求api都被阻塞了

于是再次ssh 服務(wù)器 安裝 arthas ,(Arthas 是Alibaba開源的Java診斷工具)

執(zhí)行thread命令 

發(fā)現(xiàn)大量http-nio的線程waiting狀態(tài),http-nio-8083-exec-這個(gè)線程其實(shí)就是出來http請(qǐng)求的tomcat線程

隨意找一個(gè)線程查看堆內(nèi)存

thread -428

這是能確認(rèn)就是api一直轉(zhuǎn)圈的問題,就是這個(gè)redis獲取連接的代碼導(dǎo)致的,

解讀這段內(nèi)存代碼  所有線程都在等 @53e5504e這個(gè)對(duì)象釋放鎖。于是jstack 全局搜了一把53e5504e ,沒有找到這個(gè)對(duì)象所在線程。

自此。問題原因能確定是 redis連接獲取的問題。但是什么原因造成獲取不到連接的還不能確定

再次執(zhí)行 arthas 的thread -b (thread -b, 找出當(dāng)前阻塞其他線程的線程)

沒有結(jié)果。這邊和想的不一樣,應(yīng)該是能找到一個(gè)阻塞線程的,于是看了下這個(gè)命令的文檔,發(fā)現(xiàn)有下面的一句話

好吧,我們剛好是后者。。。。

再次整理下思路。這次修改redis pool 配置,將獲取連接超時(shí)時(shí)間設(shè)置為2s,然后等問題再次復(fù)現(xiàn)時(shí)觀察應(yīng)用最后正常時(shí)干過什么。

添加一下配置

JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
.......
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxWaitMillis(2000);
.......
jedisConnectionFactory.afterPropertiesSet();

重啟服務(wù),等待。。。。

又過一天,再次復(fù)現(xiàn)

ssh 服務(wù)器,檢查tomcat accesslog ,發(fā)現(xiàn)大量api 請(qǐng)求出現(xiàn)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)

找到源頭第一次出現(xiàn)500地方,

發(fā)現(xiàn)以下代碼

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

分析這個(gè)代碼,stringRedisTemplate.getConnectionFactory().getConnection()獲取pool中的redisConnection后,并沒有后續(xù)操作,也就是說此時(shí)redis 連接池中的鏈接被租賃后并沒有釋放或者退還到鏈接池中,雖然業(yè)務(wù)已處理完畢 redisConnection 已經(jīng)空閑,但是pool中的redisConnection的狀態(tài)還沒有回到idle狀態(tài)

正常應(yīng)為

自此問題已經(jīng)找到。

總結(jié):spring stringRedisTemplate 對(duì)redis常規(guī)操作做了一些封裝,但還不支持像 Scan SetNx等命令,這時(shí)需要拿到j(luò)edis Connection進(jìn)行一些特殊的Commands

使用 stringRedisTemplate.getConnectionFactory().getConnection() 是不被推薦的

我們可以使用

stringRedisTemplate.execute(new RedisCallback<Cursor>() {

     @Override
     public Cursor doInRedis(RedisConnection connection) throws DataAccessException {
         
       return connection.scan(options);
     }
   });

來執(zhí)行,

或者使用完connection后 ,用

RedisConnectionUtils.releaseConnection(conn, factory);

來釋放connection.

同時(shí),redis中也不建議使用keys命令,redis pool的配置應(yīng)該合理配上,否則出現(xiàn)問題無錯(cuò)誤日志,無報(bào)錯(cuò),定位相當(dāng)困難。

 到此這篇關(guān)于redis使用不當(dāng)導(dǎo)致應(yīng)用卡死bug的過程解析的文章就介紹到這了,更多相關(guān)redis導(dǎo)致應(yīng)用卡死內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Redis中跳表的實(shí)現(xiàn)原理分析

    Redis中跳表的實(shí)現(xiàn)原理分析

    Redis中的跳表是一種高效的多層鏈表結(jié)構(gòu),通過隨機(jī)概率算法決定節(jié)點(diǎn)的層數(shù),從而實(shí)現(xiàn)快速的插入、刪除和查詢操作,跳表的平均時(shí)間復(fù)雜度為O(logn),最差情況為O(n),每個(gè)節(jié)點(diǎn)包含值和指向更高層節(jié)點(diǎn)的指針,以及回退指針以提高操作效率
    2025-02-02
  • Redis如何高效刪除大key

    Redis如何高效刪除大key

    這篇文章主要介紹了Redis如何高效刪除大key問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Redisson實(shí)現(xiàn)分布式鎖、鎖續(xù)約的案例

    Redisson實(shí)現(xiàn)分布式鎖、鎖續(xù)約的案例

    這篇文章主要介紹了Redisson如何實(shí)現(xiàn)分布式鎖、鎖續(xù)約,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • Redis集群方案

    Redis集群方案

    前段時(shí)間搞了搞Redis集群,想用做推薦系統(tǒng)的線上存儲(chǔ),說來挺有趣,這邊基礎(chǔ)架構(gòu)不太完善,因此需要我們做推薦系統(tǒng)的自己來搭這個(gè)存儲(chǔ)環(huán)境,就自己折騰了折騰
    2020-07-07
  • redis禁止幾個(gè)危險(xiǎn)命令的方法

    redis禁止幾個(gè)危險(xiǎn)命令的方法

    今天小編就為大家分享一篇redis禁止幾個(gè)危險(xiǎn)命令的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • Redis中的延遲雙刪

    Redis中的延遲雙刪

    這篇文章主要介紹了Redis中的延遲雙刪問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • redis?消息隊(duì)列完成秒殺過期訂單處理方法(一)

    redis?消息隊(duì)列完成秒殺過期訂單處理方法(一)

    這篇文章主要介紹了redis?消息隊(duì)列完成秒殺過期訂單處理方法,包括redis?消息通知處理代金券過期問題–失效問題的分析,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • Redis中LRU淘汰策略的深入分析

    Redis中LRU淘汰策略的深入分析

    這篇文章主要給大家介紹了關(guān)于Redis中LRU淘汰策略的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Redis批量刪除指定前綴的Key兩種方法

    Redis批量刪除指定前綴的Key兩種方法

    redis作為緩存服務(wù)器在項(xiàng)目中經(jīng)常使用,使用redis存儲(chǔ)數(shù)據(jù)時(shí),我們經(jīng)常會(huì)將key分組,這篇文章主要給大家介紹了關(guān)于Redis批量刪除指定前綴的Key兩種方法,需要的朋友可以參考下
    2024-01-01
  • Redis?使用?List?實(shí)現(xiàn)消息隊(duì)列的優(yōu)缺點(diǎn)

    Redis?使用?List?實(shí)現(xiàn)消息隊(duì)列的優(yōu)缺點(diǎn)

    這篇文章主要介紹了Redis?使用?List?實(shí)現(xiàn)消息隊(duì)列有哪些利弊,小編結(jié)合消息隊(duì)列的特點(diǎn)一步步帶大家分析使用?Redis?的?List?作為消息隊(duì)列的實(shí)現(xiàn)原理,并分享如何把?SpringBoot?與?Redission?整合運(yùn)用到項(xiàng)目中,需要的朋友可以參考下
    2022-01-01

最新評(píng)論

杭锦旗| 平谷区| 徐汇区| 陇西县| 突泉县| 来凤县| 拉萨市| 文昌市| 美姑县| 贺州市| 黄大仙区| 长治县| 印江| 紫云| 什邡市| 贵阳市| 醴陵市| 沾化县| 龙岩市| 二连浩特市| 紫云| 云阳县| 巴林左旗| 正定县| 化州市| 大庆市| 永仁县| 罗江县| 高安市| 富源县| 濮阳县| 鄂伦春自治旗| 陵川县| 萨迦县| 安平县| 绵竹市| 乌兰察布市| 乌兰浩特市| 泸水县| 焉耆| 普格县|