default to canonical hostname instead of localhost

This commit is contained in:
Xavier Léauté 2015-02-18 16:44:48 -08:00
parent b42b41cca7
commit 53d2b961c5
3 changed files with 18 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

@ -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());
} }