Skip to content

Commit d1908fd

Browse files
TheTweakmp911de
authored andcommitted
Add support for bracket-less IPv6 addresses to CLUSTER NODES Converter.
Closes #2678 Original pull request: #2679
1 parent 158c73e commit d1908fd

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

src/main/java/org/springframework/data/redis/connection/convert/Converters.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
* @author Christoph Strobl
6161
* @author daihuabin
6262
* @author John Blum
63+
* @author Sorokin Evgeniy
6364
*/
6465
public abstract class Converters {
6566

@@ -560,21 +561,28 @@ enum ClusterNodesConverter implements Converter<String, RedisClusterNode> {
560561
public RedisClusterNode convert(String source) {
561562

562563
String[] args = source.split(" ");
563-
String[] hostAndPort = StringUtils.split(args[HOST_PORT_INDEX], ":");
564564

565-
Assert.notNull(hostAndPort, "ClusterNode information does not define host and port");
565+
int lastColonIndex = args[HOST_PORT_INDEX].lastIndexOf(":");
566+
567+
Assert.isTrue(lastColonIndex >= 0 && lastColonIndex < args[HOST_PORT_INDEX].length() - 1,
568+
"ClusterNode information does not define host and port");
569+
570+
String portPart = args[HOST_PORT_INDEX].substring(lastColonIndex + 1);
571+
String hostPart = args[HOST_PORT_INDEX].substring(0, lastColonIndex);
566572

567573
SlotRange range = parseSlotRange(args);
568574
Set<Flag> flags = parseFlags(args);
569575

570-
String portPart = hostAndPort[1];
571-
572576
if (portPart.contains("@")) {
573577
portPart = portPart.substring(0, portPart.indexOf('@'));
574578
}
575579

580+
if (hostPart.startsWith("[") && hostPart.endsWith("]")) {
581+
hostPart = hostPart.substring(1, hostPart.length() - 1);
582+
}
583+
576584
RedisClusterNodeBuilder nodeBuilder = RedisClusterNode.newRedisClusterNode()
577-
.listeningAt(hostAndPort[0], Integer.parseInt(portPart)) //
585+
.listeningAt(hostPart, Integer.parseInt(portPart)) //
578586
.withId(args[ID_INDEX]) //
579587
.promotedAs(flags.contains(Flag.MASTER) ? NodeType.MASTER : NodeType.REPLICA) //
580588
.serving(range) //

src/test/java/org/springframework/data/redis/connection/convert/ConvertersUnitTests.java

+39
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
*
3232
* @author Christoph Strobl
3333
* @author Mark Paluch
34+
* @author Sorokin Evgeniy
3435
*/
3536
class ConvertersUnitTests {
3637

@@ -60,6 +61,12 @@ class ConvertersUnitTests {
6061

6162
private static final String CLUSTER_NODE_WITHOUT_HOST = "ef570f86c7b1a953846668debc177a3a16733420 :6379 fail,master - 0 0 1 connected";
6263

64+
private static final String CLUSTER_NODE_WITH_SINGLE_IPV6_HOST = "67adfe3df1058896e3cb49d2863e0f70e7e159fa 2a02:6b8:c67:9c:0:6d8b:33da:5a2c:6380@16380,redis-master master,nofailover - 0 1692108412315 1 connected 0-5460";
65+
66+
private static final String CLUSTER_NODE_WITH_SINGLE_IPV6_HOST_SQUARE_BRACKETS = "67adfe3df1058896e3cb49d2863e0f70e7e159fa [2a02:6b8:c67:9c:0:6d8b:33da:5a2c]:6380@16380,redis-master master,nofailover - 0 1692108412315 1 connected 0-5460";
67+
68+
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";
69+
6370
@Test // DATAREDIS-315
6471
void toSetOfRedis30ClusterNodesShouldConvertSingleStringNodesResponseCorrectly() {
6572

@@ -222,4 +229,36 @@ void toSetOfRedisClusterNodesShouldAllowEmptyHostname() {
222229
assertThat(node.getSlotRange().getSlots().size()).isEqualTo(0);
223230
}
224231

232+
@Test // https://github.com/spring-projects/spring-data-redis/issues/2678
233+
void toClusterNodeWithIPv6Hostname() {
234+
RedisClusterNode node = Converters.toClusterNode(CLUSTER_NODE_WITH_SINGLE_IPV6_HOST);
235+
236+
assertThat(node.getId()).isEqualTo("67adfe3df1058896e3cb49d2863e0f70e7e159fa");
237+
assertThat(node.getHost()).isEqualTo("2a02:6b8:c67:9c:0:6d8b:33da:5a2c");
238+
assertThat(node.hasValidHost()).isTrue();
239+
assertThat(node.getPort()).isEqualTo(6380);
240+
assertThat(node.getType()).isEqualTo(NodeType.MASTER);
241+
assertThat(node.getFlags()).contains(Flag.MASTER);
242+
assertThat(node.getLinkState()).isEqualTo(LinkState.CONNECTED);
243+
assertThat(node.getSlotRange().getSlots().size()).isEqualTo(5461);
244+
}
245+
246+
@Test // https://github.com/spring-projects/spring-data-redis/issues/2678
247+
void toClusterNodeWithIPv6HostnameSquareBrackets() {
248+
RedisClusterNode node = Converters.toClusterNode(CLUSTER_NODE_WITH_SINGLE_IPV6_HOST_SQUARE_BRACKETS);
249+
250+
assertThat(node.getId()).isEqualTo("67adfe3df1058896e3cb49d2863e0f70e7e159fa");
251+
assertThat(node.getHost()).isEqualTo("2a02:6b8:c67:9c:0:6d8b:33da:5a2c");
252+
assertThat(node.hasValidHost()).isTrue();
253+
assertThat(node.getPort()).isEqualTo(6380);
254+
assertThat(node.getType()).isEqualTo(NodeType.MASTER);
255+
assertThat(node.getFlags()).contains(Flag.MASTER);
256+
assertThat(node.getLinkState()).isEqualTo(LinkState.CONNECTED);
257+
assertThat(node.getSlotRange().getSlots().size()).isEqualTo(5461);
258+
}
259+
260+
@Test // https://github.com/spring-projects/spring-data-redis/issues/2678
261+
void toClusterNodeWithInvalidIPv6Hostname() {
262+
assertThatIllegalArgumentException().isThrownBy(() -> Converters.toClusterNode(CLUSTER_NODE_WITH_SINGLE_INVALID_IPV6_HOST));
263+
}
225264
}

0 commit comments

Comments
 (0)