久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

因Redis使用不當導致應用卡死Bug的過程是怎樣的

196次閱讀
沒有評論

共計 6071 個字符,預計需要花費 16 分鐘才能閱讀完成。

這篇文章給大家介紹因 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 的一個地方發現問題:

/** * 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(); 這個代碼應該是一個租賃的代碼,接著跟

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 會一直執行,然后一直循環了 開始懷疑這個值沒有配置

找到 redis pool 配置,發現確實沒有配置 MaxWaitMillis,配置后 else 代碼也是一個 Exception 并不能解決問題

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

到這邊 發現 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, 然后等問題再次復現時觀察應用最后正常時干過什么。

添加一下配置

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 地方,

發現以下代碼..

..... 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()

是不被推薦的

我們可以使用

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

來執行,

或者使用完 connection 后,用

RedisConnectionUtils.releaseConnection(conn, factory);

來釋放 connection.

同時,redis 中也不建議使用 keys 命令,redis pool 的配置應該合理配上,否則出現問題無錯誤日志,無報錯,定位相當困難。

關于因 Redis 使用不當導致應用卡死 Bug 的過程是怎樣的就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-07-18發表,共計6071字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 米易县| 阳高县| 邯郸市| 双柏县| 新津县| 焉耆| 三原县| 高密市| 平乐县| 中西区| 岗巴县| 醴陵市| 伊吾县| 黄山市| 岳阳市| 泾源县| 宣化县| 科技| 公安县| 朝阳县| 洪雅县| 浦北县| 石泉县| 思茅市| 乐清市| 且末县| 诸城市| 九龙县| 顺平县| 廊坊市| 江口县| 苏尼特左旗| 嘉荫县| 会理县| 阳城县| 根河市| 赞皇县| 嘉祥县| 遂昌县| 中山市| 普陀区|