自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Redis使用不當(dāng),把應(yīng)用搞掛了!

存儲 存儲軟件 Redis
今年以來,運(yùn)氣都不太好。公司在大力發(fā)展,招了不少新同事。新來的同事在使用 Redis 時(shí),寫了一個(gè) Bug,導(dǎo)致應(yīng)用卡死。

 [[404845]]

圖片來自包圖網(wǎng)

老板直接批評了我,說我也有連帶責(zé)任,怎么帶的團(tuán)隊(duì),質(zhì)量不過關(guān),造成重量級生產(chǎn)事故,好在未造成財(cái)產(chǎn)損失!

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

剛開始當(dāng)測試抱怨環(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ī)器問題(測試環(huán)境本身就很脆弱)。

于是 ssh 登陸服務(wù)器 ,執(zhí)行以下命令:

這時(shí)發(fā)現(xiàn)機(jī)器還算正常,但是內(nèi)心還是??,于是打算看下 JVM 堆棧信息。先看下問題應(yīng)用比較耗資源的線程。

執(zhí)行 top -H -p 12798,如下圖:

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

  • 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)場,先 dump 了問題進(jìn)程所有堆內(nèi)存,然后 debug 模式重啟測試環(huán)境應(yīng)用,打算問題再顯時(shí)直接遠(yuǎn)程 debug 問題機(jī)器。

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

自己隨意找了一個(gè)接口,斷點(diǎn)在接口入口地方。悲劇開始,什么也沒有發(fā)生!API 等待服務(wù)響應(yīng),沒進(jìn)斷點(diǎn)。

這時(shí)候有點(diǎn)懵逼,冷靜了一會,在入口之前的 AOP 地方下了個(gè)斷點(diǎn),再 debug 一次,這次進(jìn)了斷點(diǎn)。F8 N 次后發(fā)現(xiàn)在執(zhí)行 Redis 命令的時(shí)候卡住了。

繼續(xù)跟蹤,最后在到 jedis 的一個(gè)地方發(fā)現(xiàn)問題:

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

上面 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.   } 

return internalPool.borrowObject();這個(gè)代碼應(yīng)該是一個(gè)租賃的代碼。接著跟蹤:

  1. public T borrowObject(long borrowMaxWaitMillis) throws Exception { 
  2.     this.assertOpen(); 
  3.     AbandonedConfig ac = this.abandonedConfig; 
  4.     if (ac != null && ac.getRemoveAbandonedOnBorrow() && this.getNumIdle() < 2 && this.getNumActive() > this.getMaxTotal() - 3) { 
  5.         this.removeAbandoned(ac); 
  6.     } 
  7.  
  8.     PooledObject<T> p = null
  9.     boolean blockWhenExhausted = this.getBlockWhenExhausted(); 
  10.     long waitTime = 0L; 
  11.  
  12.     while(p == null) { 
  13.         boolean create = false
  14.         if (blockWhenExhausted) { 
  15.             p = (PooledObject)this.idleObjects.pollFirst(); 
  16.             if (p == null) { 
  17.                 create = true
  18.                 p = this.create(); 
  19.             } 
  20.  
  21.             if (p == null) { 
  22.                 if (borrowMaxWaitMillis < 0L) { 
  23.                     p = (PooledObject)this.idleObjects.takeFirst(); 
  24.                 } else { 
  25.                     waitTime = System.currentTimeMillis(); 
  26.                     p = (PooledObject)this.idleObjects.pollFirst(borrowMaxWaitMillis, TimeUnit.MILLISECONDS); 
  27.                     waitTime = System.currentTimeMillis() - waitTime; 
  28.                 } 
  29.             } 
  30.  
  31.             if (p == null) { 
  32.                 throw new NoSuchElementException("Timeout waiting for idle object"); 
  33.             } 

其中有段代碼:

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

borrowMaxWaitMillis<0 會一直執(zhí)行,然后一直循環(huán)了。開始懷疑這個(gè)值沒有配置。

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

繼續(xù) 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; 

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

于是再次 ssh 服務(wù)器安裝 arthas(Arthas 是 Alibaba 開源的 Java 診斷工具)執(zhí)行 thread 命令:

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

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

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

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

自此,問題原因能確定是 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í)干過什么。

添加以下配置:

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

重啟服務(wù),等待... 又過一天,再次復(fù)現(xiàn)。

ssh 登陸服務(wù)器,檢查 Tomcat accesslog,發(fā)現(xiàn)大量 API 請求出現(xiàn) 500。

  1. org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource fr 
  2. om the pool 
  3.     at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:140) 
  4.     at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:229) 
  5.     at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:57) 
  6.     at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:128) 
  7.     at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:91) 
  8.     at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:78) 
  9.     at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:177) 
  10.     at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:152) 
  11.     at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:85) 
  12.     at org.springframework.data.redis.core.DefaultHashOperations.get(DefaultHashOperations.java:48) 

找到源頭第一次出現(xiàn) 500 地方,發(fā)現(xiàn)以下代碼:

  1. ....... 
  2. Cursor c = stringRedisTemplate.getConnectionFactory().getConnection().scan(options); 
  3. while (c.hasNext()) { 
  4. .....,, 
  5.    } 

分析這段代碼:

  1. 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 對 Redis 常規(guī)操作做了一些封裝,但還不支持像 Scan SetNx 等命令。這時(shí)需要拿到 jedis Connection 進(jìn)行一些特殊的 Commands。

不推薦使用:

  1. stringRedisTemplate.getConnectionFactory().getConnection() 

我們可以使用下面代碼來執(zhí)行:

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

或者使用完 connection 后執(zhí)行:

  1. RedisConnectionUtils.releaseConnection(conn, factory); 

來釋放 Connection。

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

作者:小木-_-

編輯:陶家龍

出處:my.oschina.net/xiaomu0082/blog/2990388

 

責(zé)任編輯:武曉燕 來源: 開源博客
相關(guān)推薦

2021-05-20 10:02:50

系統(tǒng)Redis技巧

2009-12-17 14:53:52

VS2008程序

2019-10-10 15:40:17

redisbug數(shù)據(jù)庫

2022-10-25 18:00:00

Redis事務(wù)生產(chǎn)事故

2020-10-22 07:09:19

TCP網(wǎng)絡(luò)協(xié)議

2021-08-26 14:26:25

Java代碼集合

2021-09-11 19:00:54

Intro元素MemoryCache

2021-07-11 09:34:45

ArrayListLinkedList

2022-06-21 11:24:05

多線程運(yùn)維

2024-02-04 08:26:38

線程池參數(shù)內(nèi)存

2024-06-28 10:01:04

2011-08-18 13:49:32

筆記本技巧

2024-09-05 08:07:55

2010-01-06 10:56:47

華為交換機(jī)使用

2021-02-06 13:11:28

SQL系統(tǒng)數(shù)據(jù)庫

2021-08-10 15:32:12

Redis緩存數(shù)據(jù)庫

2022-06-08 08:06:05

LinuxJVM內(nèi)存

2022-07-24 09:46:48

優(yōu)雅停機(jī)代碼

2020-11-06 15:00:58

PHPMySQL數(shù)據(jù)庫

2019-09-11 10:09:00

Java虛擬機(jī)算法
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號