From 0a0a1e7d8beac27cd07dd0f445e55e9c6646e10c Mon Sep 17 00:00:00 2001 From: Imesha Date: Wed, 30 Nov 2016 05:25:24 +0530 Subject: [PATCH] CURATOR-360 - Allow Zookeeper servers in TestingCluster to listen on network interfaces other than localhost --- .../org/apache/curator/test/InstanceSpec.java | 34 ++++++++++++++++--- .../curator/test/QuorumConfigBuilder.java | 2 +- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/curator-test/src/main/java/org/apache/curator/test/InstanceSpec.java b/curator-test/src/main/java/org/apache/curator/test/InstanceSpec.java index 32b1738f9b..bc0272c5dd 100644 --- a/curator-test/src/main/java/org/apache/curator/test/InstanceSpec.java +++ b/curator-test/src/main/java/org/apache/curator/test/InstanceSpec.java @@ -72,6 +72,7 @@ public class InstanceSpec private final int tickTime; private final int maxClientCnxns; private final Map customProperties; + private final String hostname; public static InstanceSpec newInstanceSpec() { @@ -116,7 +117,7 @@ public static int getRandomPort() */ public InstanceSpec(File dataDirectory, int port, int electionPort, int quorumPort, boolean deleteDataDirectoryOnClose, int serverId) { - this(dataDirectory, port, electionPort, quorumPort, deleteDataDirectoryOnClose, serverId, -1, -1, null); + this(dataDirectory, port, electionPort, quorumPort, deleteDataDirectoryOnClose, serverId, -1, -1, null, null); } /** @@ -130,7 +131,7 @@ public InstanceSpec(File dataDirectory, int port, int electionPort, int quorumPo * @param maxClientCnxns max number of client connections from the same IP. Set -1 to use default server configuration */ public InstanceSpec(File dataDirectory, int port, int electionPort, int quorumPort, boolean deleteDataDirectoryOnClose, int serverId, int tickTime, int maxClientCnxns) { - this(dataDirectory, port, electionPort, quorumPort, deleteDataDirectoryOnClose, serverId, tickTime, maxClientCnxns, null); + this(dataDirectory, port, electionPort, quorumPort, deleteDataDirectoryOnClose, serverId, tickTime, maxClientCnxns, null, null); } /** @@ -145,6 +146,23 @@ public InstanceSpec(File dataDirectory, int port, int electionPort, int quorumPo * @param customProperties other properties to be passed to the server */ public InstanceSpec(File dataDirectory, int port, int electionPort, int quorumPort, boolean deleteDataDirectoryOnClose, int serverId, int tickTime, int maxClientCnxns, Map customProperties) + { + this(dataDirectory, port, electionPort, quorumPort, deleteDataDirectoryOnClose, serverId, tickTime, maxClientCnxns, customProperties, null); + } + + /** + * @param dataDirectory where to store data/logs/etc. + * @param port the port to listen on - each server in the ensemble must use a unique port + * @param electionPort the electionPort to listen on - each server in the ensemble must use a unique electionPort + * @param quorumPort the quorumPort to listen on - each server in the ensemble must use a unique quorumPort + * @param deleteDataDirectoryOnClose if true, the data directory will be deleted when {@link TestingCluster#close()} is called + * @param serverId the server ID for the instance + * @param tickTime tickTime. Set -1 to used fault server configuration + * @param maxClientCnxns max number of client connections from the same IP. Set -1 to use default server configuration + * @param customProperties other properties to be passed to the server + * @param hostname Hostname or IP if the cluster is intending to be bounded into external interfaces + */ + public InstanceSpec(File dataDirectory, int port, int electionPort, int quorumPort, boolean deleteDataDirectoryOnClose, int serverId, int tickTime, int maxClientCnxns, Map customProperties,String hostname) { this.dataDirectory = (dataDirectory != null) ? dataDirectory : Files.createTempDir(); this.port = (port >= 0) ? port : getRandomPort(); @@ -155,6 +173,7 @@ public InstanceSpec(File dataDirectory, int port, int electionPort, int quorumPo this.tickTime = (tickTime > 0 ? tickTime : -1); // -1 to set default value this.maxClientCnxns = (maxClientCnxns >= 0 ? maxClientCnxns : -1); // -1 to set default value this.customProperties = customProperties != null ? Collections.unmodifiableMap(customProperties) : Collections.emptyMap(); + this.hostname = hostname == null ? localhost : hostname; } public int getServerId() @@ -184,7 +203,7 @@ public int getQuorumPort() public String getConnectString() { - return localhost + ":" + port; + return hostname + ":" + port; } public int getTickTime() @@ -206,6 +225,10 @@ public Map getCustomProperties() { return customProperties; } + public String getHostname() { + return hostname; + } + @Override public String toString() { @@ -219,6 +242,7 @@ public String toString() ", tickTime=" + tickTime + ", maxClientCnxns=" + maxClientCnxns + ", customProperties=" + customProperties + + ", hostname=" + hostname + "} " + super.toString(); } @@ -236,13 +260,13 @@ public boolean equals(Object o) InstanceSpec that = (InstanceSpec)o; - return port == that.port; + return hostname.equals(that.getHostname()) && port == that.port; } @Override public int hashCode() { - return port; + return hostname.hashCode() + port; } } diff --git a/curator-test/src/main/java/org/apache/curator/test/QuorumConfigBuilder.java b/curator-test/src/main/java/org/apache/curator/test/QuorumConfigBuilder.java index 4e2016345a..fb4039d38a 100644 --- a/curator-test/src/main/java/org/apache/curator/test/QuorumConfigBuilder.java +++ b/curator-test/src/main/java/org/apache/curator/test/QuorumConfigBuilder.java @@ -100,7 +100,7 @@ public QuorumPeerConfig buildConfig(int instanceIndex) throws Exception { for ( InstanceSpec thisSpec : instanceSpecs ) { - properties.setProperty("server." + thisSpec.getServerId(), String.format("localhost:%d:%d", thisSpec.getQuorumPort(), thisSpec.getElectionPort())); + properties.setProperty("server." + thisSpec.getServerId(), String.format("%s:%d:%d", thisSpec.getHostname(), thisSpec.getQuorumPort(), thisSpec.getElectionPort())); } } Map customProperties = spec.getCustomProperties();