Skip to content

Commit

Permalink
resolve hostname for the hostname column in sys.nodes
Browse files Browse the repository at this point in the history
it was previously done in ES, but removed after
ES 2.0.0
elastic/elasticsearch#13014
  • Loading branch information
kovrus committed Jul 15, 2016
1 parent 773f706 commit ee15c21
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ Changes for Crate
Unreleased
==========

- Fix: the ``hostname`` column of the ``sys.nodes`` table returns a hostname
instead of an IP address.

- Added initial experimental support for the postgres wire protocol. This is
disabled by default.

- Fixed an issue that caused select statements to get stuck if used as part of
a bulk request. Now a proper error is raised.

- Updated Elasticsearch to 2.3.4

- Improved the error message of `COPY FROM` statements which fail due to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.node.DiscoveryNode;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.Charset;

public class NodeHostnameExpression extends SysNodeExpression<BytesRef> {

private final ClusterService clusterService;
Expand All @@ -37,9 +42,21 @@ public NodeHostnameExpression(ClusterService clusterService) {
public BytesRef value() {
DiscoveryNode localNode = clusterService.localNode();
if (localNode != null) {
return new BytesRef(localNode.getHostName());
final String hostAddress = localNode.getHostAddress();
return new BytesRef(resolveHostname(hostAddress));
}
return null;
}

private String resolveHostname(final String address) {
try {
byte[] ip = address.getBytes(Charset.forName("UTF-8"));
InetAddress inetAddress = InetAddress.getByAddress(ip);
return inetAddress.getHostName();
} catch (UnknownHostException e) {
// return an ip address, if a hostname was not resolved
return address;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.common.inject.ModulesBuilder;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.BoundTransportAddress;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
Expand Down Expand Up @@ -133,7 +134,7 @@ protected void configure() {
}

DiscoveryNode node = mock(DiscoveryNode.class);
when(node.getHostName()).thenReturn("localhost");
when(node.getHostAddress()).thenReturn("192.168.0.2");
when(nodeStats.getNode()).thenReturn(node);
when(clusterService.localNode()).thenReturn(node);

Expand Down Expand Up @@ -434,6 +435,24 @@ public void testOsInfo() throws Exception {
assertEquals(4, cores);
}

@Test
public void testHostnameExpressionIpCannotBeResolved() throws Exception {
ReferenceInfo refInfo = refInfo("sys.nodes.hostname", DataTypes.STRING, RowGranularity.NODE);
SimpleObjectExpression hostnameExpression = (SimpleObjectExpression) resolver.getImplementation(refInfo);
assertThat(BytesRefs.toString(hostnameExpression.value()), is("192.168.0.2"));
}

@Test
public void testHostnameExpressionResolveLocalhost() throws Exception {
ClusterService clusterService = mock(ClusterService.class);
DiscoveryNode discoveryNode = mock(DiscoveryNode.class);
when(discoveryNode.getHostAddress()).thenReturn("127.0.0.1");
when(clusterService.localNode()).thenReturn(discoveryNode);

NodeHostnameExpression expression = new NodeHostnameExpression(clusterService);
assertThat(BytesRefs.toString(expression.value()), is("localhost"));
}

@Test
public void testNestedBytesRefExpressionsString() throws Exception {
ReferenceInfo refInfo = refInfo("sys.nodes.version", DataTypes.OBJECT, RowGranularity.NODE);
Expand Down

0 comments on commit ee15c21

Please sign in to comment.