Search in sources :

Example 41 with Partitions

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

the class RedisClusterClient method fetchPartitions.

private CompletionStage<Partitions> fetchPartitions(Iterable<RedisURI> topologyRefreshSource) {
    CompletionStage<Map<RedisURI, Partitions>> topology = refresh.loadViews(topologyRefreshSource, getClusterClientOptions().getSocketOptions().getConnectTimeout(), useDynamicRefreshSources());
    return topology.thenApply(partitions -> {
        if (partitions.isEmpty()) {
            throw new RedisException(String.format("Cannot retrieve initial cluster partitions from initial URIs %s", topologyRefreshSource));
        }
        Partitions loadedPartitions = determinePartitions(this.partitions, partitions);
        RedisURI viewedBy = getViewedBy(partitions, loadedPartitions);
        for (RedisClusterNode partition : loadedPartitions) {
            if (viewedBy != null) {
                RedisURI uri = partition.getUri();
                RedisClusterURIUtil.applyUriConnectionSettings(viewedBy, uri);
            }
        }
        topologyRefreshScheduler.activateTopologyRefreshIfNeeded();
        return loadedPartitions;
    });
}
Also used : Partitions(io.lettuce.core.cluster.models.partitions.Partitions) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) Map(java.util.Map)

Example 42 with Partitions

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

the class DefaultClusterTopologyRefresh method getNodeSpecificViews.

NodeTopologyViews getNodeSpecificViews(Requests requestedTopology, Requests requestedInfo) {
    List<RedisClusterNodeSnapshot> allNodes = new ArrayList<>();
    Map<String, NodeTopologyView> self = new HashMap<>();
    Set<RedisURI> nodes = requestedTopology.nodes();
    List<NodeTopologyView> views = new ArrayList<>();
    for (RedisURI nodeUri : nodes) {
        try {
            NodeTopologyView nodeTopologyView = NodeTopologyView.from(nodeUri, requestedTopology, requestedInfo);
            if (!nodeTopologyView.isAvailable()) {
                continue;
            }
            RedisClusterNode node = nodeTopologyView.getOwnPartition();
            if (node.getUri() == null) {
                node.setUri(nodeUri);
            } else {
                node.addAlias(nodeUri);
            }
            self.put(node.getNodeId(), nodeTopologyView);
            List<RedisClusterNodeSnapshot> nodeWithStats = new ArrayList<>(nodeTopologyView.getPartitions().size());
            for (RedisClusterNode partition : nodeTopologyView.getPartitions()) {
                if (validNode(partition)) {
                    nodeWithStats.add(new RedisClusterNodeSnapshot(partition));
                }
            }
            allNodes.addAll(nodeWithStats);
            Partitions partitions = new Partitions();
            partitions.addAll(nodeWithStats);
            nodeTopologyView.setPartitions(partitions);
            views.add(nodeTopologyView);
        } catch (CompletionException e) {
            logger.warn(String.format("Cannot retrieve partition view from %s, error: %s", nodeUri, e));
        }
    }
    for (RedisClusterNodeSnapshot node : allNodes) {
        if (!self.containsKey(node.getNodeId())) {
            continue;
        }
        NodeTopologyView view = self.get(node.getNodeId());
        node.setConnectedClients(view.getConnectedClients());
        node.setReplOffset(view.getReplicationOffset());
        node.setLatencyNs(view.getLatency());
    }
    for (NodeTopologyView view : views) {
        view.postProcessPartitions();
    }
    return new NodeTopologyViews(views);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) RedisURI(io.lettuce.core.RedisURI) ArrayList(java.util.ArrayList) Partitions(io.lettuce.core.cluster.models.partitions.Partitions) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) CompletionException(java.util.concurrent.CompletionException)

Example 43 with Partitions

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

the class ClusterTopologyRefreshUnitTests method partitionsReturnedAsReported.

@Test
void partitionsReturnedAsReported() throws Exception {
    System.setProperty("io.lettuce.core.topology.sort", "none");
    String NODE_1_VIEW = "2 127.0.0.1:7381 master - 111 1401258245007 222 connected 7000 12000 12002-16383\n" + "1 127.0.0.1:7380 master,myself - 0 1401258245007 2 disconnected 8000-11999\n";
    String NODE_2_VIEW = "2 127.0.0.1:7381 master,myself - 111 1401258245007 222 connected 7000 12000 12002-16383\n" + "1 127.0.0.1:7380 master - 0 1401258245007 2 disconnected 8000-11999\n";
    Requests requests = createClusterNodesRequests(1, NODE_1_VIEW);
    requests = createClusterNodesRequests(2, NODE_2_VIEW).mergeWith(requests);
    Requests clientRequests = createClientListRequests(1, "c1\nc2\n").mergeWith(createClientListRequests(2, "c1\nc2\n"));
    NodeTopologyViews nodeSpecificViews = sut.getNodeSpecificViews(requests, clientRequests);
    Collection<Partitions> values = nodeSpecificViews.toMap().values();
    assertThat(values).hasSize(2);
    for (Partitions value : values) {
        assertThat(value).extracting("nodeId").containsSequence("2", "1");
    }
    System.getProperties().remove("io.lettuce.core.topology.sort");
}
Also used : Partitions(io.lettuce.core.cluster.models.partitions.Partitions) Test(org.junit.jupiter.api.Test)

Example 44 with Partitions

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

the class ClusterTopologyRefreshUnitTests method discoveredAdditionalNodesShouldBeOrderedUsingLatency.

@Test
void discoveredAdditionalNodesShouldBeOrderedUsingLatency() {
    List<RedisURI> seed = Collections.singletonList(RedisURI.create("127.0.0.1", 7380));
    when(nodeConnectionFactory.connectToNodeAsync(any(RedisCodec.class), eq(InetSocketAddress.createUnresolved("127.0.0.1", 7380)))).thenReturn(completedFuture((StatefulRedisConnection) connection1));
    when(nodeConnectionFactory.connectToNodeAsync(any(RedisCodec.class), eq(InetSocketAddress.createUnresolved("127.0.0.1", 7381)))).thenReturn(completedFuture((StatefulRedisConnection) connection2));
    Map<RedisURI, Partitions> partitionsMap = sut.loadViews(seed, Duration.ofSeconds(1), true).toCompletableFuture().join();
    Partitions partitions = partitionsMap.values().iterator().next();
    List<RedisClusterNode> nodes = TopologyComparators.sortByLatency(partitions);
    assertThat(nodes).hasSize(2).extracting(RedisClusterNode::getUri).contains(RedisURI.create("127.0.0.1", 7381), seed.get(0));
}
Also used : RedisCodec(io.lettuce.core.codec.RedisCodec) Partitions(io.lettuce.core.cluster.models.partitions.Partitions) RedisClusterNode(io.lettuce.core.cluster.models.partitions.RedisClusterNode) RedisURI(io.lettuce.core.RedisURI) StatefulRedisConnection(io.lettuce.core.api.StatefulRedisConnection) Test(org.junit.jupiter.api.Test)

Example 45 with Partitions

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

the class TopologyComparatorsUnitTests method isChangedSamePartitions.

@Test
void isChangedSamePartitions() {
    String nodes = "c37ab8396be428403d4e55c0d317348be27ed973 127.0.0.1:7381 master - 111 1401258245007 222 connected 7000 12000 12002-16383\n" + "3d005a179da7d8dc1adae6409d47b39c369e992b 127.0.0.1:7380 master - 0 1401258245007 2 disconnected 8000-11999\n";
    Partitions partitions1 = ClusterPartitionParser.parse(nodes);
    Partitions partitions2 = ClusterPartitionParser.parse(nodes);
    assertThat(isChanged(partitions1, partitions2)).isFalse();
}
Also used : Partitions(io.lettuce.core.cluster.models.partitions.Partitions) Test(org.junit.jupiter.api.Test)

Aggregations

Partitions (io.lettuce.core.cluster.models.partitions.Partitions)69 Test (org.junit.jupiter.api.Test)50 RedisClusterNode (io.lettuce.core.cluster.models.partitions.RedisClusterNode)36 RedisURI (io.lettuce.core.RedisURI)20 ArrayList (java.util.ArrayList)13 StatefulRedisConnection (io.lettuce.core.api.StatefulRedisConnection)10 PartitionsConsensusTestSupport.createPartitions (io.lettuce.core.cluster.PartitionsConsensusTestSupport.createPartitions)9 RedisCodec (io.lettuce.core.codec.RedisCodec)9 CompletableFuture (java.util.concurrent.CompletableFuture)8 Exceptions (io.lettuce.core.internal.Exceptions)6 Futures (io.lettuce.core.internal.Futures)6 InternalLogger (io.netty.util.internal.logging.InternalLogger)6 InternalLoggerFactory (io.netty.util.internal.logging.InternalLoggerFactory)6 List (java.util.List)6 Function (java.util.function.Function)6 LettuceAssert (io.lettuce.core.internal.LettuceAssert)5 Collection (java.util.Collection)5 Iterator (java.util.Iterator)5 Map (java.util.Map)5 CompletionStage (java.util.concurrent.CompletionStage)5