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;
});
}
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);
}
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");
}
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));
}
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();
}
Aggregations