Fix #526. Node parsing error uppon :0

This commit is contained in:
Marcos Nils
2014-02-04 22:34:48 -03:00
parent 35291bf17d
commit d846149ae1
2 changed files with 11 additions and 6 deletions

View File

@@ -27,15 +27,17 @@ 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();
discoverClusterNodesAndSlots(jedis);
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 +65,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());