Redisson distributed lock source code 06: Fair lock queuing and locking

Time:2022-5-14

preface

In the previous article, I analyzed the lock source code of fair lock and concluded:

  1. Redis hash data structure: store the current lock, redis key is the lock, hash field is the lock thread, and hash value is the number of re-entry;
  2. Redis list data structure: it acts as a thread waiting queue, and the new waiting thread will be placed on the right side of the queue using the rpush command;
  3. Redis sorted set ordered set data structure: it stores the order of waiting threads, and the score is used as the timeout timestamp of waiting threads.

Now let’s look at how the thread handles the lock failure after it is put into the waiting queue?

Queue lock

Source code entry:org.redisson.RedissonLock#lock(long, java.util.concurrent.TimeUnit, boolean)

After the thread enters the queue, it will appear in the Java codewhile (true)Always loop calltryAcquire, try to acquire the lock.

Finally cameRedissonFairLock#tryLockInnerAsyncMethod.

For convenience, re paste the Lua script and the meaning of the parameters of the script.

  1. Keys [1]: the name of the lock,anyLock
  2. Keys [2]: lock the waiting queue,redisson_lock_queue:{anyLock}
  3. Keys [3]: set of thread lock time in the waiting queue,redisson_lock_timeout:{anyLock}, which is stored in the collection according to the time stamp of the lock;
  4. Argv [1]: Lock timeout 30000
  5. Argv [2]: UUID: ThreadID combinationa3da2c83-b084-425c-a70f-5d9a08b37f31:1
  6. Argv [3]: threadwaittime defaults to 300000
  7. Argv [4]: currenttime current timestamp

Redisson distributed lock source code 06: Fair lock queuing and locking

Source code analysis

Redisson distributed lock source code 06: Fair lock queuing and locking

The first part, while loop:

  1. From waiting queueredisson_lock_queue:{anyLock}Get the first waiting thread in;
  2. Timeout collection from waiting threadredisson_lock_timeout:{anyLock}Get the score of the first waiting thread in;
  3. If there is no timeout, it will end directly. If there is a timeout, it will be removed directly.

In the second part, the current lock exists. Skip it directly.

In the third part, the current lock is not a lock holding thread. Skip it directly.

Redisson distributed lock source code 06: Fair lock queuing and locking

Part IV,

Redisson distributed lock source code 06: Fair lock queuing and locking

Directly returns how long the current lock will expire.

The current version of redisson is 3.15.6, which is slightly different from different versions.

Queue rearrangement

There is no reordering here, because officials think it is a bug and fix it again.

You can read:Justin Corpron 2019/5/10, 04:13 Fix timeout drift in RedissonFairLock

The biggest change is the addition of the fourth part.

Redisson distributed lock source code 06: Fair lock queuing and locking

The figure only represents the difference between the two versions, not the revision of this version.

summary

Redisson distributed lock source code 06: Fair lock queuing and locking

When a thread fails to acquire a lock and enters the waiting queue,ttl != null, there are constant attempts to acquire locks in Java code.

When the lock does not exist and the current thread is waiting for the queue header, the lock is obtained directly. This queuing process is the withdrawal of fair lock.

Relevant recommendations