Merge pull request #527 from xetorthio/node_parsing

Fix #526. Node parsing error uppon :0
This commit is contained in:
Jonathan Leibiusky
2014-02-05 10:38:21 -05:00
2 changed files with 14 additions and 7 deletions

View File

@@ -27,15 +27,19 @@ public abstract class JedisClusterConnectionHandler {
JedisPool jp = new JedisPool(hostAndPort.getHost(), JedisPool jp = new JedisPool(hostAndPort.getHost(),
hostAndPort.getPort()); hostAndPort.getPort());
this.nodes.put(hostAndPort.getHost() + hostAndPort.getPort(), jp); this.nodes.put(hostAndPort.getHost() + hostAndPort.getPort(), jp);
discoverClusterNodesAndSlots(jp); Jedis jedis = jp.getResource();
try {
discoverClusterNodesAndSlots(jedis);
} finally {
jp.returnResource(jedis);
}
} }
} }
private void discoverClusterNodesAndSlots(JedisPool jp) { private void discoverClusterNodesAndSlots(Jedis jedis) {
String localNodes = jp.getResource().clusterNodes(); String localNodes = jedis.clusterNodes();
for (String nodeInfo : localNodes.split("\n")) { for (String nodeInfo : localNodes.split("\n")) {
HostAndPort node = getHostAndPortFromNodeLine(nodeInfo); HostAndPort node = getHostAndPortFromNodeLine(nodeInfo, jedis);
JedisPool nodePool = new JedisPool(node.getHost(), node.getPort()); JedisPool nodePool = new JedisPool(node.getHost(), node.getPort());
this.nodes.put(node.getHost() + node.getPort(), nodePool); this.nodes.put(node.getHost() + node.getPort(), nodePool);
populateNodeSlots(nodeInfo, nodePool); populateNodeSlots(nodeInfo, nodePool);
@@ -63,8 +67,12 @@ public abstract class JedisClusterConnectionHandler {
} }
} }
private HostAndPort getHostAndPortFromNodeLine(String nodeInfo) { private HostAndPort getHostAndPortFromNodeLine(String nodeInfo, Jedis currentConnection) {
String stringHostAndPort = nodeInfo.split(" ", 3)[1]; String stringHostAndPort = nodeInfo.split(" ", 3)[1];
if (":0".equals(stringHostAndPort)) {
return new HostAndPort(currentConnection.getClient().getHost(),
currentConnection.getClient().getPort());
}
String[] arrayHostAndPort = stringHostAndPort.split(":"); String[] arrayHostAndPort = stringHostAndPort.split(":");
return new HostAndPort(arrayHostAndPort[0], return new HostAndPort(arrayHostAndPort[0],
Integer.valueOf(arrayHostAndPort[1])); Integer.valueOf(arrayHostAndPort[1]));

View File

@@ -44,7 +44,6 @@ public class JedisClusterTest extends Assert {
// ---- configure cluster // ---- configure cluster
// add nodes to cluster // add nodes to cluster
node1.clusterMeet("127.0.0.1", nodeInfo1.getPort());
node1.clusterMeet("127.0.0.1", nodeInfo2.getPort()); node1.clusterMeet("127.0.0.1", nodeInfo2.getPort());
node1.clusterMeet("127.0.0.1", nodeInfo3.getPort()); node1.clusterMeet("127.0.0.1", nodeInfo3.getPort());