Skip to content

Commit 75eed8f

Browse files
committed
Polish for #2690.
Closes #2690 Original pull request: #2691
1 parent ec4370f commit 75eed8f

File tree

8 files changed

+301
-238
lines changed

8 files changed

+301
-238
lines changed

src/main/java/org/springframework/data/redis/cache/RedisCache.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ public class RedisCache extends AbstractValueAdaptingCache {
6969
private final String name;
7070

7171
/**
72-
* Create a new {@link RedisCache}.
72+
* Create a new {@link RedisCache} with the given {@link String name}.
7373
*
7474
* @param name {@link String name} for this {@link Cache}; must not be {@literal null}.
75-
* @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations by executing the
76-
* necessary Redis commands; must not be {@literal null}.
77-
* @param cacheConfiguration {@link RedisCacheConfiguration} applied to this {@link RedisCache} on creation; must not
78-
* be {@literal null}.
75+
* @param cacheWriter {@link RedisCacheWriter} used to perform {@link RedisCache} operations by
76+
* executing the necessary Redis commands; must not be {@literal null}.
77+
* @param cacheConfiguration {@link RedisCacheConfiguration} applied to this {@link RedisCache} on creation;
78+
* must not be {@literal null}.
7979
* @throws IllegalArgumentException if either the given {@link RedisCacheWriter} or {@link RedisCacheConfiguration}
80-
* are {@literal null} or the given {@link String} name for this {@link RedisCache} is {@literal null}.
80+
* are {@literal null} or the given {@link String} name for this {@link RedisCache} is {@literal null}.
8181
*/
8282
protected RedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfiguration) {
8383

@@ -160,15 +160,14 @@ public <T> T get(Object key, Callable<T> valueLoader) {
160160
private <T> T getSynchronized(Object key, Callable<T> valueLoader) {
161161

162162
lock.lock();
163+
163164
try {
164165
ValueWrapper result = get(key);
165166

166167
return result != null ? (T) result.get() : loadCacheValue(key, valueLoader);
167168
} finally {
168169
lock.unlock();
169-
170170
}
171-
172171
}
173172

174173
/**
@@ -422,6 +421,7 @@ private String convertCollectionLikeOrMapKey(Object key, TypeDescriptor source)
422421
target.append("}");
423422

424423
return target.toString();
424+
425425
} else if (source.isCollection() || source.isArray()) {
426426

427427
StringJoiner stringJoiner = new StringJoiner(",");

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* The methods follow as much as possible the Redis names and conventions.
3030
* <p>
3131
* {@link RedisConnection Redis connections}, unlike perhaps their underlying native connection are not Thread-safe and
32-
* should not be shared across multiple threads.
32+
* should not be shared across multiple threads, concurrently or simultaneously.
3333
*
3434
* @author Costin Leau
3535
* @author Christoph Strobl

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

+15-11
Original file line numberDiff line numberDiff line change
@@ -805,12 +805,14 @@ public void returnResourceForSpecificNode(RedisClusterNode node, Object client)
805805
*/
806806
public static class JedisClusterTopologyProvider implements ClusterTopologyProvider {
807807

808-
private final Object lock = new Object();
809-
private final JedisCluster cluster;
810-
private final long cacheTimeMs;
811808
private long time = 0;
809+
810+
private final long cacheTimeMs;
811+
812812
private @Nullable ClusterTopology cached;
813813

814+
private final JedisCluster cluster;
815+
814816
/**
815817
* Create new {@link JedisClusterTopologyProvider}. Uses a default cache timeout of 100 milliseconds.
816818
*
@@ -847,32 +849,34 @@ public ClusterTopology getTopology() {
847849
Map<String, Exception> errors = new LinkedHashMap<>();
848850

849851
List<Entry<String, ConnectionPool>> list = new ArrayList<>(cluster.getClusterNodes().entrySet());
852+
850853
Collections.shuffle(list);
851854

852855
for (Entry<String, ConnectionPool> entry : list) {
853856

854857
try (Connection connection = entry.getValue().getResource()) {
855858

856859
time = System.currentTimeMillis();
860+
857861
Set<RedisClusterNode> nodes = Converters.toSetOfRedisClusterNodes(new Jedis(connection).clusterNodes());
858862

859-
synchronized (lock) {
860-
cached = new ClusterTopology(nodes);
861-
}
863+
cached = new ClusterTopology(nodes);
864+
862865
return cached;
863-
} catch (Exception ex) {
864-
errors.put(entry.getKey(), ex);
866+
867+
} catch (Exception cause) {
868+
errors.put(entry.getKey(), cause);
865869
}
866870
}
867871

868-
StringBuilder sb = new StringBuilder();
872+
StringBuilder stringBuilder = new StringBuilder();
869873

870874
for (Entry<String, Exception> entry : errors.entrySet()) {
871-
sb.append(String.format("\r\n\t- %s failed: %s", entry.getKey(), entry.getValue().getMessage()));
875+
stringBuilder.append(String.format("\r\n\t- %s failed: %s", entry.getKey(), entry.getValue().getMessage()));
872876
}
873877

874878
throw new ClusterStateFailureException(
875-
"Could not retrieve cluster information; CLUSTER NODES returned with error" + sb.toString());
879+
"Could not retrieve cluster information; CLUSTER NODES returned with error" + stringBuilder);
876880
}
877881

878882
/**

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

+24-18
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,26 @@
3333
import org.springframework.util.Assert;
3434

3535
/**
36-
* Connection provider for Cluster connections.
36+
* {@link LettuceConnectionProvider} and {@link RedisClientProvider} for Redis Cluster connections.
3737
*
3838
* @author Mark Paluch
3939
* @author Christoph Strobl
4040
* @author Bruce Cloud
41+
* @author John Blum
4142
* @since 2.0
4243
*/
4344
class ClusterConnectionProvider implements LettuceConnectionProvider, RedisClientProvider {
4445

45-
private final RedisClusterClient client;
46-
private final RedisCodec<?, ?> codec;
47-
private final Optional<ReadFrom> readFrom;
46+
private volatile boolean initialized;
4847

4948
private final Lock lock = new ReentrantLock();
5049

51-
private volatile boolean initialized;
50+
@Nullable
51+
private final ReadFrom readFrom;
52+
53+
private final RedisClusterClient client;
54+
55+
private final RedisCodec<?, ?> codec;
5256

5357
/**
5458
* Create new {@link ClusterConnectionProvider}.
@@ -75,18 +79,22 @@ class ClusterConnectionProvider implements LettuceConnectionProvider, RedisClien
7579

7680
this.client = client;
7781
this.codec = codec;
78-
this.readFrom = Optional.ofNullable(readFrom);
82+
this.readFrom = readFrom;
83+
}
84+
85+
private Optional<ReadFrom> getReadFrom() {
86+
return Optional.ofNullable(this.readFrom);
7987
}
8088

8189
@Override
8290
public <T extends StatefulConnection<?, ?>> CompletableFuture<T> getConnectionAsync(Class<T> connectionType) {
8391

8492
if (!initialized) {
8593

86-
// partitions have to be initialized before asynchronous usage.
87-
// Needs to happen only once. Initialize eagerly if
88-
// blocking is not an options.
94+
// Partitions have to be initialized before asynchronous usage.
95+
// Needs to happen only once. Initialize eagerly if blocking is not an options.
8996
lock.lock();
97+
9098
try {
9199
if (!initialized) {
92100
client.getPartitions();
@@ -100,27 +108,25 @@ class ClusterConnectionProvider implements LettuceConnectionProvider, RedisClien
100108
if (connectionType.equals(StatefulRedisPubSubConnection.class)
101109
|| connectionType.equals(StatefulRedisClusterPubSubConnection.class)) {
102110

103-
return client.connectPubSubAsync(codec) //
104-
.thenApply(connectionType::cast);
111+
return client.connectPubSubAsync(codec).thenApply(connectionType::cast);
105112
}
106113

107114
if (StatefulRedisClusterConnection.class.isAssignableFrom(connectionType)
108115
|| connectionType.equals(StatefulConnection.class)) {
109116

110-
return client.connectAsync(codec) //
111-
.thenApply(connection -> {
112-
113-
readFrom.ifPresent(connection::setReadFrom);
117+
return client.connectAsync(codec).thenApply(connection -> {
118+
getReadFrom().ifPresent(connection::setReadFrom);
114119
return connectionType.cast(connection);
115120
});
116121
}
117122

118-
return LettuceFutureUtils
119-
.failed(new InvalidDataAccessApiUsageException("Connection type " + connectionType + " not supported"));
123+
String message = String.format("Connection type %s not supported", connectionType);
124+
125+
return LettuceFutureUtils.failed(new InvalidDataAccessApiUsageException(message));
120126
}
121127

122128
@Override
123129
public RedisClusterClient getRedisClient() {
124-
return client;
130+
return this.client;
125131
}
126132
}

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,9 @@ protected interface LettuceMultiKeyClusterCommandCallback<T>
554554
static class LettuceClusterNodeResourceProvider implements ClusterNodeResourceProvider, DisposableBean {
555555

556556
private final Lock lock = new ReentrantLock();
557+
557558
private final LettuceConnectionProvider connectionProvider;
559+
558560
private volatile @Nullable StatefulRedisClusterConnection<byte[], byte[]> connection;
559561

560562
LettuceClusterNodeResourceProvider(LettuceConnectionProvider connectionProvider) {
@@ -567,14 +569,16 @@ public RedisClusterCommands<byte[], byte[]> getResourceForSpecificNode(RedisClus
567569

568570
Assert.notNull(node, "Node must not be null");
569571

570-
if (connection == null) {
571-
lock.lock();
572+
if (this.connection == null) {
573+
574+
this.lock.lock();
575+
572576
try {
573-
if (connection == null) {
574-
this.connection = connectionProvider.getConnection(StatefulRedisClusterConnection.class);
577+
if (this.connection == null) {
578+
this.connection = this.connectionProvider.getConnection(StatefulRedisClusterConnection.class);
575579
}
576580
} finally {
577-
lock.unlock();
581+
this.lock.unlock();
578582
}
579583
}
580584

@@ -586,6 +590,7 @@ public void returnResourceForSpecificNode(RedisClusterNode node, Object resource
586590

587591
@Override
588592
public void destroy() throws Exception {
593+
589594
if (this.connection != null) {
590595
this.connectionProvider.release(this.connection);
591596
}

0 commit comments

Comments
 (0)