Merge pull request #1138 from metamx/better-default-hostname

Better default hostname
This commit is contained in:
Fangjin Yang 2015-02-18 17:37:34 -08:00
commit 25db9abb7f
4 changed files with 22 additions and 8 deletions

View File

@ -18,6 +18,7 @@
package io.druid.indexing.worker.config; package io.druid.indexing.worker.config;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import io.druid.server.DruidNode;
import javax.validation.constraints.Min; import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@ -28,7 +29,7 @@ public class WorkerConfig
{ {
@JsonProperty @JsonProperty
@NotNull @NotNull
private String ip = "localhost"; private String ip = DruidNode.getDefaultHost();
@JsonProperty @JsonProperty
@NotNull @NotNull

View File

@ -58,12 +58,16 @@ RUN apt-get clean && rm -rf /tmp/* /var/tmp/*
# - 8081: HTTP (coordinator) # - 8081: HTTP (coordinator)
# - 8082: HTTP (broker) # - 8082: HTTP (broker)
# - 8083: HTTP (historical) # - 8083: HTTP (historical)
# - 8090: HTTP (overlord)
# - 8091: HTTP (middlemanager)
# - 3306: MySQL # - 3306: MySQL
# - 2181 2888 3888: ZooKeeper # - 2181 2888 3888: ZooKeeper
# - 8100 8101 8102 8103 8104 : peon ports # - 8100 8101 8102 8103 8104 : peon ports
EXPOSE 8081 EXPOSE 8081
EXPOSE 8082 EXPOSE 8082
EXPOSE 8083 EXPOSE 8083
EXPOSE 8090
EXPOSE 8091
EXPOSE 3306 EXPOSE 3306
EXPOSE 2181 2888 3888 EXPOSE 2181 2888 3888
EXPOSE 8100 8101 8102 8103 8104 EXPOSE 8100 8101 8102 8103 8104

View File

@ -24,18 +24,19 @@ import com.google.common.base.Preconditions;
import com.google.common.net.HostAndPort; import com.google.common.net.HostAndPort;
import com.google.inject.name.Named; import com.google.inject.name.Named;
import com.metamx.common.IAE; import com.metamx.common.IAE;
import com.metamx.common.ISE;
import io.druid.common.utils.SocketUtil; import io.druid.common.utils.SocketUtil;
import javax.validation.constraints.Max; import javax.validation.constraints.Max;
import javax.validation.constraints.Min; import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.net.InetAddress;
import java.net.UnknownHostException;
/** /**
*/ */
public class DruidNode public class DruidNode
{ {
public static final String DEFAULT_HOST = "localhost";
@JsonProperty("service") @JsonProperty("service")
@NotNull @NotNull
private String serviceName; private String serviceName;
@ -81,7 +82,7 @@ public class DruidNode
this.serviceName = serviceName; this.serviceName = serviceName;
if(host == null && port == null) { if(host == null && port == null) {
host = DEFAULT_HOST; host = getDefaultHost();
port = -1; port = -1;
} }
else { else {
@ -92,7 +93,7 @@ public class DruidNode
throw new IAE("Conflicting host:port [%s] and port [%d] settings", host, port); throw new IAE("Conflicting host:port [%s] and port [%d] settings", host, port);
} }
} else { } else {
hostAndPort = HostAndPort.fromParts(DEFAULT_HOST, port); hostAndPort = HostAndPort.fromParts(getDefaultHost(), port);
} }
host = hostAndPort.getHostText(); host = hostAndPort.getHostText();
@ -136,6 +137,14 @@ public class DruidNode
} }
} }
public static String getDefaultHost() {
try {
return InetAddress.getLocalHost().getCanonicalHostName();
} catch(UnknownHostException e) {
throw new ISE(e, "Unable to determine host name");
}
}
@Override @Override
public String toString() public String toString()
{ {

View File

@ -30,9 +30,9 @@ public class DruidNodeTest
DruidNode node; DruidNode node;
node = new DruidNode(service, null, null); node = new DruidNode(service, null, null);
Assert.assertEquals(DruidNode.DEFAULT_HOST, node.getHost()); Assert.assertEquals(DruidNode.getDefaultHost(), node.getHost());
Assert.assertEquals(-1, node.getPort()); Assert.assertEquals(-1, node.getPort());
Assert.assertEquals("localhost", node.getHostAndPort()); Assert.assertEquals(DruidNode.getDefaultHost(), node.getHostAndPort());
node = new DruidNode(service, "2001:db8:85a3::8a2e:370:7334", -1); node = new DruidNode(service, "2001:db8:85a3::8a2e:370:7334", -1);
Assert.assertEquals("2001:db8:85a3::8a2e:370:7334", node.getHost()); Assert.assertEquals("2001:db8:85a3::8a2e:370:7334", node.getHost());
@ -82,7 +82,7 @@ public class DruidNodeTest
Assert.assertEquals("[2001:db8:85a3::8a2e:370:7334]:123", node.getHostAndPort()); Assert.assertEquals("[2001:db8:85a3::8a2e:370:7334]:123", node.getHostAndPort());
node = new DruidNode(service, null, 123); node = new DruidNode(service, null, 123);
Assert.assertEquals(DruidNode.DEFAULT_HOST, node.getHost()); Assert.assertEquals(DruidNode.getDefaultHost(), node.getHost());
Assert.assertEquals(123, node.getPort()); Assert.assertEquals(123, node.getPort());
} }