当前位置:网站首页>redisson看门狗机制,redisson看门狗性能问题,redisson源码解析
redisson看门狗机制,redisson看门狗性能问题,redisson源码解析
2022-07-01 07:16:00 【秃了也弱了。】
redisson看门狗机制
官网解释
Redisson内部提供了一个监控锁的看门狗,它的作用是在Redisson实例被关闭前,不断的延长锁的有效期。默认情况下,看门狗的检查锁的超时时间是30秒钟,也可以通过修改Config.lockWatchdogTimeout来另行指定。
看门狗开启条件
我们可以看到,leaseTime != -1时,只执行tryLockInnerAsync方法,其它情况会执行下面的代码,而leaseTime 就是我们调用
lock(10, TimeUnit.SECONDS);方法传入的时间参数。
由此可知:redisson如果只是用lock.lock();不传过期时间的话,会启动看门狗机制,传过期时间的话,就不会启动看门狗机制。
// org.redisson.RedissonLock#tryAcquireAsync
private <T> RFuture<Long> tryAcquireAsync(long waitTime, long leaseTime, TimeUnit unit, long threadId) {
if (leaseTime != -1) {
return tryLockInnerAsync(waitTime, leaseTime, unit, threadId, RedisCommands.EVAL_LONG);
}
RFuture<Long> ttlRemainingFuture = tryLockInnerAsync(waitTime,
commandExecutor.getConnectionManager().getCfg().getLockWatchdogTimeout(),
TimeUnit.MILLISECONDS, threadId, RedisCommands.EVAL_LONG);
// future模式,抢到锁之后开启看门狗
ttlRemainingFuture.onComplete((ttlRemaining, e) -> {
if (e != null) {
return;
}
// lock acquired
if (ttlRemaining == null) {
scheduleExpirationRenewal(threadId); // 开启看门狗
}
});
return ttlRemainingFuture;
}
看门狗如何开启的
以下代码就是开启看门狗的方法,我们可以看到,启动了一个TimerTask进行倒计时,默认倒计时时间为internalLockLeaseTime / 3,也就是默认的10秒钟(默认过期时间是30秒)。
// org.redisson.RedissonLock#renewExpiration
private void renewExpiration() {
ExpirationEntry ee = EXPIRATION_RENEWAL_MAP.get(getEntryName());
if (ee == null) {
return;
}
Timeout task = commandExecutor.getConnectionManager().newTimeout(new TimerTask() {
@Override
public void run(Timeout timeout) throws Exception {
ExpirationEntry ent = EXPIRATION_RENEWAL_MAP.get(getEntryName());
if (ent == null) {
return;
}
Long threadId = ent.getFirstThreadId();
if (threadId == null) {
return;
}
// 关键方法,使用lua脚本刷新过期时间
RFuture<Boolean> future = renewExpirationAsync(threadId);
future.onComplete((res, e) -> {
if (e != null) {
log.error("Can't update lock " + getName() + " expiration", e);
return;
}
if (res) {
// reschedule itself
renewExpiration(); // 不断地调用自己,刷新过期时间
}
});
}
}, internalLockLeaseTime / 3, TimeUnit.MILLISECONDS);
ee.setTimeout(task);
}
看门狗的性能问题
很多小伙伴都认为看门狗是非常消耗性能的,其实性能的确是会有一些消耗,但是没有很多。
前几天有个小伙伴抛出了一个疑问:假如说每个线程都启动一个TimerTask来不断刷新过期时间,岂不是服务器很快就“炸了”?
其实不然,只有抢占到锁的线程才会开启看门狗,并不是每个等待的线程都会开启一个看门狗。也就是说——基本上每一个锁会对应一个看门狗,而不是每一个线程对应一个看门狗。
这样看来,是不是性能浪费的就不是很多了?
其实看门狗机制主要是用于业务代码执行时间忽长忽短的,如果一个业务代码,我们确定它在10秒钟之内就会执行完毕,完全可以取消这个看门狗机制,来提升一部分性能。
强大的redisson
redisson非常强大,完美的解决了分布式系统下的很多问题。
具体请查阅文档:
redisson使用全解——redisson官方文档+注释(上篇)
redisson使用全解——redisson官方文档+注释(中篇)
redisson使用全解——redisson官方文档+注释(下篇)
边栏推荐
- 用手机在指南针上开户靠谱吗?这样有没有什么安全隐患
- Fix the problem that the AI video intelligent platform easycvr device video cannot be played
- 【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)
- 在券商账户上买基金安全吗
- Automated test platform (13): interface automation framework and platform comparison, application scenario analysis and design ideas sharing
- The programmer of Beipiao posted a post for help late at night: I am lonely when my girlfriend is gone
- The game is real! China software cup releases a new industrial innovation competition, and schools and enterprises can participate in it jointly
- Subclasses call methods and properties of the parent class with the same name
- The computer has a network, but all browser pages can't be opened. What's the matter?
- DC-4靶机
猜你喜欢
随机推荐
【MATLAB】求解非线性规划
[recommendation technology] matlab simulation of network information recommendation technology based on collaborative filtering
The programmer of Beipiao posted a post for help late at night: I am lonely when my girlfriend is gone
[matlab] solve nonlinear programming
灰度何以跌下神坛?
ctfshow-web354(SSRF)
Is it reliable to open an account on the compass with your mobile phone? Is there any potential safety hazard
[FPGA frame difference] FPGA implementation of frame difference target tracking based on vmodcam camera
ctfhub-端口扫描(SSRF)
Product learning (III) - demand list
Unity2021-Scene视图中物体无法直接选中的解决办法
Product learning (II) - competitive product analysis
运维管理系统,人性化操作体验
电脑有网络,但所有浏览器网页都打不开,是怎么回事?
未来互联网人才还稀缺吗?哪些技术方向热门?
Solution to the problem that objects in unity2021 scene view cannot be directly selected
Chinese explanation of common rclone subcommands
Understanding of Turing test and Chinese Room
iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事
华泰证券开户是安全可靠的么?怎么开华泰证券账户









