当前位置:网站首页>Redisson watchdog mechanism, redisson watchdog performance problems, redisson source code analysis
Redisson watchdog mechanism, redisson watchdog performance problems, redisson source code analysis
2022-07-01 07:19:00 【Bald and weak.】
List of articles
redisson The watchdog mechanism
Website to explain
Redisson Inside a watchdog lock is provided , Its function is to Redisson Before the instance is closed , Keep extending the validity of lock . By default , The timeout for the watchdog to check the lock is 30 Second , You can also modify Config.lockWatchdogTimeout To specify otherwise .
Watchdog open condition
We can see ,leaseTime != -1 when , Only execute tryLockInnerAsync Method , In other cases, the following code will be executed , and leaseTime That's what we call
lock(10, TimeUnit.SECONDS); The time parameter passed in by the method .
Thus we can see that :redisson If you just use lock.lock(); If the expiration time is not transmitted , Will activate the watchdog mechanism , If you send the expiration time , The watchdog mechanism will not be activated .
// 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 Pattern , After grabbing the lock, open the watchdog
ttlRemainingFuture.onComplete((ttlRemaining, e) -> {
if (e != null) {
return;
}
// lock acquired
if (ttlRemaining == null) {
scheduleExpirationRenewal(threadId); // Open the watchdog
}
});
return ttlRemainingFuture;
}
How the watchdog opens
The following code is the way to open the watchdog , We can see , It starts a TimerTask Count down , The default countdown time is internalLockLeaseTime / 3, Which is the default 10 Second ( The default expiration time is 30 second ).
// 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;
}
// Key methods , Use lua Script refresh expiration time
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(); // Constantly calling yourself , Refresh expiration time
}
});
}
}, internalLockLeaseTime / 3, TimeUnit.MILLISECONDS);
ee.setTimeout(task);
}
The performance of the watchdog
Many kids think that the watchdog is very performance consuming , In fact, there will be some performance consumption , But not many .
A few days ago, a little friend threw a question : Suppose every thread starts one TimerTask To constantly refresh the expiration time , Isn't it that the server will soon “ Fried ”?
It's not , Only the thread that preempts the lock will open the watchdog , Not every waiting thread will open a watchdog . in other words —— Basically, each lock corresponds to a watchdog , Instead of each thread corresponding to a watchdog .
So it looks like , Isn't there a lot of performance waste ?
In fact, the watchdog mechanism is mainly used for the execution time of business code varying from long to short , If a business code , We're sure it's 10 The execution will be completed in seconds , This watchdog mechanism can be completely abolished , To improve some performance .
Powerful redisson
redisson Very powerful , It perfectly solves many problems under the distributed system .
Please refer to the document for details :
redisson Use the total solution ——redisson Official documents + notes ( Part 1 )
redisson Use the total solution ——redisson Official documents + notes ( medium-length )
redisson Use the total solution ——redisson Official documents + notes ( The next part )
边栏推荐
- EasyNVS云管理平台功能重构:支持新增用户、修改信息等
- go-etcd
- C language implementation [minesweeping game] full version (implementation source code)
- Fix the problem that the AI video intelligent platform easycvr device video cannot be played
- C语言实现【扫雷游戏】完整版(实现源码)
- Will Internet talents be scarce in the future? Which technology directions are popular?
- ctfshow-web355,356(SSRF)
- kdtree(kd树)笔记
- C # read and write customized config file
- 【图像处理】图像直方图均衡化系统含GUI界面
猜你喜欢
We found a huge hole in MySQL: do not judge the number of rows affected by update!!!
Easynvs cloud management platform function reconfiguration: support adding users, modifying information, etc
redisson使用全解——redisson官方文檔+注釋(上篇)
C语言实现【扫雷游戏】完整版(实现源码)
ctfshow-web351(SSRF)
【电气介数】电气介数及考虑HVDC和FACTS元件的电气介数计算
[Tikhonov] image super-resolution reconstruction based on Tikhonov regularization
[FPGA frame difference] FPGA implementation of frame difference target tracking based on vmodcam camera
C# Newtonsoft. Use of job in JSON
為什麼這麼多人轉行產品經理?產品經理發展前景如何?
随机推荐
DC-4靶机
2022电工(中级)复训题库及答案
Microsoft announces open source (Godel) language model chat robot
[FPGA frame difference] FPGA implementation of frame difference target tracking based on vmodcam camera
[programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array
Is it reliable to open an account on the compass with your mobile phone? Is there any potential safety hazard
ctfshow-web351(SSRF)
Félicitations pour l'inscription réussie de wuxinghe
Subclasses call methods and properties of the parent class with the same name
Ctfhub port scan (SSRF)
Unity2021-Scene视图中物体无法直接选中的解决办法
atguigu----脚手架--02-使用脚手架(2)
Image style migration cyclegan principle
【FPGA帧差】基于VmodCAM摄像头的帧差法目标跟踪FPGA实现
Warm congratulations on the successful listing of five elements hehe liquor
运维管理有什么实用的技巧吗
Fix the problem that the AI video intelligent platform easycvr device video cannot be played
1286_FreeRTOS的任务优先级设置实现分析
STM32F1与STM32CubeIDE编程实例-NEC协议红外接收与解码
Apple账号密码自动填充