前言
假设我们给锁设置的过期时间太短,业务还没执行完成,锁就过期了,这块应该如何处理呢?是否可以给分布式锁续期?
解决方案:先设置一个过期时间,然后我们开启一个守护线程,定时去检测这个锁的失效时间,如果锁快要过期了,操作共享资源还未完成,那么就自动对锁进行续期,重新设置过期时间。
幸运的是有一个库把这些工作都帮我们封装好了,那就是 redisson,redisson 是 java 语言实现的 redis sdk 客户端,它能给 redis 分布式锁实现过期时间自动续期。
当然,redisson 不只是会做这个,除此之外,还封装了很多易用的功能:
- 可重入锁
- 乐观锁
- 公平锁
- 读写锁
- redlock
这里我们只讲怎么实现续期,有需要的小伙伴可以自己去了解其他的功能哦。
在使用分布式锁时,redisson 采用了自动续期的方案来避免锁过期,这个守护线程我们一般也把它叫做 “看门狗(watch dog)” 线程。
watch dog自动延期机制
只要客户端一旦加锁成功,就会启动一个 watch dog 看门狗。watch dog 是一个后台线程,会每隔 10 秒检查一下,如果客户端还持有锁 key,那么就会不断的延长锁 key 的生存时间。
如果负责存储这个分布式锁的 redission 节点宕机后,而且这个锁正好处于锁住的状态时,这个锁会出现锁死的状态,为了避免这种情况的发生,redisson 提供了一个监控锁的看门狗,它的作用是在 redisson 实例被关闭前,不断的延长锁的有效期。默认情况下,看门狗的续期时间是 30 秒,也可以通过修改 config.lockwatchdogtimeout 来指定。

另外 redisson 还提供了可以指定 leasetime 参数的加锁方法来指定加锁的时间。超过这个时间后锁便自动解开了,不会延长锁的有效期。
接下来我们从源码看一下是怎么实现的。
源码分析
首先我们先写一个 dome 一步步点击进去看。
config config = new config();
config.usesingleserver().setaddress("redis://127.0.0.1:6379");
redissonclient redisson = redisson.create(config);
rlock lock = redisson.getlock("mylock");
lock.lock();
rlock lock = redisson.getlock(“mylock”); 这句代码就是为了获取锁的实例,然后我们可以看到它返回的是一个 redissonlock 对象
//name:锁的名称
public rlock getlock(string name) {
//默认创建的同步执行器, (存在异步执行器, 因为锁的获取和释放是有强一致性要求, 默认同步)
return new redissonlock(this.connectionmanager.getcommandexecutor(), name);
}
点击 redissonlock 进去,发现这是一个 redissonlock 构造方法,主要初始化一些属性。
public redissonlock(commandasyncexecutor commandexecutor, string name) {
super(commandexecutor, name);
this.commandexecutor = commandexecutor;
//唯一id
this.id = commandexecutor.getconnectionmanager().getid();
//等待获取锁时间
this.internallockleasetime = commandexecutor.getconnectionmanager().getcfg().getlockwatchdogtimeout();
//id + 锁名称
this.entryname = this.id + ":" + name;
//发布订阅
this.pubsub = commandexecutor.getconnectionmanager().getsubscribeservice().getlockpubsub();
}
我们点击 getlockwatchdogtimeout() 进去看一下:
public class config {
private long lockwatchdogtimeout = 30 * 1000;
public long getlockwatchdogtimeout() {
return lockwatchdogtimeout;
}
//省略
}
从 internallockleasetime 这个单词也可以看出,这个加的分布式锁的超时时间默认是 30 秒,现在我们知道默认是 30 秒,那么这个看门狗多久时间来延长一次有效期呢?我们接着往下看。
这里我们选择 lock.lock(); 点击进去看:
public void lock() {
try {
this.lock(-1l, (timeunit)null, false);
} catch (interruptedexception var2) {
throw new illegalstateexception();
}
}
private void lock(long leasetime, timeunit unit, boolean interruptibly) throws interruptedexception {
long threadid = thread.currentthread().getid();
long ttl = this.tryacquire(leasetime, unit, threadid);
if (ttl != null) {
rfuture<redissonlockentry> future = this.subscribe(threadid);
if (interruptibly) {
this.commandexecutor.syncsubscriptioninterrupted(future);
} else {
this.commandexecutor.syncsubscription(future);
}
上面参数的含义:
leasetime: 加锁到期时间, -1 使用默认值 30 秒
unit: 时间单位, 毫秒、秒、分钟、小时…
interruptibly: 是否可被中断标示
而 this.tryacquire()这个方法中是用来执行加锁, 继续跳进去看:
private <t> rfuture<long> tryacquireasync(long leasetime, timeunit unit, long threadid) {
//执行 trylock(...) 才会进入
if (leasetime != -1l) {
//进行异步获取锁
return this.trylockinnerasync(leasetime, unit, threadid, rediscommands.eval_long);
} else {
//尝试异步获取锁, 获取锁成功返回空, 否则返回锁剩余过期时间
rfuture<long> ttlremainingfuture = this.trylockinnerasync(this.commandexecutor.getconnectionmanager().getcfg().getlockwatchdogtimeout(), timeunit.milliseconds, threadid, rediscommands.eval_long);
//ttlremainingfuture 执行完成后触发此操作
ttlremainingfuture.oncomplete((ttlremaining, e) -> {
if (e == null) {
//ttlremaining == null 代表获取了锁
//获取到锁后执行续时操作
if (ttlremaining == null) {
this.scheduleexpirationrenewal(threadid);
}
}
});
return ttlremainingfuture;
}
}
我们继续选择 scheduleexpirationrenewal() 跳进去看:
private void scheduleexpirationrenewal(long threadid) {
redissonlock.expirationentry entry = new redissonlock.expirationentry();
redissonlock.expirationentry oldentry = (redissonlock.expirationentry)expiration_renewal_map.putifabsent(this.getentryname(), entry);
if (oldentry != null) {
oldentry.addthreadid(threadid);
} else {
entry.addthreadid(threadid);
this.renewexpiration();
}
}
接着进去 renewexpiration() 方法看:
该方法就是开启定时任务,也就是 watch dog 去进行锁续期。
private void renewexpiration() {
//从容器中去获取要被续期的锁
redissonlock.expirationentry ee = (redissonlock.expirationentry)expiration_renewal_map.get(this.getentryname());
//容器中没有要续期的锁,直接返回null
if (ee != null) {
//创建定时任务
//并且执行的时间为 30000/3 毫秒,也就是 10 秒后
timeout task = this.commandexecutor.getconnectionmanager().newtimeout(new timertask() {
public void run(timeout timeout) throws exception {
//从容器中取出线程
redissonlock.expirationentry ent = (redissonlock.expirationentry)redissonlock.expiration_renewal_map.get(redissonlock.this.getentryname());
if (ent != null) {
long threadid = ent.getfirstthreadid();
if (threadid != null) {
//redis进行锁续期
//这个方法的作用其实底层也是去执行lua脚本
rfuture<boolean> future = redissonlock.this.renewexpirationasync(threadid);
//同理去处理redis续命结果
future.oncomplete((res, e) -> {
if (e != null) {
redissonlock.log.error("can't update lock " + redissonlock.this.getname() + " expiration", e);
} else {
//如果成功续期,递归继续创建下一个 10s 后的任务
if (res) {
//递归继续创建下一个10s后的任务
redissonlock.this.renewexpiration();
}
}
});
}
}
}
}, this.internallockleasetime / 3l, timeunit.milliseconds);
ee.settimeout(task);
}
}
从这里我们就知道,获取锁成功就会开启一个定时任务,也就是 watchdog 看门狗,定时任务会定期检查去续期renewexpirationasync(threadid)。
从这里我们明白,该定时调度每次调用的时间差是 internallockleasetime / 3,也就是 10 秒。
总结
面试的时候简单明了的回答这个问题就是:
只要客户端一旦加锁成功,就会启动一个 watch dog 看门狗,他是一个后台线程,会每隔 10 秒检查一下,如果客户端还持有锁 key,那么就会不断的延长锁 key 的过期时间。
默认情况下,加锁的时间是 30 秒,.如果加锁的业务没有执行完,就会进行一次续期,把锁重置成 30 秒,万一业务的机器宕机了,那就续期不了,30 秒之后锁就解开了。
到此这篇关于redis锁的过期时间小于业务的执行时间如何续期的文章就介绍到这了,更多相关redis 锁续期内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论