当前位置:网站首页>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.

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 )

原网站

版权声明
本文为[Bald and weak.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207010716076958.html