From eaf249af90c729c777cec16e283f2112990f4f16 Mon Sep 17 00:00:00 2001
From: Thomas Darimont
Date: Mon, 3 Feb 2014 22:29:33 +0100
Subject: [PATCH] DATAREDIS-188 - Infinite loop renaming a non-existent
Collection when using Lettuce.
We run into an infinite loop in org.springframework.data.redis.support.collections.CollectionUtils.rename(K, K, RedisOperations) if no value was associated with the key which leads to operations.hasKey(...) always returning false which prevents the actual renaming. RedisCollections will only send rename command if values are present as empty collections cannot be stored and therefor cannot be renamed.
Original pull request: #28
---
.../data/redis/core/BoundKeyOperations.java | 6 +-
.../collections/AbstractRedisCollection.java | 92 +++++++++-----
.../redis/support/BoundKeyOperationsTest.java | 54 +++++---
.../AbstractRedisCollectionUnitTests.java | 119 ++++++++++++++++++
4 files changed, 216 insertions(+), 55 deletions(-)
create mode 100644 src/test/java/org/springframework/data/redis/support/collections/AbstractRedisCollectionUnitTests.java
diff --git a/src/main/java/org/springframework/data/redis/core/BoundKeyOperations.java b/src/main/java/org/springframework/data/redis/core/BoundKeyOperations.java
index 15dfef6b6..abd4326f8 100644
--- a/src/main/java/org/springframework/data/redis/core/BoundKeyOperations.java
+++ b/src/main/java/org/springframework/data/redis/core/BoundKeyOperations.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2011-2013 the original author or authors.
+ * Copyright 2011-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import org.springframework.data.redis.connection.DataType;
*
*
* @author Costin Leau
+ * @author Christoph Strobl
*/
public interface BoundKeyOperations {
@@ -77,7 +78,8 @@ public interface BoundKeyOperations {
Boolean persist();
/**
- * Renames the key.
+ * Renames the key.
+ * Note: The new name for empty collections will be propagated on add of first element.
*
* @param newKey new key
*/
diff --git a/src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java b/src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java
index 1dd7f526f..b79622fc9 100644
--- a/src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java
+++ b/src/main/java/org/springframework/data/redis/support/collections/AbstractRedisCollection.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2011-2013 the original author or authors.
+ * Copyright 2011-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@ import org.springframework.data.redis.core.RedisOperations;
* works only with normal, non-pipeline/multi-exec connections as it requires a reply to be sent right away.
*
* @author Costin Leau
+ * @author Christoph Strobl
*/
public abstract class AbstractRedisCollection extends AbstractCollection implements RedisCollection {
@@ -48,6 +49,7 @@ public abstract class AbstractRedisCollection extends AbstractCollection i
return operations;
}
+ @Override
public boolean addAll(Collection extends E> c) {
boolean modified = false;
for (E e : c) {
@@ -56,10 +58,7 @@ public abstract class AbstractRedisCollection extends AbstractCollection i
return modified;
}
- public abstract boolean add(E e);
-
- public abstract void clear();
-
+ @Override
public boolean containsAll(Collection> c) {
boolean contains = true;
for (Object object : c) {
@@ -68,8 +67,7 @@ public abstract class AbstractRedisCollection extends AbstractCollection i
return contains;
}
- public abstract boolean remove(Object o);
-
+ @Override
public boolean removeAll(Collection> c) {
boolean modified = false;
for (Object object : c) {
@@ -78,6 +76,60 @@ public abstract class AbstractRedisCollection extends AbstractCollection i
return modified;
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.redis.core.BoundKeyOperations#expire(long, java.util.concurrent.TimeUnit)
+ */
+ @Override
+ public Boolean expire(long timeout, TimeUnit unit) {
+ return operations.expire(key, timeout, unit);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.redis.core.BoundKeyOperations#expireAt(java.util.Date)
+ */
+ @Override
+ public Boolean expireAt(Date date) {
+ return operations.expireAt(key, date);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.redis.core.BoundKeyOperations#getExpire()
+ */
+ @Override
+ public Long getExpire() {
+ return operations.getExpire(key);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.redis.core.BoundKeyOperations#persist()
+ */
+ @Override
+ public Boolean persist() {
+ return operations.persist(key);
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.redis.core.BoundKeyOperations#rename(java.lang.Object)
+ */
+ @Override
+ public void rename(final String newKey) {
+ if (!this.isEmpty()) {
+ CollectionUtils.rename(key, newKey, operations);
+ }
+ key = newKey;
+ }
+
+ protected void checkResult(Object obj) {
+ if (obj == null) {
+ throw new IllegalStateException("Cannot read collection with Redis connection in pipeline/multi-exec mode");
+ }
+ }
+
public boolean equals(Object o) {
if (o == this)
return true;
@@ -105,30 +157,4 @@ public abstract class AbstractRedisCollection extends AbstractCollection i
return sb.toString();
}
- public Boolean expire(long timeout, TimeUnit unit) {
- return operations.expire(key, timeout, unit);
- }
-
- public Boolean expireAt(Date date) {
- return operations.expireAt(key, date);
- }
-
- public Long getExpire() {
- return operations.getExpire(key);
- }
-
- public Boolean persist() {
- return operations.persist(key);
- }
-
- public void rename(final String newKey) {
- CollectionUtils.rename(key, newKey, operations);
- key = newKey;
- }
-
- protected void checkResult(Object obj) {
- if (obj == null) {
- throw new IllegalStateException("Cannot read collection with Redis connection in pipeline/multi-exec mode");
- }
- }
}
diff --git a/src/test/java/org/springframework/data/redis/support/BoundKeyOperationsTest.java b/src/test/java/org/springframework/data/redis/support/BoundKeyOperationsTest.java
index af8bb4a8f..7602103af 100644
--- a/src/test/java/org/springframework/data/redis/support/BoundKeyOperationsTest.java
+++ b/src/test/java/org/springframework/data/redis/support/BoundKeyOperationsTest.java
@@ -19,21 +19,23 @@ import static org.junit.Assert.*;
import static org.junit.Assume.*;
import java.util.Collection;
-import java.util.List;
import java.util.Map;
-import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.AfterClass;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
+import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.ConnectionFactoryTracker;
import org.springframework.data.redis.ObjectFactory;
import org.springframework.data.redis.connection.ConnectionUtils;
+import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.BoundKeyOperations;
+import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
@@ -42,13 +44,20 @@ import org.springframework.data.redis.support.atomic.RedisAtomicLong;
* @author Costin Leau
* @author Jennifer Hickey
* @author Thomas Darimont
+ * @author Christoph Strobl
*/
@RunWith(Parameterized.class)
public class BoundKeyOperationsTest {
+
+ @SuppressWarnings("rawtypes")//
private BoundKeyOperations keyOps;
+
private ObjectFactory