-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add examples module, update doc and publish 0.0.7-GA
- Loading branch information
Showing
48 changed files
with
500 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,121 @@ | ||
# DistributedLock | ||
# Halo-DistributedLock | ||
|
||
![Build](https://img.shields.io/github/actions/workflow/status/hellooo-stack/hellooo-distributedlock/maven.yml) | ||
![Code Size](https://img.shields.io/github/languages/code-size/hellooo-stack/hellooo-distributedlock) | ||
![Maven Central](https://img.shields.io/maven-central/v/site.hellooo/hellooo-distributedlock) | ||
![GitHub license](https://img.shields.io/github/license/hellooo-stack/hellooo-distributedlock) | ||
|
||
DistributedLock is a lightweight distributed lock framework that provides reliable consistency features. It can be used with only the Lock interface. | ||
![Build](https://img.shields.io/github/actions/workflow/status/hellooo-stack/halo-distributedlock/maven.yml) | ||
![Code Size](https://img.shields.io/github/languages/code-size/hellooo-stack/halo-distributedlock) | ||
![Maven Central](https://img.shields.io/maven-central/v/site.hellooo/halo-distributedlock) | ||
![GitHub license](https://img.shields.io/github/license/hellooo-stack/halo-distributedlock) | ||
|
||
halo-distributedlock is a simple and reliable distributed lock implementation. | ||
It is designed to help you learn the principles of distributed locking | ||
and provides a lightweight solution for your production environment (assuming you are using single instance Redis). | ||
Whether you're a beginner or an experienced developer, It's worth to take a look at. | ||
|
||
# Features | ||
- Reentrant distributed locking | ||
- Supports tryLock(), lock(), unlock() operations | ||
- Supports lock leasing | ||
- Supports distributed lock, distributed unlock operations with a singleton Redis. | ||
- Supports lock leasing, lock blocking and lock reentrant. | ||
|
||
# Quick Start | ||
Step one: Add maven dependency | ||
```xml | ||
<dependency> | ||
<groupId>site.hellooo</groupId> | ||
<artifactId>hellooo-distributedlock</artifactId> | ||
<version>>${hellooo-distributedlock.version}</version> | ||
<artifactId>halo-distributedlock-core</artifactId> | ||
<version>>${halo-distributedlock.version}</version> | ||
</dependency> | ||
``` | ||
|
||
Step two: lock your resources with Lock.lock() | ||
```java | ||
public class Main { | ||
public class SingleProcessMultiThreadContention { | ||
public static void main(String[] args) { | ||
LockOptions lockOptions = LockOptions.options() | ||
.build(); | ||
lockCompetition(); | ||
} | ||
|
||
public static void lockCompetition() { | ||
long start = System.currentTimeMillis(); | ||
|
||
ConfigReader.RedisConfig redisConfig = ConfigReader.redis(); | ||
String host = redisConfig.getHost(); | ||
int port = redisConfig.getPort(); | ||
|
||
// define the redis source | ||
JedisPool pool = new JedisPool("localhost", 6379); | ||
for (int i = 0; i < 10; i++) { | ||
JedisPool jedisPool = new JedisPool(host, port); | ||
for (int i = 0; i < 500; i++) { | ||
final int threadNumber = i; | ||
Thread thread = new Thread(() -> { | ||
Thread.currentThread().setName("Thread " + threadNumber); | ||
|
||
try (Jedis jedis = pool.getResource()) { | ||
try { | ||
Lock lock = new ReentrantDistributedLock(lockOptions, "my_lock", new RedisLockHandler(jedis)); | ||
// lock | ||
lock.lock(); | ||
System.out.println("thread" + Thread.currentThread().getName() + " locked!"); | ||
Thread.sleep(1000); | ||
System.out.println("thread" + Thread.currentThread().getName() + " lock released!"); | ||
// unlock | ||
lock.unlock(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
Thread.currentThread().setName("locking_thread_" + threadNumber); | ||
|
||
Lock lock = new ReentrantDistributedLockBuilder() | ||
.lockOptions(LockOptions.ofDefault()) | ||
.jedisPool(jedisPool) | ||
.lockTarget("my_lock") | ||
.build(); | ||
|
||
System.out.println("process [" + ProcessUtils.getProcessId() + "] thread [" + Thread.currentThread().getName() + "] is getting lock"); | ||
lock.lock(); | ||
System.out.println("process [" + ProcessUtils.getProcessId() + "] thread [" + Thread.currentThread().getName() + "] got lock"); | ||
lock.unlock(); | ||
System.out.println("process [" + ProcessUtils.getProcessId() + "] thread [" + Thread.currentThread().getName() + "] released lock"); | ||
}); | ||
thread.start(); | ||
} | ||
|
||
Runtime.getRuntime().addShutdownHook(new Thread(() -> { | ||
jedisPool.close(); | ||
System.out.println("cause: " + (System.currentTimeMillis() - start) + "ms"); | ||
})); | ||
} | ||
} | ||
``` | ||
|
||
# Examples | ||
You can find some examples in the [examples](https://github.com/hellooo-stack/halo-distributedlock/tree/master/examples) module. | ||
If you want to simulate multiple-process, multiple-thread competition, you should run multiprocess/Step0, | ||
and then run multiprocess/Step1 within a 5-second window. You will see the result like this: | ||
``` | ||
# process1: | ||
# process [99991] thread [locking_thread_0] is getting lock | ||
# process [99991] thread [locking_thread_8] is getting lock | ||
# process [99991] thread [locking_thread_7] is getting lock | ||
# process [99991] thread [locking_thread_6] is getting lock | ||
# process [99991] thread [locking_thread_2] is getting lock | ||
# process [99991] thread [locking_thread_5] is getting lock | ||
# process [99991] thread [locking_thread_4] is getting lock | ||
# process [99991] thread [locking_thread_3] is getting lock | ||
# process [99991] thread [locking_thread_9] is getting lock | ||
# process [99991] thread [locking_thread_1] is getting lock | ||
# process [99991] thread [locking_thread_8] got lock | ||
# process [99991] thread [locking_thread_8] released lock | ||
# process [99991] thread [locking_thread_7] got lock | ||
# process [99991] thread [locking_thread_7] released lock | ||
# process [99991] thread [locking_thread_3] got lock | ||
# process [99991] thread [locking_thread_3] released lock | ||
# process [99991] thread [locking_thread_0] got lock | ||
# process [99991] thread [locking_thread_0] released lock | ||
# process [99991] thread [locking_thread_9] got lock | ||
# process [99991] thread [locking_thread_9] released lock | ||
# process [99991] thread [locking_thread_2] got lock | ||
# process [99991] thread [locking_thread_2] released lock | ||
# ... | ||
# process2: | ||
# process [96469] thread [locking_thread_498] is getting lock | ||
# process [96469] thread [locking_thread_499] is getting lock | ||
# process [96469] thread [locking_thread_152] got lock | ||
# process [96469] thread [locking_thread_152] released lock | ||
# process [96469] thread [locking_thread_139] got lock | ||
# process [96469] thread [locking_thread_139] released lock | ||
# process [96469] thread [locking_thread_417] got lock | ||
# process [96469] thread [locking_thread_417] released lock | ||
# process [96469] thread [locking_thread_213] got lock | ||
# process [96469] thread [locking_thread_213] released lock | ||
# process [96469] thread [locking_thread_458] got lock | ||
# process [96469] thread [locking_thread_458] released lock | ||
# process [96469] thread [locking_thread_124] got lock | ||
# process [96469] thread [locking_thread_124] released lock | ||
# process [96469] thread [locking_thread_204] got lock | ||
# process [96469] thread [locking_thread_204] released lock | ||
# ... | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>site.hellooo</groupId> | ||
<artifactId>halo-distributedlock</artifactId> | ||
<version>${revision}</version> | ||
</parent> | ||
|
||
<artifactId>halo-distributedlock-core</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>redis.clients</groupId> | ||
<artifactId>jedis</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
13 changes: 13 additions & 0 deletions
13
core/src/main/java/site/hellooo/distributedlock/core/DistributedLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package site.hellooo.distributedlock.core; | ||
|
||
import site.hellooo.distributedlock.core.enums.Coordinator; | ||
import site.hellooo.distributedlock.core.enums.LockType; | ||
|
||
import java.util.concurrent.locks.Lock; | ||
|
||
public interface DistributedLock extends Lock { | ||
|
||
LockType lockType(); | ||
|
||
Coordinator coordinatorType(); | ||
} |
2 changes: 1 addition & 1 deletion
2
...hellooo/distributedlock/LockCallback.java → ...oo/distributedlock/core/LockCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
.../hellooo/distributedlock/LockContext.java → ...ooo/distributedlock/core/LockContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
.../hellooo/distributedlock/LockHandler.java → ...ooo/distributedlock/core/LockHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...te/hellooo/distributedlock/LockState.java → ...llooo/distributedlock/core/LockState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package site.hellooo.distributedlock; | ||
package site.hellooo.distributedlock.core; | ||
|
||
import java.io.Serializable; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...ite/hellooo/distributedlock/Reusable.java → ...ellooo/distributedlock/core/Reusable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...oo/distributedlock/common/ArgChecker.java → ...stributedlock/core/common/ArgChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...oo/distributedlock/common/ClassUtils.java → ...stributedlock/core/common/ClassUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../distributedlock/common/NetworkUtils.java → ...ributedlock/core/common/NetworkUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../distributedlock/common/ProcessUtils.java → ...ributedlock/core/common/ProcessUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...o/distributedlock/common/StringUtils.java → ...tributedlock/core/common/StringUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...o/distributedlock/config/LockOptions.java → ...tributedlock/core/config/LockOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...oo/distributedlock/enums/Coordinator.java → ...stributedlock/core/enums/Coordinator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...llooo/distributedlock/enums/LockType.java → .../distributedlock/core/enums/LockType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...BuilderEssentialFieldNotSetException.java → ...BuilderEssentialFieldNotSetException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...xception/GenericRuntimeLockException.java → ...xception/GenericRuntimeLockException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ception/LockStateNotRemovedException.java → ...ception/LockStateNotRemovedException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...k/exception/LockStateNotSetException.java → ...e/exception/LockStateNotSetException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...tion/LockStateRemoveExpiredException.java → ...tion/LockStateRemoveExpiredException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ception/LockStateSetExpiredException.java → ...ception/LockStateSetExpiredException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...tedlock/impl/AbstractDistributedLock.java → ...ck/core/impl/AbstractDistributedLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
...ributedlock/impl/LockCallbackFactory.java → ...edlock/core/impl/LockCallbackFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 7 additions & 7 deletions
14
...istributedlock/impl/LockStateBuilder.java → ...butedlock/core/impl/LockStateBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.