Skip to content

Commit

Permalink
fix(distributed-lock): Do not execute locked method if token is not a…
Browse files Browse the repository at this point in the history
…cquired after all retries
  • Loading branch information
alturkovic committed Jun 14, 2023
1 parent f41fc3c commit 00a1b5a
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ You can also create an alias for your lock so you don't have to specify `@Locked

Started tracking the changes since 1.2.0 so no changelogs available for earlier versions.

==== 1.5.4

- BUGFIX: Do not execute locked method if token is not acquired after all retries

==== 1.5.3

- BUGFIX: `RetriableLock` should return `null` if lock is not acquired after the last retry
Expand Down
2 changes: 1 addition & 1 deletion distributed-lock-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.github.alturkovic</groupId>
<artifactId>distributed-lock</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
</parent>

<artifactId>distributed-lock-api</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion distributed-lock-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.github.alturkovic</groupId>
<artifactId>distributed-lock</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
</parent>

<artifactId>distributed-lock-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ private Object executeLockedMethod(final MethodInvocation invocation, final Lock
final long expiration = intervalConverter.toMillis(context.getLocked().expiration());
try {
Lock lock = retriableLockFactory.generate(context.getLock(), context.getLocked());
context.setToken(lock.acquire(context.getKeys(), context.getLocked().storeId(), expiration));
String token = lock.acquire(context.getKeys(), context.getLocked().storeId(), expiration);
if (StringUtils.isEmpty(token)) {
throw new IllegalStateException("No token acquired");
}
context.setToken(token);
} catch (final Exception e) {
throw new DistributedLockException(String.format("Unable to acquire lock with expression: %s", context.getLocked().expression()), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.github.alturkovic.lock.advice.support.SimpleLock;
import com.github.alturkovic.lock.advice.support.SimpleLock.LockedKey;
import com.github.alturkovic.lock.advice.support.SimpleLocked;
import com.github.alturkovic.lock.exception.DistributedLockException;
import com.github.alturkovic.lock.interval.BeanFactoryAwareIntervalConverter;
import com.github.alturkovic.lock.interval.IntervalConverter;
import com.github.alturkovic.lock.key.SpelKeyGenerator;
Expand All @@ -46,6 +47,7 @@
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.when;

public class LockBeanPostProcessorTest {
Expand Down Expand Up @@ -143,6 +145,12 @@ public void shouldRefreshLock() throws InterruptedException {
lock.getLockMap().clear();
}

@Test
public void shouldThrowWhenNoTokenIsAcquiredAfterRetries() {
assertThatThrownBy(() -> lockedInterface.noToken("!noToken"))
.isInstanceOf(DistributedLockException.class);
}

private interface LockedInterface {

@Locked(prefix = "lock:", expression = "#s", type = SimpleLock.class)
Expand Down Expand Up @@ -171,6 +179,9 @@ private interface LockedInterface {

@SimpleLocked(refresh = @Interval("100"), expiration = @Interval("200"))
void sleep() throws InterruptedException;

@SimpleLocked(expression = "#token")
void noToken(String token);
}

private class LockedInterfaceImpl implements LockedInterface {
Expand Down Expand Up @@ -221,6 +232,10 @@ public void sleep() throws InterruptedException {
TimeUnit.SECONDS.sleep(1);
}

@Override
public void noToken(String token) {
}

public int getStaticValue() {
return 4;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public class SimpleLock implements Lock {
public String acquire(final List<String> keys, final String storeId, final long expiration) {
log.debug("Acquiring lock for keys {} in store {} with expiration: {}", keys, storeId, expiration);

if (keys.contains("!noToken")) {
return null;
}

final List<LockedKey> lockedKeysWithExpiration = lockMap.get(storeId);
if (lockedKeysWithExpiration != null) {
final List<String> locksForStore = lockedKeysWithExpiration.stream()
Expand Down
2 changes: 1 addition & 1 deletion distributed-lock-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.github.alturkovic</groupId>
<artifactId>distributed-lock</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
</parent>

<artifactId>distributed-lock-example</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion distributed-lock-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.github.alturkovic</groupId>
<artifactId>distributed-lock</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
</parent>

<artifactId>distributed-lock-jdbc</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion distributed-lock-mongo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.github.alturkovic</groupId>
<artifactId>distributed-lock</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
</parent>

<artifactId>distributed-lock-mongo</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion distributed-lock-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>com.github.alturkovic</groupId>
<artifactId>distributed-lock</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
</parent>

<artifactId>distributed-lock-redis</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.alturkovic</groupId>
<artifactId>distributed-lock</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
<packaging>pom</packaging>

<name>distributed-lock</name>
Expand Down

0 comments on commit 00a1b5a

Please sign in to comment.