Never imagined that the interview was actually planted on how to renew the Redis distributed lock! Senseless...

real case scenario

Insert picture description here

The correct posture of Redis distributed lock

When many students use distributed locks, they directly use Baidu search to find a Redis distributed lock tool class. The key is that the tool class is also full of many System.out.println(); and other statements. In fact, Redis The more correct posture for distributed locks is to use redisson, a client tool.

How to answer

Before you start, remember to like, collect and follow. Friends who need to download the PDF version and get more knowledge points and interview questions can click the link below to get it for free

Link: Click here! ! ! 799215493 secret code: CSDN

Insert picture description here

First of all, if you have used Redis's distributed locks in the correct posture and read the corresponding official documents, this question is So easy. Let's take a look

Insert picture description here
Frankly speaking, if you are good at English, then you may understand the English document better

By default lock watchdog timeout is 30 seconds and can be changed
through Config.lockWatchdogTimeout setting.

But if you read the Chinese document

The default timeout period for the watchdog to check the lock is 30 seconds

This sentence is an ambiguous sentence from the perspective of language analysis. It has two meanings.

1. The watchdog defaults to 30 seconds to check the timeout period of a lock

2. Watchdog will check the lock timeout time, the lock time time is 30 seconds by default

Seeing this, I hope everyone will not hack my elementary school physical education teacher, although he and the Chinese teacher are the same person. Chinese is not good, we can use the source code!

Insert picture description here

Source code analysis

We wrote the simplest demo based on the example given in the official document. The example is based on the wave operation of Ctr+C and Ctr+V in the screenshot above, as follows

public class DemoMain {
    
    

    public static void main(String[] args) throws Exception {
    
    
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");

        RedissonClient redisson = Redisson.create(config);
        RLock lock = redisson.getLock("anyLock");

        lock.lock();
        //lock.unlock();
    }
}

create

Insert picture description here
From here we know that the two parameters of internalLockLeaseTime and lockWatchdogTimeout are equal.

The default value of lockWatchdogTimeout is as follows

public class Config {
    
    
	
	private long lockWatchdogTimeout = 30 * 1000;
		
	public long getLockWatchdogTimeout() {
    
    
		return lockWatchdogTimeout;
	}
	
	//省略无关代码
}

It can also be seen from the word internalLockLeaseTime that the timeout time of this added distributed lock is 30 seconds by default. But there is another problem, that is, how often does this watchdog extend the validity period? Let's look down

lock

Insert picture description here
From the place in the box in my picture, we know that a timed task, which is a watchdog, will be started when the lock is successfully acquired.The timed task will be checked regularly to renew the renewExpirationAsync(threadId).

The HashedWheelTimer in the netty-common package is used here for timing. It has established a close cooperative relationship with major search engines. You only need to search this category on any search engine to know the meaning of the relevant API parameters.

From the figure, we understand that the time difference between each call of the timing schedule is internalLockLeaseTime/3, which is 10 seconds.

The truth is revealed

Through source code analysis, we know that by default, the lock time is 30 seconds. If the locked business is not executed, then when 30-10 = 20 seconds, a renewal will be performed and the lock will be reset to 30 seconds. At this time, some students may ask again, what if the business machine goes down? If the timed task fails to run, it will not be renewed. Then naturally the lock will be unlocked after 30 seconds.

Write at the end

Here I have prepared interview materials for first-line manufacturers and my original super hardcore PDF technical documents, as well as multiple sets of resume templates that I have carefully prepared for everyone (continuously updated). I hope everyone can find their favorite job!

Friends in need can click the link below to get it for free

Link: Click here! ! ! 799215493 secret code: CSDN

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_48655626/article/details/109752811