diff --git a/pom.xml b/pom.xml index 2b32ab4..2e048af 100644 --- a/pom.xml +++ b/pom.xml @@ -4,10 +4,11 @@ com.phonepe DLM - 1.0.0-SNAPSHOT + 1.0.1-SNAPSHOT https://github.com/PhonePe/DLM Distributed Lock Manager + 2024 scm:git:https://github.com/PhonePe/DLM.git @@ -56,17 +57,6 @@ https://travis-ci.org/PhonePe/DLM - - - ossrh - https://oss.sonatype.org/content/repositories/snapshots - - - ossrh - https://oss.sonatype.org/service/local/staging/deploy/maven2/ - - - clojars.org @@ -91,7 +81,9 @@ 3.3.1 3.0.0-M5 0.8.7 - 1.6.7 + 0.5.0 + 2.5.3 + 1.6 4.3.1 @@ -108,6 +100,12 @@ PhonePe_DLM phonepe https://sonarcloud.io + + + + **/src/test/com/phonepe/dlm/**/*.java, + **/src/test/com/phonepe/dlm/*.java + @@ -235,6 +233,10 @@ org.apache.maven.plugins maven-javadoc-plugin ${maven.javadoc.plugin.version} + + ${jdk.source.version} + false + attach-javadocs @@ -250,14 +252,24 @@ ${maven.surefire.plugin.version} - org.sonatype.plugins - nexus-staging-maven-plugin - 1.6.7 + org.sonatype.central + central-publishing-maven-plugin + ${sonatype.central.publishing.maven.plugin.version} true - ossrh - https://s01.oss.sonatype.org/ - true + central + true + + + + org.apache.maven.plugins + maven-release-plugin + ${maven.release.plugin.version} + + true + false + release + deploy @@ -265,104 +277,65 @@ - coverage - - - - org.jacoco - jacoco-maven-plugin - ${jacoco.maven.plugin.version} - - - prepare-agent - - prepare-agent - - - - report - - report - - - - XML - - - - - - - - + release + + + release + true + + + + + + org.apache.maven.plugins + maven-gpg-plugin + ${maven.gpg.plugin.version} + + + sign-artifacts + verify + + sign + + + + --pinentry-mode + loopback + + + + + + + + - release - - - - org.sonatype.central - central-publishing-maven-plugin - 0.4.0 - true - - central - true - true - - - - org.apache.maven.plugins - maven-source-plugin - 3.3.0 - - - attach-sources - verify - - jar-no-fork - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - 3.6.3 - - - attach-javadoc - - jar - - - - - java - none - - - - org.apache.maven.plugins - maven-gpg-plugin - 3.1.0 - - - sign-artifacts - verify - - sign - - - - - - --pinentry-mode - loopback - - - - - - + coverage + + + + org.jacoco + jacoco-maven-plugin + ${jacoco.maven.plugin.version} + + + + prepare-agent + + + + report + test + + report + + + + + + + + + diff --git a/src/main/java/com/phonepe/dlm/exception/DLMException.java b/src/main/java/com/phonepe/dlm/exception/DLMException.java index 37af974..9763f2a 100644 --- a/src/main/java/com/phonepe/dlm/exception/DLMException.java +++ b/src/main/java/com/phonepe/dlm/exception/DLMException.java @@ -21,6 +21,8 @@ import lombok.EqualsAndHashCode; import lombok.ToString; +import java.util.Objects; + @Data @ToString(callSuper = true) @EqualsAndHashCode(callSuper = true) @@ -44,10 +46,10 @@ public static DLMException propagate(final Throwable throwable) { } public static DLMException propagate(final String message, final Throwable throwable) { - if (throwable instanceof DLMException) { - return (DLMException) throwable; - } else if (throwable.getCause() instanceof DLMException) { - return (DLMException) throwable.getCause(); + if (throwable instanceof DLMException exception) { + return exception; + } else if (throwable.getCause() instanceof DLMException causeException) { + return causeException; } return DLMException.builder() .errorCode(ErrorCode.INTERNAL_ERROR) diff --git a/src/main/java/com/phonepe/dlm/lock/storage/aerospike/AerospikeStore.java b/src/main/java/com/phonepe/dlm/lock/storage/aerospike/AerospikeStore.java index e70a429..5cfda8a 100644 --- a/src/main/java/com/phonepe/dlm/lock/storage/aerospike/AerospikeStore.java +++ b/src/main/java/com/phonepe/dlm/lock/storage/aerospike/AerospikeStore.java @@ -57,7 +57,7 @@ public void write(String lockId, LockLevel lockLevel, String farmId, Duration tt final WritePolicy writePolicy = new WritePolicy(aerospikeClient.getWritePolicyDefault()); writePolicy.generationPolicy = GenerationPolicy.EXPECT_GEN_EQUAL; writePolicy.generation = 0; - writePolicy.expiration = Long.valueOf(ttlSeconds.getSeconds()).intValue(); // as only int is supported + writePolicy.expiration = (int) ttlSeconds.getSeconds(); // as only int is supported writePolicy.commitLevel = CommitLevel.COMMIT_MASTER; // Committing to master only, as there is no read required so there is no chance of dirty reads. try { final List binList = new ArrayList<>(); diff --git a/src/test/java/com/phonepe/dlm/DistributedLockWithAerospikeTest.java b/src/test/java/com/phonepe/dlm/DistributedLockWithAerospikeTest.java index 24f0aeb..e5292b0 100644 --- a/src/test/java/com/phonepe/dlm/DistributedLockWithAerospikeTest.java +++ b/src/test/java/com/phonepe/dlm/DistributedLockWithAerospikeTest.java @@ -20,20 +20,19 @@ import com.aerospike.client.Host; import com.aerospike.client.policy.ClientPolicy; import com.google.common.collect.Maps; +import com.phonepe.dlm.exception.DLMException; +import com.phonepe.dlm.exception.ErrorCode; import com.phonepe.dlm.lock.Lock; import com.phonepe.dlm.lock.base.LockBase; import com.phonepe.dlm.lock.level.LockLevel; import com.phonepe.dlm.lock.mode.LockMode; import com.phonepe.dlm.lock.storage.aerospike.AerospikeStore; +import com.phonepe.dlm.util.DLMExceptionMatcher; import com.phonepe.dlm.util.TestUtils; -import com.phonepe.dlm.exception.DLMException; -import com.phonepe.dlm.exception.ErrorCode; import io.appform.testcontainers.aerospike.AerospikeContainerConfiguration; import io.appform.testcontainers.aerospike.container.AerospikeContainer; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.*; +import org.junit.rules.ExpectedException; import java.time.Duration; import java.util.ArrayList; @@ -54,6 +53,8 @@ public class DistributedLockWithAerospikeTest { public static final int AEROSPIKE_PORT = 3000; private static final AerospikeContainer AEROSPIKE_DOCKER_CONTAINER; + @Rule + public ExpectedException exceptionThrown = ExpectedException.none(); private DistributedLockManager lockManager; public AerospikeClient aerospikeClient; @@ -68,7 +69,8 @@ public class DistributedLockWithAerospikeTest { @Before public void setUp() { aerospikeClient = new AerospikeClient(new ClientPolicy(), - new Host(AEROSPIKE_DOCKER_CONTAINER.getContainerIpAddress(), AEROSPIKE_DOCKER_CONTAINER.getConnectionPort())); + new Host(AEROSPIKE_DOCKER_CONTAINER.getHost(), AEROSPIKE_DOCKER_CONTAINER.getConnectionPort())); + aerospikeClient.truncate(aerospikeClient.getInfoPolicyDefault(), AEROSPIKE_NAMESPACE, null, null); lockManager = DistributedLockManager.builder() .clientId("CLIENT_ID") @@ -126,59 +128,37 @@ public void lockTestPositiveXDC() { } @Test - public void testAcquireLockWithWait() { + public void testLockUnavailableForAcquireLock() { final Lock lock = lockManager.getLockInstance("NEW_LOCK_ID", LockLevel.DC); - lockManager.acquireLock(lock, Duration.ofSeconds(2)); // Lock acquired for 2 seconds - Assert.assertTrue(lock.getAcquiredStatus().get()); - - try { - lockManager.tryAcquireLock(lock); // Try acquiring a lock and fail if lock is already acquired - } catch (DLMException e) { - Assert.assertEquals(ErrorCode.LOCK_UNAVAILABLE, e.getErrorCode()); - } - lockManager.acquireLock(lock); // Wait and try acquiring the lock. - Assert.assertTrue(lock.getAcquiredStatus().get()); - try { - lockManager.acquireLock(lock, Duration.ofSeconds(2), Duration.ofSeconds(2)); // Wait for 2 seconds only for acquiring the lock - Assert.fail("Flow should not have reached here"); - } catch (DLMException e) { - Assert.assertEquals(ErrorCode.LOCK_UNAVAILABLE, e.getErrorCode()); // As it won't be released for next 90 secs default - } + exceptionThrown.expect(DLMExceptionMatcher.hasCode(ErrorCode.LOCK_UNAVAILABLE)); + lockManager.acquireLock(lock, Duration.ofSeconds(2), Duration.ofSeconds(2)); // Wait for 2 seconds only for acquiring the lock } - @Test(expected = DLMException.class) - public void lockTestNegative1() { + @Test + public void testLockUnavailableForTryAcquireLockWithSameLockInstance() { final Lock lock = lockManager.getLockInstance("LOCK_ID", LockLevel.DC); lockManager.tryAcquireLock(lock); - Assert.assertTrue(lock.getAcquiredStatus() - .get()); + Assert.assertTrue(lock.getAcquiredStatus().get()); + + exceptionThrown.expect(DLMExceptionMatcher.hasCode(ErrorCode.LOCK_UNAVAILABLE)); lockManager.tryAcquireLock(lock); } - @Test(expected = DLMException.class) - public void lockTestNegative2() { + @Test + public void testLockUnavailableForTryAcquireLockWithDifferentLockInstance() { Lock lock = lockManager.getLockInstance("LOCK_ID", LockLevel.DC); lockManager.tryAcquireLock(lock); - Assert.assertTrue(lock.getAcquiredStatus() - .get()); - lock = lockManager.getLockInstance("LOCK_ID", LockLevel.DC); - lockManager.tryAcquireLock(lock); - } + Assert.assertTrue(lock.getAcquiredStatus().get()); - @Test(expected = DLMException.class) - public void lockTestNegative3() { - final Lock lock = lockManager.getLockInstance("LOCK_ID", LockLevel.DC); + exceptionThrown.expect(DLMExceptionMatcher.hasCode(ErrorCode.LOCK_UNAVAILABLE)); + lock = lockManager.getLockInstance("LOCK_ID", LockLevel.DC); lockManager.tryAcquireLock(lock); - Assert.assertTrue(lock.getAcquiredStatus() - .get()); - final Lock lock1 = lockManager.getLockInstance("LOCK_ID", LockLevel.DC); - lockManager.tryAcquireLock(lock1); } @Test - public void concurrentLockAttempt() throws InterruptedException { + public void concurrentLockAttempt() { final int attempts = Runtime.getRuntime() .availableProcessors(); final Map trackers = Maps.newConcurrentMap(); diff --git a/src/test/java/com/phonepe/dlm/DistributedLockWithHBaseTest.java b/src/test/java/com/phonepe/dlm/DistributedLockWithHBaseTest.java index 692ea13..2f51790 100644 --- a/src/test/java/com/phonepe/dlm/DistributedLockWithHBaseTest.java +++ b/src/test/java/com/phonepe/dlm/DistributedLockWithHBaseTest.java @@ -25,7 +25,7 @@ import com.phonepe.dlm.util.HBaseConnectionStub; import com.phonepe.dlm.exception.DLMException; import com.phonepe.dlm.exception.ErrorCode; -import com.phonepe.dlm.util.DLSExceptionMatcher; +import com.phonepe.dlm.util.DLMExceptionMatcher; import com.phonepe.dlm.util.TestUtils; import org.junit.*; import org.junit.rules.ExpectedException; @@ -143,7 +143,7 @@ public void testInitializeWithException() { .build()) .build()) .build(); - exceptionThrown.expect(DLSExceptionMatcher.hasCode(ErrorCode.TABLE_CREATION_ERROR)); + exceptionThrown.expect(DLMExceptionMatcher.hasCode(ErrorCode.TABLE_CREATION_ERROR)); lockManager.initialize(); } @@ -202,7 +202,7 @@ public void concurrentLockAttempt() { } catch (DLMException e) { trackers.computeIfAbsent("FAILED_ACQUIRES", x -> new AtomicInteger(0)) .getAndIncrement(); - } catch (Exception e) { + } catch (Exception ignored) { // ignore; } finally { boolean result = lockManager.releaseLock(lock); diff --git a/src/test/java/com/phonepe/dlm/util/DLSExceptionMatcher.java b/src/test/java/com/phonepe/dlm/util/DLMExceptionMatcher.java similarity index 85% rename from src/test/java/com/phonepe/dlm/util/DLSExceptionMatcher.java rename to src/test/java/com/phonepe/dlm/util/DLMExceptionMatcher.java index c7ae06e..27524e1 100644 --- a/src/test/java/com/phonepe/dlm/util/DLSExceptionMatcher.java +++ b/src/test/java/com/phonepe/dlm/util/DLMExceptionMatcher.java @@ -25,16 +25,16 @@ * @author shantanu.tiwari * Created on 29/12/21 */ -public class DLSExceptionMatcher extends TypeSafeMatcher { +public class DLMExceptionMatcher extends TypeSafeMatcher { private final ErrorCode expectedErrorCode; private ErrorCode foundErrorCode; - private DLSExceptionMatcher(ErrorCode expectedErrorCode) { + private DLMExceptionMatcher(ErrorCode expectedErrorCode) { this.expectedErrorCode = expectedErrorCode; } - public static DLSExceptionMatcher hasCode(ErrorCode errorCode) { - return new DLSExceptionMatcher(errorCode); + public static DLMExceptionMatcher hasCode(ErrorCode errorCode) { + return new DLMExceptionMatcher(errorCode); } @Override