Home Java javaTutorial Detailed explanation of the principle of distributed lock implemented by redisson

Detailed explanation of the principle of distributed lock implemented by redisson

Mar 07, 2017 am 10:26 AM
redisson Distributed lock

This article will introduce in detail the principle of distributed lock implemented by redisson. It has a very good reference value. Let’s take a look at it with the editor.

Redisson distributed lock

One of the previous annotation-based locks is the basic redis Distributed lock, I implement the lock based on RLock provided by the redisson component. This article will take a look at how redisson implements the lock.

The mechanisms for implementing locks are different in different versions.

The recently released version 3.2.3 of redisson quoted is different. Different versions may implement locks. The mechanism is different. The early version seemed to use simple setnx, getset and other conventional commands to complete the configuration. However, in the later period, the implementation principle was changed because redis supported script Lua.

<dependency>
 <groupId>org.redisson</groupId>
 <artifactId>redisson</artifactId>
 <version>3.2.3</version>
</dependency>
Copy after login

setnx needs to be completed with getset and transactions, so as to better avoid deadlock problems, and the new version can avoid the use of transactions because it supports lua scripts As well as operating multiple redis commands, the semantic expression is clearer.

Characteristics of RLock interface

Inherits the standard interface Lock

It has all the features of the standard lock interface, such as lock, unlock, trylock, etc.

Extended standard interface Lock

Extends many methods, the most commonly used ones are: forced lock release, lock with validity period, and a set of Asynchronous methods. The first two methods are mainly to solve the deadlock problem that may be caused by standard lock. For example, after a thread acquires a lock, the machine where the thread is located crashes. At this time, the thread that acquired the lock cannot release the lock normally, causing the remaining threads waiting for the lock to wait.

Reentrant mechanism

The implementation of each version is different. The main consideration for reentrancy is performance. When the same thread does not release the lock If you apply for a lock resource again, you do not need to go through the application process. You only need to continue to return the acquired lock and record the number of reentries. This is similar to the ReentrantLock function in jdk. The number of reentries is used in conjunction with the hincrby command. The detailed parameters are in the code below.

How to determine if it is the same thread?

redisson’s solution is to add a guid of the RedissonLock instance to the id of the current thread, and return

through getLockName.

public class RedissonLock extends RedissonExpirable implements RLock {
 final UUID id;
 protected RedissonLock(CommandExecutor commandExecutor, String name, UUID id) {
  super(commandExecutor, name);
  this.internalLockLeaseTime = TimeUnit.SECONDS.toMillis(30L);
  this.commandExecutor = commandExecutor;
  this.id = id;
 }
 String getLockName(long threadId) {
  return this.id + ":" + threadId;
 }
Copy after login

Two scenarios for RLock to acquire a lock

Here is the source code of tryLock: the tryAcquire method is to apply Lock and return the remaining time of the lock validity period. If it is empty, it means that the lock has not been directly acquired and returned by other threads. If the time is acquired, the waiting competition logic will be entered.

public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException {
  long time = unit.toMillis(waitTime);
  long current = System.currentTimeMillis();
  final long threadId = Thread.currentThread().getId();
  Long ttl = this.tryAcquire(leaseTime, unit);
  if(ttl == null) {
   //直接获取到锁
   return true;
  } else {
   //有竞争的后续看
  }
 }
Copy after login

No competition, acquire the lock directly

Let’s first look at acquiring the lock first and What is redis doing behind the lock release? You can use the redis monitor to monitor the execution of redis in the background. When we add @RequestLockable to the method, we actually call lock and unlock. The following is the redis command:

Lock

Due to the high The version of redis supports lua scripts, so redisson also supports it and adopts script mode. If you are not familiar with lua scripts, you can look it up. The logic of executing the lua command is as follows:

<T> RFuture<T> tryLockInnerAsync(long leaseTime, TimeUnit unit, long threadId, RedisStrictCommand<T> command) {
    this.internalLockLeaseTime = unit.toMillis(leaseTime);
    return this.commandExecutor.evalWriteAsync(this.getName(), LongCodec.INSTANCE, command, 
    "if (redis.call(\&#39;exists\&#39;, KEYS[1]) == 0) then redis.call(\&#39;hset\&#39;, KEYS[1], ARGV[2], 1); 
    redis.call(\&#39;pexpire\&#39;, KEYS[1], ARGV[1]); return nil; end; 
    if (redis.call(\&#39;hexists\&#39;, KEYS[1], ARGV[2]) == 1) then redis.call(\&#39;hincrby\&#39;, KEYS[1], ARGV[2], 1); 
    redis.call(\&#39;pexpire\&#39;, KEYS[1], ARGV[1]); return nil; end; 
    return redis.call(\&#39;pttl\&#39;, KEYS[1]);", 
    Collections.singletonList(this.getName()), new Object[]{Long.valueOf(this.internalLockLeaseTime), this.getLockName(threadId)});
  }
Copy after login

Locking process:

  1. Determine whether the lock key exists. If it does not exist, directly call hset to store the current thread information and set the expiration time. Return nil to tell the client to obtain the lock directly.

  2. Determine whether the lock key exists. If it exists, increase the number of reentries by 1, reset the expiration time, and return nil to tell the client to obtain the lock directly.

  3. Has been locked by other threads, returns the remaining time of the lock validity period, and tells the client that it needs to wait.

"EVAL" 
"if (redis.call(&#39;exists&#39;, KEYS[1]) == 0) then 
redis.call(&#39;hset&#39;, KEYS[1], ARGV[2], 1); 
redis.call(&#39;pexpire&#39;, KEYS[1], ARGV[1]); 
return nil; end;
if (redis.call(&#39;hexists&#39;, KEYS[1], ARGV[2]) == 1) then 
redis.call(&#39;hincrby&#39;, KEYS[1], ARGV[2], 1); 
redis.call(&#39;pexpire&#39;, KEYS[1], ARGV[1]); 
return nil; end;
return redis.call(&#39;pttl&#39;, KEYS[1]);"
 "1" "lock.com.csp.product.api.service.ProductAppService.searchProductByPage#0" 
 "1000" "346e1eb8-5bfd-4d49-9870-042df402f248:21"
Copy after login

The above Lua script will be converted into a real redis command. The following is the actual redis command executed after the Lua script operation. .

1486642677.053488 [0 lua] "exists" "lock.com.csp.product.api.service.ProductAppService.searchProductByPage#0"
1486642677.053515 [0 lua] "hset" "lock.com.csp.product.api.service.ProductAppService.searchProductByPage#0" 
"346e1eb8-5bfd-4d49-9870-042df402f248:21" "1"
1486642677.053540 [0 lua] "pexpire" "lock.com.csp.product.api.service.ProductAppService.searchProductByPage#0" "1000"
Copy after login

Unlocking

Unlocking process Looks more complicated:

  1. If the lock key does not exist, send a message saying that the lock is available

  2. If the lock is not held by the current thread If locked, return nil

  3. Since reentrancy is supported, the number of reentries needs to be reduced by 1 when unlocking

  4. If the calculated reentry If the number of re-entries is >0, the expiration time will be reset.

  5. If the calculated number of re-entries is <=0, a message will be sent saying that the lock is available

"EVAL" 
"if (redis.call(&#39;exists&#39;, KEYS[1]) == 0) then
 redis.call(&#39;publish&#39;, KEYS[2], ARGV[1]);
 return 1; end;
if (redis.call(&#39;hexists&#39;, KEYS[1], ARGV[3]) == 0) then 
return nil;end; 
local counter = redis.call(&#39;hincrby&#39;, KEYS[1], ARGV[3], -1); 
if (counter > 0) then redis.call(&#39;pexpire&#39;, KEYS[1], ARGV[2]); return 0; 
else redis.call(&#39;del&#39;, KEYS[1]); redis.call(&#39;publish&#39;, KEYS[2], ARGV[1]); return 1; end; 
return nil;"
"2" "lock.com.csp.product.api.service.ProductAppService.searchProductByPage#0" 
"redisson_lock__channel:{lock.com.csp.product.api.service.ProductAppService.searchProductByPage#0}"
 "0" "1000"
 "346e1eb8-5bfd-4d49-9870-042df402f248:21"
Copy after login

Unlock the redis command without competition:

Mainly sends an unlock message to wake up the threads waiting in the queue to compete for the lock again .

1486642678.493691 [0 lua] "exists" "lock.com.csp.product.api.service.ProductAppService.searchProductByPage#0"
1486642678.493712 [0 lua] "publish" "redisson_lock__channel:{lock.com.csp.product.api.service.ProductAppService.searchProductByPage#0}" "0"
Copy after login

有竞争,等待

有竞争的情况在redis端的lua脚本是相同的,只是不同的条件执行不同的redis命令,复杂的在redisson的源码上。当通过tryAcquire发现锁被其它线程申请时,需要进入等待竞争逻辑中。

  • this.await返回false,说明等待时间已经超出获取锁最大等待时间,取消订阅并返回获取锁失败

  • this.await返回true,进入循环尝试获取锁。

public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException {
    long time = unit.toMillis(waitTime);
    long current = System.currentTimeMillis();
    final long threadId = Thread.currentThread().getId();
    Long ttl = this.tryAcquire(leaseTime, unit);
    if(ttl == null) {
      return true;
    } else {
      //重点是这段
      time -= System.currentTimeMillis() - current;
      if(time <= 0L) {
        return false;
      } else {
        current = System.currentTimeMillis();
        final RFuture subscribeFuture = this.subscribe(threadId);
        if(!this.await(subscribeFuture, time, TimeUnit.MILLISECONDS)) {
          if(!subscribeFuture.cancel(false)) {
            subscribeFuture.addListener(new FutureListener() {
              public void operationComplete(Future<RedissonLockEntry> future) throws Exception {
                if(subscribeFuture.isSuccess()) {
                  RedissonLock.this.unsubscribe(subscribeFuture, threadId);
                }
              }
            });
          }
          return false;
        } else {
          boolean var16;
          try {
            time -= System.currentTimeMillis() - current;
            if(time <= 0L) {
              boolean currentTime1 = false;
              return currentTime1;
            }
            do {
              long currentTime = System.currentTimeMillis();
              ttl = this.tryAcquire(leaseTime, unit);
              if(ttl == null) {
                var16 = true;
                return var16;
              }
              time -= System.currentTimeMillis() - currentTime;
              if(time <= 0L) {
                var16 = false;
                return var16;
              }
              currentTime = System.currentTimeMillis();
              if(ttl.longValue() >= 0L && ttl.longValue() < time) {
                this.getEntry(threadId).getLatch().tryAcquire(ttl.longValue(), TimeUnit.MILLISECONDS);
              } else {
                this.getEntry(threadId).getLatch().tryAcquire(time, TimeUnit.MILLISECONDS);
              }
              time -= System.currentTimeMillis() - currentTime;
            } while(time > 0L);
            var16 = false;
          } finally {
            this.unsubscribe(subscribeFuture, threadId);
          }
          return var16;
        }
      }
    }
  }
Copy after login

循环尝试一般有如下几种方法:

  • while循环,一次接着一次的尝试,这个方法的缺点是会造成大量无效的锁申请。

  • Thread.sleep,在上面的while方案中增加睡眠时间以降低锁申请次数,缺点是这个睡眠的时间设置比较难控制。

  • 基于信息量,当锁被其它资源占用时,当前线程订阅锁的释放事件,一旦锁释放会发消息通知待等待的锁进行竞争,有效的解决了无效的锁申请情况。核心逻辑是this.getEntry(threadId).getLatch().tryAcquire,this.getEntry(threadId).getLatch()返回的是一个信号量,有兴趣可以再研究研究。

redisson依赖

由于redisson不光是针对锁,提供了很多客户端操作redis的方法,所以会依赖一些其它的框架,比如netty,如果只是简单的使用锁也可以自己去实现。

 以上就是redisson实现分布式锁原理详解的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Distributed lock: 5 cases, from entry to burial Distributed lock: 5 cases, from entry to burial Aug 24, 2023 pm 02:48 PM

What I want to share with you today is distributed locks. This article uses five cases, diagrams, source code analysis, etc. to analyze. Common locks such as synchronized and Lock are all implemented based on a single JVM. What should we do in a distributed scenario? At this time, distributed locks appeared.

How SpringBoot integrates Redisson to implement delay queue How SpringBoot integrates Redisson to implement delay queue May 30, 2023 pm 02:40 PM

Usage scenario 1. The order was placed successfully but the payment was not made within 30 minutes. The payment timed out and the order was automatically canceled. 2. The order was signed and no evaluation was conducted for 7 days after signing. If the order times out and is not evaluated, the system defaults to a positive rating. 3. The order is placed successfully. If the merchant does not receive the order for 5 minutes, the order is cancelled. 4. The delivery times out, and push SMS reminder... For scenarios with long delays and low real-time performance, we can Use task scheduling to perform regular polling processing. For example: xxl-job Today we will pick

Using ZooKeeper for distributed lock processing in Java API development Using ZooKeeper for distributed lock processing in Java API development Jun 17, 2023 pm 10:36 PM

As modern applications continue to evolve and the need for high availability and concurrency grows, distributed system architectures are becoming more common. In a distributed system, multiple processes or nodes run at the same time and complete tasks together, and synchronization between processes becomes particularly important. Since many nodes in a distributed environment can access shared resources at the same time, how to deal with concurrency and synchronization issues has become an important task in a distributed system. In this regard, ZooKeeper has become a very popular solution. ZooKee

Comparison of Etcd in Redis implementation of distributed locks Comparison of Etcd in Redis implementation of distributed locks Jun 20, 2023 pm 05:51 PM

With the gradual popularization of distributed systems, distributed locks have become an important means to ensure system stability and data consistency. As a high-performance distributed memory database, Redis has naturally become one of the important implementations of distributed locks. However, in recent years, Etcd has received more and more attention as an emerging distributed consistency solution. This article will discuss the similarities and differences between Redis' implementation of distributed locks and Etcd from aspects such as implementation principles and comparative analysis. The principle of Redis implementing distributed locks The implementation of Redis distributed locks

Java Redis Redisson configuration example analysis Java Redis Redisson configuration example analysis Apr 25, 2023 am 08:19 AM

需要的Mavenorg.springframework.bootspring-boot-starter-data-redisio.lettucelettuce-coreredis.clientsjedisorg.springframework.sessionspring-session-data-redisorg.redissonredisson3.17.5application-redis.ymlspring:redis:host:106.12.174.220port:6379password

The king solution among distributed locks - Redisson The king solution among distributed locks - Redisson Aug 24, 2023 pm 03:31 PM

If you have been using Redis before, you will get twice the result with half the effort by using Redisson. Redisson provides the simplest and most convenient way to use Redis. The purpose of Redisson is to promote users' separation of concerns (Separation of Concern) from Redis, so that users can focus more on processing business logic.

Learn about Redisson caching technology Learn about Redisson caching technology Jun 21, 2023 am 09:54 AM

Redisson is a Redis-based caching solution for Java applications. It provides many useful features that make using Redis as a cache in Java applications more convenient and efficient. The caching functions provided by Redisson include: 1. Distributed mapping (Map): Redisson provides some APIs for creating distributed maps. These maps can contain key-value pairs, hash entries, or objects, and they can support sharing among multiple nodes.

Using Redis to implement distributed locks in PHP Using Redis to implement distributed locks in PHP May 15, 2023 pm 03:51 PM

With the rapid development of the Internet and the sharp increase in website visits, the importance of distributed systems has gradually become prominent. In distributed systems, issues of concurrency synchronization and data consistency are inevitably involved. Distributed locks, as a means of solving concurrency synchronization problems, have gradually been widely used in distributed systems. In PHP, Redis can be used to implement distributed locks, which this article will introduce. What is a distributed lock? In a distributed system, when multiple machines process the same task together, in order to avoid the occurrence of multiple machines

See all articles