In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to understand the scenario of distributed locks". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the scene of distributed locks.
A case of a second kill scene
For the second-kill scenario of goods, we need to prevent concurrency problems such as inventory oversold or repeated deductions. We usually need to use distributed locks to solve the problem of data inconsistency caused by competition for shared resources.
Taking the second kill scenario of a mobile phone as an example, we usually have three steps in the process of panic buying:
Deduct the inventory of the corresponding goods; 2. Create orders for goods; 3. The user pays.
For such a scenario, we can use distributed locking to solve the problem. For example, when the user enters the "placing an order" link, we can lock the inventory of goods, and then complete the deduction of inventory and other operations. After the operation is complete. Release the lock and let the next user continue to enter the inventory to ensure the security of the inventory; it can also reduce the number of DB rollbacks due to second kill failure. The whole process is shown in the following figure:
Note: the granularity of locks should be weighed according to specific scenarios and requirements.
Three kinds of distributed locks
For the distributed lock implementation of Zookeeper, it is mainly realized by using two characteristics of Zookeeper:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
A node of Zookeeper cannot be created repeatedly
Watcher snooping Mechanism of Zookeeper
Unfair lock
For unfair locks, the process of adding locks is shown in the following figure.
Advantages and disadvantages
In fact, the above implementation has both advantages and disadvantages:
Advantages:
The implementation is relatively simple, there is a notification mechanism, which can provide faster response, which is similar to the idea of ReentrantLock. For scenarios where node deletion fails, Session timeout ensures that the node can be deleted.
Disadvantages:
Heavyweight, at the same time, there will be "surprise" problems in the case of a large number of locks.
"shock group" means that when a node is deleted, a large number of threads subscribing to Watcher will call back the deletion action of this node, which is very disadvantageous to the Zk cluster. Therefore, it is necessary to avoid this phenomenon.
Solve the "surprise group":
In order to solve the "shock group" problem, we need to give up the strategy of subscribing to a node, so what do we do?
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
We abstract the lock into a directory, under which multiple threads create instantaneous sequential nodes. Because Zookeeper will guarantee the sequence of nodes for us, we can use the order of nodes to judge locks.
First, create a sequential node, and then get the smallest node in the current directory to determine whether the smallest node is the current node. If so, acquire the lock successfully, if not, fail to acquire the lock.
The node that failed to acquire the lock gets a sequential node on the current node, registers listening to this node, and notifies the current node when the node is deleted.
The next node is notified after the node is deleted when unlock.
Fair lock
Based on the disadvantage of unfair lock, we can avoid it through the following scheme.
Advantages and disadvantages
Advantages: with the help of temporary sequential nodes, the concurrent competitive locks of multiple nodes can be avoided and the pressure on the server side can be alleviated.
Disadvantages: for read-write scenarios, the consistency problem cannot be solved. If you also acquire locks while reading, this will lead to performance degradation. For such problems, we can implement read-write locks, such as ReadWriteLock in jdk.
Read-write lock implementation
For the characteristics of read-write lock: read-write lock can be read concurrently if multiple threads are reading, it is a lock-free state, if there is a write lock in operation, then read-write lock needs to wait for write lock. When adding a write lock, because the previous read locks are all concurrent, you need to listen to the execution of the write lock after the last read lock is completed. The steps are as follows:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Read request, if the previous read lock, can be read directly, do not need to listen. If the front is one or more write locks, then only the last write lock needs to be monitored.
Write request, only need to listen to the previous node. The Watcher mechanism is the same as mutexes.
Distributed lock actual combat
The environment used in the source code of this article: JDK 1.8, Zookeeper 3.6.x
Curator component implements the application of POM dependent org.apache.curator curator-framework 2.13.0 org.apache.curator curator-recipes 2.13.0 mutex
Because of the "shock" effect of Zookeeper unfair lock, unfair lock is not the best choice in Zookeeper. The following is an example of simulating a second kill to use Zookeeper distributed locks.
Public class MutexTest {static ExecutorService executor = Executors.newFixedThreadPool (8); static AtomicInteger stock = new AtomicInteger (3); public static void main (String [] args) throws InterruptedException {CuratorFramework client = getZkClient (); String key = "/ lock/lockId_111/111"; final InterProcessMutex mutex = new InterProcessMutex (client, key); for (int I = 0; I
< 99; i++) { executor.submit(() ->{if (stock.get ())
< 0) { System.err.println("库存不足, 直接返回"); return; } try { boolean acquire = mutex.acquire(200, TimeUnit.MILLISECONDS); if (acquire) { int s = stock.decrementAndGet(); if (s < 0) { System.err.println("进入秒杀,库存不足"); } else { System.out.println("购买成功, 剩余库存: " + s); } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (mutex.isAcquiredInThisProcess()) mutex.release(); } catch (Exception e) { e.printStackTrace(); } } }); } while (true) { if (executor.isTerminated()) { executor.shutdown(); System.out.println("秒杀完毕剩余库存为:" + stock.get()); } TimeUnit.MILLISECONDS.sleep(100); } } private static CuratorFramework getZkClient() { String zkServerAddress = "127.0.0.1:2181"; ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3, 5000); CuratorFramework zkClient = CuratorFrameworkFactory.builder() .connectString(zkServerAddress) .sessionTimeoutMs(5000) .connectionTimeoutMs(5000) .retryPolicy(retryPolicy) .build(); zkClient.start(); return zkClient; } }读写锁运用 读写锁可以用来保证缓存双写的强一致性的,因为读写锁在多线程读的时候是无锁的, 只有在前面有写锁的时候才会等待写锁完成后访问数据。 public class ReadWriteLockTest { static ExecutorService executor = Executors.newFixedThreadPool(8); static AtomicInteger stock = new AtomicInteger(3); static InterProcessMutex readLock; static InterProcessMutex writeLock; public static void main(String[] args) throws InterruptedException { CuratorFramework client = getZkClient(); String key = "/lock/lockId_111/1111"; InterProcessReadWriteLock readWriteLock = new InterProcessReadWriteLock(client, key); readLock = readWriteLock.readLock(); writeLock = readWriteLock.writeLock(); for (int i = 0; i < 16; i++) { executor.submit(() ->{try {boolean read = readLock.acquire (2000, TimeUnit.MILLISECONDS); if (read) {int num = stock.get (); System.out.println ("read inventory, current inventory is:" + num) If (num < 0) {System.err.println ("insufficient inventory, return directly"); return;} catch (Exception e) {e.printStackTrace () } finally {if (readLock.isAcquiredInThisProcess ()) {try {readLock.release ();} catch (Exception e) {e.printStackTrace () } try {boolean acquire = writeLock.acquire (2000, TimeUnit.MILLISECONDS); if (acquire) {int s = stock.get (); if (s)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.