Skip to content

Commit

Permalink
use cluster hostname if available
Browse files Browse the repository at this point in the history
  • Loading branch information
chenby committed Sep 3, 2024
1 parent bde7126 commit 401671f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static <T> XClusterNodes parse(List<String> conf, Set<T> set, Map<Short,
int cIdx = hostAndPort.indexOf(":");
int aIdx = hostAndPort.indexOf("@");
int commaIdx = hostAndPort.indexOf(",");
String host = "";
String host;
if (commaIdx == -1) {
host = hostAndPort.substring(0, cIdx);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.moilioncircle.redis.rdb.cli.conf;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.junit.Test;

/**
* @author Baoyi Chen
*/
public class NodeConfParserTest {

@Test
public void test0() throws Exception {
String conf = readConf("rdt/nodes.conf");
XClusterNodes nodes = NodeConfParser.parse(conf);
assertEquals(nodes.getNodes().get(0).getHostAndPort().getHost(), "127.0.0.1");
assertEquals(nodes.getNodes().get(1).getHostAndPort().getHost(), "127.0.0.1");
assertEquals(nodes.getNodes().get(2).getHostAndPort().getHost(), "127.0.0.1");
assertEquals(nodes.getNodes().get(3).getHostAndPort().getHost(), "127.0.0.1");
assertEquals(nodes.getNodes().get(4).getHostAndPort().getHost(), "127.0.0.1");
assertEquals(nodes.getNodes().get(5).getHostAndPort().getHost(), "127.0.0.1");
}

@Test
public void test1() throws Exception {
String conf = readConf("rdt/nodes-hostname.conf");
XClusterNodes nodes = NodeConfParser.parse(conf);
assertEquals(nodes.getNodes().get(0).getHostAndPort().getHost(), "redis-host1");
assertEquals(nodes.getNodes().get(1).getHostAndPort().getHost(), "redis-host2");
assertEquals(nodes.getNodes().get(2).getHostAndPort().getHost(), "redis-host3");
assertEquals(nodes.getNodes().get(3).getHostAndPort().getHost(), "redis-host4");
assertEquals(nodes.getNodes().get(4).getHostAndPort().getHost(), "redis-host5");
assertEquals(nodes.getNodes().get(5).getHostAndPort().getHost(), "redis-host6");
}

private static String readConf(String path) throws IOException, URISyntaxException {
return new String(Files.readAllBytes(Paths.get(NodeConfParserTest.class.getClassLoader().getResource(path).toURI())), StandardCharsets.UTF_8);
}
}
7 changes: 7 additions & 0 deletions src/test/resources/rdt/nodes-hostname.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
126be0dd065a87eea5cb4036d1d4ee958c02078a 127.0.0.1:10002@20002,redis-host1 master - 0 1531044047088 2 connected 5461-10922
e5a488eb96774d76ccec147be93e50b29ea01801 127.0.0.1:10006@20006,redis-host2 slave 126be0dd065a87eea5cb4036d1d4ee958c02078a 0 1531044047088 6 connected
34b6e1dfb871ad30398ef5edd6b9a954617e6ec1 127.0.0.1:10003@20003,redis-host3 master - 0 1531044047088 3 connected 10923-16383
115ce322ff004253556f3ac0829566edce0aeaea 127.0.0.1:10004@20004,redis-host4 slave 34b6e1dfb871ad30398ef5edd6b9a954617e6ec1 0 1531044047993 4 connected
84dd95d7e10734490c94d10e9a58cd7c57021506 127.0.0.1:10005@20005,redis-host5 slave 89d020a7e727e81f003836207902ae26fe05fd51 0 1531044047000 5 connected
89d020a7e727e81f003836207902ae26fe05fd51 127.0.0.1:10001@20001,redis-host6 myself,master - 0 1531044047000 1 connected 0-5460
vars currentEpoch 6 lastVoteEpoch 0

0 comments on commit 401671f

Please sign in to comment.