日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

我們今天來看看這個(gè) redis 的看門狗機(jī)制,畢竟現(xiàn)在還是有很多是會(huì)使用 Redis 來實(shí)現(xiàn)分布式鎖的,我們現(xiàn)在看看這個(gè) Redis 是怎么實(shí)現(xiàn)分布式鎖的,然后我們再來分析這個(gè) Redis 的看門狗機(jī)制,如果沒有這個(gè)機(jī)制,很多使用 Redis 來做分布式鎖的小伙伴們,經(jīng)常給導(dǎo)致死鎖。

Redis 實(shí)現(xiàn)分布式鎖

Redis實(shí)現(xiàn)分布式鎖,最主要的就是這幾個(gè)條件

獲取鎖

  • 互斥:確保只能有一個(gè)線程獲取鎖
  • 非阻塞:嘗試一次,成功返回true,失敗返回false

釋放鎖

  • 手動(dòng)釋放
  • 超時(shí)釋放:獲取鎖時(shí)添加一個(gè)超時(shí)時(shí)間

上代碼:

@Resource
    private RedisTemplate redisTemplate;

    public static final String UNLOCK_LUA;

    /**
     * 釋放鎖腳本,原子操作
     */
    static {
        StringBuilder sb = new StringBuilder();
        sb.Append("if redis.call("get",KEYS[1]) == ARGV[1] ");
        sb.append("then ");
        sb.append("    return redis.call("del",KEYS[1]) ");
        sb.append("else ");
        sb.append("    return 0 ");
        sb.append("end ");
        UNLOCK_LUA = sb.toString();
    }


    /**
     * 獲取分布式鎖,原子操作
     * @param lockKey
     * @param requestId 唯一ID, 可以使用UUID.randomUUID().toString();
     * @param expire
     * @param timeUnit
     * @return
     */
    public boolean tryLock(String lockKey, String requestId, long expire, TimeUnit timeUnit) {
        try{
            RedisCallback<Boolean> callback = (connection) -> {
                return connection.set(lockKey.getBytes(Charset.forName("UTF-8")), requestId.getBytes(Charset.forName("UTF-8")), Expiration.seconds(timeUnit.toSeconds(expire)), RedisStringCommands.SetOption.SET_IF_ABSENT);
            };
            return (Boolean)redisTemplate.execute(callback);
        } catch (Exception e) {
            log.error("redis lock error.", e);
        }
        return false;
    }

    /**
     * 釋放鎖
     * @param lockKey
     * @param requestId 唯一ID
     * @return
     */
    public boolean releaseLock(String lockKey, String requestId) {
        RedisCallback<Boolean> callback = (connection) -> {
            return connection.eval(UNLOCK_LUA.getBytes(), ReturnType.BOOLEAN ,1, lockKey.getBytes(Charset.forName("UTF-8")), requestId.getBytes(Charset.forName("UTF-8")));
        };
        return (Boolean)redisTemplate.execute(callback);
    }

    /**
     * 獲取Redis鎖的value值
     * @param lockKey
     * @return
     */
    public String get(String lockKey) {
        try {
            RedisCallback<String> callback = (connection) -> {
                return new String(connection.get(lockKey.getBytes()), Charset.forName("UTF-8"));
            };
            return (String)redisTemplate.execute(callback);
        } catch (Exception e) {
            log.error("get redis occurred an exception", e);
        }
        return null;
    }

這種實(shí)現(xiàn)方式就是相當(dāng)于我們直接使用 Redis 來自己實(shí)現(xiàn)的分布式鎖,但是也不是沒有框架給我們來實(shí)現(xiàn),那就是Redission。而看門狗機(jī)制是Redission提供的一種自動(dòng)延期機(jī)制,這個(gè)機(jī)制使得Redission提供的分布式鎖是可以自動(dòng)續(xù)期的。

為什么需要看門狗機(jī)制

分布式鎖是不能設(shè)置永不過期的,這是為了避免在分布式的情況下,一個(gè)節(jié)點(diǎn)獲取鎖之后宕機(jī)從而出現(xiàn)死鎖的情況,所以需要個(gè)分布式鎖設(shè)置一個(gè)過期時(shí)間。但是這樣會(huì)導(dǎo)致一個(gè)線程拿到鎖后,在鎖的過期時(shí)間到達(dá)的時(shí)候程序還沒運(yùn)行完,導(dǎo)致鎖超時(shí)釋放了,那么其他線程就能獲取鎖進(jìn)來,從而出現(xiàn)問題。

所以,看門狗機(jī)制的自動(dòng)續(xù)期,就很好地解決了這一個(gè)問題。

Redisson已經(jīng)幫我們實(shí)現(xiàn)了這個(gè)分布式鎖,我們需要的就是調(diào)用,那么我們來看看 Redisson 的源碼,他是如何來實(shí)現(xiàn)看門狗機(jī)制的。

tryLock

RedissonLock類下:

public boolean tryLock(long wAItTime, TimeUnit unit) throws InterruptedException {
    return tryLock(waitTime, -1, unit);
}
  • waitTime:獲取鎖的最大等待時(shí)間(沒有傳默認(rèn)為-1)
  • leaseTime:鎖自動(dòng)釋放的時(shí)間(沒有傳的話默認(rèn)-1)
  • unit:時(shí)間的單位(等待時(shí)間和鎖自動(dòng)釋放的時(shí)間單位)
 
@Override
    public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException {
        long time = unit.toMillis(waitTime);
        long current = System.currentTimeMillis();
        long threadId = Thread.currentThread().getId();
        Long ttl = tryAcquire(waitTime, leaseTime, unit, threadId);
        // lock acquired
        if (ttl == null) {
            return true;
        }
        
        time -= System.currentTimeMillis() - current;
        if (time <= 0) {
            acquireFailed(waitTime, unit, threadId);
            return false;
        }
        
        current = System.currentTimeMillis();
        RFuture<RedissonLockEntry> subscribeFuture = subscribe(threadId);
        if (!subscribeFuture.await(time, TimeUnit.MILLISECONDS)) {
            if (!subscribeFuture.cancel(false)) {
                subscribeFuture.onComplete((res, e) -> {
                    if (e == null) {
                        unsubscribe(subscribeFuture, threadId);
                    }
                });
            }
            acquireFailed(waitTime, unit, threadId);
            return false;
        }

        try {
            time -= System.currentTimeMillis() - current;
            if (time <= 0) {
                acquireFailed(waitTime, unit, threadId);
                return false;
            }
        
            while (true) {
                long currentTime = System.currentTimeMillis();
                ttl = tryAcquire(waitTime, leaseTime, unit, threadId);
                // lock acquired
                if (ttl == null) {
                    return true;
                }

                time -= System.currentTimeMillis() - currentTime;
                if (time <= 0) {
                    acquireFailed(waitTime, unit, threadId);
                    return false;
                }

                // waiting for message
                currentTime = System.currentTimeMillis();
                if (ttl >= 0 && ttl < time) {
                    subscribeFuture.getNow().getLatch().tryAcquire(ttl, TimeUnit.MILLISECONDS);
                } else {
                    subscribeFuture.getNow().getLatch().tryAcquire(time, TimeUnit.MILLISECONDS);
                }

                time -= System.currentTimeMillis() - currentTime;
                if (time <= 0) {
                    acquireFailed(waitTime, unit, threadId);
                    return false;
                }
            }
        } finally {
            unsubscribe(subscribeFuture, threadId);
        }
//        return get(tryLockAsync(waitTime, leaseTime, unit));
    }

上面這一段代碼最主要的內(nèi)容講述看門狗機(jī)制的實(shí)際上應(yīng)該算是 tryAcquire

最終落地為tryAcquireAsync

//如果獲取鎖失敗,返回的結(jié)果是這個(gè)key的剩余有效期
        RFuture<Long> ttlRemainingFuture = this.tryLockInnerAsync(waitTime, this.commandExecutor.getConnectionManager().getCfg().getLockWatchdogTimeout(), TimeUnit.MILLISECONDS, threadId, RedisCommands.EVAL_LONG);
        //上面獲取鎖回調(diào)成功之后,執(zhí)行這代碼塊的內(nèi)容
        ttlRemainingFuture.onComplete((ttlRemaining, e) -> {
            //不存在異常
            if (e == null) {
                //剩余有效期為null
                if (ttlRemaining == null) {
                    //這個(gè)函數(shù)是解決最長等待有效期的問題
                    this.scheduleExpirationRenewal(threadId);
                }

            }
        });
        return ttlRemainingFuture;

調(diào)用tryLockInnerAsync,如果獲取鎖失敗,返回的結(jié)果是這個(gè)key的剩余有效期,如果獲取鎖成功,則返回null。

獲取鎖成功后,如果檢測不存在異常并且獲取鎖成功(ttlRemaining == null)。

那么則執(zhí)行this.scheduleExpirationRenewal(threadId);來啟動(dòng)看門狗機(jī)制。

看門狗機(jī)制提供的默認(rèn)超時(shí)時(shí)間是30*1000毫秒,也就是30秒

如果一個(gè)線程獲取鎖后,運(yùn)行程序到釋放鎖所花費(fèi)的時(shí)間大于鎖自動(dòng)釋放時(shí)間(也就是看門狗機(jī)制提供的超時(shí)時(shí)間30s),那么Redission會(huì)自動(dòng)給redis中的目標(biāo)鎖延長超時(shí)時(shí)間。

在Redission中想要啟動(dòng)看門狗機(jī)制,那么我們就不用獲取鎖的時(shí)候自己定義leaseTime(鎖自動(dòng)釋放時(shí)間)。

但是 Redisson 和我們自己定義實(shí)現(xiàn)分布式鎖不一樣,如果自己定義了鎖自動(dòng)釋放時(shí)間的話,無論是通過lock還是tryLock方法,都無法啟用看門狗機(jī)制。

所以你了解分布式鎖的看門狗機(jī)制了么?

分享到:
標(biāo)簽:分布式
用戶無頭像

網(wǎng)友整理

注冊時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定