Jedis returnResource使用注意事項

在線上環境發現了一個工做線程異常終止,看日誌先是一些SocketTimeoutException,而後忽然有一個ClassCastExceptionjava

redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
...
java.lang.ClassCastException: [B cannot be cast to java.lang.Long
        at redis.clients.jedis.Connection.getIntegerReply(Connection.java:208)
        at redis.clients.jedis.Jedis.sismember(Jedis.java:1307)

通過在本地人工模擬網絡異常的情境,最終復現了線上的這一異常。又通過深刻分析(提出假設-->驗證假設),最終找出了致使這一問題的緣由。見以下示例代碼:git

JedisPool pool = ...;
Jedis jedis = pool.getResource();
String value = jedis.get("foo");
System.out.println("Make SocketTimeoutException");
System.in.read(); //等待制造SocketTimeoutException
try {
    value = jedis.get("foo");
    System.out.println(value);
} catch (JedisConnectionException e) {
    e.printStackTrace();
}
System.out.println("Recover from SocketTimeoutException");
System.in.read();  //等待恢復
Thread.sleep(5000); // 繼續休眠一段時間 等待網絡徹底恢復
boolean isMember = jedis.sismember("urls", "baidu.com");

以及日誌輸出:github

bar
Make SocketTimeoutException
redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
Recover from SocketTimeoutException
    at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:210)
    at redis.clients.util.RedisInputStream.readByte(RedisInputStream.java:47)
    at redis.clients.jedis.Protocol.process(Protocol.java:131)
    at redis.clients.jedis.Protocol.read(Protocol.java:196)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:283)
    at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:202)
    at redis.clients.jedis.Connection.getBulkReply(Connection.java:191)
    at redis.clients.jedis.Jedis.get(Jedis.java:101)
    at com.tcl.recipevideohunter.JedisTest.main(JedisTest.java:23)
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:152)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at java.net.SocketInputStream.read(SocketInputStream.java:108)
    at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:204)
    ... 8 more
Exception in thread "main" java.lang.ClassCastException: [B cannot be cast to java.lang.Long
    at redis.clients.jedis.Connection.getIntegerReply(Connection.java:208)
    at redis.clients.jedis.Jedis.sismember(Jedis.java:1307)
    at com.tcl.recipevideohunter.JedisTest.main(JedisTest.java:32)

分析:redis

等執行第二遍的get("foo")時,網絡超時,並未實際發送 get foo 命令,等執行sismember時,網絡已恢復正常,而且是同一個jedis實例,因而將以前的get foo命令(已在輸出流緩存中)一併發送。spring

執行順序以下所示:sql

127.0.0.1:9379> get foo
"bar"
127.0.0.1:9379> sismember urls baidu.com
(integer) 1

故在上述示例代碼中最後的sismember獲得的結果是get foo的結果,即一個字符串,而sismember須要的是一個Long型,故致使了ClassCastException。shell

爲何線上會出現這一問題呢?緣由是其執行redis的邏輯相似這樣:緩存

while(true){
        Jedis jedis = null;
    try {
        jedis = pool.getResource();
        //some redis operation here.
    } catch (Exception e) {
       logger.error(e);
    } finally {
        pool.returnResource(jedis);
    }
}

因如果網絡異常的話,pool.returnResource(jedis)仍能成功執行,即能將其返回到池中(這時jedis並不爲空)。等網絡恢復後,並是多線程環境,致使後續其餘某個線程得到了同一個Jedis實例(pool.getResource()),網絡

若該線程中的jedis操做返回類型與該jedis實例在網絡異常期間第一條未執行成功的jedis操做的返回類型不匹配(如一個是get,一個是sismember),則就會出現ClassCastException異常。多線程

這還算幸運的,若返回的是同一類型的話(如lpop("queue_order_pay_failed"),lpop("queue_order_pay_success")),那我真不敢想象。

如在上述示例代碼中的sismember前插入一get("nonexist-key")(redis中不存在該key,即應該返回空).

value = jedis.get("nonexist-key");
System.out.println(value);
boolean isMember = jedis.sismember("urls", "baidu.com");
System.out.println(isMember);

實際的日誌輸出爲:

bar
Exception in thread "main" java.lang.NullPointerException
    at redis.clients.jedis.Jedis.sismember(Jedis.java:1307)
    at com.tcl.recipevideohunter.JedisTest.main(JedisTest.java:37)

分析:

get("nonexist-key")獲得是以前的get("foo")的結果, 而sismember獲得的是get("nonexist-key")的結果,而get("nonexist-key")返回爲空,因而這時是報空指針異常了.

解決方法:不能無論什麼狀況都一概使用returnResource。更健壯可靠以及優雅的處理方式以下所示:

while(true){
    Jedis jedis = null;
    boolean broken = false;
    try {
        jedis = jedisPool.getResource();
        return jedisAction.action(jedis); //模板方法
    } catch (JedisException e) {
        broken = handleJedisException(e);
        throw e;
    } finally {
        closeResource(jedis, broken);
    }
}
 
/**
 * Handle jedisException, write log and return whether the connection is broken.
 */
protected boolean handleJedisException(JedisException jedisException) {
    if (jedisException instanceof JedisConnectionException) {
        logger.error("Redis connection " + jedisPool.getAddress() + " lost.", jedisException);
    } else if (jedisException instanceof JedisDataException) {
        if ((jedisException.getMessage() != null) && (jedisException.getMessage().indexOf("READONLY") != -1)) {
            logger.error("Redis connection " + jedisPool.getAddress() + " are read-only slave.", jedisException);
        } else {
            // dataException, isBroken=false
            return false;
        }
    } else {
        logger.error("Jedis exception happen.", jedisException);
    }
    return true;
}
/**
 * Return jedis connection to the pool, call different return methods depends on the conectionBroken status.
 */
protected void closeResource(Jedis jedis, boolean conectionBroken) {
    try {
        if (conectionBroken) {
            jedisPool.returnBrokenResource(jedis);
        } else {
            jedisPool.returnResource(jedis);
        }
    } catch (Exception e) {
        logger.error("return back jedis failed, will fore close the jedis.", e);
        JedisUtils.destroyJedis(jedis);
    }
}

該段代碼來自:https://github.com/springside/springside4/blob/master/modules/redis/src/main/java/org/springside/modules/nosql/redis/JedisTemplate.java

補充:

Ubuntu本地模擬訪問redis網絡超時:

 sudo iptables -A INPUT -p tcp --dport 6379 -j DROP

恢復網絡:

 sudo iptables -F


補充:

若jedis操做邏輯相似下面所示的話,

Jedis jedis = null;
try {
    jedis = jedisSentinelPool.getResource();
    return jedis.get(key);
}catch(JedisConnectionException e) {
    jedisSentinelPool.returnBrokenResource(jedis);
    logger.error("", e);
    throw e;
}catch (Exception e) {
    logger.error("", e);
    throw e;
}
finally {
    jedisSentinelPool.returnResource(jedis);
}

若一旦發生了JedisConnectionException,如網絡異常,會先執行returnBrokenResource,這時jedis已被destroy了。而後進入了finally,再一次執行returnResource,這時會報錯:

redis.clients.jedis.exceptions.JedisException: Could not return the resource to the pool
    at redis.clients.util.Pool.returnResourceObject(Pool.java:65)
    at redis.clients.jedis.JedisSentinelPool.returnResource(JedisSentinelPool.java:221)

臨時解決方法:

jedisSentinelPool.returnBrokenResource(jedis);
jedis=null; //這時不會實際執行returnResource中的相關動做了

但不建議這樣處理,更嚴謹的釋放資源方法見前文所述。

相關文章
相關標籤/搜索