diff --git a/src/main/java/io/lettuce/core/cluster/PooledClusterConnectionProvider.java b/src/main/java/io/lettuce/core/cluster/PooledClusterConnectionProvider.java index 56ece5d733..a82dee16c3 100644 --- a/src/main/java/io/lettuce/core/cluster/PooledClusterConnectionProvider.java +++ b/src/main/java/io/lettuce/core/cluster/PooledClusterConnectionProvider.java @@ -74,7 +74,6 @@ class PooledClusterConnectionProvider private static final InternalLogger logger = InternalLoggerFactory.getInstance(PooledClusterConnectionProvider.class); - // Contains NodeId-identified and HostAndPort-identified connections. private final Lock stateLock = new ReentrantLock(); private final boolean debugEnabled = logger.isDebugEnabled(); @@ -158,46 +157,38 @@ public CompletableFuture> getConnectionAsync(Conne private CompletableFuture> getWriteConnection(int slot) { - CompletableFuture> writer;// avoid races when reconfiguring partitions. - - stateLock.lock(); - try { - writer = writers[slot]; - } finally { - stateLock.unlock(); + CompletableFuture> writer = writers[slot]; + if (writer != null) { + return writer; } - if (writer == null) { - RedisClusterNode master = partitions.getMasterBySlot(slot); - if (master == null) { - clusterEventListener.onUncoveredSlot(slot); - return Futures.failed(new PartitionSelectorException("Cannot determine a partition for slot " + slot + ".", - partitions.clone())); - } + RedisClusterNode master = partitions.getMasterBySlot(slot); + if (master == null) { + clusterEventListener.onUncoveredSlot(slot); + return Futures.failed( + new PartitionSelectorException("Cannot determine a partition for slot " + slot + ".", partitions.clone())); + } - // Use always host and port for slot-oriented operations. We don't want to get reconnected on a different - // host because the nodeId can be handled by a different host. - RedisURI uri = master.getUri(); - ConnectionKey key = new ConnectionKey(ConnectionIntent.WRITE, uri.getHost(), uri.getPort()); + // Use always host and port for slot-oriented operations. We don't want to get reconnected on a different + // host because the nodeId can be handled by a different host. + RedisURI uri = master.getUri(); + ConnectionKey key = new ConnectionKey(ConnectionIntent.WRITE, uri.getHost(), uri.getPort()); - ConnectionFuture> future = getConnectionAsync(key); + ConnectionFuture> future = getConnectionAsync(key); - return future.thenApply(connection -> { + return future.thenApply(connection -> { - stateLock.lock(); - try { - if (writers[slot] == null) { - writers[slot] = CompletableFuture.completedFuture(connection); - } - } finally { - stateLock.unlock(); + stateLock.lock(); + try { + if (writers[slot] == null) { + writers[slot] = CompletableFuture.completedFuture(connection); } + } finally { + stateLock.unlock(); + } - return connection; - }).toCompletableFuture(); - } - - return writer; + return connection; + }).toCompletableFuture(); } private CompletableFuture> getReadConnection(int slot) { @@ -654,7 +645,6 @@ public ReadFrom getReadFrom() { } /** - * * @return number of connections. */ long getConnectionCount() {