preface

The interviewer asked me if I had any problems with Dubbo. I smiled. Later, another fan told me that she was abused during the interview. Since this fan is a former fan **, and will start a new round of interview on Monday, in order to reward his long-term support, SO I wrote this overnight, hoping to help him in the following interview.

The real case

Redis distributed lock correct posture

According to Fat zhao, many students in the use of distributed lock, are directly baidu search to find a Redis distributed lock tool class directly used. The key is that the utility class is also full of system.out.println (); In fact, Redis distributed lock is more correct posture is to use redisson this client tool. Search Github, the largest gay dating site, for details.

How to answer

First of all, if you used Redis distributed lock posture correctly, and read the corresponding official documentation, this question is So easy. Let’s take a look at

Frankly speaking, if you are good at English, it is easier to read English documents

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

But if you’re looking at a 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, he has two meanings

1. The watchdog checks the lock timeout interval of 30 seconds by default

2. The dogs will check the timeout period of the lock, which is 30 seconds by default

See here, I hope you don’t black my primary school PE teacher, although he and language teacher is the same individual. Language is not, we can source to gather!

Source code analysis

We wrote the simplest demo according to 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();}}Copy the code

create

From this we know that internalLockLeaseTime and lockWatchdogTimeout are equal.

LockWatchdogTimeout The default value is as follows

public class Config {
	
	private long lockWatchdogTimeout = 30 * 1000;
		
	public long getLockWatchdogTimeout(a) {
		return lockWatchdogTimeout;
	}
	
	// omit extraneous code
}
Copy the code

InternalLockLeaseTime indicates that the added distributed lock timeout is 30 seconds by default. But there is still a question, that is the watchdog, how often to renew the validity period? Let’s go down

lock

As you can see from the box in my diagram, obtaining the lock will start a timed task, namely watchdog, which will be checked periodically to renew renew context Async(threadId).

The HashedWheelTimer in netty-Common package is regularly used here. The Official Account has established close cooperation with major search engines. You only need to search this class in any search engine to know the meaning of relevant API parameters.

From the figure, we know that the time difference between each call of the scheduled schedule is internalLockLeaseTime / 3. That’s 10 seconds.

The truth

Through source code analysis, we know that by default, lock time is 30 seconds. If the lock is not completed, a renewal will be performed to reset the lock to 30 seconds when 30-10 = 20 seconds. Then at this time there may be students asked, that business machine in case of downtime? If you can’t run a scheduled task, you can’t renew it, so naturally the lock will be unlocked after 30 seconds.

Write in the last

If you are an old fan of our official account, and you have any questions in the interview or work process, please feel free to contact us. But fat dynasty is a serious Java development, we just tune the interface, not flirting!