Skip to content

Commit 9af17ab

Browse files
committed
Polishing.
Move executor from ClusterConfiguration to connection factories as the executor is a Spring concept that isn't tied to endpoint details or the client config. Reorder static factory methods after constructors and property accessors after static factory methods. Inline single-line single-use methods that aren't intended as extension hooks for easier readability. Remove NonNull annotations as default non-nullability is defined on the package level. Simplify tests to use integration tests to avoid excessive mocking. See #2594 Original pull request: #2669
1 parent 5edfbac commit 9af17ab

9 files changed

+1199
-1301
lines changed

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

+9-16
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232
import org.springframework.data.redis.TooManyClusterRedirectionsException;
3333
import org.springframework.data.redis.connection.util.ByteArraySet;
3434
import org.springframework.data.redis.connection.util.ByteArrayWrapper;
35-
import org.springframework.lang.NonNull;
3635
import org.springframework.lang.Nullable;
37-
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
3836
import org.springframework.util.Assert;
3937
import org.springframework.util.CollectionUtils;
4038
import org.springframework.util.ObjectUtils;
@@ -78,7 +76,7 @@ public ClusterCommandExecutor(ClusterTopologyProvider topologyProvider, ClusterN
7876
* @param topologyProvider must not be {@literal null}.
7977
* @param resourceProvider must not be {@literal null}.
8078
* @param exceptionTranslation must not be {@literal null}.
81-
* @param executor can be {@literal null}. Defaulted to {@link ThreadPoolTaskExecutor}.
79+
* @param executor the task executor to null, defaults to {@link SimpleAsyncTaskExecutor} if {@literal null}.
8280
*/
8381
public ClusterCommandExecutor(ClusterTopologyProvider topologyProvider, ClusterNodeResourceProvider resourceProvider,
8482
ExceptionTranslationStrategy exceptionTranslation, @Nullable AsyncTaskExecutor executor) {
@@ -90,11 +88,7 @@ public ClusterCommandExecutor(ClusterTopologyProvider topologyProvider, ClusterN
9088
this.topologyProvider = topologyProvider;
9189
this.resourceProvider = resourceProvider;
9290
this.exceptionTranslationStrategy = exceptionTranslation;
93-
this.executor = resolveTaskExecutor(executor);
94-
}
95-
96-
private @NonNull AsyncTaskExecutor resolveTaskExecutor(@Nullable AsyncTaskExecutor taskExecutor) {
97-
return taskExecutor != null ? taskExecutor : DEFAULT_TASK_EXECUTOR;
91+
this.executor = executor != null ? executor : DEFAULT_TASK_EXECUTOR;
9892
}
9993

10094
/**
@@ -149,9 +143,8 @@ private <S, T> NodeResult<T> executeCommandOnSingleNode(ClusterCommandCallback<S
149143
RuntimeException translatedException = convertToDataAccessException(cause);
150144

151145
if (translatedException instanceof ClusterRedirectException clusterRedirectException) {
152-
return executeCommandOnSingleNode(cmd, topologyProvider.getTopology()
153-
.lookup(clusterRedirectException.getTargetHost(), clusterRedirectException.getTargetPort()),
154-
redirectCount + 1);
146+
return executeCommandOnSingleNode(cmd, topologyProvider.getTopology().lookup(
147+
clusterRedirectException.getTargetHost(), clusterRedirectException.getTargetPort()), redirectCount + 1);
155148
} else {
156149
throw translatedException != null ? translatedException : cause;
157150
}
@@ -182,7 +175,7 @@ private RedisClusterNode lookupNode(RedisClusterNode node) {
182175
* @param cmd must not be {@literal null}.
183176
* @return never {@literal null}.
184177
* @throws ClusterCommandExecutionFailureException if a failure occurs while executing the given
185-
* {@link ClusterCommandCallback command} on any given {@link RedisClusterNode node}.
178+
* {@link ClusterCommandCallback command} on any given {@link RedisClusterNode node}.
186179
*/
187180
public <S, T> MultiNodeResult<T> executeCommandOnAllNodes(final ClusterCommandCallback<S, T> cmd) {
188181
return executeCommandAsyncOnNodes(cmd, getClusterTopology().getActiveMasterNodes());
@@ -193,7 +186,7 @@ public <S, T> MultiNodeResult<T> executeCommandOnAllNodes(final ClusterCommandCa
193186
* @param nodes must not be {@literal null}.
194187
* @return never {@literal null}.
195188
* @throws ClusterCommandExecutionFailureException if a failure occurs while executing the given
196-
* {@link ClusterCommandCallback command} on any given {@link RedisClusterNode node}.
189+
* {@link ClusterCommandCallback command} on any given {@link RedisClusterNode node}.
197190
* @throws IllegalArgumentException in case the node could not be resolved to a topology-known node
198191
*/
199192
public <S, T> MultiNodeResult<T> executeCommandAsyncOnNodes(ClusterCommandCallback<S, T> callback,
@@ -295,7 +288,7 @@ private <T> MultiNodeResult<T> collectResults(Map<NodeExecution, Future<NodeResu
295288
* @param commandCallback must not be {@literal null}.
296289
* @return never {@literal null}.
297290
* @throws ClusterCommandExecutionFailureException if a failure occurs while executing the given
298-
* {@link MultiKeyClusterCommandCallback command}.
291+
* {@link MultiKeyClusterCommandCallback command}.
299292
*/
300293
public <S, T> MultiNodeResult<T> executeMultiKeyCommand(MultiKeyClusterCommandCallback<S, T> commandCallback,
301294
Iterable<byte[]> keys) {
@@ -315,8 +308,8 @@ public <S, T> MultiNodeResult<T> executeMultiKeyCommand(MultiKeyClusterCommandCa
315308

316309
if (entry.getKey().isMaster()) {
317310
for (PositionalKey key : entry.getValue()) {
318-
futures.put(new NodeExecution(entry.getKey(), key), this.executor.submit(() ->
319-
executeMultiKeyCommandOnSingleNode(commandCallback, entry.getKey(), key.getBytes())));
311+
futures.put(new NodeExecution(entry.getKey(), key), this.executor
312+
.submit(() -> executeMultiKeyCommandOnSingleNode(commandCallback, entry.getKey(), key.getBytes())));
320313
}
321314
}
322315
}

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

+8-21
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package org.springframework.data.redis.connection;
1717

18-
import static org.springframework.util.StringUtils.commaDelimitedListToSet;
19-
2018
import java.util.Collection;
2119
import java.util.Collections;
2220
import java.util.HashMap;
@@ -26,7 +24,6 @@
2624

2725
import org.springframework.core.env.MapPropertySource;
2826
import org.springframework.core.env.PropertySource;
29-
import org.springframework.core.task.AsyncTaskExecutor;
3027
import org.springframework.data.redis.connection.RedisConfiguration.ClusterConfiguration;
3128
import org.springframework.data.redis.util.RedisAssertions;
3229
import org.springframework.lang.Nullable;
@@ -36,8 +33,8 @@
3633
import org.springframework.util.StringUtils;
3734

3835
/**
39-
* Configuration class used to set up a {@link RedisConnection} via {@link RedisConnectionFactory} for connecting
40-
* to <a href="https://redis.io/topics/cluster-spec">Redis Cluster</a>. Useful when setting up a highly available Redis
36+
* Configuration class used to set up a {@link RedisConnection} via {@link RedisConnectionFactory} for connecting to
37+
* <a href="https://redis.io/topics/cluster-spec">Redis Cluster</a>. Useful when setting up a highly available Redis
4138
* environment.
4239
*
4340
* @author Christoph Strobl
@@ -52,8 +49,6 @@ public class RedisClusterConfiguration implements RedisConfiguration, ClusterCon
5249

5350
private @Nullable Integer maxRedirects;
5451

55-
private @Nullable AsyncTaskExecutor executor;
56-
5752
private RedisPassword password = RedisPassword.none();
5853

5954
private final Set<RedisNode> clusterNodes;
@@ -103,10 +98,12 @@ public RedisClusterConfiguration(PropertySource<?> propertySource) {
10398
this.clusterNodes = new LinkedHashSet<>();
10499

105100
if (propertySource.containsProperty(REDIS_CLUSTER_NODES_CONFIG_PROPERTY)) {
101+
106102
Object redisClusterNodes = propertySource.getProperty(REDIS_CLUSTER_NODES_CONFIG_PROPERTY);
107-
appendClusterNodes(commaDelimitedListToSet(String.valueOf(redisClusterNodes)));
103+
appendClusterNodes(StringUtils.commaDelimitedListToSet(String.valueOf(redisClusterNodes)));
108104
}
109105
if (propertySource.containsProperty(REDIS_CLUSTER_MAX_REDIRECTS_CONFIG_PROPERTY)) {
106+
110107
Object clusterMaxRedirects = propertySource.getProperty(REDIS_CLUSTER_MAX_REDIRECTS_CONFIG_PROPERTY);
111108
this.maxRedirects = NumberUtils.parseNumber(String.valueOf(clusterMaxRedirects), Integer.class);
112109
}
@@ -204,16 +201,6 @@ public RedisPassword getPassword() {
204201
return password;
205202
}
206203

207-
@Override
208-
public void setAsyncTaskExecutor(@Nullable AsyncTaskExecutor executor) {
209-
this.executor = executor;
210-
}
211-
212-
@Nullable @Override
213-
public AsyncTaskExecutor getAsyncTaskExecutor() {
214-
return this.executor;
215-
}
216-
217204
@Override
218205
public boolean equals(@Nullable Object obj) {
219206

@@ -226,9 +213,9 @@ public boolean equals(@Nullable Object obj) {
226213
}
227214

228215
return ObjectUtils.nullSafeEquals(this.clusterNodes, that.clusterNodes)
229-
&& ObjectUtils.nullSafeEquals(this.maxRedirects, that.maxRedirects)
230-
&& ObjectUtils.nullSafeEquals(this.username, that.username)
231-
&& ObjectUtils.nullSafeEquals(this.password, that.password);
216+
&& ObjectUtils.nullSafeEquals(this.maxRedirects, that.maxRedirects)
217+
&& ObjectUtils.nullSafeEquals(this.username, that.username)
218+
&& ObjectUtils.nullSafeEquals(this.password, that.password);
232219
}
233220

234221
@Override

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

-15
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.function.IntSupplier;
2222
import java.util.function.Supplier;
2323

24-
import org.springframework.core.task.AsyncTaskExecutor;
2524
import org.springframework.lang.Nullable;
2625
import org.springframework.util.Assert;
2726

@@ -346,20 +345,6 @@ interface WithDomainSocket {
346345
*/
347346
interface ClusterConfiguration extends WithPassword {
348347

349-
/**
350-
* Configures the {@link AsyncTaskExecutor} used to execute commands asynchronously across the cluster.
351-
*
352-
* @param executor {@link AsyncTaskExecutor} used to execute commands asynchronously across the cluster.
353-
*/
354-
void setAsyncTaskExecutor(AsyncTaskExecutor executor);
355-
356-
/**
357-
* Returns the configured {@link AsyncTaskExecutor} used to execute commands asynchronously across the cluster.
358-
*
359-
* @return the configured {@link AsyncTaskExecutor} used to execute commands asynchronously across the cluster.
360-
*/
361-
AsyncTaskExecutor getAsyncTaskExecutor();
362-
363348
/**
364349
* Returns an {@link Collections#unmodifiableSet(Set) Set} of {@link RedisNode cluster nodes}.
365350
*

0 commit comments

Comments
 (0)