Skip to content

Commit 444b290

Browse files
christophstroblodrotbohm
authored andcommitted
DATAREDIS-348 - Polishing.
Fixed connection problems leaving sockets open. Added missing JUnit rules for Sentinels. Added missing author information update, license header and Javadoc. Upgraded test script to use Redis 3.0.2. Upgrade test script to shut down Redis in case of test errors. Added Redis 3.0 to TestProfileValueSource. Enabled ZRANGEBYLEX tests for lettuce. Original pull request: #144. Related pull request: #104.
1 parent 7426b27 commit 444b290

22 files changed

+226
-191
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
REDIS_VERSION:=2.8.19
15+
REDIS_VERSION:=3.0.2
1616

1717
#######
1818
# Redis
@@ -95,5 +95,5 @@ stop: redis-stop sentinel-stop
9595
test:
9696
$(MAKE) start
9797
sleep 2
98-
$(PWD)/gradlew clean build -DrunLongTests=true -S
98+
-$(PWD)/gradlew clean build -DrunLongTests=true -S
9999
$(MAKE) stop

src/asciidoc/reference/redis.adoc

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,25 @@ NOTE: Please note that currently only http://github.com/xetorthio/jedis[Jedis] a
169169

170170
[source,java]
171171
----
172+
/**
173+
* jedis
174+
*/
172175
@Bean
173176
public RedisConnectionFactory jedisConnectionFactory() {
174177
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration() .master("mymaster")
175178
.sentinel("127.0.0.1", 26379) .sentinel("127.0.0.1", 26380);
176179
return new JedisConnectionFactory(sentinelConfig);
177180
}
181+
182+
/**
183+
* lettuce
184+
*/
185+
@Bean
186+
public RedisConnectionFactory lettuceConnectionFactory() {
187+
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration().master("mymaster")
188+
.sentinel("127.0.0.1", 26379) .sentinel("127.0.0.1", 26380);
189+
return new LettuceConnectionFactory(sentinelConfig);
190+
}
178191
----
179192

180193
[TIP]
@@ -185,16 +198,6 @@ public RedisConnectionFactory jedisConnectionFactory() {
185198
- `spring.redis.sentinel.master`: name of the master node.
186199
- `spring.redis.sentinel.nodes`: Comma delimited list of host:port pairs.
187200
====
188-
=======
189-
[source,java]
190-
----
191-
@Bean
192-
public RedisConnectionFactory lettuceConnectionFactory() {
193-
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration() .master("mymaster")
194-
.sentinel("127.0.0.1", 26379) .sentinel("127.0.0.1", 26380);
195-
return new LettuceConnectionFactory(sentinelConfig);
196-
}
197-
----
198201

199202
Sometimes direct interaction with the one of the Sentinels is required. Using `RedisConnectionFactory.getSentinelConnection()` or `RedisConnection.getSentinelCommands()` gives you access to the first active Sentinel configured.
200203

src/main/java/org/springframework/data/redis/connection/lettuce/AuthenticatingRedisClient.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,14 +18,17 @@
1818
import com.lambdaworks.redis.RedisAsyncConnection;
1919
import com.lambdaworks.redis.RedisClient;
2020
import com.lambdaworks.redis.RedisConnection;
21+
import com.lambdaworks.redis.RedisURI;
2122
import com.lambdaworks.redis.codec.RedisCodec;
2223
import com.lambdaworks.redis.pubsub.RedisPubSubConnection;
2324

2425
/**
2526
* Extension of {@link RedisClient} that calls auth on all new connections using the supplied credentials
2627
*
2728
* @author Jennifer Hickey
28-
* @deprecated Use password in RedisURI
29+
* @author Mar Paluch
30+
* @author Christoph Strobl
31+
* @deprecated since 1.6 - Please use {@link RedisURI#setPassword(String)}
2932
*/
3033
@Deprecated
3134
public class AuthenticatingRedisClient extends RedisClient {

src/main/java/org/springframework/data/redis/connection/lettuce/DefaultLettucePool.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717

1818
import java.util.concurrent.TimeUnit;
1919

20-
import com.lambdaworks.redis.RedisURI;
2120
import org.apache.commons.pool2.BasePooledObjectFactory;
2221
import org.apache.commons.pool2.PooledObject;
2322
import org.apache.commons.pool2.impl.DefaultPooledObject;
@@ -30,6 +29,7 @@
3029

3130
import com.lambdaworks.redis.RedisAsyncConnection;
3231
import com.lambdaworks.redis.RedisClient;
32+
import com.lambdaworks.redis.RedisURI;
3333

3434
/**
3535
* Default implementation of {@link LettucePool}.
@@ -72,6 +72,7 @@ public DefaultLettucePool(String hostName, int port) {
7272
* based on sentinels.
7373
*
7474
* @param sentinelConfiguration The Sentinel configuration
75+
* @since 1.6
7576
*/
7677
public DefaultLettucePool(RedisSentinelConfiguration sentinelConfiguration) {
7778
this.sentinelConfiguration = sentinelConfiguration;
@@ -92,7 +93,7 @@ public DefaultLettucePool(String hostName, int port, GenericObjectPoolConfig poo
9293

9394
/**
9495
* @return true when {@link RedisSentinelConfiguration} is present.
95-
* @since 1.5
96+
* @since 1.6
9697
*/
9798
public boolean isRedisSentinelAware() {
9899
return sentinelConfiguration != null;
@@ -106,12 +107,11 @@ public void afterPropertiesSet() {
106107
}
107108

108109
/**
109-
*
110110
* @return a RedisURI pointing either to a single Redis host or containing a set of sentinels.
111111
*/
112112
private RedisURI getRedisURI() {
113113

114-
if(isRedisSentinelAware()) {
114+
if (isRedisSentinelAware()) {
115115
return LettuceConverters.sentinelConfigurationToRedisURI(sentinelConfiguration);
116116
}
117117

@@ -120,7 +120,7 @@ private RedisURI getRedisURI() {
120120

121121
private RedisURI createSimpleHostRedisURI() {
122122
RedisURI.Builder builder = RedisURI.Builder.redis(hostName, port);
123-
if(password != null) {
123+
if (password != null) {
124124
builder.withPassword(password);
125125
}
126126
builder.withTimeout(timeout, TimeUnit.MILLISECONDS);
@@ -162,7 +162,6 @@ public void destroy() {
162162
}
163163

164164
/**
165-
*
166165
* @return The Redis client
167166
*/
168167
public RedisClient getClient() {

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3568,6 +3568,10 @@ private RedisURI getRedisURI(RedisNode node) {
35683568
return RedisURI.Builder.redis(node.getHost(), node.getPort()).build();
35693569
}
35703570

3571+
/*
3572+
* (non-Javadoc)
3573+
* @see org.springframework.data.redis.connection.AbstractRedisConnection#getSentinelConnection(org.springframework.data.redis.connection.RedisNode)
3574+
*/
35713575
@Override
35723576
protected RedisSentinelConnection getSentinelConnection(RedisNode sentinel) {
35733577
RedisSentinelAsyncConnection<String, String> connection = client.connectSentinelAsync(getRedisURI(sentinel));

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2014 the original author or authors.
2+
* Copyright 2011-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,9 +18,6 @@
1818

1919
import java.util.concurrent.TimeUnit;
2020

21-
import com.lambdaworks.redis.LettuceFutures;
22-
import com.lambdaworks.redis.RedisFuture;
23-
import com.lambdaworks.redis.RedisURI;
2421
import org.apache.commons.logging.Log;
2522
import org.apache.commons.logging.LogFactory;
2623
import org.springframework.beans.factory.DisposableBean;
@@ -36,9 +33,12 @@
3633
import org.springframework.data.redis.connection.RedisSentinelConnection;
3734
import org.springframework.util.Assert;
3835

36+
import com.lambdaworks.redis.LettuceFutures;
3937
import com.lambdaworks.redis.RedisAsyncConnection;
4038
import com.lambdaworks.redis.RedisClient;
4139
import com.lambdaworks.redis.RedisException;
40+
import com.lambdaworks.redis.RedisFuture;
41+
import com.lambdaworks.redis.RedisURI;
4242

4343
/**
4444
* Connection factory creating <a href="http://github.com/mp911de/lettuce">Lettuce</a>-based connections.
@@ -55,6 +55,7 @@
5555
* @author Costin Leau
5656
* @author Jennifer Hickey
5757
* @author Thomas Darimont
58+
* @author Mark Paluch
5859
*/
5960
public class LettuceConnectionFactory implements InitializingBean, DisposableBean, RedisConnectionFactory {
6061

@@ -98,7 +99,7 @@ public LettuceConnectionFactory(String host, int port) {
9899
* Constructs a new {@link LettuceConnectionFactory} instance using the given {@link RedisSentinelConfiguration}
99100
*
100101
* @param sentinelConfiguration
101-
* @since 1.5
102+
* @since 1.6
102103
*/
103104
public LettuceConnectionFactory(RedisSentinelConfiguration sentinelConfiguration) {
104105
this.sentinelConfiguration = sentinelConfiguration;
@@ -156,11 +157,11 @@ public void validateConnection() {
156157
try {
157158
RedisFuture<String> ping = connection.ping();
158159
LettuceFutures.awaitAll(timeout, TimeUnit.MILLISECONDS, ping);
159-
if(PING_REPLY.equalsIgnoreCase(ping.get())) {
160+
if (PING_REPLY.equalsIgnoreCase(ping.get())) {
160161
valid = true;
161162
}
162163
} catch (Exception e) {
163-
log.debug("Validation failed", e);
164+
log.debug("Validation failed", e);
164165
}
165166
}
166167

@@ -313,15 +314,19 @@ public void setPassword(String password) {
313314

314315
/**
315316
* Returns the shutdown timeout for shutting down the RedisClient (in milliseconds).
317+
*
316318
* @return shutdown timeout
319+
* @since 1.6
317320
*/
318321
public long getShutdownTimeout() {
319322
return shutdownTimeout;
320323
}
321324

322325
/**
323326
* Sets the shutdown timeout for shutting down the RedisClient (in milliseconds).
327+
*
324328
* @param shutdownTimeout the shutdown timeout
329+
* @since 1.6
325330
*/
326331
public void setShutdownTimeout(long shutdownTimeout) {
327332
this.shutdownTimeout = shutdownTimeout;
@@ -379,7 +384,7 @@ protected RedisAsyncConnection<byte[], byte[]> createLettuceConnector() {
379384

380385
private RedisClient createRedisClient() {
381386

382-
if(isRedisSentinelAware()) {
387+
if (isRedisSentinelAware()) {
383388
RedisURI redisURI = getSentinelRedisURI();
384389
return new RedisClient(redisURI);
385390
}
@@ -389,7 +394,7 @@ private RedisClient createRedisClient() {
389394
}
390395

391396
RedisURI.Builder builder = RedisURI.Builder.redis(hostName, port);
392-
if(password != null) {
397+
if (password != null) {
393398
builder.withPassword(password);
394399
}
395400
builder.withTimeout(timeout, TimeUnit.MILLISECONDS);

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/test/java/org/springframework/data/redis/RedisTestProfileValueSource.java

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2014 the original author or authors.
2+
* Copyright 2011-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,10 +15,13 @@
1515
*/
1616
package org.springframework.data.redis;
1717

18-
import org.springframework.data.redis.connection.RedisConnection;
19-
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
18+
import java.io.IOException;
19+
20+
import org.springframework.data.redis.connection.jedis.JedisConverters;
2021
import org.springframework.test.annotation.ProfileValueSource;
2122

23+
import redis.clients.jedis.Jedis;
24+
2225
/**
2326
* Implementation of {@link ProfileValueSource} that handles profile value name "redisVersion" by checking the current
2427
* version of Redis. 2.4.x will be returned as "2.4" and 2.6.x will be returned as "2.6". Any other version found will
@@ -34,6 +37,7 @@ public class RedisTestProfileValueSource implements ProfileValueSource {
3437
private static final String REDIS_24 = "2.4";
3538
private static final String REDIS_26 = "2.6";
3639
private static final String REDIS_28 = "2.8";
40+
private static final String REDIS_30 = "3.0";
3741
private static final String REDIS_VERSION_KEY = "redisVersion";
3842

3943
private static RedisTestProfileValueSource INSTANCE;
@@ -46,22 +50,35 @@ public class RedisTestProfileValueSource implements ProfileValueSource {
4650

4751
private static Version tryDetectRedisVersionOrReturn(Version fallbackVersion) {
4852

49-
try {
50-
JedisConnectionFactory factory = new JedisConnectionFactory();
51-
factory.afterPropertiesSet();
53+
Version version = fallbackVersion;
5254

53-
RedisConnection connection = factory.getConnection();
54-
Version redisVersion = RedisVersionUtils.getRedisVersion(connection);
55+
Jedis jedis = new Jedis(SettingsUtils.getHost(), SettingsUtils.getPort(), 100);
56+
try {
5557

56-
connection.close();
57-
factory.destroy();
58+
jedis.connect();
59+
String info = jedis.info();
60+
String versionString = (String) JedisConverters.stringToProps().convert(info).get("redis_version");
61+
62+
version = RedisVersionUtils.parseVersion(versionString);
63+
} finally {
64+
65+
try {
66+
// force socket to be closed
67+
jedis.getClient().quit();
68+
jedis.getClient().getSocket().close();
69+
try {
70+
// need to wait a bit
71+
Thread.sleep(100);
72+
} catch (InterruptedException e) {
73+
// just ignore it
74+
}
75+
} catch (IOException e1) {
76+
// ignore as well
77+
}
78+
jedis.close();
5879

59-
return redisVersion;
60-
} catch (Exception ex) {
61-
System.err.println("Couldn't detect redis version!");
6280
}
63-
64-
return fallbackVersion;
81+
return version;
6582
}
6683

6784
public RedisTestProfileValueSource() {
@@ -74,6 +91,10 @@ public String get(String key) {
7491
return System.getProperty(key);
7592
}
7693

94+
if (redisVersion.compareTo(RedisVersionUtils.parseVersion(REDIS_30)) >= 0) {
95+
return REDIS_30;
96+
}
97+
7798
if (redisVersion.compareTo(RedisVersionUtils.parseVersion(REDIS_28)) >= 0) {
7899
return REDIS_28;
79100
}

src/test/java/org/springframework/data/redis/SettingsUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public abstract class SettingsUtils {
2525
private static final Properties SETTINGS;
2626

2727
static {
28-
DEFAULTS.put("host", "localhost");
28+
DEFAULTS.put("host", "127.0.0.1");
2929
DEFAULTS.put("port", "6379");
3030

3131
SETTINGS = new Properties(DEFAULTS);

0 commit comments

Comments
 (0)