Skip to content

Improve the performance of obtaining write connections through double-check locks. #3228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ class PooledClusterConnectionProvider<K, V>

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();
Expand Down Expand Up @@ -158,46 +157,38 @@ public CompletableFuture<StatefulRedisConnection<K, V>> getConnectionAsync(Conne

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

CompletableFuture<StatefulRedisConnection<K, V>> writer;// avoid races when reconfiguring partitions.

stateLock.lock();
try {
writer = writers[slot];
} finally {
stateLock.unlock();
CompletableFuture<StatefulRedisConnection<K, V>> 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<StatefulRedisConnection<K, V>> future = getConnectionAsync(key);
ConnectionFuture<StatefulRedisConnection<K, V>> 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<StatefulRedisConnection<K, V>> getReadConnection(int slot) {
Expand Down Expand Up @@ -654,7 +645,6 @@ public ReadFrom getReadFrom() {
}

/**
*
* @return number of connections.
*/
long getConnectionCount() {
Expand Down