Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for lock in DefaultRedisCacheWriter.doLock() #2879

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
* @author Mark Paluch
* @author André Prata
* @author John Blum
* @author ChanYoung Joung
* @since 2.0
*/
class DefaultRedisCacheWriter implements RedisCacheWriter {
Expand Down Expand Up @@ -319,12 +320,15 @@ void lock(String name) {
}

@Nullable
private Boolean doLock(String name, Object contextualKey, @Nullable Object contextualValue,
RedisConnection connection) {
protected Boolean doLock(String name, Object contextualKey, @Nullable Object contextualValue,
RedisConnection connection) {

Expiration expiration = Expiration.from(this.lockTtl.getTimeToLive(contextualKey, contextualValue));

return connection.stringCommands().set(createCacheLockKey(name), new byte[0], expiration, SetOption.SET_IF_ABSENT);
while (!ObjectUtils.nullSafeEquals(connection.stringCommands().set(createCacheLockKey(name), new byte[0], expiration, SetOption.SET_IF_ABSENT),true)) {
checkAndPotentiallyWaitUntilUnlocked(name, connection);
}
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
import org.springframework.data.redis.test.condition.RedisDriver;
import org.springframework.data.redis.test.extension.parametrized.MethodSource;
import org.springframework.data.redis.test.extension.parametrized.ParameterizedRedisTest;
import org.springframework.lang.Nullable;

/**
* Integration tests for {@link DefaultRedisCacheWriter}.
*
* @author Christoph Strobl
* @author Mark Paluch
* @author ChanYoung Joung
*/
@MethodSource("testParams")
public class DefaultRedisCacheWriterTests {
Expand Down Expand Up @@ -419,6 +421,45 @@ void noOpStatisticsCollectorReturnsEmptyStatsInstance() {
assertThat(stats.getPuts()).isZero();
}

@ParameterizedRedisTest
void doLockShouldGetLock() throws InterruptedException {

int threadCount = 3;
CountDownLatch beforeWrite = new CountDownLatch(threadCount);
CountDownLatch afterWrite = new CountDownLatch(threadCount);

DefaultRedisCacheWriter cw = new DefaultRedisCacheWriter(connectionFactory, Duration.ofMillis(50),
BatchStrategies.keys()){
@Nullable
protected Boolean doLock(String name, Object contextualKey, @Nullable Object contextualValue,
RedisConnection connection) {
Boolean doLock = super.doLock(name, contextualKey, contextualValue, connection);
assertThat(doLock).isTrue();
return doLock;
}
};

cw.lock(CACHE_NAME);

for (int i = 0; i < threadCount; i++) {
Thread th = new Thread(() -> {
beforeWrite.countDown();
cw.putIfAbsent(CACHE_NAME, binaryCacheKey, binaryCacheValue, Duration.ZERO);
afterWrite.countDown();
});

th.start();
}

beforeWrite.await();

Thread.sleep(200);

cw.unlock(CACHE_NAME);
afterWrite.await();

}

private void doWithConnection(Consumer<RedisConnection> callback) {

try (RedisConnection connection = connectionFactory.getConnection()) {
Expand Down
Loading