nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Unknown reply: O 异常处理记录
前几天修改redis中value 的时间时遇到了个坑,使用的是redis版本 2.4.1 + spring-data-redis-1.6.0 服务端使用的是twemproxy。
具体操作是 原来value 的过期时间为 3600*24 *7 单位是 TimeUnit.SECONDS (7天)。现在要求修改为30天,
我直接修改3600*24 *30 单位保持不变。不过通过查阅资料和翻看源码。找到了问题所在,在这里记录下。
异常如下:
Unknown reply: O; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Unknown reply: O
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Unknown reply: O
at redis.clients.jedis.Protocol.process(Protocol.java:128)
at redis.clients.jedis.Protocol.read(Protocol.java:187)
at redis.clients.jedis.Connection.getIntegerReply(Connection.java:201)
at redis.clients.jedis.BinaryJedis.expire(BinaryJedis.java:330)
at org.springframework.data.redis.connection.jedis.JedisConnection.expire(JedisConnection.java:807)
at org.springframework.data.redis.core.RedisTemplate 7.doInRedis(RedisTemplate.java:647)atorg.springframework.data.redis.core.RedisTemplate 7.doInRedis(RedisTemplate.java:640)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:191)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:153)
at org.springframework.data.redis.core.RedisTemplate.expire(RedisTemplate.java:640)
at com.youku.ddshow.service.impl.UserDeviceTokenServiceImpl.addUserDeviceToken(UserDeviceTokenServiceImpl.java:32)
at com.youku.ddshow.service.impl.UserDeviceTokenServiceImpl.updateUserDeviceToken(UserDeviceTokenServiceImpl.java:66)
at com.youku.ddshow.service.MACTokenHandler.getMACToken4Logined(MACTokenHandler.java:95)
… 37 more
通过跟踪代码发现是 RedisTemplate.expire 出现异常。
public Boolean expire(K key, final long timeout, final TimeUnit unit) {final byte[] rawKey = this.rawKey(key);final long rawTimeout = TimeoutUtils.toMillis(timeout, unit);return (Boolean)this.execute(new RedisCallback() {public Boolean doInRedis(RedisConnection connection) {try {return connection.pExpire(rawKey, rawTimeout);} catch (Exception var3) {return connection.expire(rawKey, TimeoutUtils.toSeconds(timeout, unit));}}}, true);}
//这里使用了twemproxy 是不支持Time的操作,故抛出异常使用connection.expire处理,此处是个坑public Boolean pExpire(byte[] key, long millis) {if(millis > 2147483647L) {return this.pExpireAt(key, this.time().longValue() + millis);} else {try {if(this.isPipelined()) {this.pipeline(new JedisConnection.JedisResult(this.pipeline.pexpire(key, (int)millis), JedisConverters.longToBoolean()));return null;} else if(this.isQueueing()) {this.transaction(new JedisConnection.JedisResult(this.transaction.pexpire(key, (int)millis), JedisConverters.longToBoolean()));return null;} else {return JedisConverters.toBoolean(this.jedis.pexpire(key, (int)millis));}} catch (Exception var5) {throw this.convertJedisAccessException(var5);}}}//跳到这里继续执行 注意参数 是秒 long seconds public Boolean expire(byte[] key, long seconds) {if(seconds > 2147483647L) {return this.pExpireAt(key, this.time().longValue() + TimeUnit.SECONDS.toMillis(seconds));} else {try {if(this.isPipelined()) {this.pipeline(new JedisConnection.JedisResult(this.pipeline.expire(key, (int)seconds), JedisConverters.longToBoolean()));return null;} else if(this.isQueueing()) {this.transaction(new JedisConnection.JedisResult(this.transaction.expire(key, (int)seconds), JedisConverters.longToBoolean()));return null;} else {return JedisConverters.toBoolean(this.jedis.expire(key, (int)seconds));//**执行此处**}} catch (Exception var5) {throw this.convertJedisAccessException(var5);}}}//通过继续向下找源码,终于找到抛出异常的代码private static Object process(RedisInputStream is) {try {byte e = is.readByte();if(e == 45) {processError(is);return null;} else if(e == 42) {return processMultiBulkReply(is);} else if(e == 58) {return processInteger(is);} else if(e == 36) {return processBulkReply(is);} else if(e == 43) {return processStatusCodeReply(is);} else {throw new JedisConnectionException("Unknown reply: " + (char)e);}} catch (IOException var2) {throw new JedisConnectionException(var2);}}
对照ASCII码表 O 代码 77 不是这几个常量,其实还是通信错误导致。
然后修改TimeUnit.DAYS 30 问题解决。
参考: http://irfen.me/redis-expire-exception