Search in sources :

Example 6 with ReadFrom

use of io.lettuce.core.ReadFrom in project lettuce-core by lettuce-io.

the class ReadFromUnitTests method regex.

@Test
void regex() {
    ReadFrom sut = ReadFrom.regex(Pattern.compile(".*region-1.*"));
    RedisClusterNode node1 = createNodeWithHost("redis-node-1.region-1.example.com");
    RedisClusterNode node2 = createNodeWithHost("redis-node-2.region-1.example.com");
    RedisClusterNode node3 = createNodeWithHost("redis-node-1.region-2.example.com");
    RedisClusterNode node4 = createNodeWithHost("redis-node-2.region-2.example.com");
    List<RedisNodeDescription> result = sut.select(getNodes(node1, node2, node3, node4));
    assertThat(result).hasSize(2).containsExactly(node1, node2);
}
Also used : RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) RedisNodeDescription(io.lettuce.core.models.role.RedisNodeDescription) ReadFrom(io.lettuce.core.ReadFrom) Test(org.junit.jupiter.api.Test)

Example 7 with ReadFrom

use of io.lettuce.core.ReadFrom in project lettuce-core by lettuce-io.

the class PooledClusterConnectionProvider method getReadConnection.

private CompletableFuture<StatefulRedisConnection<K, V>> getReadConnection(int slot) {
    // avoid races when reconfiguring partitions.
    CompletableFuture<StatefulRedisConnection<K, V>>[] readerCandidates;
    boolean cached = true;
    synchronized (stateLock) {
        readerCandidates = readers[slot];
    }
    if (readerCandidates == null) {
        RedisClusterNode upstream = partitions.getPartitionBySlot(slot);
        if (upstream == null) {
            clusterEventListener.onUncoveredSlot(slot);
            return Futures.failed(new PartitionSelectorException(String.format("Cannot determine a partition to read for slot %d.", slot), partitions.clone()));
        }
        List<RedisNodeDescription> candidates = getReadCandidates(upstream);
        List<RedisNodeDescription> selection = readFrom.select(new ReadFrom.Nodes() {

            @Override
            public List<RedisNodeDescription> getNodes() {
                return candidates;
            }

            @Override
            public Iterator<RedisNodeDescription> iterator() {
                return candidates.iterator();
            }
        });
        if (selection.isEmpty()) {
            clusterEventListener.onUncoveredSlot(slot);
            return Futures.failed(new PartitionSelectorException(String.format("Cannot determine a partition to read for slot %d with setting %s.", slot, readFrom), partitions.clone()));
        }
        readerCandidates = getReadFromConnections(selection);
        cached = false;
    }
    CompletableFuture<StatefulRedisConnection<K, V>>[] selectedReaderCandidates = readerCandidates;
    if (cached) {
        return CompletableFuture.allOf(readerCandidates).thenCompose(v -> {
            boolean orderSensitive = isOrderSensitive(selectedReaderCandidates);
            if (!orderSensitive) {
                CompletableFuture<StatefulRedisConnection<K, V>> candidate = findRandomActiveConnection(selectedReaderCandidates, Function.identity());
                if (candidate != null) {
                    return candidate;
                }
            }
            for (CompletableFuture<StatefulRedisConnection<K, V>> candidate : selectedReaderCandidates) {
                if (candidate.join().isOpen()) {
                    return candidate;
                }
            }
            return selectedReaderCandidates[0];
        });
    }
    CompletableFuture<StatefulRedisConnection<K, V>[]> filteredReaderCandidates = new CompletableFuture<>();
    CompletableFuture.allOf(readerCandidates).thenApply(v -> selectedReaderCandidates).whenComplete((candidates, throwable) -> {
        if (throwable == null) {
            filteredReaderCandidates.complete(getConnections(candidates));
            return;
        }
        StatefulRedisConnection<K, V>[] connections = getConnections(selectedReaderCandidates);
        if (connections.length == 0) {
            filteredReaderCandidates.completeExceptionally(throwable);
            return;
        }
        filteredReaderCandidates.complete(connections);
    });
    return filteredReaderCandidates.thenApply(statefulRedisConnections -> {
        boolean orderSensitive = isOrderSensitive(statefulRedisConnections);
        CompletableFuture<StatefulRedisConnection<K, V>>[] toCache = new CompletableFuture[statefulRedisConnections.length];
        for (int i = 0; i < toCache.length; i++) {
            toCache[i] = CompletableFuture.completedFuture(statefulRedisConnections[i]);
        }
        synchronized (stateLock) {
            readers[slot] = toCache;
        }
        if (!orderSensitive) {
            StatefulRedisConnection<K, V> candidate = findRandomActiveConnection(selectedReaderCandidates, CompletableFuture::join);
            if (candidate != null) {
                return candidate;
            }
        }
        for (StatefulRedisConnection<K, V> candidate : statefulRedisConnections) {
            if (candidate.isOpen()) {
                return candidate;
            }
        }
        return statefulRedisConnections[0];
    });
}
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) StatefulRedisConnection(io.lettuce.core.api.StatefulRedisConnection) CompletableFuture(java.util.concurrent.CompletableFuture) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) RedisNodeDescription(io.lettuce.core.models.role.RedisNodeDescription) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ReadFrom(io.lettuce.core.ReadFrom)

Example 8 with ReadFrom

use of io.lettuce.core.ReadFrom in project lettuce-core by lettuce-io.

the class ReadFromUnitTests method subnetIpv4RuleIpv6NodeGiven.

@Test
void subnetIpv4RuleIpv6NodeGiven() {
    ReadFrom sut = ReadFrom.subnet("0.0.0.0/0");
    RedisClusterNode ipv6node = createNodeWithHost("2001:db8:abcd:1000::");
    List<RedisNodeDescription> result = sut.select(getNodes(ipv6node));
    assertThat(result).isEmpty();
}
Also used : RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) RedisNodeDescription(io.lettuce.core.models.role.RedisNodeDescription) ReadFrom(io.lettuce.core.ReadFrom) Test(org.junit.jupiter.api.Test)

Example 9 with ReadFrom

use of io.lettuce.core.ReadFrom in project lettuce-core by lettuce-io.

the class ReadFromUnitTests method subnetNodeWithHostname.

@Test
void subnetNodeWithHostname() {
    ReadFrom sut = ReadFrom.subnet("0.0.0.0/0");
    RedisClusterNode hostNode = createNodeWithHost("example.com");
    RedisClusterNode localhostNode = createNodeWithHost("localhost");
    List<RedisNodeDescription> result = sut.select(getNodes(hostNode, localhostNode));
    assertThat(result).isEmpty();
}
Also used : RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) RedisNodeDescription(io.lettuce.core.models.role.RedisNodeDescription) ReadFrom(io.lettuce.core.ReadFrom) Test(org.junit.jupiter.api.Test)

Example 10 with ReadFrom

use of io.lettuce.core.ReadFrom in project lettuce-core by lettuce-io.

the class ReadFromUnitTests method subnetIpv6RuleIpv4NodeGiven.

@Test
void subnetIpv6RuleIpv4NodeGiven() {
    ReadFrom sut = ReadFrom.subnet("::/0");
    RedisClusterNode node = createNodeWithHost("192.0.2.1");
    List<RedisNodeDescription> result = sut.select(getNodes(node));
    assertThat(result).isEmpty();
}
Also used : RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) RedisNodeDescription(io.lettuce.core.models.role.RedisNodeDescription) ReadFrom(io.lettuce.core.ReadFrom) Test(org.junit.jupiter.api.Test)

Aggregations

ReadFrom (io.lettuce.core.ReadFrom)11 Test (org.junit.jupiter.api.Test)10 RedisClusterNode (io.lettuce.core.cluster.models.partitions.RedisClusterNode)8 RedisNodeDescription (io.lettuce.core.models.role.RedisNodeDescription)8 List (java.util.List)4 AbstractRedisClientTest (io.lettuce.core.AbstractRedisClientTest)2 ArrayList (java.util.ArrayList)2 ConnectionFuture (io.lettuce.core.ConnectionFuture)1 OrderingReadFromAccessor (io.lettuce.core.OrderingReadFromAccessor)1 RedisChannelWriter (io.lettuce.core.RedisChannelWriter)1 RedisConnectionException (io.lettuce.core.RedisConnectionException)1 RedisException (io.lettuce.core.RedisException)1 RedisFuture (io.lettuce.core.RedisFuture)1 RedisURI (io.lettuce.core.RedisURI)1 StatefulConnection (io.lettuce.core.api.StatefulConnection)1 StatefulRedisConnection (io.lettuce.core.api.StatefulRedisConnection)1 PushMessage (io.lettuce.core.api.push.PushMessage)1 ConnectionKey (io.lettuce.core.cluster.ClusterNodeConnectionFactory.ConnectionKey)1 RedisClusterPushListener (io.lettuce.core.cluster.api.push.RedisClusterPushListener)1 Partitions (io.lettuce.core.cluster.models.partitions.Partitions)1