Search in sources :

Example 26 with RedisClusterNode

use of io.lettuce.core.cluster.models.partitions.RedisClusterNode 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;
}
Also used : Arrays(java.util.Arrays) Partitions(io.lettuce.core.cluster.models.partitions.Partitions) CompletableFuture(java.util.concurrent.CompletableFuture) StatefulConnection(io.lettuce.core.api.StatefulConnection) RedisNodeDescription(io.lettuce.core.models.role.RedisNodeDescription) Function(java.util.function.Function) RedisChannelWriter(io.lettuce.core.RedisChannelWriter) ConnectionKey(io.lettuce.core.cluster.ClusterNodeConnectionFactory.ConnectionKey) Futures(io.lettuce.core.internal.Futures) ArrayList(java.util.ArrayList) LettuceAssert(io.lettuce.core.internal.LettuceAssert) OrderingReadFromAccessor(io.lettuce.core.OrderingReadFromAccessor) StatefulRedisConnection(io.lettuce.core.api.StatefulRedisConnection) PushMessage(io.lettuce.core.api.push.PushMessage) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) RedisFuture(io.lettuce.core.RedisFuture) AsyncConnectionProvider(io.lettuce.core.internal.AsyncConnectionProvider) Iterator(java.util.Iterator) RedisException(io.lettuce.core.RedisException) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) Exceptions(io.lettuce.core.internal.Exceptions) Collection(java.util.Collection) RedisClusterPushListener(io.lettuce.core.cluster.api.push.RedisClusterPushListener) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) RedisCodec(io.lettuce.core.codec.RedisCodec) RedisURI(io.lettuce.core.RedisURI) List(java.util.List) RedisConnectionException(io.lettuce.core.RedisConnectionException) HostAndPort(io.lettuce.core.internal.HostAndPort) ReadFrom(io.lettuce.core.ReadFrom) InternalLogger(io.netty.util.internal.logging.InternalLogger) ConnectionFuture(io.lettuce.core.ConnectionFuture) InternalLoggerFactory(io.netty.util.internal.logging.InternalLoggerFactory) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) RedisURI(io.lettuce.core.RedisURI) ConnectionKey(io.lettuce.core.cluster.ClusterNodeConnectionFactory.ConnectionKey) StatefulRedisConnection(io.lettuce.core.api.StatefulRedisConnection)

Example 27 with RedisClusterNode

use of io.lettuce.core.cluster.models.partitions.RedisClusterNode in project lettuce-core by lettuce-io.

the class RedisAdvancedClusterAsyncCommandsImpl method randomkey.

@Override
public RedisFuture<K> randomkey() {
    Partitions partitions = getStatefulConnection().getPartitions();
    if (partitions.isEmpty()) {
        return super.randomkey();
    }
    int index = ThreadLocalRandom.current().nextInt(partitions.size());
    RedisClusterNode partition = partitions.getPartition(index);
    CompletableFuture<K> future = getConnectionAsync(partition.getUri().getHost(), partition.getUri().getPort()).thenCompose(RedisKeyAsyncCommands::randomkey);
    return new PipelinedRedisFuture<>(future);
}
Also used : Partitions(io.lettuce.core.cluster.models.partitions.Partitions) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) RedisKeyAsyncCommands(io.lettuce.core.api.async.RedisKeyAsyncCommands)

Example 28 with RedisClusterNode

use of io.lettuce.core.cluster.models.partitions.RedisClusterNode in project lettuce-core by lettuce-io.

the class RedisAdvancedClusterReactiveCommandsImpl method executeOnNodes.

/**
 * Run a command on all available nodes that match {@code filter}.
 *
 * @param function function producing the command
 * @param filter filter function for the node selection
 * @param <T> result type
 * @return map of a key (counter) and commands.
 */
protected <T> Map<String, Publisher<T>> executeOnNodes(Function<RedisClusterReactiveCommands<K, V>, ? extends Publisher<T>> function, Predicate<RedisClusterNode> filter) {
    Map<String, Publisher<T>> executions = new HashMap<>();
    for (RedisClusterNode redisClusterNode : getStatefulConnection().getPartitions()) {
        if (!filter.test(redisClusterNode)) {
            continue;
        }
        RedisURI uri = redisClusterNode.getUri();
        Mono<RedisClusterReactiveCommands<K, V>> connection = getConnectionReactive(uri.getHost(), uri.getPort());
        executions.put(redisClusterNode.getNodeId(), connection.flatMapMany(function::apply));
    }
    return executions;
}
Also used : HashMap(java.util.HashMap) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) RedisClusterReactiveCommands(io.lettuce.core.cluster.api.reactive.RedisClusterReactiveCommands) Publisher(org.reactivestreams.Publisher)

Example 29 with RedisClusterNode

use of io.lettuce.core.cluster.models.partitions.RedisClusterNode in project lettuce-core by lettuce-io.

the class RedisClusterClient method getTopologyRefreshSource.

/**
 * Returns the seed {@link RedisURI} for the topology refreshing. This method is called before each topology refresh to
 * provide an {@link Iterable} of {@link RedisURI} that is used to perform the next topology refresh.
 * <p>
 * Subclasses of {@link RedisClusterClient} may override that method.
 *
 * @return {@link Iterable} of {@link RedisURI} for the next topology refresh.
 */
protected Iterable<RedisURI> getTopologyRefreshSource() {
    boolean initialSeedNodes = !useDynamicRefreshSources();
    Iterable<RedisURI> seed;
    if (initialSeedNodes || partitions == null || partitions.isEmpty()) {
        seed = this.initialUris;
    } else {
        List<RedisURI> uris = new ArrayList<>();
        for (RedisClusterNode partition : TopologyComparators.sortByUri(partitions)) {
            uris.add(partition.getUri());
        }
        seed = uris;
    }
    return seed;
}
Also used : RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) ArrayList(java.util.ArrayList)

Example 30 with RedisClusterNode

use of io.lettuce.core.cluster.models.partitions.RedisClusterNode in project lettuce-core by lettuce-io.

the class RedisClusterClient method loadPartitionsAsync.

/**
 * Retrieve partitions. Nodes within {@link Partitions} are ordered by latency. Lower latency nodes come first.
 *
 * @return future that emits {@link Partitions} upon a successful topology lookup.
 * @since 6.0
 */
protected CompletableFuture<Partitions> loadPartitionsAsync() {
    Iterable<RedisURI> topologyRefreshSource = getTopologyRefreshSource();
    CompletableFuture<Partitions> future = new CompletableFuture<>();
    fetchPartitions(topologyRefreshSource).whenComplete((nodes, throwable) -> {
        if (throwable == null) {
            future.complete(nodes);
            return;
        }
        // Attempt recovery using initial seed nodes
        if (useDynamicRefreshSources() && topologyRefreshSource != initialUris) {
            fetchPartitions(initialUris).whenComplete((nextNodes, nextThrowable) -> {
                if (nextThrowable != null) {
                    Throwable exception = Exceptions.unwrap(nextThrowable);
                    exception.addSuppressed(Exceptions.unwrap(throwable));
                    future.completeExceptionally(exception);
                } else {
                    future.complete(nextNodes);
                }
            });
        } else {
            future.completeExceptionally(Exceptions.unwrap(throwable));
        }
    });
    Predicate<RedisClusterNode> nodeFilter = getClusterClientOptions().getNodeFilter();
    if (nodeFilter != ClusterClientOptions.DEFAULT_NODE_FILTER) {
        return future.thenApply(partitions -> {
            List<RedisClusterNode> toRemove = new ArrayList<>();
            for (RedisClusterNode partition : partitions) {
                if (!nodeFilter.test(partition)) {
                    toRemove.add(partition);
                }
            }
            partitions.removeAll(toRemove);
            return partitions;
        });
    }
    return future;
}
Also used : Partitions(io.lettuce.core.cluster.models.partitions.Partitions) CompletableFuture(java.util.concurrent.CompletableFuture) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) ArrayList(java.util.ArrayList)

Aggregations

RedisClusterNode (io.lettuce.core.cluster.models.partitions.RedisClusterNode)94 Test (org.junit.jupiter.api.Test)60 Partitions (io.lettuce.core.cluster.models.partitions.Partitions)36 ArrayList (java.util.ArrayList)18 RedisURI (io.lettuce.core.RedisURI)13 StatefulRedisConnection (io.lettuce.core.api.StatefulRedisConnection)12 RedisNodeDescription (io.lettuce.core.models.role.RedisNodeDescription)10 CompletableFuture (java.util.concurrent.CompletableFuture)10 ReadFrom (io.lettuce.core.ReadFrom)9 RedisCodec (io.lettuce.core.codec.RedisCodec)9 List (java.util.List)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Function (java.util.function.Function)6 CompletionStage (java.util.concurrent.CompletionStage)5 RedisException (io.lettuce.core.RedisException)4 RedisFuture (io.lettuce.core.RedisFuture)4 Exceptions (io.lettuce.core.internal.Exceptions)4 Futures (io.lettuce.core.internal.Futures)4 InternalLogger (io.netty.util.internal.logging.InternalLogger)4