Search in sources :

Example 6 with StatefulRedisPubSubConnection

use of io.lettuce.core.pubsub.StatefulRedisPubSubConnection in project lettuce-core by lettuce-io.

the class SentinelTopologyRefresh method initializeSentinels.

/**
 * Initialize/extend connections to Sentinel servers.
 *
 * @return
 */
private CompletionStage<Void> initializeSentinels() {
    if (closed) {
        return closeFuture;
    }
    Duration timeout = getTimeout();
    List<ConnectionFuture<StatefulRedisPubSubConnection<String, String>>> connectionFutures = potentiallyConnectSentinels();
    if (connectionFutures.isEmpty()) {
        return CompletableFuture.completedFuture(null);
    }
    if (closed) {
        return closeAsync();
    }
    SentinelTopologyRefreshConnections collector = collectConnections(connectionFutures);
    CompletionStage<SentinelTopologyRefreshConnections> completionStage = collector.getOrTimeout(timeout, redisClient.getResources().eventExecutorGroup());
    return completionStage.whenComplete((aVoid, throwable) -> {
        if (throwable != null) {
            closeAsync();
        }
    }).thenApply(noop -> (Void) null);
}
Also used : Arrays(java.util.Arrays) StringCodec(io.lettuce.core.codec.StringCodec) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Futures(io.lettuce.core.internal.Futures) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BiPredicate(java.util.function.BiPredicate) Duration(java.time.Duration) Map(java.util.Map) LettuceLists(io.lettuce.core.internal.LettuceLists) AsyncCloseable(io.lettuce.core.api.AsyncCloseable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StatefulRedisPubSubConnection(io.lettuce.core.pubsub.StatefulRedisPubSubConnection) Set(java.util.Set) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) EventRecorder(io.lettuce.core.event.jfr.EventRecorder) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) RedisURI(io.lettuce.core.RedisURI) List(java.util.List) RedisPubSubAdapter(io.lettuce.core.pubsub.RedisPubSubAdapter) CompletionStage(java.util.concurrent.CompletionStage) RedisClient(io.lettuce.core.RedisClient) Closeable(java.io.Closeable) InternalLogger(io.netty.util.internal.logging.InternalLogger) ConnectionFuture(io.lettuce.core.ConnectionFuture) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ConnectionFuture(io.lettuce.core.ConnectionFuture) Duration(java.time.Duration)

Example 7 with StatefulRedisPubSubConnection

use of io.lettuce.core.pubsub.StatefulRedisPubSubConnection in project lettuce-core by lettuce-io.

the class SentinelTopologyRefresh method potentiallyConnectSentinels.

/**
 * Inspect whether additional Sentinel connections are required based on the which Sentinels are currently connected.
 *
 * @return list of futures that are notified with the connection progress.
 */
private List<ConnectionFuture<StatefulRedisPubSubConnection<String, String>>> potentiallyConnectSentinels() {
    List<ConnectionFuture<StatefulRedisPubSubConnection<String, String>>> connectionFutures = new ArrayList<>();
    for (RedisURI sentinel : sentinels) {
        if (pubSubConnections.containsKey(sentinel)) {
            continue;
        }
        ConnectionFuture<StatefulRedisPubSubConnection<String, String>> future = redisClient.connectPubSubAsync(CODEC, sentinel);
        pubSubConnections.put(sentinel, future);
        future.whenComplete((connection, throwable) -> {
            if (throwable != null || closed) {
                pubSubConnections.remove(sentinel);
            }
            if (closed) {
                connection.closeAsync();
            }
        });
        connectionFutures.add(future);
    }
    return connectionFutures;
}
Also used : ConnectionFuture(io.lettuce.core.ConnectionFuture) RedisURI(io.lettuce.core.RedisURI) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) StatefulRedisPubSubConnection(io.lettuce.core.pubsub.StatefulRedisPubSubConnection)

Example 8 with StatefulRedisPubSubConnection

use of io.lettuce.core.pubsub.StatefulRedisPubSubConnection in project lettuce-core by lettuce-io.

the class SentinelTopologyRefreshUnitTests method bindWithSecondSentinelFails.

@Test
void bindWithSecondSentinelFails() {
    sut = new SentinelTopologyRefresh(redisClient, "mymaster", Arrays.asList(host1, host2));
    when(redisClient.connectPubSubAsync(any(StringCodec.class), eq(host2))).thenReturn(ConnectionFuture.from(null, Futures.failed(new RedisConnectionException("err"))));
    sut.bind(refreshRunnable);
    Map<RedisURI, StatefulRedisPubSubConnection<String, String>> connections = (Map) ReflectionTestUtils.getField(sut, "pubSubConnections");
    assertThat(connections).containsKey(host1).hasSize(1);
}
Also used : StringCodec(io.lettuce.core.codec.StringCodec) RedisURI(io.lettuce.core.RedisURI) RedisConnectionException(io.lettuce.core.RedisConnectionException) Map(java.util.Map) StatefulRedisPubSubConnection(io.lettuce.core.pubsub.StatefulRedisPubSubConnection) Test(org.junit.jupiter.api.Test)

Aggregations

StatefulRedisPubSubConnection (io.lettuce.core.pubsub.StatefulRedisPubSubConnection)8 RedisURI (io.lettuce.core.RedisURI)4 RedisClient (io.lettuce.core.RedisClient)3 Map (java.util.Map)3 ConnectionFuture (io.lettuce.core.ConnectionFuture)2 RedisConnectionException (io.lettuce.core.RedisConnectionException)2 StringCodec (io.lettuce.core.codec.StringCodec)2 CommandExpiryWriter (io.lettuce.core.protocol.CommandExpiryWriter)2 PubSubEndpoint (io.lettuce.core.pubsub.PubSubEndpoint)2 RedisPubSubAdapter (io.lettuce.core.pubsub.RedisPubSubAdapter)2 ArrayList (java.util.ArrayList)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Test (org.junit.jupiter.api.Test)2 AsyncCloseable (io.lettuce.core.api.AsyncCloseable)1 EventRecorder (io.lettuce.core.event.jfr.EventRecorder)1 Futures (io.lettuce.core.internal.Futures)1 LettuceLists (io.lettuce.core.internal.LettuceLists)1 PubSubMessageHandler (io.lettuce.core.masterreplica.SentinelTopologyRefresh.PubSubMessageHandler)1 AsyncCommand (io.lettuce.core.protocol.AsyncCommand)1 StatefulRedisPubSubConnectionImpl (io.lettuce.core.pubsub.StatefulRedisPubSubConnectionImpl)1