来源:
my.oschina.net/xiaomu0082/blog/2990388
首先说下问题现象:内网sandbox环境API持续1周出现应用卡死,所有api无响应现象
刚开始当测试抱怨环境响应慢的时候 ,我们重启一下应用,应用恢复正常,于是没做处理 。但是后来问题出现频率越来越频繁,越来越多的同事开始抱怨,于是感觉代码可能有问题,开始排查 。
top 命令首先发现开发的本地ide没有发现问题,应用卡死时候数据库,redis都正常,并且无特殊错误日志 。开始怀疑是sandbox环境机器问题(测试环境本身就很脆!_!)
于是ssh上了服务器 执行以下命令
top
文章插图
图片
这时发现机器还算正常,于是打算看下jvm 堆栈信息
先看下问题应用比较耗资源的线程
执行 top -H -p 12798
文章插图
图片
找到前3个相对比较耗资源的线程
jstack 命令jstack 查看堆内存
jstack 12798 |grep 12799的16进制 31ff
文章插图
图片
没看出什么问题,上下10行也看看,于是执行
文章插图
图片
看到一些线程都是处于lock状态 。但没有出现业务相关的代码,忽略了 。这时候没有什么头绪 。思考一番 。决定放弃这次卡死状态的机器
为了保护事故现场 先 dump了问题进程所有堆内存,然后debug模式重启测试环境应用,打算问题再显时直接远程debug问题机器
Tomcat 远程 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()后线程开始waitpublic 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;}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 使用Redis时要避免的5个错误
- |在职场中如何做才能让领导和同事觉得「这年轻人靠谱、有前途」?
- redis主从同步参数repl_backlog_size测算
- 华为架构师整理Redis数据结构的大厂最佳实践
- 极简Redis使用
- Redis常用的数据结构
- 泡面|防晒品可别乱用!
- 超详细Redis内容整理
- 5分钟掌握NIO底层原理
- nginx + lua + redis实现限流