【📕分布式锁通关指南 07】源码剖析redisson利用看门狗机制异步维持客户端锁

引言

在上篇中,我们梳理了redisson的可重入锁的加锁流程,而加锁必然就会有锁续期的问题,那么看门狗机制是维持锁续期的关键。因此,在本篇中我们将剖析redisson中的看门狗机制究竟是如何实现的。

利用Watchdog机制异步维持客户端锁

看门狗机制是redisson解决锁续期问题而设置的,在前文中我们也有手写过,这里我们看看"正版"的是如何执行的。

1.首先,在获取锁时会触发看门狗机制:

java 复制代码
private Long tryAcquire(long leaseTime, TimeUnit unit, long threadId) {
    Long ttl = null;
    // 如果未指定租约时间,则使用看门狗机制
    if (leaseTime != -1) {
        ttl = tryLockInnerAsync(leaseTime, unit, threadId, RedisCommands.EVAL_LONG);
    } else {
        // 使用默认的看门狗超时时间(默认30秒)
        ttl = tryLockInnerAsync(commandExecutor.getConnectionManager().getCfg().getLockWatchdogTimeout(),
                TimeUnit.MILLISECONDS, threadId, RedisCommands.EVAL_LONG);
        // 启动看门狗
        scheduleExpirationRenewal(threadId);
    }
    return ttl;
}

2.看门狗启动逻辑:

java 复制代码
private void scheduleExpirationRenewal(long threadId) {
    ExpirationEntry entry = new ExpirationEntry();
    ExpirationEntry oldEntry = EXPIRATION_RENEWAL_MAP.putIfAbsent(getEntryName(), entry);
    if (oldEntry != null) {
        oldEntry.addThreadId(threadId);
    } else {
        entry.addThreadId(threadId);
        // 重点:续期任务的调度
        renewExpiration();
    }
}

// 续期任务调度
private void renewExpiration() {
    // 创建异步续期任务
    Timeout task = commandExecutor.getConnectionManager().newTimeout(new TimerTask() {
        @Override
        public void run(Timeout timeout) throws Exception {
            ExpirationEntry ee = EXPIRATION_RENEWAL_MAP.get(getEntryName());
            if (ee == null) {
                return;
            }
            
            // 通过Lua脚本延长锁的过期时间
            RFuture<Boolean> future = renewExpirationAsync(threadId);
            future.onComplete((success, e) -> {
                if (e != null) {
                    log.error("Can't update lock " + getName() + " expiration", e);
                    return;
                }
                
                if (success) {
                    // 如果续期成功,递归调用,实现循环续期
                    renewExpiration();
                }
            });
        }
        // 续期间隔为看门狗超时时间的1/3
    }, internalLockLeaseTime / 3, TimeUnit.MILLISECONDS);
    
    ee.setTimeout(task);
}

3.续期的核心 Lua 脚本:

java 复制代码
protected RFuture<Boolean> renewExpirationAsync(long threadId) {
    return commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_BOOLEAN,
        // 检查锁是否存在且被当前线程持有
        "if (redis.call('hexists', KEYS[1], ARGV[2]) == 1) then " +
            // 重新设置过期时间
            "redis.call('pexpire', KEYS[1], ARGV[1]); " +
            "return 1; " +
        "end; " +
        "return 0;",
        Collections.singletonList(getName()),
        internalLockLeaseTime, // 默认30秒
        getLockName(threadId));
}

4.锁释放时清理看门狗:

java 复制代码
void cancelExpirationRenewal(Long threadId) {
    ExpirationEntry task = EXPIRATION_RENEWAL_MAP.get(getEntryName());
    if (task == null) {
        return;
    }
    
    if (threadId != null) {
        task.removeThreadId(threadId);
    }
    
    if (threadId == null || task.hasNoThreads()) {
        // 取消定时任务
        task.getTimeout().cancel();
        EXPIRATION_RENEWAL_MAP.remove(getEntryName());
    }
}

让我们总结一下看门狗机制的工作流程:

  • 触发条件
    • 当调用 lock() 方法且未指定租约时间时
    • 默认使用 30 秒的看门狗超时时间
  • 核心原理
    • 在获取锁成功后,创建一个定时任务
    • 定时任务每隔 10 秒(默认超时时间的 1/3)执行一次
    • 通过 Lua 脚本检查锁是否还存在并延长过期时间
  • 续期策略
    • 每次续期都将过期时间刷新为 30 秒
    • 只要业务线程还持有锁,就会一直续期
    • 续期操作是异步的,不会阻塞业务线程
  • 自动清理
    • 当锁释放时,自动取消续期任务
    • 通过 EXPIRATION_RENEWAL_MAP 管理所有续期任务
  • 安全保证
    • 使用 Lua 脚本保证续期操作的原子性
    • 只有当前持有锁的线程才能续期成功
    • 如果业务异常导致线程中断,看门狗也会停止续期

另外,通过学习源码,我们要正确使用看门狗机制,我们需要注意:不要使用带超时参数的加锁方法,否则看门狗机制不会生效;确保业务执行时间不会过长,否则会产生大量续期操作;记得在完成业务后及时释放锁,避免资源浪费。

小结

看门狗机制的关键在于定时对锁的状态进行检查,回想下我们当时手撸是通过循环进行的,这里显然高明多了,采用的定时任务,并且定时任务的执行频率也是过期时间的1/3,执行效率也比我们采用循环的方式高得多。

相关推荐
想用offer打牌5 分钟前
MCP (Model Context Protocol) 技术理解 - 第二篇
后端·aigc·mcp
陌上丨1 小时前
Redis的Key和Value的设计原则有哪些?
数据库·redis·缓存
KYGALYX1 小时前
服务异步通信
开发语言·后端·微服务·ruby
掘了2 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
爬山算法2 小时前
Hibernate(90)如何在故障注入测试中使用Hibernate?
java·后端·hibernate
Moment2 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
Cobyte3 小时前
AI全栈实战:使用 Python+LangChain+Vue3 构建一个 LLM 聊天应用
前端·后端·aigc
程序员侠客行4 小时前
Mybatis连接池实现及池化模式
java·后端·架构·mybatis
Honmaple4 小时前
QMD (Quarto Markdown) 搭建与使用指南
后端
PP东4 小时前
Flowable学习(二)——Flowable概念学习
java·后端·学习·flowable