Skip to content

Commit 2bcd5b2

Browse files
committed
docs: add examples module, update doc and publish 0.0.7-GA
1 parent 0ee512d commit 2bcd5b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+500
-183
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ Hi there! We're thrilled that you'd like to contribute to this project. Your hel
44

55
## Submitting a pull request
66

7-
1. [Fork][fork] and clone the repository
7+
1. Fork and clone the repository
88
2. Create a new branch: `git checkout -b my-branch-name`
99
3. Make your change and remember to add tests
1010
4. Build the project locally and run local tests
11-
5. Push to your fork and [submit a pull request][pr]
11+
5. Push to your fork and submit a pull request
1212
6. Pat your self on the back and wait for your pull request to be reviewed and merged.
1313

1414
Thanks for your contributing!

README.md

Lines changed: 95 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,121 @@
1-
# DistributedLock
1+
# Halo-DistributedLock
22

3-
![Build](https://img.shields.io/github/actions/workflow/status/hellooo-stack/hellooo-distributedlock/maven.yml)
4-
![Code Size](https://img.shields.io/github/languages/code-size/hellooo-stack/hellooo-distributedlock)
5-
![Maven Central](https://img.shields.io/maven-central/v/site.hellooo/hellooo-distributedlock)
6-
![GitHub license](https://img.shields.io/github/license/hellooo-stack/hellooo-distributedlock)
7-
8-
DistributedLock is a lightweight distributed lock framework that provides reliable consistency features. It can be used with only the Lock interface.
3+
![Build](https://img.shields.io/github/actions/workflow/status/hellooo-stack/halo-distributedlock/maven.yml)
4+
![Code Size](https://img.shields.io/github/languages/code-size/hellooo-stack/halo-distributedlock)
5+
![Maven Central](https://img.shields.io/maven-central/v/site.hellooo/halo-distributedlock)
6+
![GitHub license](https://img.shields.io/github/license/hellooo-stack/halo-distributedlock)
97

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.
1012

1113
# 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.
1516

1617
# Quick Start
1718
Step one: Add maven dependency
1819
```xml
1920
<dependency>
2021
<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>
2324
</dependency>
2425
```
2526

2627
Step two: lock your resources with Lock.lock()
2728
```java
28-
public class Main {
29+
public class SingleProcessMultiThreadContention {
2930
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();
3240

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++) {
3643
final int threadNumber = i;
3744
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");
5458
});
5559
thread.start();
5660
}
61+
62+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
63+
jedisPool.close();
64+
System.out.println("cause: " + (System.currentTimeMillis() - start) + "ms");
65+
}));
5766
}
5867
}
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+
# ...
5999
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+
# ...
60119
```
120+
121+

core/pom.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>site.hellooo</groupId>
8+
<artifactId>halo-distributedlock</artifactId>
9+
<version>${revision}</version>
10+
</parent>
11+
12+
<artifactId>halo-distributedlock-core</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>redis.clients</groupId>
17+
<artifactId>jedis</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.assertj</groupId>
25+
<artifactId>assertj-core</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.mockito</groupId>
29+
<artifactId>mockito-core</artifactId>
30+
</dependency>
31+
</dependencies>
32+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package site.hellooo.distributedlock.core;
2+
3+
import site.hellooo.distributedlock.core.enums.Coordinator;
4+
import site.hellooo.distributedlock.core.enums.LockType;
5+
6+
import java.util.concurrent.locks.Lock;
7+
8+
public interface DistributedLock extends Lock {
9+
10+
LockType lockType();
11+
12+
Coordinator coordinatorType();
13+
}

src/main/java/site/hellooo/distributedlock/LockCallback.java renamed to core/src/main/java/site/hellooo/distributedlock/core/LockCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package site.hellooo.distributedlock;
1+
package site.hellooo.distributedlock.core;
22

33
public interface LockCallback {
44
// execute immediately after the lock granted to current thread

src/main/java/site/hellooo/distributedlock/LockContext.java renamed to core/src/main/java/site/hellooo/distributedlock/core/LockContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package site.hellooo.distributedlock;
1+
package site.hellooo.distributedlock.core;
22

3-
import site.hellooo.distributedlock.config.LockOptions;
3+
import site.hellooo.distributedlock.core.config.LockOptions;
44

55
import java.util.concurrent.atomic.AtomicReference;
66

src/main/java/site/hellooo/distributedlock/LockHandler.java renamed to core/src/main/java/site/hellooo/distributedlock/core/LockHandler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package site.hellooo.distributedlock;
1+
package site.hellooo.distributedlock.core;
22

3-
import site.hellooo.distributedlock.enums.Coordinator;
4-
import site.hellooo.distributedlock.exception.LockStateNotRemovedException;
5-
import site.hellooo.distributedlock.exception.LockStateNotSetException;
6-
import site.hellooo.distributedlock.exception.LockStateRemoveExpiredException;
7-
import site.hellooo.distributedlock.exception.LockStateSetExpiredException;
3+
import site.hellooo.distributedlock.core.enums.Coordinator;
4+
import site.hellooo.distributedlock.core.exception.LockStateNotRemovedException;
5+
import site.hellooo.distributedlock.core.exception.LockStateNotSetException;
6+
import site.hellooo.distributedlock.core.exception.LockStateRemoveExpiredException;
7+
import site.hellooo.distributedlock.core.exception.LockStateSetExpiredException;
88

99
public interface LockHandler {
1010

src/main/java/site/hellooo/distributedlock/LockState.java renamed to core/src/main/java/site/hellooo/distributedlock/core/LockState.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package site.hellooo.distributedlock;
1+
package site.hellooo.distributedlock.core;
22

33
import java.io.Serializable;
44

src/main/java/site/hellooo/distributedlock/Reusable.java renamed to core/src/main/java/site/hellooo/distributedlock/core/Reusable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package site.hellooo.distributedlock;
1+
package site.hellooo.distributedlock.core;
22

33
public interface Reusable<T> {
44
T copy();

src/main/java/site/hellooo/distributedlock/common/ArgChecker.java renamed to core/src/main/java/site/hellooo/distributedlock/core/common/ArgChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package site.hellooo.distributedlock.common;
1+
package site.hellooo.distributedlock.core.common;
22

33
public final class ArgChecker {
44
public static void check(boolean predicate) {

0 commit comments

Comments
 (0)