|
1 | | -# DistributedLock |
| 1 | +# Halo-DistributedLock |
2 | 2 |
|
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | -DistributedLock is a lightweight distributed lock framework that provides reliable consistency features. It can be used with only the Lock interface. |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
9 | 7 |
|
| 8 | +halo-distributedlock is a simple and reliable distributed lock implementation. |
| 9 | +It is designed to help you learn the principles of distributed locking |
| 10 | +and provides a lightweight solution for your production environment (assuming you are using single instance Redis). |
| 11 | +Whether you're a beginner or an experienced developer, It's worth to take a look at. |
10 | 12 |
|
11 | 13 | # Features |
12 | | -- Reentrant distributed locking |
13 | | -- Supports tryLock(), lock(), unlock() operations |
14 | | -- Supports lock leasing |
| 14 | +- Supports distributed lock, distributed unlock operations with a singleton Redis. |
| 15 | +- Supports lock leasing, lock blocking and lock reentrant. |
15 | 16 |
|
16 | 17 | # Quick Start |
17 | 18 | Step one: Add maven dependency |
18 | 19 | ```xml |
19 | 20 | <dependency> |
20 | 21 | <groupId>site.hellooo</groupId> |
21 | | - <artifactId>hellooo-distributedlock</artifactId> |
22 | | - <version>>${hellooo-distributedlock.version}</version> |
| 22 | + <artifactId>halo-distributedlock-core</artifactId> |
| 23 | + <version>>${halo-distributedlock.version}</version> |
23 | 24 | </dependency> |
24 | 25 | ``` |
25 | 26 |
|
26 | 27 | Step two: lock your resources with Lock.lock() |
27 | 28 | ```java |
28 | | -public class Main { |
| 29 | +public class SingleProcessMultiThreadContention { |
29 | 30 | public static void main(String[] args) { |
30 | | - LockOptions lockOptions = LockOptions.options() |
31 | | - .build(); |
| 31 | + lockCompetition(); |
| 32 | + } |
| 33 | + |
| 34 | + public static void lockCompetition() { |
| 35 | + long start = System.currentTimeMillis(); |
| 36 | + |
| 37 | + ConfigReader.RedisConfig redisConfig = ConfigReader.redis(); |
| 38 | + String host = redisConfig.getHost(); |
| 39 | + int port = redisConfig.getPort(); |
32 | 40 |
|
33 | | -// define the redis source |
34 | | - JedisPool pool = new JedisPool("localhost", 6379); |
35 | | - for (int i = 0; i < 10; i++) { |
| 41 | + JedisPool jedisPool = new JedisPool(host, port); |
| 42 | + for (int i = 0; i < 500; i++) { |
36 | 43 | final int threadNumber = i; |
37 | 44 | Thread thread = new Thread(() -> { |
38 | | - Thread.currentThread().setName("Thread " + threadNumber); |
39 | | - |
40 | | - try (Jedis jedis = pool.getResource()) { |
41 | | - try { |
42 | | - Lock lock = new ReentrantDistributedLock(lockOptions, "my_lock", new RedisLockHandler(jedis)); |
43 | | -// lock |
44 | | - lock.lock(); |
45 | | - System.out.println("thread" + Thread.currentThread().getName() + " locked!"); |
46 | | - Thread.sleep(1000); |
47 | | - System.out.println("thread" + Thread.currentThread().getName() + " lock released!"); |
48 | | -// unlock |
49 | | - lock.unlock(); |
50 | | - } catch (Exception e) { |
51 | | - e.printStackTrace(); |
52 | | - } |
53 | | - } |
| 45 | + Thread.currentThread().setName("locking_thread_" + threadNumber); |
| 46 | + |
| 47 | + Lock lock = new ReentrantDistributedLockBuilder() |
| 48 | + .lockOptions(LockOptions.ofDefault()) |
| 49 | + .jedisPool(jedisPool) |
| 50 | + .lockTarget("my_lock") |
| 51 | + .build(); |
| 52 | + |
| 53 | + System.out.println("process [" + ProcessUtils.getProcessId() + "] thread [" + Thread.currentThread().getName() + "] is getting lock"); |
| 54 | + lock.lock(); |
| 55 | + System.out.println("process [" + ProcessUtils.getProcessId() + "] thread [" + Thread.currentThread().getName() + "] got lock"); |
| 56 | + lock.unlock(); |
| 57 | + System.out.println("process [" + ProcessUtils.getProcessId() + "] thread [" + Thread.currentThread().getName() + "] released lock"); |
54 | 58 | }); |
55 | 59 | thread.start(); |
56 | 60 | } |
| 61 | + |
| 62 | + Runtime.getRuntime().addShutdownHook(new Thread(() -> { |
| 63 | + jedisPool.close(); |
| 64 | + System.out.println("cause: " + (System.currentTimeMillis() - start) + "ms"); |
| 65 | + })); |
57 | 66 | } |
58 | 67 | } |
| 68 | +``` |
| 69 | + |
| 70 | +# Examples |
| 71 | +You can find some examples in the [examples](https://github.com/hellooo-stack/halo-distributedlock/tree/master/examples) module. |
| 72 | +If you want to simulate multiple-process, multiple-thread competition, you should run multiprocess/Step0, |
| 73 | +and then run multiprocess/Step1 within a 5-second window. You will see the result like this: |
| 74 | +``` |
| 75 | +# process1: |
| 76 | +# process [99991] thread [locking_thread_0] is getting lock |
| 77 | +# process [99991] thread [locking_thread_8] is getting lock |
| 78 | +# process [99991] thread [locking_thread_7] is getting lock |
| 79 | +# process [99991] thread [locking_thread_6] is getting lock |
| 80 | +# process [99991] thread [locking_thread_2] is getting lock |
| 81 | +# process [99991] thread [locking_thread_5] is getting lock |
| 82 | +# process [99991] thread [locking_thread_4] is getting lock |
| 83 | +# process [99991] thread [locking_thread_3] is getting lock |
| 84 | +# process [99991] thread [locking_thread_9] is getting lock |
| 85 | +# process [99991] thread [locking_thread_1] is getting lock |
| 86 | +# process [99991] thread [locking_thread_8] got lock |
| 87 | +# process [99991] thread [locking_thread_8] released lock |
| 88 | +# process [99991] thread [locking_thread_7] got lock |
| 89 | +# process [99991] thread [locking_thread_7] released lock |
| 90 | +# process [99991] thread [locking_thread_3] got lock |
| 91 | +# process [99991] thread [locking_thread_3] released lock |
| 92 | +# process [99991] thread [locking_thread_0] got lock |
| 93 | +# process [99991] thread [locking_thread_0] released lock |
| 94 | +# process [99991] thread [locking_thread_9] got lock |
| 95 | +# process [99991] thread [locking_thread_9] released lock |
| 96 | +# process [99991] thread [locking_thread_2] got lock |
| 97 | +# process [99991] thread [locking_thread_2] released lock |
| 98 | +# ... |
59 | 99 |
|
| 100 | +
|
| 101 | +# process2: |
| 102 | +# process [96469] thread [locking_thread_498] is getting lock |
| 103 | +# process [96469] thread [locking_thread_499] is getting lock |
| 104 | +# process [96469] thread [locking_thread_152] got lock |
| 105 | +# process [96469] thread [locking_thread_152] released lock |
| 106 | +# process [96469] thread [locking_thread_139] got lock |
| 107 | +# process [96469] thread [locking_thread_139] released lock |
| 108 | +# process [96469] thread [locking_thread_417] got lock |
| 109 | +# process [96469] thread [locking_thread_417] released lock |
| 110 | +# process [96469] thread [locking_thread_213] got lock |
| 111 | +# process [96469] thread [locking_thread_213] released lock |
| 112 | +# process [96469] thread [locking_thread_458] got lock |
| 113 | +# process [96469] thread [locking_thread_458] released lock |
| 114 | +# process [96469] thread [locking_thread_124] got lock |
| 115 | +# process [96469] thread [locking_thread_124] released lock |
| 116 | +# process [96469] thread [locking_thread_204] got lock |
| 117 | +# process [96469] thread [locking_thread_204] released lock |
| 118 | +# ... |
60 | 119 | ``` |
| 120 | + |
| 121 | + |
0 commit comments