From e4d2666f79b77cd2b2c8a5d6152719db030cf95c Mon Sep 17 00:00:00 2001 From: Nikita Bahliuk Date: Thu, 24 Aug 2023 13:21:43 +0300 Subject: [PATCH 1/3] Add ExtensibleLockProvider functionality to JedisLockProvider --- .../redis/jedis4/JedisLockProvider.java | 45 +++++++++++++++---- .../JedisLockProviderIntegrationTest.java | 17 +++---- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/providers/redis/shedlock-provider-redis-jedis4/src/main/java/net/javacrumbs/shedlock/provider/redis/jedis4/JedisLockProvider.java b/providers/redis/shedlock-provider-redis-jedis4/src/main/java/net/javacrumbs/shedlock/provider/redis/jedis4/JedisLockProvider.java index 830bc343d..52a83be31 100644 --- a/providers/redis/shedlock-provider-redis-jedis4/src/main/java/net/javacrumbs/shedlock/provider/redis/jedis4/JedisLockProvider.java +++ b/providers/redis/shedlock-provider-redis-jedis4/src/main/java/net/javacrumbs/shedlock/provider/redis/jedis4/JedisLockProvider.java @@ -17,8 +17,8 @@ import net.javacrumbs.shedlock.core.AbstractSimpleLock; import net.javacrumbs.shedlock.core.ClockProvider; +import net.javacrumbs.shedlock.core.ExtensibleLockProvider; import net.javacrumbs.shedlock.core.LockConfiguration; -import net.javacrumbs.shedlock.core.LockProvider; import net.javacrumbs.shedlock.core.SimpleLock; import net.javacrumbs.shedlock.support.LockException; import net.javacrumbs.shedlock.support.annotation.NonNull; @@ -40,7 +40,7 @@ *

* See https://redis.io/commands/set */ -public class JedisLockProvider implements LockProvider { +public class JedisLockProvider implements ExtensibleLockProvider { private static final String KEY_PREFIX = "job-lock"; private static final String ENV_DEFAULT = "default"; @@ -86,20 +86,44 @@ public Optional lock(@NonNull LockConfiguration lockConfiguration) { String rez = jedisTemplate.set(key, buildValue(), setParams().nx().px(expireTime)); if ("OK".equals(rez)) { - return Optional.of(new RedisLock(key, jedisTemplate, lockConfiguration)); + return Optional.of(new RedisLock(key, this, lockConfiguration)); } return Optional.empty(); } + private Optional extend(LockConfiguration lockConfiguration) { + long expireTime = getMsUntil(lockConfiguration.getLockAtMostUntil()); + + String key = buildKey(lockConfiguration.getName(), this.environment); + + String rez = extendKeyExpiration(key, expireTime); + + if ("OK".equals(rez)) { + return Optional.of(new RedisLock(key, this, lockConfiguration)); + } + + return Optional.empty(); + } + + + private String extendKeyExpiration(String key, long expiration) { + return jedisTemplate.set(key, buildValue(), setParams().xx().px(expiration)); + } + + private void deleteKey(String key) { + jedisTemplate.del(key); + } + + private static final class RedisLock extends AbstractSimpleLock { private final String key; - private final JedisTemplate jedisTemplate; + private final JedisLockProvider jedisLockProvider; - private RedisLock(String key, JedisTemplate jedisTemplate, LockConfiguration lockConfiguration) { + private RedisLock(String key, JedisLockProvider jedisLockProvider, LockConfiguration lockConfiguration) { super(lockConfiguration); this.key = key; - this.jedisTemplate = jedisTemplate; + this.jedisLockProvider = jedisLockProvider; } @Override @@ -109,14 +133,19 @@ public void doUnlock() { // lock at least until is in the past if (keepLockFor <= 0) { try { - jedisTemplate.del(key); + jedisLockProvider.deleteKey(key); } catch (Exception e) { throw new LockException("Can not remove node", e); } } else { - jedisTemplate.set(key, buildValue(), setParams().xx().px(keepLockFor)); + jedisLockProvider.extendKeyExpiration(key, keepLockFor); } } + + @Override + protected Optional doExtend(LockConfiguration newConfiguration) { + return jedisLockProvider.extend(newConfiguration); + } } private static long getMsUntil(Instant instant) { diff --git a/providers/redis/shedlock-provider-redis-jedis4/src/test/java/net/javacrumbs/shedlock/provider/redis/jedis4/JedisLockProviderIntegrationTest.java b/providers/redis/shedlock-provider-redis-jedis4/src/test/java/net/javacrumbs/shedlock/provider/redis/jedis4/JedisLockProviderIntegrationTest.java index c6d255f28..cfe1e114f 100644 --- a/providers/redis/shedlock-provider-redis-jedis4/src/test/java/net/javacrumbs/shedlock/provider/redis/jedis4/JedisLockProviderIntegrationTest.java +++ b/providers/redis/shedlock-provider-redis-jedis4/src/test/java/net/javacrumbs/shedlock/provider/redis/jedis4/JedisLockProviderIntegrationTest.java @@ -15,8 +15,8 @@ */ package net.javacrumbs.shedlock.provider.redis.jedis4; -import net.javacrumbs.shedlock.core.LockProvider; -import net.javacrumbs.shedlock.test.support.AbstractLockProviderIntegrationTest; +import net.javacrumbs.shedlock.core.ExtensibleLockProvider; +import net.javacrumbs.shedlock.test.support.AbstractExtensibleLockProviderIntegrationTest; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.testcontainers.junit.jupiter.Container; @@ -37,8 +37,8 @@ public class JedisLockProviderIntegrationTest { public static final RedisContainer redis = new RedisContainer(PORT); @Nested - class Cluster extends AbstractLockProviderIntegrationTest { - private LockProvider lockProvider; + class Cluster extends AbstractExtensibleLockProviderIntegrationTest { + private ExtensibleLockProvider lockProvider; private JedisCluster jedisCluster; @@ -62,14 +62,14 @@ private String getLock(String lockName) { } @Override - protected LockProvider getLockProvider() { + protected ExtensibleLockProvider getLockProvider() { return lockProvider; } } @Nested - class Pool extends AbstractLockProviderIntegrationTest { - private LockProvider lockProvider; + class Pool extends AbstractExtensibleLockProviderIntegrationTest { + private ExtensibleLockProvider lockProvider; private JedisPool jedisPool; @@ -98,8 +98,9 @@ private String getLock(String lockName, Jedis jedis) { } @Override - protected LockProvider getLockProvider() { + protected ExtensibleLockProvider getLockProvider() { return lockProvider; } } + } From 38330d8ea73e827f16e58c995907a3ec7a540832 Mon Sep 17 00:00:00 2001 From: Lukas Krecan Date: Thu, 24 Aug 2023 16:59:25 +0200 Subject: [PATCH 2/3] Make test less flaky --- .../support/AbstractExtensibleLockProviderIntegrationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shedlock-test-support/src/main/java/net/javacrumbs/shedlock/test/support/AbstractExtensibleLockProviderIntegrationTest.java b/shedlock-test-support/src/main/java/net/javacrumbs/shedlock/test/support/AbstractExtensibleLockProviderIntegrationTest.java index b8874859f..b275b9e20 100644 --- a/shedlock-test-support/src/main/java/net/javacrumbs/shedlock/test/support/AbstractExtensibleLockProviderIntegrationTest.java +++ b/shedlock-test-support/src/main/java/net/javacrumbs/shedlock/test/support/AbstractExtensibleLockProviderIntegrationTest.java @@ -57,8 +57,8 @@ public void shouldNotBeAbleToExtendUnlockedLock() { @Test public void shouldNotBeAbleToExtendExpiredLock() { Optional lock = getLockProvider().lock(lockConfig(LOCK_NAME1, Duration.ofMillis(1), Duration.ZERO)); - sleepFor(Duration.ofMillis(1)); assertThat(lock).isNotEmpty(); + sleepFor(Duration.ofMillis(2)); Optional newLock = lock.get().extend(Duration.ofSeconds(10), Duration.ZERO); assertThat(newLock).isEmpty(); From bf49a9b6c93dddc56ee94719fbe8b773f578875b Mon Sep 17 00:00:00 2001 From: Lukas Krecan Date: Thu, 24 Aug 2023 17:08:16 +0200 Subject: [PATCH 3/3] Add missing annotations --- .../shedlock/provider/redis/jedis4/JedisLockProvider.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/providers/redis/shedlock-provider-redis-jedis4/src/main/java/net/javacrumbs/shedlock/provider/redis/jedis4/JedisLockProvider.java b/providers/redis/shedlock-provider-redis-jedis4/src/main/java/net/javacrumbs/shedlock/provider/redis/jedis4/JedisLockProvider.java index 52a83be31..7b9f8a34b 100644 --- a/providers/redis/shedlock-provider-redis-jedis4/src/main/java/net/javacrumbs/shedlock/provider/redis/jedis4/JedisLockProvider.java +++ b/providers/redis/shedlock-provider-redis-jedis4/src/main/java/net/javacrumbs/shedlock/provider/redis/jedis4/JedisLockProvider.java @@ -38,7 +38,7 @@ /** * Uses Redis's `SET resource-name anystring NX PX max-lock-ms-time` as locking mechanism. *

- * See https://redis.io/commands/set + * See Set command */ public class JedisLockProvider implements ExtensibleLockProvider { @@ -143,7 +143,8 @@ public void doUnlock() { } @Override - protected Optional doExtend(LockConfiguration newConfiguration) { + @NonNull + protected Optional doExtend(@NonNull LockConfiguration newConfiguration) { return jedisLockProvider.extend(newConfiguration); } }