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