Skip to content

Commit d3123e8

Browse files
Chenrujie-85c00887447tishun
authored
Improve the performance of obtaining write connections through double-check locks. (#3228)
* Improves the performance of obtaining write connections. Signed-off-by: c00887447 <[email protected]> * Polishing * Formatter carelessly forgotten --------- Signed-off-by: c00887447 <[email protected]> Co-authored-by: c00887447 <[email protected]> Co-authored-by: Tihomir Mateev <[email protected]>
1 parent bc26105 commit d3123e8

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

src/main/java/io/lettuce/core/cluster/PooledClusterConnectionProvider.java

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ class PooledClusterConnectionProvider<K, V>
7474

7575
private static final InternalLogger logger = InternalLoggerFactory.getInstance(PooledClusterConnectionProvider.class);
7676

77-
// Contains NodeId-identified and HostAndPort-identified connections.
7877
private final Lock stateLock = new ReentrantLock();
7978

8079
private final boolean debugEnabled = logger.isDebugEnabled();
@@ -158,46 +157,38 @@ public CompletableFuture<StatefulRedisConnection<K, V>> getConnectionAsync(Conne
158157

159158
private CompletableFuture<StatefulRedisConnection<K, V>> getWriteConnection(int slot) {
160159

161-
CompletableFuture<StatefulRedisConnection<K, V>> writer;// avoid races when reconfiguring partitions.
162-
163-
stateLock.lock();
164-
try {
165-
writer = writers[slot];
166-
} finally {
167-
stateLock.unlock();
160+
CompletableFuture<StatefulRedisConnection<K, V>> writer = writers[slot];
161+
if (writer != null) {
162+
return writer;
168163
}
169164

170-
if (writer == null) {
171-
RedisClusterNode master = partitions.getMasterBySlot(slot);
172-
if (master == null) {
173-
clusterEventListener.onUncoveredSlot(slot);
174-
return Futures.failed(new PartitionSelectorException("Cannot determine a partition for slot " + slot + ".",
175-
partitions.clone()));
176-
}
165+
RedisClusterNode master = partitions.getMasterBySlot(slot);
166+
if (master == null) {
167+
clusterEventListener.onUncoveredSlot(slot);
168+
return Futures.failed(
169+
new PartitionSelectorException("Cannot determine a partition for slot " + slot + ".", partitions.clone()));
170+
}
177171

178-
// Use always host and port for slot-oriented operations. We don't want to get reconnected on a different
179-
// host because the nodeId can be handled by a different host.
180-
RedisURI uri = master.getUri();
181-
ConnectionKey key = new ConnectionKey(ConnectionIntent.WRITE, uri.getHost(), uri.getPort());
172+
// Use always host and port for slot-oriented operations. We don't want to get reconnected on a different
173+
// host because the nodeId can be handled by a different host.
174+
RedisURI uri = master.getUri();
175+
ConnectionKey key = new ConnectionKey(ConnectionIntent.WRITE, uri.getHost(), uri.getPort());
182176

183-
ConnectionFuture<StatefulRedisConnection<K, V>> future = getConnectionAsync(key);
177+
ConnectionFuture<StatefulRedisConnection<K, V>> future = getConnectionAsync(key);
184178

185-
return future.thenApply(connection -> {
179+
return future.thenApply(connection -> {
186180

187-
stateLock.lock();
188-
try {
189-
if (writers[slot] == null) {
190-
writers[slot] = CompletableFuture.completedFuture(connection);
191-
}
192-
} finally {
193-
stateLock.unlock();
181+
stateLock.lock();
182+
try {
183+
if (writers[slot] == null) {
184+
writers[slot] = CompletableFuture.completedFuture(connection);
194185
}
186+
} finally {
187+
stateLock.unlock();
188+
}
195189

196-
return connection;
197-
}).toCompletableFuture();
198-
}
199-
200-
return writer;
190+
return connection;
191+
}).toCompletableFuture();
201192
}
202193

203194
private CompletableFuture<StatefulRedisConnection<K, V>> getReadConnection(int slot) {
@@ -654,7 +645,6 @@ public ReadFrom getReadFrom() {
654645
}
655646

656647
/**
657-
*
658648
* @return number of connections.
659649
*/
660650
long getConnectionCount() {

0 commit comments

Comments
 (0)