diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5a176f7..5589a5a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,11 +4,11 @@ Hi there! We're thrilled that you'd like to contribute to this project. Your hel ## Submitting a pull request -1. [Fork][fork] and clone the repository +1. Fork and clone the repository 2. Create a new branch: `git checkout -b my-branch-name` 3. Make your change and remember to add tests 4. Build the project locally and run local tests -5. Push to your fork and [submit a pull request][pr] +5. Push to your fork and submit a pull request 6. Pat your self on the back and wait for your pull request to be reviewed and merged. Thanks for your contributing! diff --git a/README.md b/README.md index 5962787..b15c8d8 100644 --- a/README.md +++ b/README.md @@ -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 site.hellooo - hellooo-distributedlock - >${hellooo-distributedlock.version} + halo-distributedlock-core + >${halo-distributedlock.version} ``` 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 +# ... ``` + + diff --git a/core/pom.xml b/core/pom.xml new file mode 100644 index 0000000..acd90e2 --- /dev/null +++ b/core/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + site.hellooo + halo-distributedlock + ${revision} + + + halo-distributedlock-core + + + + redis.clients + jedis + + + junit + junit + + + org.assertj + assertj-core + + + org.mockito + mockito-core + + + diff --git a/core/src/main/java/site/hellooo/distributedlock/core/DistributedLock.java b/core/src/main/java/site/hellooo/distributedlock/core/DistributedLock.java new file mode 100644 index 0000000..34f77ac --- /dev/null +++ b/core/src/main/java/site/hellooo/distributedlock/core/DistributedLock.java @@ -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(); +} diff --git a/src/main/java/site/hellooo/distributedlock/LockCallback.java b/core/src/main/java/site/hellooo/distributedlock/core/LockCallback.java similarity index 89% rename from src/main/java/site/hellooo/distributedlock/LockCallback.java rename to core/src/main/java/site/hellooo/distributedlock/core/LockCallback.java index f3c3256..089aedd 100644 --- a/src/main/java/site/hellooo/distributedlock/LockCallback.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/LockCallback.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock; +package site.hellooo.distributedlock.core; public interface LockCallback { // execute immediately after the lock granted to current thread diff --git a/src/main/java/site/hellooo/distributedlock/LockContext.java b/core/src/main/java/site/hellooo/distributedlock/core/LockContext.java similarity index 86% rename from src/main/java/site/hellooo/distributedlock/LockContext.java rename to core/src/main/java/site/hellooo/distributedlock/core/LockContext.java index c990504..001f38b 100644 --- a/src/main/java/site/hellooo/distributedlock/LockContext.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/LockContext.java @@ -1,6 +1,6 @@ -package site.hellooo.distributedlock; +package site.hellooo.distributedlock.core; -import site.hellooo.distributedlock.config.LockOptions; +import site.hellooo.distributedlock.core.config.LockOptions; import java.util.concurrent.atomic.AtomicReference; diff --git a/src/main/java/site/hellooo/distributedlock/LockHandler.java b/core/src/main/java/site/hellooo/distributedlock/core/LockHandler.java similarity index 59% rename from src/main/java/site/hellooo/distributedlock/LockHandler.java rename to core/src/main/java/site/hellooo/distributedlock/core/LockHandler.java index 7e3df09..d1d632b 100644 --- a/src/main/java/site/hellooo/distributedlock/LockHandler.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/LockHandler.java @@ -1,10 +1,10 @@ -package site.hellooo.distributedlock; +package site.hellooo.distributedlock.core; -import site.hellooo.distributedlock.enums.Coordinator; -import site.hellooo.distributedlock.exception.LockStateNotRemovedException; -import site.hellooo.distributedlock.exception.LockStateNotSetException; -import site.hellooo.distributedlock.exception.LockStateRemoveExpiredException; -import site.hellooo.distributedlock.exception.LockStateSetExpiredException; +import site.hellooo.distributedlock.core.enums.Coordinator; +import site.hellooo.distributedlock.core.exception.LockStateNotRemovedException; +import site.hellooo.distributedlock.core.exception.LockStateNotSetException; +import site.hellooo.distributedlock.core.exception.LockStateRemoveExpiredException; +import site.hellooo.distributedlock.core.exception.LockStateSetExpiredException; public interface LockHandler { diff --git a/src/main/java/site/hellooo/distributedlock/LockState.java b/core/src/main/java/site/hellooo/distributedlock/core/LockState.java similarity index 89% rename from src/main/java/site/hellooo/distributedlock/LockState.java rename to core/src/main/java/site/hellooo/distributedlock/core/LockState.java index 6719bf5..3b8c45f 100644 --- a/src/main/java/site/hellooo/distributedlock/LockState.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/LockState.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock; +package site.hellooo.distributedlock.core; import java.io.Serializable; diff --git a/src/main/java/site/hellooo/distributedlock/Reusable.java b/core/src/main/java/site/hellooo/distributedlock/core/Reusable.java similarity index 52% rename from src/main/java/site/hellooo/distributedlock/Reusable.java rename to core/src/main/java/site/hellooo/distributedlock/core/Reusable.java index 249e89e..2551c22 100644 --- a/src/main/java/site/hellooo/distributedlock/Reusable.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/Reusable.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock; +package site.hellooo.distributedlock.core; public interface Reusable { T copy(); diff --git a/src/main/java/site/hellooo/distributedlock/common/ArgChecker.java b/core/src/main/java/site/hellooo/distributedlock/core/common/ArgChecker.java similarity index 94% rename from src/main/java/site/hellooo/distributedlock/common/ArgChecker.java rename to core/src/main/java/site/hellooo/distributedlock/core/common/ArgChecker.java index cbf3016..19056ca 100644 --- a/src/main/java/site/hellooo/distributedlock/common/ArgChecker.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/common/ArgChecker.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.common; +package site.hellooo.distributedlock.core.common; public final class ArgChecker { public static void check(boolean predicate) { diff --git a/src/main/java/site/hellooo/distributedlock/common/ClassUtils.java b/core/src/main/java/site/hellooo/distributedlock/core/common/ClassUtils.java similarity index 83% rename from src/main/java/site/hellooo/distributedlock/common/ClassUtils.java rename to core/src/main/java/site/hellooo/distributedlock/core/common/ClassUtils.java index b6a4406..a966892 100644 --- a/src/main/java/site/hellooo/distributedlock/common/ClassUtils.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/common/ClassUtils.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.common; +package site.hellooo.distributedlock.core.common; public class ClassUtils { public static String getObjClassName(O object) { diff --git a/src/main/java/site/hellooo/distributedlock/common/NetworkUtils.java b/core/src/main/java/site/hellooo/distributedlock/core/common/NetworkUtils.java similarity index 87% rename from src/main/java/site/hellooo/distributedlock/common/NetworkUtils.java rename to core/src/main/java/site/hellooo/distributedlock/core/common/NetworkUtils.java index 5af99ce..8a2dccc 100644 --- a/src/main/java/site/hellooo/distributedlock/common/NetworkUtils.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/common/NetworkUtils.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.common; +package site.hellooo.distributedlock.core.common; import java.net.InetAddress; import java.net.UnknownHostException; diff --git a/src/main/java/site/hellooo/distributedlock/common/ProcessUtils.java b/core/src/main/java/site/hellooo/distributedlock/core/common/ProcessUtils.java similarity index 91% rename from src/main/java/site/hellooo/distributedlock/common/ProcessUtils.java rename to core/src/main/java/site/hellooo/distributedlock/core/common/ProcessUtils.java index c897fd0..78930a3 100644 --- a/src/main/java/site/hellooo/distributedlock/common/ProcessUtils.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/common/ProcessUtils.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.common; +package site.hellooo.distributedlock.core.common; import java.lang.management.ManagementFactory; diff --git a/src/main/java/site/hellooo/distributedlock/common/StringUtils.java b/core/src/main/java/site/hellooo/distributedlock/core/common/StringUtils.java similarity index 91% rename from src/main/java/site/hellooo/distributedlock/common/StringUtils.java rename to core/src/main/java/site/hellooo/distributedlock/core/common/StringUtils.java index 8ab3a0c..6cb2620 100644 --- a/src/main/java/site/hellooo/distributedlock/common/StringUtils.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/common/StringUtils.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.common; +package site.hellooo.distributedlock.core.common; public class StringUtils { public static String empty() { diff --git a/src/main/java/site/hellooo/distributedlock/config/LockOptions.java b/core/src/main/java/site/hellooo/distributedlock/core/config/LockOptions.java similarity index 96% rename from src/main/java/site/hellooo/distributedlock/config/LockOptions.java rename to core/src/main/java/site/hellooo/distributedlock/core/config/LockOptions.java index ee5e659..5e64565 100644 --- a/src/main/java/site/hellooo/distributedlock/config/LockOptions.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/config/LockOptions.java @@ -1,8 +1,8 @@ -package site.hellooo.distributedlock.config; +package site.hellooo.distributedlock.core.config; -import site.hellooo.distributedlock.Reusable; -import site.hellooo.distributedlock.common.ArgChecker; -import site.hellooo.distributedlock.enums.Coordinator; +import site.hellooo.distributedlock.core.Reusable; +import site.hellooo.distributedlock.core.common.ArgChecker; +import site.hellooo.distributedlock.core.enums.Coordinator; import java.io.Serializable; import java.util.StringJoiner; diff --git a/src/main/java/site/hellooo/distributedlock/enums/Coordinator.java b/core/src/main/java/site/hellooo/distributedlock/core/enums/Coordinator.java similarity index 85% rename from src/main/java/site/hellooo/distributedlock/enums/Coordinator.java rename to core/src/main/java/site/hellooo/distributedlock/core/enums/Coordinator.java index 1215534..243939f 100644 --- a/src/main/java/site/hellooo/distributedlock/enums/Coordinator.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/enums/Coordinator.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.enums; +package site.hellooo.distributedlock.core.enums; public enum Coordinator { diff --git a/src/main/java/site/hellooo/distributedlock/enums/LockType.java b/core/src/main/java/site/hellooo/distributedlock/core/enums/LockType.java similarity index 81% rename from src/main/java/site/hellooo/distributedlock/enums/LockType.java rename to core/src/main/java/site/hellooo/distributedlock/core/enums/LockType.java index 0b2ffd0..e2442b5 100644 --- a/src/main/java/site/hellooo/distributedlock/enums/LockType.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/enums/LockType.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.enums; +package site.hellooo.distributedlock.core.enums; public enum LockType { diff --git a/src/main/java/site/hellooo/distributedlock/exception/BuilderEssentialFieldNotSetException.java b/core/src/main/java/site/hellooo/distributedlock/core/exception/BuilderEssentialFieldNotSetException.java similarity index 81% rename from src/main/java/site/hellooo/distributedlock/exception/BuilderEssentialFieldNotSetException.java rename to core/src/main/java/site/hellooo/distributedlock/core/exception/BuilderEssentialFieldNotSetException.java index 3e336c3..10d4c09 100644 --- a/src/main/java/site/hellooo/distributedlock/exception/BuilderEssentialFieldNotSetException.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/exception/BuilderEssentialFieldNotSetException.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.exception; +package site.hellooo.distributedlock.core.exception; public class BuilderEssentialFieldNotSetException extends RuntimeException { public BuilderEssentialFieldNotSetException() { diff --git a/src/main/java/site/hellooo/distributedlock/exception/GenericRuntimeLockException.java b/core/src/main/java/site/hellooo/distributedlock/core/exception/GenericRuntimeLockException.java similarity index 79% rename from src/main/java/site/hellooo/distributedlock/exception/GenericRuntimeLockException.java rename to core/src/main/java/site/hellooo/distributedlock/core/exception/GenericRuntimeLockException.java index 265a1e7..a4c6da2 100644 --- a/src/main/java/site/hellooo/distributedlock/exception/GenericRuntimeLockException.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/exception/GenericRuntimeLockException.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.exception; +package site.hellooo.distributedlock.core.exception; public class GenericRuntimeLockException extends RuntimeException { public GenericRuntimeLockException() { diff --git a/src/main/java/site/hellooo/distributedlock/exception/LockStateNotRemovedException.java b/core/src/main/java/site/hellooo/distributedlock/core/exception/LockStateNotRemovedException.java similarity index 89% rename from src/main/java/site/hellooo/distributedlock/exception/LockStateNotRemovedException.java rename to core/src/main/java/site/hellooo/distributedlock/core/exception/LockStateNotRemovedException.java index ad375e4..961a89d 100644 --- a/src/main/java/site/hellooo/distributedlock/exception/LockStateNotRemovedException.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/exception/LockStateNotRemovedException.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.exception; +package site.hellooo.distributedlock.core.exception; import java.io.IOException; diff --git a/src/main/java/site/hellooo/distributedlock/exception/LockStateNotSetException.java b/core/src/main/java/site/hellooo/distributedlock/core/exception/LockStateNotSetException.java similarity index 88% rename from src/main/java/site/hellooo/distributedlock/exception/LockStateNotSetException.java rename to core/src/main/java/site/hellooo/distributedlock/core/exception/LockStateNotSetException.java index 2a13b18..9312d7e 100644 --- a/src/main/java/site/hellooo/distributedlock/exception/LockStateNotSetException.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/exception/LockStateNotSetException.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.exception; +package site.hellooo.distributedlock.core.exception; import java.io.IOException; diff --git a/src/main/java/site/hellooo/distributedlock/exception/LockStateRemoveExpiredException.java b/core/src/main/java/site/hellooo/distributedlock/core/exception/LockStateRemoveExpiredException.java similarity index 82% rename from src/main/java/site/hellooo/distributedlock/exception/LockStateRemoveExpiredException.java rename to core/src/main/java/site/hellooo/distributedlock/core/exception/LockStateRemoveExpiredException.java index 53b904c..64fe83d 100644 --- a/src/main/java/site/hellooo/distributedlock/exception/LockStateRemoveExpiredException.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/exception/LockStateRemoveExpiredException.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.exception; +package site.hellooo.distributedlock.core.exception; import java.io.IOException; diff --git a/src/main/java/site/hellooo/distributedlock/exception/LockStateSetExpiredException.java b/core/src/main/java/site/hellooo/distributedlock/core/exception/LockStateSetExpiredException.java similarity index 81% rename from src/main/java/site/hellooo/distributedlock/exception/LockStateSetExpiredException.java rename to core/src/main/java/site/hellooo/distributedlock/core/exception/LockStateSetExpiredException.java index abd958a..fd1868d 100644 --- a/src/main/java/site/hellooo/distributedlock/exception/LockStateSetExpiredException.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/exception/LockStateSetExpiredException.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.exception; +package site.hellooo.distributedlock.core.exception; import java.io.IOException; diff --git a/src/main/java/site/hellooo/distributedlock/impl/AbstractDistributedLock.java b/core/src/main/java/site/hellooo/distributedlock/core/impl/AbstractDistributedLock.java similarity index 90% rename from src/main/java/site/hellooo/distributedlock/impl/AbstractDistributedLock.java rename to core/src/main/java/site/hellooo/distributedlock/core/impl/AbstractDistributedLock.java index 63daa07..4e433be 100644 --- a/src/main/java/site/hellooo/distributedlock/impl/AbstractDistributedLock.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/impl/AbstractDistributedLock.java @@ -1,6 +1,6 @@ -package site.hellooo.distributedlock.impl; +package site.hellooo.distributedlock.core.impl; -import site.hellooo.distributedlock.DistributedLock; +import site.hellooo.distributedlock.core.DistributedLock; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; diff --git a/src/main/java/site/hellooo/distributedlock/impl/LockCallbackFactory.java b/core/src/main/java/site/hellooo/distributedlock/core/impl/LockCallbackFactory.java similarity index 59% rename from src/main/java/site/hellooo/distributedlock/impl/LockCallbackFactory.java rename to core/src/main/java/site/hellooo/distributedlock/core/impl/LockCallbackFactory.java index 06e41ec..a7e56af 100644 --- a/src/main/java/site/hellooo/distributedlock/impl/LockCallbackFactory.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/impl/LockCallbackFactory.java @@ -1,9 +1,9 @@ -package site.hellooo.distributedlock.impl; +package site.hellooo.distributedlock.core.impl; -import site.hellooo.distributedlock.LockCallback; -import site.hellooo.distributedlock.LockContext; -import site.hellooo.distributedlock.enums.Coordinator; -import site.hellooo.distributedlock.impl.redis.RedisLockCallback; +import site.hellooo.distributedlock.core.LockCallback; +import site.hellooo.distributedlock.core.LockContext; +import site.hellooo.distributedlock.core.enums.Coordinator; +import site.hellooo.distributedlock.core.impl.redis.RedisLockCallback; public class LockCallbackFactory { public static LockCallback of(Coordinator coordinator, LockContext lockContext) { diff --git a/src/main/java/site/hellooo/distributedlock/impl/LockStateBuilder.java b/core/src/main/java/site/hellooo/distributedlock/core/impl/LockStateBuilder.java similarity index 80% rename from src/main/java/site/hellooo/distributedlock/impl/LockStateBuilder.java rename to core/src/main/java/site/hellooo/distributedlock/core/impl/LockStateBuilder.java index 2aa6650..25c5f76 100644 --- a/src/main/java/site/hellooo/distributedlock/impl/LockStateBuilder.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/impl/LockStateBuilder.java @@ -1,12 +1,12 @@ -package site.hellooo.distributedlock.impl; +package site.hellooo.distributedlock.core.impl; -import site.hellooo.distributedlock.LockState; -import site.hellooo.distributedlock.common.ArgChecker; -import site.hellooo.distributedlock.common.StringUtils; -import site.hellooo.distributedlock.config.LockOptions; -import site.hellooo.distributedlock.enums.Coordinator; -import site.hellooo.distributedlock.impl.redis.RedisLockState; +import site.hellooo.distributedlock.core.LockState; +import site.hellooo.distributedlock.core.common.ArgChecker; +import site.hellooo.distributedlock.core.common.StringUtils; +import site.hellooo.distributedlock.core.config.LockOptions; +import site.hellooo.distributedlock.core.enums.Coordinator; +import site.hellooo.distributedlock.core.impl.redis.RedisLockState; public class LockStateBuilder { diff --git a/src/main/java/site/hellooo/distributedlock/impl/ReentrantDistributedLock.java b/core/src/main/java/site/hellooo/distributedlock/core/impl/ReentrantDistributedLock.java similarity index 93% rename from src/main/java/site/hellooo/distributedlock/impl/ReentrantDistributedLock.java rename to core/src/main/java/site/hellooo/distributedlock/core/impl/ReentrantDistributedLock.java index 2d31388..986b9c0 100644 --- a/src/main/java/site/hellooo/distributedlock/impl/ReentrantDistributedLock.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/impl/ReentrantDistributedLock.java @@ -1,12 +1,12 @@ -package site.hellooo.distributedlock.impl; - -import site.hellooo.distributedlock.*; -import site.hellooo.distributedlock.config.LockOptions; -import site.hellooo.distributedlock.enums.Coordinator; -import site.hellooo.distributedlock.enums.LockType; -import site.hellooo.distributedlock.exception.GenericRuntimeLockException; -import site.hellooo.distributedlock.exception.LockStateNotRemovedException; -import site.hellooo.distributedlock.exception.LockStateNotSetException; +package site.hellooo.distributedlock.core.impl; + +import site.hellooo.distributedlock.core.config.LockOptions; +import site.hellooo.distributedlock.core.*; +import site.hellooo.distributedlock.core.enums.Coordinator; +import site.hellooo.distributedlock.core.enums.LockType; +import site.hellooo.distributedlock.core.exception.GenericRuntimeLockException; +import site.hellooo.distributedlock.core.exception.LockStateNotRemovedException; +import site.hellooo.distributedlock.core.exception.LockStateNotSetException; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; diff --git a/src/main/java/site/hellooo/distributedlock/impl/ReentrantDistributedLockBuilder.java b/core/src/main/java/site/hellooo/distributedlock/core/impl/ReentrantDistributedLockBuilder.java similarity index 68% rename from src/main/java/site/hellooo/distributedlock/impl/ReentrantDistributedLockBuilder.java rename to core/src/main/java/site/hellooo/distributedlock/core/impl/ReentrantDistributedLockBuilder.java index 916ba1a..3d01962 100644 --- a/src/main/java/site/hellooo/distributedlock/impl/ReentrantDistributedLockBuilder.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/impl/ReentrantDistributedLockBuilder.java @@ -1,12 +1,12 @@ -package site.hellooo.distributedlock.impl; +package site.hellooo.distributedlock.core.impl; import redis.clients.jedis.JedisPool; -import site.hellooo.distributedlock.LockHandler; -import site.hellooo.distributedlock.common.StringUtils; -import site.hellooo.distributedlock.config.LockOptions; -import site.hellooo.distributedlock.enums.Coordinator; -import site.hellooo.distributedlock.exception.BuilderEssentialFieldNotSetException; -import site.hellooo.distributedlock.impl.redis.RedisLockHandler; +import site.hellooo.distributedlock.core.LockHandler; +import site.hellooo.distributedlock.core.common.StringUtils; +import site.hellooo.distributedlock.core.config.LockOptions; +import site.hellooo.distributedlock.core.enums.Coordinator; +import site.hellooo.distributedlock.core.exception.BuilderEssentialFieldNotSetException; +import site.hellooo.distributedlock.core.impl.redis.RedisLockHandler; public class ReentrantDistributedLockBuilder { @@ -16,8 +16,6 @@ public class ReentrantDistributedLockBuilder { private String lockTarget = null; - private LockHandler lockHandler = null; - private JedisPool jedisPool; public ReentrantDistributedLockBuilder lockOptions(LockOptions lockOptions) { @@ -30,11 +28,6 @@ public ReentrantDistributedLockBuilder lockTarget(String lockTarget) { return this; } - public ReentrantDistributedLockBuilder lockHandler(LockHandler lockHandler) { - this.lockHandler = lockHandler; - return this; - } - public ReentrantDistributedLockBuilder jedisPool(JedisPool jedisPool) { this.jedisPool = jedisPool; return this; @@ -46,6 +39,7 @@ public ReentrantDistributedLock build() { throw new BuilderEssentialFieldNotSetException("Fatal: miss essential component 'lockTarget'!"); } + LockHandler lockHandler = null; Coordinator coordinator = lockOptions.getCoordinator(); switch (coordinator) { case REDIS_SINGLETON: @@ -53,9 +47,7 @@ public ReentrantDistributedLock build() { throw new BuilderEssentialFieldNotSetException("Fatal: miss essential component 'jedis'!"); } - if (lockHandler == null) { - lockHandler = new RedisLockHandler(this.jedisPool); - } + lockHandler = new RedisLockHandler(this.jedisPool); break; case REDIS_CLUSTER: case ZOOKEEPER: diff --git a/src/main/java/site/hellooo/distributedlock/impl/redis/AbstractRemotingThread.java b/core/src/main/java/site/hellooo/distributedlock/core/impl/redis/AbstractRemotingThread.java similarity index 88% rename from src/main/java/site/hellooo/distributedlock/impl/redis/AbstractRemotingThread.java rename to core/src/main/java/site/hellooo/distributedlock/core/impl/redis/AbstractRemotingThread.java index fcf3761..c8d4f05 100644 --- a/src/main/java/site/hellooo/distributedlock/impl/redis/AbstractRemotingThread.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/impl/redis/AbstractRemotingThread.java @@ -1,6 +1,6 @@ -package site.hellooo.distributedlock.impl.redis; +package site.hellooo.distributedlock.core.impl.redis; -import site.hellooo.distributedlock.LockContext; +import site.hellooo.distributedlock.core.LockContext; // this class is only design for redis business processing abstract class AbstractRemotingThread extends Thread { diff --git a/src/main/java/site/hellooo/distributedlock/impl/redis/RedisLockCallback.java b/core/src/main/java/site/hellooo/distributedlock/core/impl/redis/RedisLockCallback.java similarity index 94% rename from src/main/java/site/hellooo/distributedlock/impl/redis/RedisLockCallback.java rename to core/src/main/java/site/hellooo/distributedlock/core/impl/redis/RedisLockCallback.java index 59b18dd..00a2d37 100644 --- a/src/main/java/site/hellooo/distributedlock/impl/redis/RedisLockCallback.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/impl/redis/RedisLockCallback.java @@ -1,7 +1,7 @@ -package site.hellooo.distributedlock.impl.redis; +package site.hellooo.distributedlock.core.impl.redis; -import site.hellooo.distributedlock.LockCallback; -import site.hellooo.distributedlock.LockContext; +import site.hellooo.distributedlock.core.LockCallback; +import site.hellooo.distributedlock.core.LockContext; import java.util.concurrent.atomic.AtomicReference; diff --git a/src/main/java/site/hellooo/distributedlock/impl/redis/RedisLockHandler.java b/core/src/main/java/site/hellooo/distributedlock/core/impl/redis/RedisLockHandler.java similarity index 88% rename from src/main/java/site/hellooo/distributedlock/impl/redis/RedisLockHandler.java rename to core/src/main/java/site/hellooo/distributedlock/core/impl/redis/RedisLockHandler.java index 53f7d5d..f2982c9 100644 --- a/src/main/java/site/hellooo/distributedlock/impl/redis/RedisLockHandler.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/impl/redis/RedisLockHandler.java @@ -1,18 +1,18 @@ -package site.hellooo.distributedlock.impl.redis; +package site.hellooo.distributedlock.core.impl.redis; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.params.SetParams; -import site.hellooo.distributedlock.LockContext; -import site.hellooo.distributedlock.LockHandler; -import site.hellooo.distributedlock.LockState; -import site.hellooo.distributedlock.common.ArgChecker; -import site.hellooo.distributedlock.common.StringUtils; -import site.hellooo.distributedlock.enums.Coordinator; -import site.hellooo.distributedlock.exception.LockStateNotRemovedException; -import site.hellooo.distributedlock.exception.LockStateNotSetException; -import site.hellooo.distributedlock.exception.LockStateRemoveExpiredException; -import site.hellooo.distributedlock.exception.LockStateSetExpiredException; +import site.hellooo.distributedlock.core.LockContext; +import site.hellooo.distributedlock.core.LockHandler; +import site.hellooo.distributedlock.core.LockState; +import site.hellooo.distributedlock.core.common.ArgChecker; +import site.hellooo.distributedlock.core.common.StringUtils; +import site.hellooo.distributedlock.core.enums.Coordinator; +import site.hellooo.distributedlock.core.exception.LockStateNotRemovedException; +import site.hellooo.distributedlock.core.exception.LockStateNotSetException; +import site.hellooo.distributedlock.core.exception.LockStateRemoveExpiredException; +import site.hellooo.distributedlock.core.exception.LockStateSetExpiredException; import java.util.Arrays; import java.util.Collections; diff --git a/src/main/java/site/hellooo/distributedlock/impl/redis/RedisLockState.java b/core/src/main/java/site/hellooo/distributedlock/core/impl/redis/RedisLockState.java similarity index 82% rename from src/main/java/site/hellooo/distributedlock/impl/redis/RedisLockState.java rename to core/src/main/java/site/hellooo/distributedlock/core/impl/redis/RedisLockState.java index 27e591d..d662c06 100644 --- a/src/main/java/site/hellooo/distributedlock/impl/redis/RedisLockState.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/impl/redis/RedisLockState.java @@ -1,8 +1,8 @@ -package site.hellooo.distributedlock.impl.redis; +package site.hellooo.distributedlock.core.impl.redis; -import site.hellooo.distributedlock.LockState; -import site.hellooo.distributedlock.common.NetworkUtils; -import site.hellooo.distributedlock.common.ProcessUtils; +import site.hellooo.distributedlock.core.LockState; +import site.hellooo.distributedlock.core.common.NetworkUtils; +import site.hellooo.distributedlock.core.common.ProcessUtils; public class RedisLockState implements LockState { diff --git a/src/main/java/site/hellooo/distributedlock/impl/redis/RemotingLeaseThread.java b/core/src/main/java/site/hellooo/distributedlock/core/impl/redis/RemotingLeaseThread.java similarity index 87% rename from src/main/java/site/hellooo/distributedlock/impl/redis/RemotingLeaseThread.java rename to core/src/main/java/site/hellooo/distributedlock/core/impl/redis/RemotingLeaseThread.java index a4bf143..2e4b6fd 100644 --- a/src/main/java/site/hellooo/distributedlock/impl/redis/RemotingLeaseThread.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/impl/redis/RemotingLeaseThread.java @@ -1,6 +1,6 @@ -package site.hellooo.distributedlock.impl.redis; +package site.hellooo.distributedlock.core.impl.redis; -import site.hellooo.distributedlock.LockContext; +import site.hellooo.distributedlock.core.LockContext; public class RemotingLeaseThread extends AbstractRemotingThread { diff --git a/src/main/java/site/hellooo/distributedlock/impl/redis/RemotingRetryLockThread.java b/core/src/main/java/site/hellooo/distributedlock/core/impl/redis/RemotingRetryLockThread.java similarity index 85% rename from src/main/java/site/hellooo/distributedlock/impl/redis/RemotingRetryLockThread.java rename to core/src/main/java/site/hellooo/distributedlock/core/impl/redis/RemotingRetryLockThread.java index 3969b32..c3554fb 100644 --- a/src/main/java/site/hellooo/distributedlock/impl/redis/RemotingRetryLockThread.java +++ b/core/src/main/java/site/hellooo/distributedlock/core/impl/redis/RemotingRetryLockThread.java @@ -1,8 +1,8 @@ -package site.hellooo.distributedlock.impl.redis; +package site.hellooo.distributedlock.core.impl.redis; -import site.hellooo.distributedlock.LockContext; -import site.hellooo.distributedlock.common.ClassUtils; -import site.hellooo.distributedlock.impl.ReentrantDistributedLock; +import site.hellooo.distributedlock.core.LockContext; +import site.hellooo.distributedlock.core.common.ClassUtils; +import site.hellooo.distributedlock.core.impl.ReentrantDistributedLock; public class RemotingRetryLockThread extends AbstractRemotingThread { diff --git a/src/test/java/site/hellooo/distributedlock/common/ArgChecker.java b/core/src/test/java/site/hellooo/distributedlock/core/common/ArgChecker.java similarity index 67% rename from src/test/java/site/hellooo/distributedlock/common/ArgChecker.java rename to core/src/test/java/site/hellooo/distributedlock/core/common/ArgChecker.java index 41d24d2..3bff3ea 100644 --- a/src/test/java/site/hellooo/distributedlock/common/ArgChecker.java +++ b/core/src/test/java/site/hellooo/distributedlock/core/common/ArgChecker.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.common; +package site.hellooo.distributedlock.core.common; import org.junit.Test; diff --git a/src/test/java/site/hellooo/distributedlock/common/NetworkUtilsTest.java b/core/src/test/java/site/hellooo/distributedlock/core/common/NetworkUtilsTest.java similarity index 68% rename from src/test/java/site/hellooo/distributedlock/common/NetworkUtilsTest.java rename to core/src/test/java/site/hellooo/distributedlock/core/common/NetworkUtilsTest.java index d0c689c..8e221ef 100644 --- a/src/test/java/site/hellooo/distributedlock/common/NetworkUtilsTest.java +++ b/core/src/test/java/site/hellooo/distributedlock/core/common/NetworkUtilsTest.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.common; +package site.hellooo.distributedlock.core.common; import org.junit.Test; diff --git a/src/test/java/site/hellooo/distributedlock/common/ProcessUtilsTest.java b/core/src/test/java/site/hellooo/distributedlock/core/common/ProcessUtilsTest.java similarity index 68% rename from src/test/java/site/hellooo/distributedlock/common/ProcessUtilsTest.java rename to core/src/test/java/site/hellooo/distributedlock/core/common/ProcessUtilsTest.java index 4a1b36e..47895f5 100644 --- a/src/test/java/site/hellooo/distributedlock/common/ProcessUtilsTest.java +++ b/core/src/test/java/site/hellooo/distributedlock/core/common/ProcessUtilsTest.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.common; +package site.hellooo.distributedlock.core.common; import org.junit.Test; diff --git a/src/test/java/site/hellooo/distributedlock/common/StringUtilsTest.java b/core/src/test/java/site/hellooo/distributedlock/core/common/StringUtilsTest.java similarity index 68% rename from src/test/java/site/hellooo/distributedlock/common/StringUtilsTest.java rename to core/src/test/java/site/hellooo/distributedlock/core/common/StringUtilsTest.java index f7b63f6..0da3212 100644 --- a/src/test/java/site/hellooo/distributedlock/common/StringUtilsTest.java +++ b/core/src/test/java/site/hellooo/distributedlock/core/common/StringUtilsTest.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.common; +package site.hellooo.distributedlock.core.common; import org.junit.Test; diff --git a/src/test/java/site/hellooo/distributedlock/impl/ReentrantDistributedLockTest.java b/core/src/test/java/site/hellooo/distributedlock/core/impl/ReentrantDistributedLockTest.java similarity index 91% rename from src/test/java/site/hellooo/distributedlock/impl/ReentrantDistributedLockTest.java rename to core/src/test/java/site/hellooo/distributedlock/core/impl/ReentrantDistributedLockTest.java index ec4e514..ca25764 100644 --- a/src/test/java/site/hellooo/distributedlock/impl/ReentrantDistributedLockTest.java +++ b/core/src/test/java/site/hellooo/distributedlock/core/impl/ReentrantDistributedLockTest.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.impl; +package site.hellooo.distributedlock.core.impl; import org.junit.Test; diff --git a/src/test/java/site/hellooo/distributedlock/impl/redis/RemotingLeaseThreadTest.java b/core/src/test/java/site/hellooo/distributedlock/core/impl/redis/RemotingLeaseThreadTest.java similarity index 68% rename from src/test/java/site/hellooo/distributedlock/impl/redis/RemotingLeaseThreadTest.java rename to core/src/test/java/site/hellooo/distributedlock/core/impl/redis/RemotingLeaseThreadTest.java index bd4ce02..4b5fdf1 100644 --- a/src/test/java/site/hellooo/distributedlock/impl/redis/RemotingLeaseThreadTest.java +++ b/core/src/test/java/site/hellooo/distributedlock/core/impl/redis/RemotingLeaseThreadTest.java @@ -1,4 +1,4 @@ -package site.hellooo.distributedlock.impl.redis; +package site.hellooo.distributedlock.core.impl.redis; import org.junit.Test; diff --git a/examples/pom.xml b/examples/pom.xml new file mode 100644 index 0000000..3495578 --- /dev/null +++ b/examples/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + site.hellooo + halo-distributedlock + ${revision} + + + halo-distributedlock-examples + + + + site.hellooo + halo-distributedlock-core + ${revision} + + + + com.google.code.gson + gson + 2.8.9 + + + \ No newline at end of file diff --git a/examples/src/main/java/site/hellooo/distributedlock/examples/ConfigReader.java b/examples/src/main/java/site/hellooo/distributedlock/examples/ConfigReader.java new file mode 100644 index 0000000..35abc19 --- /dev/null +++ b/examples/src/main/java/site/hellooo/distributedlock/examples/ConfigReader.java @@ -0,0 +1,46 @@ +package site.hellooo.distributedlock.examples; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +import java.io.InputStream; +import java.io.InputStreamReader; + +public class ConfigReader { + + private static final String CONFIG_FILE = "config.json"; + private static final Gson GSON = new GsonBuilder() + .create(); + + private static JsonObject getConfig() { + InputStream inputStream = ConfigReader.class.getClassLoader().getResourceAsStream(CONFIG_FILE); + + if (inputStream == null) { + throw new RuntimeException("Failed to load configuration file: " + CONFIG_FILE); + } + + InputStreamReader reader = new InputStreamReader(inputStream); + return (JsonObject) JsonParser.parseReader(reader); + } + + public static RedisConfig redis() { + JsonObject config = getConfig(); + JsonObject jedisConfig = config.getAsJsonObject("jedis"); + return GSON.fromJson(jedisConfig.toString(), RedisConfig.class); + } + + public static class RedisConfig { + private String host; + private int port; + + public String getHost() { + return host; + } + + public int getPort() { + return port; + } + } +} diff --git a/examples/src/main/java/site/hellooo/distributedlock/examples/SingleProcessMultiThreadContention.java b/examples/src/main/java/site/hellooo/distributedlock/examples/SingleProcessMultiThreadContention.java new file mode 100644 index 0000000..0ec16e6 --- /dev/null +++ b/examples/src/main/java/site/hellooo/distributedlock/examples/SingleProcessMultiThreadContention.java @@ -0,0 +1,48 @@ +package site.hellooo.distributedlock.examples; + +import redis.clients.jedis.JedisPool; +import site.hellooo.distributedlock.core.common.ProcessUtils; +import site.hellooo.distributedlock.core.config.LockOptions; +import site.hellooo.distributedlock.core.impl.ReentrantDistributedLockBuilder; + +import java.util.concurrent.locks.Lock; + +public class SingleProcessMultiThreadContention { + public static void main(String[] args) { + lockCompetition(); + } + + public static void lockCompetition() { + long start = System.currentTimeMillis(); + + ConfigReader.RedisConfig redisConfig = ConfigReader.redis(); + String host = redisConfig.getHost(); + int port = redisConfig.getPort(); + + JedisPool jedisPool = new JedisPool(host, port); + for (int i = 0; i < 10; i++) { + final int threadNumber = i; + Thread thread = new Thread(() -> { + 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"); + })); + } +} diff --git a/examples/src/main/java/site/hellooo/distributedlock/examples/multiprocess/Step0.java b/examples/src/main/java/site/hellooo/distributedlock/examples/multiprocess/Step0.java new file mode 100644 index 0000000..814dc34 --- /dev/null +++ b/examples/src/main/java/site/hellooo/distributedlock/examples/multiprocess/Step0.java @@ -0,0 +1,47 @@ +package site.hellooo.distributedlock.examples.multiprocess; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; +import site.hellooo.distributedlock.examples.ConfigReader; +import site.hellooo.distributedlock.examples.SingleProcessMultiThreadContention; + +public class Step0 { + public static void main(String[] args) { + ConfigReader.RedisConfig redisConfig = ConfigReader.redis(); + String host = redisConfig.getHost(); + int port = redisConfig.getPort(); + + long start = System.currentTimeMillis(); + JedisPool jedisPool = new JedisPool(host, port); + Jedis jedis = jedisPool.getResource(); + try { + jedis.set("multiprocess_step0_ready", "true"); + + boolean step1_ready = false; + while (System.currentTimeMillis() - start < 10000) { + String multiprocessValue = jedis.get("multiprocess_step1_ready"); + if ("true".equals(multiprocessValue)) { + step1_ready = true; + break; + } + Thread.sleep(2); + } + + if (!step1_ready) { + return; + } + + SingleProcessMultiThreadContention.lockCompetition(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } finally { + jedis.del("multiprocess_step0_ready"); + jedis.close(); + } + + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + jedisPool.close(); + System.out.println("cause: " + (System.currentTimeMillis() - start) + "ms"); + })); + } +} diff --git a/examples/src/main/java/site/hellooo/distributedlock/examples/multiprocess/Step1.java b/examples/src/main/java/site/hellooo/distributedlock/examples/multiprocess/Step1.java new file mode 100644 index 0000000..16460a5 --- /dev/null +++ b/examples/src/main/java/site/hellooo/distributedlock/examples/multiprocess/Step1.java @@ -0,0 +1,47 @@ +package site.hellooo.distributedlock.examples.multiprocess; + +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; +import site.hellooo.distributedlock.examples.ConfigReader; +import site.hellooo.distributedlock.examples.SingleProcessMultiThreadContention; + +public class Step1 { + public static void main(String[] args) { + ConfigReader.RedisConfig redisConfig = ConfigReader.redis(); + String host = redisConfig.getHost(); + int port = redisConfig.getPort(); + + long start = System.currentTimeMillis(); + JedisPool jedisPool = new JedisPool(host, port); + Jedis jedis = jedisPool.getResource(); + try { + jedis.set("multiprocess_step1_ready", "true"); + + boolean step0_ready = false; + while (System.currentTimeMillis() - start < 5000) { + String multiprocessValue = jedis.get("multiprocess_step0_ready"); + if ("true".equals(multiprocessValue)) { + step0_ready = true; + break; + } + Thread.sleep(2); + } + + if (!step0_ready) { + return; + } + + SingleProcessMultiThreadContention.lockCompetition(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } finally { + jedis.del("multiprocess_step1_ready"); + jedis.close(); + } + + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + jedisPool.close(); + System.out.println("cause: " + (System.currentTimeMillis() - start) + "ms"); + })); + } +} diff --git a/examples/src/main/resources/config.json b/examples/src/main/resources/config.json new file mode 100644 index 0000000..5c03e5f --- /dev/null +++ b/examples/src/main/resources/config.json @@ -0,0 +1,6 @@ +{ + "jedis": { + "host": "localhost", + "port": 6379 + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index d96b279..85a0a50 100644 --- a/pom.xml +++ b/pom.xml @@ -4,45 +4,56 @@ site.hellooo halo-distributedlock - 0.0.6-GA + ${revision} + pom + + core + + 0.0.7-GA + UTF8 8 8 + 4.2.3 4.13.2 3.23.1 4.6.1 - - - redis.clients - jedis - ${jedis.version} - - - - junit - junit - 4.13.2 - ${junit.version} - - - org.assertj - assertj-core - ${assertj-core.version} - test - - - org.mockito - mockito-core - ${mockito-core.version} - test - - - + + + + redis.clients + jedis + ${jedis.version} + + + + + junit + junit + ${junit.version} + test + + + org.assertj + assertj-core + ${assertj-core.version} + test + + + org.mockito + mockito-core + ${mockito-core.version} + test + + + + + distributed-lock a simple but reliable distributed lock implementation https://github.com/hellooo-stack/halo-distributedlock @@ -66,7 +77,7 @@ - + ossrh https://s01.oss.sonatype.org/content/repositories/snapshots @@ -80,7 +91,7 @@ release - + org.apache.maven.plugins maven-source-plugin @@ -94,7 +105,7 @@ - + org.apache.maven.plugins maven-javadoc-plugin @@ -108,7 +119,7 @@ - + org.apache.maven.plugins maven-gpg-plugin @@ -123,7 +134,7 @@ - + org.sonatype.plugins nexus-staging-maven-plugin @@ -132,7 +143,7 @@ ossrh https://s01.oss.sonatype.org/ - false + true diff --git a/src/main/java/site/hellooo/distributedlock/DistributedLock.java b/src/main/java/site/hellooo/distributedlock/DistributedLock.java deleted file mode 100644 index aaa1a48..0000000 --- a/src/main/java/site/hellooo/distributedlock/DistributedLock.java +++ /dev/null @@ -1,13 +0,0 @@ -package site.hellooo.distributedlock; - -import site.hellooo.distributedlock.enums.Coordinator; -import site.hellooo.distributedlock.enums.LockType; - -import java.util.concurrent.locks.Lock; - -public interface DistributedLock extends Lock { - - LockType lockType(); - - Coordinator coordinatorType(); -}