Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public class NodeInfo extends NodeOperationResponse {

private ImmutableMap<String, String> serviceAttributes;

@Nullable
private String hostname;

private Settings settings;

private OsInfo os;
Expand All @@ -61,10 +64,11 @@ public class NodeInfo extends NodeOperationResponse {
NodeInfo() {
}

public NodeInfo(DiscoveryNode node, ImmutableMap<String, String> serviceAttributes, Settings settings,
public NodeInfo(String hostname, DiscoveryNode node, ImmutableMap<String, String> serviceAttributes, Settings settings,
OsInfo os, ProcessInfo process, JvmInfo jvm, NetworkInfo network,
TransportInfo transport, @Nullable HttpInfo http) {
super(node);
this.hostname = hostname;
this.serviceAttributes = serviceAttributes;
this.settings = settings;
this.os = os;
Expand All @@ -75,6 +79,22 @@ public NodeInfo(DiscoveryNode node, ImmutableMap<String, String> serviceAttribut
this.http = http;
}

/**
* System's hostname. <code>null</code> in case of UnknownHostException
*/
@Nullable
public String hostname() {
return this.hostname;
}

/**
* System's hostname. <code>null</code> in case of UnknownHostException
*/
@Nullable
public String getHostname() {
return hostname();
}

/**
* The service attributes of the node.
*/
Expand Down Expand Up @@ -184,6 +204,9 @@ public static NodeInfo readNodeInfo(StreamInput in) throws IOException {
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
if (in.readBoolean()) {
hostname = in.readUTF();
}
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
int size = in.readVInt();
for (int i = 0; i < size; i++) {
Expand Down Expand Up @@ -214,6 +237,12 @@ public void readFrom(StreamInput in) throws IOException {
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
if (hostname == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeUTF(hostname);
}
out.writeVInt(serviceAttributes.size());
for (Map.Entry<String, String> entry : serviceAttributes.entrySet()) {
out.writeUTF(entry.getKey());
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/org/elasticsearch/node/service/NodeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.elasticsearch.node.service;

import java.net.InetAddress;

import com.google.common.collect.ImmutableMap;
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
Expand All @@ -27,6 +29,7 @@
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.network.NetworkUtils;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.discovery.Discovery;
import org.elasticsearch.http.HttpServer;
Expand All @@ -51,6 +54,9 @@ public class NodeService extends AbstractComponent {

private volatile ImmutableMap<String, String> serviceAttributes = ImmutableMap.of();

@Nullable
private String hostname;

@Inject
public NodeService(Settings settings, MonitorService monitorService, Discovery discovery, ClusterService clusterService, TransportService transportService, IndicesService indicesService) {
super(settings);
Expand All @@ -59,6 +65,10 @@ public NodeService(Settings settings, MonitorService monitorService, Discovery d
this.transportService = transportService;
this.indicesService = indicesService;
discovery.setNodeService(this);
InetAddress address = NetworkUtils.getLocalAddress();
if (address != null) {
this.hostname = address.getHostName();
}
}

public void setHttpServer(@Nullable HttpServer httpServer) {
Expand Down Expand Up @@ -91,7 +101,7 @@ public ImmutableMap<String, String> attributes() {
}

public NodeInfo info() {
return new NodeInfo(clusterService.state().nodes().localNode(), serviceAttributes, settings,
return new NodeInfo(hostname, clusterService.state().nodes().localNode(), serviceAttributes, settings,
monitorService.osService().info(), monitorService.processService().info(),
monitorService.jvmService().info(), monitorService.networkService().info(),
transportService.info(), httpServer == null ? null : httpServer.info());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public void onResponse(NodesInfoResponse result) {
builder.field("name", nodeInfo.node().name(), XContentBuilder.FieldCaseConversion.NONE);
builder.field("transport_address", nodeInfo.node().address().toString());

if (nodeInfo.hostname() != null) {
builder.field("hostname", nodeInfo.hostname(), XContentBuilder.FieldCaseConversion.NONE);
}

for (Map.Entry<String, String> nodeAttribute : nodeInfo.serviceAttributes().entrySet()) {
builder.field(nodeAttribute.getKey(), nodeAttribute.getValue());
}
Expand Down