preface

The RedissonMultiLock object can associate multiple Rlock objects into one interlock, and each Rlock object instance can come from a different Redisson instance.

Of course, this is the introduction on the official website, what is it specifically? Let’s take a look at the use of interlocking MultiLock and source code!

MultiLock use

According to the official documentation, the Redisson client may not be the same here. Of course, the general work will not say not to use a client.

lock

Before you read Multilock, you should have read the article about Multilock.

Source: entrance org. Redisson. RedissonMultiLock# lock ()

The default timeout LeaseTime is not set, so it is -1.

This method is too long. Let’s break it down and read it.

  1. BaseWaitTime = number of locks * 1500, which in this case is 4500 milliseconds;
  2. leaseTime == -1So waitTime = baseWaitTime, which is 4500;
  3. while (true)Call TryLock to lock until successful.

Call the TryLock method with waitTime = 4500, leaseTime = -1, and unit = MILLISECONDS.

Now let’s look at the logic in TryLock.

leaseTime ! If lambda equals negative 1 doesn’t satisfy that, I’m just going to skip this.

waitTime ! Equals -1, remainTime = 4500, lockWaitTime = 4500.

Therefore, the method failedLocksLimit() directly returns 0, which means that all locks must succeed.

Here’s the point:

Iterate through all the locks, adding locks one by one.

Lock logic is no different from reentrant lock locking. So the Lua script doesn’t do the analysis.

This is what happens when TryLock is locked.

If the lock succeeds, the successful lock is placed into the AcquiredLocks collection.

Because this is 0, lock release will be performed directly on all locks in the successfully locklocked collection in AcquiredLocks, while emptying the successful collection and restoring the iterator.

And again after each time you add the lock, you’re going to update the remaining time of remainTime, and if this is less than or equal to 0, that means you’ve timed out and you’re going to return false.

This will execute the external while (true) logic, and then revisit the RedissonMultilock # TryLock.

Release the lock

After looking at the locking logic, lock release becomes easier to understand.

The lock can be released by simply iterating over it. Lock.unlockAsync () is the method called RedissonBaseLock#unlockAsync().

conclusion

According to my understanding, the drawing is as follows:

In a nutshell, it’s key1, key2, key3… KeyN is placed in a List collection, and the lock is iterated until all of them succeed. Unlocking is done by traversing the lock again to release it.

Related to recommend

  • Redisson distributed lock source 07: fair lock release
  • Redisson distributed lock source code 06: fair lock queue lock
  • Redisson distributed lock source code 05: fair lock lock