use of io.lettuce.core.cluster.ClusterNodeConnectionFactory.ConnectionKey in project lettuce-core by lettuce-io.
the class PooledClusterConnectionProvider method getWriteConnection.
private CompletableFuture<StatefulRedisConnection<K, V>> getWriteConnection(int slot) {
// avoid races when reconfiguring partitions.
CompletableFuture<StatefulRedisConnection<K, V>> writer;
synchronized (stateLock) {
writer = writers[slot];
}
if (writer == null) {
RedisClusterNode partition = partitions.getPartitionBySlot(slot);
if (partition == 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 = partition.getUri();
ConnectionKey key = new ConnectionKey(Intent.WRITE, uri.getHost(), uri.getPort());
ConnectionFuture<StatefulRedisConnection<K, V>> future = getConnectionAsync(key);
return future.thenApply(connection -> {
synchronized (stateLock) {
if (writers[slot] == null) {
writers[slot] = CompletableFuture.completedFuture(connection);
}
}
return connection;
}).toCompletableFuture();
}
return writer;
}
use of io.lettuce.core.cluster.ClusterNodeConnectionFactory.ConnectionKey in project lettuce-core by lettuce-io.
the class PooledClusterConnectionProvider method getReadFromConnections.
private CompletableFuture<StatefulRedisConnection<K, V>>[] getReadFromConnections(List<RedisNodeDescription> selection) {
// 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.
CompletableFuture<StatefulRedisConnection<K, V>>[] readerCandidates = new CompletableFuture[selection.size()];
for (int i = 0; i < selection.size(); i++) {
RedisNodeDescription redisClusterNode = selection.get(i);
RedisURI uri = redisClusterNode.getUri();
ConnectionKey key = new ConnectionKey(redisClusterNode.getRole().isUpstream() ? Intent.WRITE : Intent.READ, uri.getHost(), uri.getPort());
readerCandidates[i] = getConnectionAsync(key).toCompletableFuture();
}
return readerCandidates;
}
use of io.lettuce.core.cluster.ClusterNodeConnectionFactory.ConnectionKey in project lettuce-core by lettuce-io.
the class AsyncConnectionProviderIntegrationTests method before.
@BeforeEach
void before() throws Exception {
serverSocket = new ServerSocket(9393, 1);
client = RedisClusterClient.create(resources, "redis://localhost");
client.setOptions(ClusterClientOptions.builder().protocolVersion(ProtocolVersion.RESP2).build());
sut = new AsyncConnectionProvider<>(new AbstractClusterNodeConnectionFactory<String, String>(resources) {
@Override
public ConnectionFuture<StatefulRedisConnection<String, String>> apply(ConnectionKey connectionKey) {
RedisURI redisURI = RedisURI.create(TestSettings.host(), serverSocket.getLocalPort());
redisURI.setTimeout(Duration.ofSeconds(5));
ConnectionFuture<StatefulRedisConnection<String, String>> future = client.connectToNodeAsync(StringCodec.UTF8, "", null, Mono.just(new InetSocketAddress(connectionKey.host, serverSocket.getLocalPort())));
connectInitiated.countDown();
return future;
}
});
}
use of io.lettuce.core.cluster.ClusterNodeConnectionFactory.ConnectionKey in project lettuce-core by lettuce-io.
the class AsyncConnectionProviderIntegrationTests method shouldCloseConnectionByKey.
@Test
void shouldCloseConnectionByKey() throws IOException {
ConnectionKey connectionKey = new ConnectionKey(ClusterConnectionProvider.Intent.READ, TestSettings.host(), TestSettings.port());
sut.getConnection(connectionKey);
sut.close(connectionKey);
assertThat(sut.getConnectionCount()).isEqualTo(0);
sut.close();
serverSocket.accept();
}
use of io.lettuce.core.cluster.ClusterNodeConnectionFactory.ConnectionKey in project lettuce-core by lettuce-io.
the class AsyncConnectionProviderIntegrationTests method connectShouldFail.
@Test
void connectShouldFail() throws Exception {
Socket socket = new Socket(TestSettings.host(), serverSocket.getLocalPort());
ClusterClientOptions clientOptions = ClusterClientOptions.builder().protocolVersion(ProtocolVersion.RESP2).socketOptions(SocketOptions.builder().connectTimeout(1, TimeUnit.SECONDS).build()).build();
client.setOptions(clientOptions);
ConnectionKey connectionKey = new ConnectionKey(ClusterConnectionProvider.Intent.READ, "8.8.8.8", TestSettings.port());
StopWatch stopWatch = new StopWatch();
assertThatThrownBy(() -> TestFutures.awaitOrTimeout(sut.getConnection(connectionKey))).hasCauseInstanceOf(ConnectTimeoutException.class);
stopWatch.start();
assertThatThrownBy(() -> TestFutures.awaitOrTimeout(sut.getConnection(connectionKey))).hasCauseInstanceOf(ConnectTimeoutException.class);
stopWatch.stop();
assertThat(TimeUnit.NANOSECONDS.toMillis(stopWatch.getNanoTime())).isBetween(0L, 1200L);
sut.close();
socket.close();
}
Aggregations