问题遇到的现象和发生背景
最近选用Redisson来处理分布式锁的问题,测试环境运行项目后,java 进程Cpu会有规律性的起伏飙高。
相关版本与配置如下
测试服务内存大小:512m
redisson版本:3.17.1
redis版本:3.2.1
redisson配置如下:
@Bean
public RedissonClient redissonClient() {
Config config = new Config();
config.setLockWatchdogTimeout(15000)
.setThreads(4)
.setNettyThreads(4)
.useSingleServer()
.setAddress("redis://" + host + ":" + port)
.setPassword(password)
.setDatabase(1);
return Redisson.create(config);
}
运行结果及报错内容
使用arthas监控发现 main线程组中 有个ConbainerBackgroud的守护线程导致cpu过高,时间在7-8秒会循环一次。
进一步使用 thread -n tid 查看堆栈信息。发现循环输出以下内容。
"redisson-netty-2-3" Id=428 cpuUsage=0.0% deltaTime=0ms time=62ms RUNNABLE (in native)
at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(Unknown Source)
at sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(Unknown Source)
at sun.nio.ch.WindowsSelectorImpl.doSelect(Unknown Source)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(Unknown Source)
at sun.nio.ch.SelectorImpl.select(Unknown Source)
at sun.nio.ch.SelectorImpl.select(Unknown Source)
at io.netty.channel.nio.SelectedSelectionKeySetSelector.select(SelectedSelectionKeySetSelector.java:68)
at io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:803)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:457)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Unknown Source)
"redisson-netty-2-4" Id=429 cpuUsage=0.0% deltaTime=0ms time=62ms RUNNABLE (in native)
at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(Native Method)
at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll(Unknown Source)
at sun.nio.ch.WindowsSelectorImpl$SubSelector.access$400(Unknown Source)
at sun.nio.ch.WindowsSelectorImpl.doSelect(Unknown Source)
at sun.nio.ch.SelectorImpl.lockAndDoSelect(Unknown Source)
at sun.nio.ch.SelectorImpl.select(Unknown Source)
at sun.nio.ch.SelectorImpl.select(Unknown Source)
at io.netty.channel.nio.SelectedSelectionKeySetSelector.select(SelectedSelectionKeySetSelector.java:68)
at io.netty.channel.nio.NioEventLoop.select(NioEventLoop.java:803)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:457)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Unknown Source)
"redisson-timer-4-1" Id=425 cpuUsage=0.0% deltaTime=0ms time=0ms TIMED_WAITING
at java.lang.Thread.sleep(Native Method)
at io.netty.util.HashedWheelTimer$Worker.waitForNextTick(HashedWheelTimer.java:569)
at io.netty.util.HashedWheelTimer$Worker.run(HashedWheelTimer.java:465)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Unknown Source)
4.我的解答思路和尝试过的方法
起初怀疑是默认线程问题,后续将线程改为4依旧存在Cpu飙高情况。
后续网上Google堆栈信息无果。是否与Netty实现有关。
5.我想要达到的结果
降低cpu占有率,并且想明白输出的堆栈信息是否为正常现象,望各位指点一二。
使用Redssion和netty没毛病,问题肯定是出在你的代码中,我猜那个守护线程应该是垃圾回收线程,高并发的话,你可以把容器换了,垃圾回收机制也换一下
jstack看看,是不是有额外信息,这点信息太少了