开心一刻
昨晚和一个朋友聊天
我:处对象吗,咱俩试试?
朋友:我有对象
我:我不信,有对象不公开?
朋友:不好公开,我当的小三
问题背景
程序在生产环境稳定的跑着
jar
jar
自己在开发环境也做了主流业务的测试,没有任何异常,稳如老狗
提测之后,测试小姐姐也没测出问题,一切都是这么美好
org.redisson.client.RedisException: ERR unknown command 'WAIT'
完整的异常堆栈信息类似如下
org.redisson.client.RedisException: ERR unknown command 'WAIT'. channel: [id: 0x84149c6e, L:/192.168.2.40:3592 - R:/47.98.21.100:6379] command: (WAIT), params: [1, 1000]
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:346)
at org.redisson.client.handler.CommandDecoder.decodeCommandBatch(CommandDecoder.java:247)
at org.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:189)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:117)
at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:102)
at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:508)
at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
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(Thread.java:748)
View Code
突然来个这个鬼玩意,脑阔有点疼
先让运维同事回滚,然后就开始了我的问题排查之旅
问题排查与处理
项目搭建
示例代码:redisson-spring-boot-demo,执行如下 test
redisson-spring-boot-starter 引入 redisson
redisson-spring-boot-starter
配置方式有很多种,官网文档做了说明,有 4 种配置方式:README.md
方式 1:
方式 2:
方式 3:
方式 4:
如果 4 种方式都配置,最终生效的是哪一种?
楼主我此刻只想给你个大嘴巴子,怎么这么多问题?
既然你们都提出来了,那我就不能不管,谁让我太爱你们了,盘它!
从哪盘,怎么盘?
源码之下无密码,我们就从源码去盘,找到自动配置类
spring-boot 的自动配置,参考:springboot2.0.3源码篇 - 自动配置的实现,发现也不是那么复杂)
RedissonAutoConfiguration
@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean(RedissonClient.class)
public RedissonClient redisson() throws IOException {
Config config = null;
Method clusterMethod = ReflectionUtils.findMethod(RedisProperties.class, "getCluster");
Method timeoutMethod = ReflectionUtils.findMethod(RedisProperties.class, "getTimeout");
Object timeoutValue = ReflectionUtils.invokeMethod(timeoutMethod, redisProperties);
int timeout;
if(null == timeoutValue){
timeout = 10000;
}else if (!(timeoutValue instanceof Integer)) {
Method millisMethod = ReflectionUtils.findMethod(timeoutValue.getClass(), "toMillis");
timeout = ((Long) ReflectionUtils.invokeMethod(millisMethod, timeoutValue)).intValue();
} else {
timeout = (Integer)timeoutValue;
}
if (redissonProperties.getConfig() != null) {
try {
config = Config.fromYAML(redissonProperties.getConfig());
} catch (IOException e) {
try {
config = Config.fromJSON(redissonProperties.getConfig());
} catch (IOException e1) {
throw new IllegalArgumentException("Can't parse config", e1);
}
}
} else if (redissonProperties.getFile() != null) {
try {
InputStream is = getConfigStream();
config = Config.fromYAML(is);
} catch (IOException e) {
// trying next format
try {
InputStream is = getConfigStream();
config = Config.fromJSON(is);
} catch (IOException e1) {
throw new IllegalArgumentException("Can't parse config", e1);
}
}
} else if (redisProperties.getSentinel() != null) {
Method nodesMethod = ReflectionUtils.findMethod(Sentinel.class, "getNodes");
Object nodesValue = ReflectionUtils.invokeMethod(nodesMethod, redisProperties.getSentinel());
String[] nodes;
if (nodesValue instanceof String) {
nodes = convert(Arrays.asList(((String)nodesValue).split(",")));
} else {
nodes = convert((List<String>)nodesValue);
}
config = new Config();
config.useSentinelServers()
.setMasterName(redisProperties.getSentinel().getMaster())
.addSentinelAddress(nodes)
.setDatabase(redisProperties.getDatabase())
.setConnectTimeout(timeout)
.setPassword(redisProperties.getPassword());
} else if (clusterMethod != null && ReflectionUtils.invokeMethod(clusterMethod, redisProperties) != null) {
Object clusterObject = ReflectionUtils.invokeMethod(clusterMethod, redisProperties);
Method nodesMethod = ReflectionUtils.findMethod(clusterObject.getClass(), "getNodes");
List<String> nodesObject = (List) ReflectionUtils.invokeMethod(nodesMethod, clusterObject);
String[] nodes = convert(nodesObject);
config = new Config();
config.useClusterServers()
.addNodeAddress(nodes)
.setConnectTimeout(timeout)
.setPassword(redisProperties.getPassword());
} else {
config = new Config();
String prefix = REDIS_PROTOCOL_PREFIX;
Method method = ReflectionUtils.findMethod(RedisProperties.class, "isSsl");
if (method != null && (Boolean)ReflectionUtils.invokeMethod(method, redisProperties)) {
prefix = REDISS_PROTOCOL_PREFIX;
}
config.useSingleServer()
.setAddress(prefix + redisProperties.getHost() + ":" + redisProperties.getPort())
.setConnectTimeout(timeout)
.setDatabase(redisProperties.getDatabase())
.setPassword(redisProperties.getPassword());
}
if (redissonAutoConfigurationCustomizers != null) {
for (RedissonAutoConfigurationCustomizer customizer : redissonAutoConfigurationCustomizers) {
customizer.customize(config);
}
}
return Redisson.create(config);
}
View Code
谁先生效,一目了然!
问题分析
有点扯远了,我们再回到主题
jar 未升级之前, redisson-spring-boot-starter 的版本是 3.13.6
redisson-spring-boot-starter 升级到 3.15.0 之后,在开发、测试环境运行正常,上生产后则报错: org.redisson.client.RedisException: ERR unknown command 'WAIT'
redisson-spring-boot-starter
redisson 的issues看看
WAIT
点进去你就会发现
这不就是我们的生产异常?
redis
3.14.0 是正常的, 3.14.1
redis ,所以楼主只能自掏腰包购买一套最低配的阿里云 redis
就冲楼主这认真负责的态度,你们不得一键三连?
我们来看下验证结果
结论确实是对的
楼主又去阿里云翻了一下手册
我们是不是可以把问题范围缩小了
redisson 3.14.0 未引入 wait 命令,而 3.14.1
但这只是我们的猜想,我们需要强有力的支撑,找谁了?肯定还得是源码!
WAIT 源码分析
3.14.0
redis-server 执行的命令不只是加锁的脚本,还有 WAIT
WAIT
3.14.0 也有 WAIT 命令,并且在阿里云 redis 的代理模式下执行是失败的,只是 redisson 并没有去管 WAIT
Redisson
3.14.0
3.14.1
redis-server 执行的命令有加锁脚本,也有 WAIT
两个命令的执行结果都有关注
redis
redis 的代理模式是不支持 WAIT 命令,所以 WAIT
而最终的执行结果是所有命令的执行结果,所以最终执行结果是失败的!
问题处理
那么如何正确的升级到生产环境了?
redisson 版本降到 3.14.0
WAIT 命令的执行结果,相当于没有 WAIT
redisson 引入 WAIT
redis
总结
1、环境一致的重要性
测试环境一定要保证和生产环境一致
否则就会出现和楼主一样的问题,其他环境都没问题,就生产有问题
环境不一致,排查问题也很棘手
Redisson 很早就会附加 WAIT 命令,只是从 3.14.1 开始才关注 WAIT
3、对于维护中的老项目,代码能不动就不动,配置能不动就不动