最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Jedis線程池returnResource異常注意事項

 更新時間:2022年03月24日 16:47:11   作者:Erica_1230  
這篇文章主要介紹了使用Jedis線程池returnResource異常注意事項,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

在線上環(huán)境發(fā)現(xiàn)了一個工作線程異常終止

看日志先是一些SocketTimeoutException,然后突然有一個ClassCastException

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)

經(jīng)過在本地人工模擬網(wǎng)絡(luò)異常的情境,最終復(fù)現(xiàn)了線上的這一異常。

又經(jīng)過深入分析(提出假設(shè)-->驗證假設(shè)),最終找出了導(dǎo)致這一問題的原因。

見如下示例代碼

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();  //等待恢復(fù)
Thread.sleep(5000); // 繼續(xù)休眠一段時間 等待網(wǎng)絡(luò)完全恢復(fù)
boolean isMember = jedis.sismember("urls", "baidu.com");

以及日志輸出

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)

分析

等執(zhí)行第二遍的get("foo")時,網(wǎng)絡(luò)超時,并未實際發(fā)送 get foo 命令,等執(zhí)行sismember時,網(wǎng)絡(luò)已恢復(fù)正常,并且是同一個jedis實例,于是將之前的get foo命令(已在輸出流緩存中)一并發(fā)送。

執(zhí)行順序如下所示

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

故在上述示例代碼中最后的sismember得到的結(jié)果是get foo的結(jié)果,即一個字符串,而sismember需要的是一個Long型,故導(dǎo)致了ClassCastException。

執(zhí)行redis的邏輯

為什么線上會出現(xiàn)這一問題呢?原因是其執(zhí)行redis的邏輯類似這樣:

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

因若是網(wǎng)絡(luò)異常的話,pool.returnResource(jedis)仍能成功執(zhí)行,即能將其返回到池中(這時jedis并不為空)。等網(wǎng)絡(luò)恢復(fù)后,并是多線程環(huán)境,導(dǎo)致后續(xù)其他某個線程獲得了同一個Jedis實例(pool.getResource()),

若該線程中的jedis操作返回類型與該jedis實例在網(wǎng)絡(luò)異常期間第一條未執(zhí)行成功的jedis操作的返回類型不匹配(如一個是get,一個是sismember),則就會出現(xiàn)ClassCastException異常。

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

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

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")的結(jié)果, 而sismember得到的是get("nonexist-key")的結(jié)果,而get("nonexist-key")返回為空,于是這時是報空指針異常了.

解決方法:

不能不管什么情況都一律使用returnResource。更健壯可靠以及優(yōu)雅的處理方式如下所示:

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);
    }
}

補充

Ubuntu本地模擬訪問redis網(wǎng)絡(luò)超時:

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

恢復(fù)網(wǎng)絡(luò):

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);
}

若一旦發(fā)生了JedisConnectionException,如網(wǎng)絡(luò)異常,會先執(zhí)行returnBrokenResource,這時jedis已被destroy了。然后進(jìn)入了finally,再一次執(zhí)行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; //這時不會實際執(zhí)行returnResource中的相關(guān)動作了

但不建議這樣處理,更嚴(yán)謹(jǐn)?shù)尼尫刨Y源方法見前文所述。

以上就是使用Jedis線程池returnResource異常注意事項的詳細(xì)內(nèi)容,更多關(guān)于Jedis線程池returnResource異常的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Redis分布式鎖之紅鎖的實現(xiàn)

    Redis分布式鎖之紅鎖的實現(xiàn)

    在Redis中,紅鎖是一種分布式鎖的實現(xiàn)機制,旨在解決多個客戶端在分布式環(huán)境中對共享資源進(jìn)行并發(fā)訪問的問題,本文主要介紹了Redis分布式鎖之紅鎖的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • 配置Redis序列化方式不生效問題及解決

    配置Redis序列化方式不生效問題及解決

    這篇文章主要介紹了配置Redis序列化方式不生效問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Redis中常見的幾種集群部署方案

    Redis中常見的幾種集群部署方案

    本文主要介紹了Redis中常見的幾種集群部署方案,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Redis中哈希結(jié)構(gòu)(Dict)的實現(xiàn)

    Redis中哈希結(jié)構(gòu)(Dict)的實現(xiàn)

    本文主要介紹了Redis中哈希結(jié)構(gòu)(Dict)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 徹底弄懂Redis的LRU淘汰策略

    徹底弄懂Redis的LRU淘汰策略

    本文主要介紹了LRU淘汰策略以及實現(xiàn)一個LRU算法,文章會結(jié)合圖解循序漸進(jìn)的講解,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 基于redis實現(xiàn)token驗證用戶是否登陸

    基于redis實現(xiàn)token驗證用戶是否登陸

    這篇文章主要為大家詳細(xì)介紹了基于redis實現(xiàn)token驗證用戶是否登陸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Redis 事務(wù)與過期時間詳細(xì)介紹

    Redis 事務(wù)與過期時間詳細(xì)介紹

    這篇文章主要介紹了Redis 事務(wù)與過期時間詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Redis安裝與使用方法小結(jié)

    Redis安裝與使用方法小結(jié)

    這篇文章主要介紹了Redis安裝與使用方法,結(jié)合實例形式分析了Redis數(shù)據(jù)庫的下載、安裝、啟動、設(shè)置及相關(guān)使用操作注意事項,需要的朋友可以參考下
    2018-04-04
  • Redis生成分布式系統(tǒng)全局唯一ID的實現(xiàn)

    Redis生成分布式系統(tǒng)全局唯一ID的實現(xiàn)

    在互聯(lián)網(wǎng)系統(tǒng)中,并發(fā)越大的系統(tǒng),數(shù)據(jù)就越大,數(shù)據(jù)越大就越需要分布式,本文主要介紹了Redis生成分布式系統(tǒng)全局唯一ID的實現(xiàn),感興趣的可以了解一下
    2021-10-10
  • redis yml配置的用法小結(jié)

    redis yml配置的用法小結(jié)

    RedisYML配置是Redis的一種配置文件格式,,對Redis的配置進(jìn)行統(tǒng)一管理,本文就來介紹了redis yml配置的用法小結(jié),具有一定的參考價值,感興趣的可以了解一下
    2024-02-02

最新評論

泗水县| 荆州市| 张家口市| 浦江县| 南召县| 云浮市| 育儿| 涟源市| 鲁甸县| 昌江| 武山县| 乐平市| 加查县| 集贤县| 栾川县| 鄄城县| 黑水县| 勐海县| 井冈山市| 隆昌县| 东港市| 垫江县| 青田县| 瑞昌市| 云龙县| 峨边| 荃湾区| 三江| 涪陵区| 双辽市| 涟源市| 广河县| 延吉市| 青川县| 呼伦贝尔市| 浪卡子县| 高邑县| 屏山县| 山阳县| 日喀则市| 兴文县|