Fixed - hostname after comma in Redis Cluster topology isn't parsed. #6121

pull/6148/head
Nikita Koksharov 6 months ago
parent 14d8962fd3
commit 2c2ce4ddb0

@ -64,18 +64,10 @@ public class ClusterNodesDecoder implements Decoder<List<ClusterNodeInfo>> {
}
if (!node.containsFlag(Flag.NOADDR)) {
String protocol = "redis://";
if (ssl) {
protocol = "rediss://";
}
String addr = params[1].split("@")[0];
String name = addr.substring(0, addr.lastIndexOf(":"));
if (name.isEmpty()) {
// skip nodes with empty address
String uri = createUri(params);
if (uri == null) {
continue;
}
String uri = protocol + addr;
node.setAddress(uri);
}
@ -104,4 +96,25 @@ public class ClusterNodesDecoder implements Decoder<List<ClusterNodeInfo>> {
return nodes;
}
private String createUri(String[] params) {
String protocol = "redis://";
if (ssl) {
protocol = "rediss://";
}
String[] parts = params[1].split(",");
String addr = parts[0].split("@")[0];
String name = addr.substring(0, addr.lastIndexOf(":"));
if (name.isEmpty()) {
// skip nodes with empty address
return null;
}
if (parts.length == 2) {
String port = addr.substring(name.length() + 1);
addr = parts[1] + ":" + port;
}
return protocol + addr;
}
}

@ -13,7 +13,7 @@ import io.netty.buffer.Unpooled;
public class ClusterNodesDecoderTest {
@Test
public void test() throws IOException {
public void testIPs() throws IOException {
ClusterNodesDecoder decoder = new ClusterNodesDecoder(false);
ByteBuf buf = Unpooled.buffer();
@ -32,4 +32,24 @@ public class ClusterNodesDecoderTest {
Assertions.assertEquals(7001, node.getAddress().getPort());
}
@Test
public void testHostnames() throws IOException {
ClusterNodesDecoder decoder = new ClusterNodesDecoder(false);
ByteBuf buf = Unpooled.buffer();
String info = "7af253f8c20a3b3fbd481801bd361ec6643c6f0b 192.168.234.129:7001@17001,hostname1 master - 0 1478865073260 8 connected 5461-10922\n" +
"a0d6a300f9f3b139c89cf45b75dbb7e4a01bb6b5 192.168.234.131:7005@17005,hostname2 slave 5b00efb410f14ba5bb0a153c057e431d9ee4562e 0 1478865072251 5 connected\n" +
"454b8aaab7d8687822923da37a91fc0eecbe7a88 192.168.234.130:7002@17002,hostname3 slave 7af253f8c20a3b3fbd481801bd361ec6643c6f0b 0 1478865072755 8 connected\n" +
"5b00efb410f14ba5bb0a153c057e431d9ee4562e 192.168.234.131:7004@17004,hostname4 master - 0 1478865071746 5 connected 10923-16383\n" +
"14edcdebea55853533a24d5cdc560ecc06ec5295 192.168.234.130:7003@17003,hostname5 myself,master - 0 0 7 connected 0-5460\n" +
"58d9f7c6d801aeebaf0e04e1aacb991e7e0ca8ff 192.168.234.129:7000@17000,hostname6 slave 14edcdebea55853533a24d5cdc560ecc06ec5295 0 1478865071241 7 connected\n";
byte[] src = info.getBytes();
buf.writeBytes(src);
List<ClusterNodeInfo> nodes = decoder.decode(buf, null);
ClusterNodeInfo node = nodes.get(0);
Assertions.assertEquals("hostname1", node.getAddress().getHost());
Assertions.assertEquals(7001, node.getAddress().getPort());
}
}

Loading…
Cancel
Save