成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

一文詳解分布式鎖的看門狗機(jī)制

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

我們今天來看看這個(gè) Redis 的看門狗機(jī)制,畢竟現(xiàn)在還是有很多是會使用 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

釋放鎖

  • 手動釋放
  • 超時(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提供的一種自動延期機(jī)制,這個(gè)機(jī)制使得Redission提供的分布式鎖是可以自動續(xù)期的。

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

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

所以,看門狗機(jī)制的自動續(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:鎖自動釋放的時(shí)間(沒有傳的話默認(rèn)-1)
  • unit:時(shí)間的單位(等待時(shí)間和鎖自動釋放的時(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);來啟動看門狗機(jī)制。

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

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

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

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

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

責(zé)任編輯:武曉燕 來源: Java極客技術(shù)
相關(guān)推薦

2021-06-28 10:51:55

Redisson分布式鎖Watchdog

2025-01-06 07:00:00

看門狗RedissonRedis

2023-03-17 16:42:45

應(yīng)用開發(fā)Ability

2016-10-25 14:35:05

分布式系統(tǒng) 存儲

2018-07-11 09:34:55

分布式架構(gòu)高可用

2024-06-13 09:25:14

2023-09-20 22:56:45

分布式追蹤應(yīng)用程序

2022-12-21 08:40:05

限流器分布式限流

2019-08-27 11:00:38

技術(shù)數(shù)據(jù)庫設(shè)計(jì)

2017-10-20 13:39:29

分布式系統(tǒng)數(shù)據(jù)存儲數(shù)據(jù)量

2019-06-19 15:40:06

分布式鎖RedisJava

2020-04-14 11:14:02

PostgreSQL分布式數(shù)據(jù)庫

2021-07-02 08:51:09

Redisson分布式鎖公平鎖

2021-06-28 10:03:44

分布式數(shù)據(jù)庫架構(gòu)

2022-08-16 10:35:00

分布式高可用方案

2018-05-10 10:53:47

分布式架構(gòu)負(fù)載均衡Web

2022-07-13 09:53:58

分布式開發(fā)

2022-06-16 08:01:24

redis分布式鎖

2023-09-21 16:10:44

2024-07-09 08:11:56

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 亚洲精品福利视频 | 日韩国产在线 | 欧美日韩视频在线第一区 | 91在线成人| 91免费入口| 日韩免费一级 | 亚洲精品中文在线 | 国产区久久 | 免费一区二区三区在线视频 | 国产在线精品一区二区三区 | 成人日韩| 日韩二区 | 欧美精品导航 | 久久久久久久久久一区 | 久久乐国产精品 | 亚洲精美视频 | 免费精品久久久久久中文字幕 | 伊人看片 | 国产精品久久久亚洲 | 久久a久久 | 一区二区三区四区日韩 | 日韩aⅴ视频 | 中文字幕av免费 | 国产综合精品一区二区三区 | av免费在线观看网站 | 91精品久久久 | 日本一区二区三区在线观看 | 精品三级在线观看 | 国产高清不卡 | 高清人人天天夜夜曰狠狠狠狠 | 久久久91精品国产一区二区三区 | 亚洲国产成人久久综合一区,久久久国产99 | 一区二区三区四区在线视频 | 久久午夜视频 | 九九热这里 | 麻豆久久久9性大片 | 看av电影 | 日韩一级免费 | 久久人体视频 | 天堂在线网 | 国产夜恋视频在线观看 |