【📕分布式锁通关指南 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,执行效率也比我们采用循环的方式高得多。

相关推荐
小林ixn18 分钟前
用 Next.js 和 Redis 撸一个 Markdown 笔记系统:RSC 实战与组件化拆解
前端·redis·next.js
笨鸟飞不快19 分钟前
RocketMQ Lite Topic 流程梳理与设计解读
后端·rocketmq
步行cgn28 分钟前
MyBatis 查询结果字段为 null?一文讲透下划线命名与驼峰映射问题
后端
技术长镜头28 分钟前
别再只会“加索引”:从磁盘页到 B+Tree,彻底理解 MySQL 索引的设计与运行
后端·mysql
生锈的键盘29 分钟前
gRPC 多路复用全链路拆解:从客户端到服务端,中间隔着 ELB 和 Nginx 到底是怎么玩的?
后端
laity1734 分钟前
python发光表白爱心(从零到一实现)
前端·后端
用户9210802628639 分钟前
2. Java 基础该怎么学,先抓业务开发真正用得上的部分
后端
AI老猿博士41 分钟前
SpringBoot自动配置揭秘
后端
程序员爱钓鱼43 分钟前
Go for 循环详解
后端·面试·go
程序员爱钓鱼43 分钟前
Rust impl详解:为Struct定义方法与关联函数
前端·后端·rust