Skip to content

Commit

Permalink
Improve Cluster Nodes processing
Browse files Browse the repository at this point in the history
without this fix there's a problem with parsing of the cluster nodes command ouput (e.g. a trailing comma after cport is making the parsing fail)

with this change we're converting regexp parsing to code parsing which also includes support for trailing commas after cport

fixes gh-2862
  • Loading branch information
marcingrzejszczak committed Sep 18, 2024
1 parent c16b863 commit bb2eb9b
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>3.2.11-SNAPSHOT</version>
<version>3.2.11-GH-2862-SNAPSHOT</version>

<name>Spring Data Redis</name>
<description>Spring Data module for Redis</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResult;
Expand All @@ -45,6 +44,7 @@
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.util.ByteUtils;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
Expand Down Expand Up @@ -545,10 +545,15 @@ enum ClusterNodesConverter implements Converter<String, RedisClusterNode> {
* <li>{@code %s:%i} (Redis 3)</li>
* <li>{@code %s:%i@%i} (Redis 4, with bus port)</li>
* <li>{@code %s:%i@%i,%s} (Redis 7, with announced hostname)</li>
*
* The output of the {@code CLUSTER NODES } command is just a space-separated CSV string, where each
* line represents a node in the cluster. The following is an example of output on Redis 7.2.0.
* You can check the latest <a href="https://redis.io/docs/latest/commands/cluster-nodes/">here</a>.
*
* {@code <id> <ip:port@cport[,hostname]> <flags> <master> <ping-sent> <pong-recv> <config-epoch> <link-state> <slot> <slot> ... <slot>}
*
* </ul>
*/
static final Pattern clusterEndpointPattern = Pattern
.compile("\\[?([0-9a-zA-Z\\-_\\.:]*)\\]?:([0-9]+)(?:@[0-9]+(?:,([^,].*))?)?");
private static final Map<String, Flag> flagLookupMap;

static {
Expand All @@ -567,18 +572,75 @@ enum ClusterNodesConverter implements Converter<String, RedisClusterNode> {
static final int LINK_STATE_INDEX = 7;
static final int SLOTS_INDEX = 8;

record AddressPortHostname(String addressPart, String portPart, @Nullable String hostnamePart) {

static AddressPortHostname of(String[] args) {
Assert.isTrue(args.length >= HOST_PORT_INDEX + 1, "ClusterNode information does not define host and port");
// <ip:port@cport[,hostname]>
String hostPort = args[HOST_PORT_INDEX];
int lastColon = hostPort.lastIndexOf(":");
Assert.isTrue(lastColon != -1, "ClusterNode information does not define host and port");
String addressPart = getAddressPart(hostPort, lastColon);
// Everything to the right of port
int indexOfColon = hostPort.indexOf(",");
boolean hasColon = indexOfColon != -1;
String hostnamePart = getHostnamePart(hasColon, hostPort, indexOfColon);
String portPart = getPortPart(hostPort, lastColon, hasColon, indexOfColon);
return new AddressPortHostname(addressPart, portPart, hostnamePart);
}

@NonNull private static String getAddressPart(String hostPort, int lastColon) {
// Everything to the left of port
// 127.0.0.1:6380
// 127.0.0.1:6380@6381
// :6380
// :6380@6381
// 2a02:6b8:c67:9c:0:6d8b:33da:5a2c:6380
// 2a02:6b8:c67:9c:0:6d8b:33da:5a2c:6380@6381
// 127.0.0.1:6380,hostname1
// 127.0.0.1:6380@6381,hostname1
// :6380,hostname1
// :6380@6381,hostname1
// 2a02:6b8:c67:9c:0:6d8b:33da:5a2c:6380,hostname1
// 2a02:6b8:c67:9c:0:6d8b:33da:5a2c:6380@6381,hostname1
String addressPart = hostPort.substring(0, lastColon);
// [2a02:6b8:c67:9c:0:6d8b:33da:5a2c]:6380
// [2a02:6b8:c67:9c:0:6d8b:33da:5a2c]:6380@6381
// [2a02:6b8:c67:9c:0:6d8b:33da:5a2c]:6380,hostname1
// [2a02:6b8:c67:9c:0:6d8b:33da:5a2c]:6380@6381,hostname1
if (addressPart.startsWith("[") && addressPart.endsWith("]")) {
addressPart = addressPart.substring(1, addressPart.length() - 1);
}
return addressPart;
}

@Nullable
private static String getHostnamePart(boolean hasColon, String hostPort, int indexOfColon) {
// Everything to the right starting from comma
String hostnamePart = hasColon ? hostPort.substring(indexOfColon + 1) : null;
return StringUtils.hasText(hostnamePart) ? hostnamePart : null;
}

@NonNull private static String getPortPart(String hostPort, int lastColon, boolean hasColon, int indexOfColon) {
String portPart = hostPort.substring(lastColon + 1);
if (portPart.contains("@")) {
portPart = portPart.substring(0, portPart.indexOf("@"));
} else if (hasColon) {
portPart = portPart.substring(0, indexOfColon);
}
return portPart;
}
}

@Override
public RedisClusterNode convert(String source) {

String[] args = source.split(" ");

Matcher matcher = clusterEndpointPattern.matcher(args[HOST_PORT_INDEX]);

Assert.isTrue(matcher.matches(), "ClusterNode information does not define host and port");

String addressPart = matcher.group(1);
String portPart = matcher.group(2);
String hostnamePart = matcher.group(3);
AddressPortHostname addressPortHostname = AddressPortHostname.of(args);
String addressPart = addressPortHostname.addressPart;
String portPart = addressPortHostname.portPart;
String hostnamePart = addressPortHostname.hostnamePart;

SlotRange range = parseSlotRange(args);
Set<Flag> flags = parseFlags(args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import static org.assertj.core.api.Assertions.*;

import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.stream.Stream;

import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -72,6 +72,10 @@ class ConvertersUnitTests {

private static final String CLUSTER_NODE_WITH_SINGLE_INVALID_IPV6_HOST = "67adfe3df1058896e3cb49d2863e0f70e7e159fa 2a02:6b8:c67:9c:0:6d8b:33da:5a2c: master,nofailover - 0 1692108412315 1 connected 0-5460";

private static final String CLUSTER_NODE_WITH_SINGLE_IPV4_EMPTY_HOSTNAME = "3765733728631672640db35fd2f04743c03119c6 10.180.0.33:11003@16379, master - 0 1708041426947 2 connected 0-5460";

private static final String CLUSTER_NODE_WITH_SINGLE_IPV4_HOSTNAME = "3765733728631672640db35fd2f04743c03119c6 10.180.0.33:11003@16379,hostname1 master - 0 1708041426947 2 connected 0-5460";

@Test // DATAREDIS-315
void toSetOfRedis30ClusterNodesShouldConvertSingleStringNodesResponseCorrectly() {

Expand Down Expand Up @@ -248,6 +252,39 @@ void toClusterNodeWithIPv6Hostname() {
assertThat(node.getSlotRange().getSlots().size()).isEqualTo(5461);
}

@Test // https://github.com/spring-projects/spring-data-redis/issues/2862
void toClusterNodeWithIPv4EmptyHostname() {
RedisClusterNode node = Converters.toClusterNode(CLUSTER_NODE_WITH_SINGLE_IPV4_EMPTY_HOSTNAME);

SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(node.getId()).isEqualTo("3765733728631672640db35fd2f04743c03119c6");
softAssertions.assertThat(node.getHost()).isEqualTo("10.180.0.33");
softAssertions.assertThat(node.hasValidHost()).isTrue();
softAssertions.assertThat(node.getPort()).isEqualTo(11003);
softAssertions.assertThat(node.getType()).isEqualTo(NodeType.MASTER);
softAssertions.assertThat(node.getFlags()).contains(Flag.MASTER);
softAssertions.assertThat(node.getLinkState()).isEqualTo(LinkState.CONNECTED);
softAssertions.assertThat(node.getSlotRange().getSlots().size()).isEqualTo(5461);
});
}

@Test // https://github.com/spring-projects/spring-data-redis/issues/2862
void toClusterNodeWithIPv4Hostname() {
RedisClusterNode node = Converters.toClusterNode(CLUSTER_NODE_WITH_SINGLE_IPV4_HOSTNAME);

SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(node.getId()).isEqualTo("3765733728631672640db35fd2f04743c03119c6");
softAssertions.assertThat(node.getHost()).isEqualTo("10.180.0.33");
softAssertions.assertThat(node.getName()).isEqualTo("hostname1");
softAssertions.assertThat(node.hasValidHost()).isTrue();
softAssertions.assertThat(node.getPort()).isEqualTo(11003);
softAssertions.assertThat(node.getType()).isEqualTo(NodeType.MASTER);
softAssertions.assertThat(node.getFlags()).contains(Flag.MASTER);
softAssertions.assertThat(node.getLinkState()).isEqualTo(LinkState.CONNECTED);
softAssertions.assertThat(node.getSlotRange().getSlots().size()).isEqualTo(5461);
});
}

@Test // GH-2678
void toClusterNodeWithIPv6HostnameSquareBrackets() {

Expand All @@ -273,12 +310,11 @@ void toClusterNodeWithInvalidIPv6Hostname() {
@MethodSource("clusterNodesEndpoints")
void shouldAcceptHostPatterns(String endpoint, String expectedAddress, String expectedPort, String expectedHostname) {

Matcher matcher = ClusterNodesConverter.clusterEndpointPattern.matcher(endpoint);
assertThat(matcher.matches()).isTrue();
ClusterNodesConverter.AddressPortHostname addressPortHostname = ClusterNodesConverter.AddressPortHostname.of(new String[] { "id", endpoint });

assertThat(matcher.group(1)).isEqualTo(expectedAddress);
assertThat(matcher.group(2)).isEqualTo(expectedPort);
assertThat(matcher.group(3)).isEqualTo(expectedHostname);
assertThat(addressPortHostname.addressPart()).isEqualTo(expectedAddress);
assertThat(addressPortHostname.portPart()).isEqualTo(expectedPort);
assertThat(addressPortHostname.hostnamePart()).isEqualTo(expectedHostname);
}

static Stream<Arguments> clusterNodesEndpoints() {
Expand Down

0 comments on commit bb2eb9b

Please sign in to comment.