欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Jedis線(xiàn)程池returnResource異常注意事項(xiàng)

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

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

看日志先是一些SocketTimeoutException,然后突然有一個(gè)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)過(guò)在本地人工模擬網(wǎng)絡(luò)異常的情境,最終復(fù)現(xiàn)了線(xiàn)上的這一異常。

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

見(jiàn)如下示例代碼

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ù)休眠一段時(shí)間 等待網(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")時(shí),網(wǎng)絡(luò)超時(shí),并未實(shí)際發(fā)送 get foo 命令,等執(zhí)行sismember時(shí),網(wǎng)絡(luò)已恢復(fù)正常,并且是同一個(gè)jedis實(shí)例,于是將之前的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é)果,即一個(gè)字符串,而sismember需要的是一個(gè)Long型,故導(dǎo)致了ClassCastException。

執(zhí)行redis的邏輯

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

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

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

這還算幸運(yùn)的,若返回的是同一類(lèi)型的話(huà)(如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);

實(shí)際的日志輸出為

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")返回為空,于是這時(shí)是報(bào)空指針異常了.

解決方法:

不能不管什么情況都一律使用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);
    }
}

補(bǔ)充

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

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

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

sudo iptables -F

補(bǔ)充:

若jedis操作邏輯類(lèi)似下面所示的話(huà),

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ò)異常,會(huì)先執(zhí)行returnBrokenResource,這時(shí)jedis已被destroy了。然后進(jìn)入了finally,再一次執(zhí)行returnResource,這時(shí)會(huì)報(bào)錯(cuò):

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)

臨時(shí)解決方法

jedisSentinelPool.returnBrokenResource(jedis);
jedis=null; //這時(shí)不會(huì)實(shí)際執(zhí)行returnResource中的相關(guān)動(dòng)作了

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

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

相關(guān)文章

  • 詳細(xì)分析Redis集群故障

    詳細(xì)分析Redis集群故障

    這篇文章主要介紹了詳細(xì)分析Redis集群故障的相關(guān)內(nèi)容,具有一定的參考價(jià)值,這里分享給大家,供需要的朋友參考。
    2017-10-10
  • redis分布式鎖之可重入鎖的實(shí)現(xiàn)代碼

    redis分布式鎖之可重入鎖的實(shí)現(xiàn)代碼

    相信大家都知道可重入鎖的作用防止在同一線(xiàn)程中多次獲取鎖而導(dǎo)致死鎖發(fā)生,本文通過(guò)幾個(gè)例子給大家分享redis分布式鎖之可重入鎖的實(shí)現(xiàn)代碼,對(duì)redis分布式鎖的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2021-05-05
  • Redis的配置、啟動(dòng)、操作和關(guān)閉方法

    Redis的配置、啟動(dòng)、操作和關(guān)閉方法

    今天小編就為大家分享一篇Redis的配置、啟動(dòng)、操作和關(guān)閉方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • 高并發(fā)下Redis如何保持?jǐn)?shù)據(jù)一致性(避免讀后寫(xiě))

    高并發(fā)下Redis如何保持?jǐn)?shù)據(jù)一致性(避免讀后寫(xiě))

    本文主要介紹了高并發(fā)下Redis如何保持?jǐn)?shù)據(jù)一致性(避免讀后寫(xiě)),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • redis與memcached的區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    redis與memcached的區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Memcached是以LiveJurnal旗下Danga Interactive公司的Bard Fitzpatric為首開(kāi)發(fā)的高性能分布式內(nèi)存緩存服務(wù)器。那么redis與memcached有什么區(qū)別呢?下面小編給大家介紹下redis與memcached的區(qū)別,感興趣的朋友參考下吧
    2017-08-08
  • Redis高可用梳理詳解

    Redis高可用梳理詳解

    高可用的本質(zhì)是有備份,在出現(xiàn)故障的時(shí)候,有backup可以提供服務(wù),本文詳細(xì)介紹了Redis的高可用,感興趣的同學(xué)可以參考閱讀
    2023-05-05
  • Redis為什么快如何實(shí)現(xiàn)高可用及持久化

    Redis為什么快如何實(shí)現(xiàn)高可用及持久化

    這篇文章主要介紹了Redis為什么快如何實(shí)現(xiàn)高可用及持久化,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • 分布式鎖為什么要選擇Zookeeper而不是Redis?看完這篇你就明白了

    分布式鎖為什么要選擇Zookeeper而不是Redis?看完這篇你就明白了

    Zookeeper的機(jī)制可以保證分布式鎖實(shí)現(xiàn)業(yè)務(wù)代碼簡(jiǎn)單,成本低,Redis如果要解決分布式鎖的問(wèn)題,對(duì)于一些復(fù)雜的情況,很難解決,成本較高,這篇文章重點(diǎn)給大家介紹分布式鎖選擇Zookeeper 而不是Redis的理由,一起看看吧
    2021-05-05
  • 壓縮列表犧牲速度來(lái)節(jié)省內(nèi)存,Redis是膨脹了嗎

    壓縮列表犧牲速度來(lái)節(jié)省內(nèi)存,Redis是膨脹了嗎

    這篇文章主要給大家解釋了Redis 當(dāng)中的 ziplist(壓縮列表)犧牲速度來(lái)節(jié)省內(nèi)存的原因,希望大家能夠喜歡
    2021-02-02
  • 詳解Redis主從復(fù)制實(shí)踐

    詳解Redis主從復(fù)制實(shí)踐

    本文將演示主從復(fù)制如何配置、實(shí)現(xiàn)以及實(shí)現(xiàn)原理,Redis主從復(fù)制三大策略,全量復(fù)制、部分復(fù)制和立即復(fù)制。
    2021-05-05

最新評(píng)論