Skip to content

Commit 0088360

Browse files
christophstroblmp911de
authored andcommitted
DATAREDIS-315 - Remove timeout/password from RedisClusterConfiguration.
Removed duplicate configuration options for timeout and password from RedisClusterConfiguration and use the ones provided via the RedisConnectionFactory. Original pull request: spring-projects#158.
1 parent 0ac118f commit 0088360

File tree

6 files changed

+73
-76
lines changed

6 files changed

+73
-76
lines changed

src/main/asciidoc/reference/redis-cluster.adoc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ public class AppConfig {
6262
6363
.Configuration Properties
6464
- `spring.redis.cluster.nodes`: Comma delimited list of host:port pairs.
65-
- `spring.redis.cluster.timeout`: Timeout (in milliseconds) for cluster operations.
6665
- `spring.redis.cluster.max-redirects`: Number of allowed cluster redirections.
67-
- `spring.redis.cluster.password`: Password to access a password-protected Redis Cluster. Must equal across all nodes.
6866
====
6967

7068
NOTE: The initial configuration points driver libraries to an initial set of cluster nodes. Changes resulting from live cluster reconfiguration will only be kept in the native driver and not be written back to the configuration.

src/main/java/org/springframework/data/redis/connection/RedisClusterConfiguration.java

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2016 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.
@@ -43,14 +43,10 @@
4343
public class RedisClusterConfiguration {
4444

4545
private static final String REDIS_CLUSTER_NODES_CONFIG_PROPERTY = "spring.redis.cluster.nodes";
46-
private static final String REDIS_CLUSTER_TIMEOUT_CONFIG_PROPERTY = "spring.redis.cluster.timeout";
4746
private static final String REDIS_CLUSTER_MAX_REDIRECTS_CONFIG_PROPERTY = "spring.redis.cluster.max-redirects";
48-
private static final String REDIS_CLUSTER_PASSWORD_PROPERTY = "spring.redis.cluster.password";
4947

5048
private Set<RedisNode> clusterNodes;
51-
private Long clusterTimeout;
5249
private Integer maxRedirects;
53-
private String password;
5450

5551
/**
5652
* Creates new {@link RedisClusterConfiguration}.
@@ -100,17 +96,10 @@ public RedisClusterConfiguration(PropertySource<?> propertySource) {
10096
appendClusterNodes(commaDelimitedListToSet(propertySource.getProperty(REDIS_CLUSTER_NODES_CONFIG_PROPERTY)
10197
.toString()));
10298
}
103-
if (propertySource.containsProperty(REDIS_CLUSTER_TIMEOUT_CONFIG_PROPERTY)) {
104-
this.clusterTimeout = NumberUtils.parseNumber(propertySource.getProperty(REDIS_CLUSTER_TIMEOUT_CONFIG_PROPERTY)
105-
.toString(), Long.class);
106-
}
10799
if (propertySource.containsProperty(REDIS_CLUSTER_MAX_REDIRECTS_CONFIG_PROPERTY)) {
108100
this.maxRedirects = NumberUtils.parseNumber(
109101
propertySource.getProperty(REDIS_CLUSTER_MAX_REDIRECTS_CONFIG_PROPERTY).toString(), Integer.class);
110102
}
111-
if (propertySource.containsProperty(REDIS_CLUSTER_PASSWORD_PROPERTY)) {
112-
this.password =propertySource.getProperty(REDIS_CLUSTER_PASSWORD_PROPERTY).toString();
113-
}
114103
}
115104

116105
/**
@@ -153,25 +142,11 @@ public void addClusterNode(RedisNode node) {
153142
* @return
154143
*/
155144
public RedisClusterConfiguration clusterNode(RedisNode node) {
145+
156146
this.clusterNodes.add(node);
157147
return this;
158148
}
159149

160-
/**
161-
* @return
162-
*/
163-
public Long getClusterTimeout() {
164-
return clusterTimeout != null && clusterTimeout > Long.MIN_VALUE ? clusterTimeout : null;
165-
}
166-
167-
/**
168-
*
169-
* @param clusterTimeout
170-
*/
171-
public void setClusterTimeout(long clusterTimeout) {
172-
this.clusterTimeout = clusterTimeout;
173-
}
174-
175150
/**
176151
* @return
177152
*/
@@ -180,29 +155,14 @@ public Integer getMaxRedirects() {
180155
}
181156

182157
/**
183-
*
184158
* @param maxRedirects
185159
*/
186160
public void setMaxRedirects(int maxRedirects) {
161+
187162
Assert.isTrue(maxRedirects >= 0, "MaxRedirects must be greater or equal to 0");
188163
this.maxRedirects = maxRedirects;
189164
}
190165

191-
/**
192-
* @return
193-
*/
194-
public String getPassword() {
195-
return password;
196-
}
197-
198-
/**
199-
*
200-
* @param password can be {@literal null} or empty.
201-
*/
202-
public void setPassword(String password) {
203-
this.password = password;
204-
}
205-
206166
/**
207167
* @param host
208168
* @param port
@@ -235,21 +195,17 @@ private RedisNode readHostAndPortFromString(String hostAndPort) {
235195
* @param password can be {@literal null} or empty.
236196
* @return
237197
*/
238-
private static Map<String, Object> asMap(Collection<String> clusterHostAndPorts, long timeout, int redirects, String password) {
198+
private static Map<String, Object> asMap(Collection<String> clusterHostAndPorts, long timeout, int redirects,
199+
String password) {
239200

240201
notNull(clusterHostAndPorts, "ClusterHostAndPorts must not be null!");
241202

242203
Map<String, Object> map = new HashMap<String, Object>();
243204
map.put(REDIS_CLUSTER_NODES_CONFIG_PROPERTY, StringUtils.collectionToCommaDelimitedString(clusterHostAndPorts));
244-
if (timeout >= 0) {
245-
map.put(REDIS_CLUSTER_TIMEOUT_CONFIG_PROPERTY, Long.valueOf(timeout));
246-
}
205+
247206
if (redirects >= 0) {
248207
map.put(REDIS_CLUSTER_MAX_REDIRECTS_CONFIG_PROPERTY, Integer.valueOf(redirects));
249208
}
250-
if (StringUtils.hasText(password)) {
251-
map.put(REDIS_CLUSTER_PASSWORD_PROPERTY, password);
252-
}
253209

254210
return map;
255211
}

src/main/java/org/springframework/data/redis/connection/jedis/JedisConnectionFactory.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2016 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.
@@ -293,11 +293,10 @@ protected JedisCluster createCluster(RedisClusterConfiguration clusterConfig, Ge
293293
hostAndPort.add(new HostAndPort(node.getHost(), node.getPort()));
294294
}
295295

296-
int timeout = clusterConfig.getClusterTimeout() != null ? clusterConfig.getClusterTimeout().intValue() : this.timeout;
297296
int redirects = clusterConfig.getMaxRedirects() != null ? clusterConfig.getMaxRedirects().intValue() : 5;
298-
299-
if (StringUtils.hasText(clusterConfig.getPassword())) {
300-
throw new UnsupportedOperationException("Jedis does not support password protected Redis Cluster configurations");
297+
298+
if (StringUtils.hasText(getPassword())) {
299+
throw new IllegalArgumentException("Jedis does not support password protected Redis Cluster configurations!");
301300
}
302301

303302
if (poolConfig != null) {

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2016 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.
@@ -40,6 +40,7 @@
4040
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
4141
import org.springframework.data.redis.connection.RedisSentinelConnection;
4242
import org.springframework.util.Assert;
43+
import org.springframework.util.StringUtils;
4344

4445
import com.lambdaworks.redis.AbstractRedisClient;
4546
import com.lambdaworks.redis.LettuceFutures;
@@ -49,7 +50,6 @@
4950
import com.lambdaworks.redis.RedisFuture;
5051
import com.lambdaworks.redis.RedisURI;
5152
import com.lambdaworks.redis.cluster.RedisClusterClient;
52-
import org.springframework.util.StringUtils;
5353

5454
/**
5555
* Connection factory creating <a href="http://github.com/mp911de/lettuce">Lettuce</a>-based connections.
@@ -465,12 +465,10 @@ private AbstractRedisClient createRedisClient() {
465465

466466
List<RedisURI> initialUris = new ArrayList<RedisURI>();
467467
for (RedisNode node : this.clusterConfiguration.getClusterNodes()) {
468-
long timeout = this.clusterConfiguration.getClusterTimeout() != null ? this.clusterConfiguration.getClusterTimeout() : this.timeout;
468+
469469
RedisURI redisURI = new RedisURI(node.getHost(), node.getPort(), timeout, TimeUnit.MILLISECONDS);
470470

471-
if(StringUtils.hasText(this.clusterConfiguration.getPassword())){
472-
redisURI.setPassword(this.clusterConfiguration.getPassword());
473-
}else if(StringUtils.hasText(password)){
471+
if (StringUtils.hasText(password)) {
474472
redisURI.setPassword(password);
475473
}
476474

src/test/java/org/springframework/data/redis/connection/RedisClusterConfigurationUnitTests.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2016 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.
@@ -50,7 +50,6 @@ public void shouldCreateRedisClusterConfigurationCorrectly() {
5050

5151
assertThat(config.getClusterNodes().size(), is(1));
5252
assertThat(config.getClusterNodes(), hasItems(new RedisNode("127.0.0.1", 123)));
53-
assertThat(config.getClusterTimeout(), nullValue());
5453
assertThat(config.getMaxRedirects(), nullValue());
5554
}
5655

@@ -112,7 +111,6 @@ public void shouldNotFailWhenGivenPropertySourceNotContainingRelevantProperties(
112111
RedisClusterConfiguration config = new RedisClusterConfiguration(new MockPropertySource());
113112

114113
assertThat(config.getMaxRedirects(), nullValue());
115-
assertThat(config.getClusterTimeout(), nullValue());
116114
assertThat(config.getClusterNodes().size(), is(0));
117115
}
118116

@@ -123,17 +121,13 @@ public void shouldNotFailWhenGivenPropertySourceNotContainingRelevantProperties(
123121
public void shouldBeCreatedCorrecltyGivenValidPropertySourceWithSingleHostPort() {
124122

125123
MockPropertySource propertySource = new MockPropertySource();
126-
propertySource.setProperty("spring.redis.cluster.timeout", "10");
127124
propertySource.setProperty("spring.redis.cluster.nodes", HOST_AND_PORT_1);
128125
propertySource.setProperty("spring.redis.cluster.max-redirects", "5");
129-
propertySource.setProperty("spring.redis.cluster.password", "foobar");
130126

131127
RedisClusterConfiguration config = new RedisClusterConfiguration(propertySource);
132128

133129
assertThat(config.getMaxRedirects(), is(5));
134-
assertThat(config.getClusterTimeout(), is(10L));
135130
assertThat(config.getClusterNodes(), hasItems(new RedisNode("127.0.0.1", 123)));
136-
assertThat(config.getPassword(), is("foobar"));
137131
}
138132

139133
/**
@@ -143,15 +137,13 @@ public void shouldBeCreatedCorrecltyGivenValidPropertySourceWithSingleHostPort()
143137
public void shouldBeCreatedCorrecltyGivenValidPropertySourceWithMultipleHostPort() {
144138

145139
MockPropertySource propertySource = new MockPropertySource();
146-
propertySource.setProperty("spring.redis.cluster.timeout", "10");
147140
propertySource.setProperty("spring.redis.cluster.nodes",
148141
StringUtils.collectionToCommaDelimitedString(Arrays.asList(HOST_AND_PORT_1, HOST_AND_PORT_2, HOST_AND_PORT_3)));
149142
propertySource.setProperty("spring.redis.cluster.max-redirects", "5");
150143

151144
RedisClusterConfiguration config = new RedisClusterConfiguration(propertySource);
152145

153146
assertThat(config.getMaxRedirects(), is(5));
154-
assertThat(config.getClusterTimeout(), is(10L));
155147
assertThat(config.getClusterNodes(),
156148
hasItems(new RedisNode("127.0.0.1", 123), new RedisNode("localhost", 456), new RedisNode("localhost", 789)));
157149
}

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactoryUnitTests.java

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2016 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,32 +15,86 @@
1515
*/
1616
package org.springframework.data.redis.connection.lettuce;
1717

18+
import static org.hamcrest.core.Is.*;
19+
import static org.hamcrest.core.IsEqual.*;
1820
import static org.hamcrest.core.IsInstanceOf.*;
1921
import static org.junit.Assert.*;
2022
import static org.springframework.test.util.ReflectionTestUtils.*;
2123

24+
import java.util.concurrent.TimeUnit;
25+
26+
import org.junit.Before;
2227
import org.junit.Test;
2328
import org.springframework.data.redis.connection.RedisClusterConfiguration;
2429

30+
import com.lambdaworks.redis.AbstractRedisClient;
31+
import com.lambdaworks.redis.RedisURI;
2532
import com.lambdaworks.redis.cluster.RedisClusterClient;
2633

2734
/**
2835
* @author Christoph Strobl
2936
*/
3037
public class LettuceConnectionFactoryUnitTests {
3138

32-
private static final RedisClusterConfiguration CLUSTER_CONFIG = new RedisClusterConfiguration().clusterNode(
33-
"127.0.0.1", 6379).clusterNode("127.0.0.1", 6380);
39+
RedisClusterConfiguration clusterConfig;
40+
41+
@Before
42+
public void setUp() {
43+
clusterConfig = new RedisClusterConfiguration().clusterNode("127.0.0.1", 6379).clusterNode("127.0.0.1", 6380);
44+
}
3445

3546
/**
3647
* @see DATAREDIS-315
3748
*/
3849
@Test
3950
public void shouldInitClientCorrectlyWhenClusterConfigPresent() {
4051

41-
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(CLUSTER_CONFIG);
52+
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(clusterConfig);
4253
connectionFactory.afterPropertiesSet();
4354

4455
assertThat(getField(connectionFactory, "client"), instanceOf(RedisClusterClient.class));
4556
}
57+
58+
/**
59+
* @see DATAREDIS-315
60+
*/
61+
@Test
62+
@SuppressWarnings("unchecked")
63+
public void timeoutShouldBeSetCorrectlyOnClusterClient() {
64+
65+
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(clusterConfig);
66+
connectionFactory.setTimeout(1000);
67+
connectionFactory.afterPropertiesSet();
68+
69+
AbstractRedisClient client = (AbstractRedisClient) getField(connectionFactory, "client");
70+
assertThat(client, instanceOf(RedisClusterClient.class));
71+
72+
Iterable<RedisURI> initialUris = (Iterable<RedisURI>) getField(client, "initialUris");
73+
74+
for (RedisURI uri : initialUris) {
75+
assertThat(uri.getTimeout(), is(equalTo(connectionFactory.getTimeout())));
76+
assertThat(uri.getUnit(), is(equalTo(TimeUnit.MILLISECONDS)));
77+
}
78+
}
79+
80+
/**
81+
* @see DATAREDIS-315
82+
*/
83+
@Test
84+
@SuppressWarnings("unchecked")
85+
public void passwordShouldBeSetCorrectlyOnClusterClient() {
86+
87+
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(clusterConfig);
88+
connectionFactory.setPassword("o_O");
89+
connectionFactory.afterPropertiesSet();
90+
91+
AbstractRedisClient client = (AbstractRedisClient) getField(connectionFactory, "client");
92+
assertThat(client, instanceOf(RedisClusterClient.class));
93+
94+
Iterable<RedisURI> initialUris = (Iterable<RedisURI>) getField(client, "initialUris");
95+
96+
for (RedisURI uri : initialUris) {
97+
assertThat(uri.getPassword(), is(equalTo(connectionFactory.getPassword().toCharArray())));
98+
}
99+
}
46100
}

0 commit comments

Comments
 (0)