Skip to content

Commit

Permalink
fix(distributed-lock): Return null if all retries fail when using `…
Browse files Browse the repository at this point in the history
…RetriableLock`
  • Loading branch information
alturkovic committed Jun 5, 2023
1 parent 21d3a55 commit f41fc3c
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 27 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.3

- BUGFIX: `RetriableLock` should return `null` if lock is not acquired after the last retry

==== 1.5.2

- BUGFIX: Use dedicated task scheduler for DistributedLock, avoid trying to override custom default scheduler
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.2</version>
<version>1.5.3</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.2</version>
<version>1.5.3</version>
</parent>

<artifactId>distributed-lock-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ private RetryPolicy resolveLockRetryPolicy(final Locked locked) {
final CompositeRetryPolicy compositeRetryPolicy = new CompositeRetryPolicy();

final RetryPolicy timeoutRetryPolicy = resolveTimeoutRetryPolicy(locked);
final RetryPolicy exceptionTypeRetryPolicy = resolveExceptionTypeRetryPolicy();

if (timeoutRetryPolicy == null) {
return null;
}

final RetryPolicy exceptionTypeRetryPolicy = resolveExceptionTypeRetryPolicy();
compositeRetryPolicy.setPolicies(new RetryPolicy[]{timeoutRetryPolicy, exceptionTypeRetryPolicy});
return compositeRetryPolicy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@ public class RetriableLock implements Lock {

@Override
public String acquire(final List<String> keys, final String storeId, final long expiration) {
return retryTemplate.execute(ctx -> {
final String token = lock.acquire(keys, storeId, expiration);
try {
return retryTemplate.execute(ctx -> {
final String token = lock.acquire(keys, storeId, expiration);

if (StringUtils.isEmpty(token)) {
throw new LockNotAvailableException(String.format("Lock not available for keys: %s in store %s", keys, storeId));
}
if (StringUtils.isEmpty(token)) {
throw new LockNotAvailableException(String.format("Lock not available for keys: %s in store %s", keys, storeId));
}

return token;
});
return token;
});
} catch (LockNotAvailableException e) {
return null;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
package com.github.alturkovic.lock.retry;

import com.github.alturkovic.lock.Lock;
import com.github.alturkovic.lock.exception.LockNotAvailableException;
import java.util.Collections;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -36,7 +35,6 @@
import org.springframework.retry.support.RetryTemplate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
Expand Down Expand Up @@ -82,15 +80,16 @@ public void shouldRetryWhenFirstAttemptIsNotSuccessful() {

@Test
public void shouldFailRetryWhenFirstAttemptIsNotSuccessful() {
assertThatThrownBy(() -> {
when(lock.acquire(anyList(), anyString(), anyLong()))
.thenReturn(null);
when(lock.acquire(anyList(), anyString(), anyLong()))
.thenReturn(null);

final RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setRetryPolicy(new NeverRetryPolicy());

final RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setRetryPolicy(new NeverRetryPolicy());
final RetriableLock retriableLock = new RetriableLock(lock, retryTemplate);
final String token = retriableLock.acquire(Collections.singletonList("key"), "defaultStore", 1000L);

final RetriableLock retriableLock = new RetriableLock(lock, retryTemplate);
retriableLock.acquire(Collections.singletonList("key"), "defaultStore", 1000L);
}).isInstanceOf(LockNotAvailableException.class);
assertThat(token).isNull();
verify(lock, times(1)).acquire(anyList(), anyString(), anyLong());
}
}
}
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.2</version>
<version>1.5.3</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.2</version>
<version>1.5.3</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.2</version>
<version>1.5.3</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.2</version>
<version>1.5.3</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.2</version>
<version>1.5.3</version>
<packaging>pom</packaging>

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

0 comments on commit f41fc3c

Please sign in to comment.