Skip to content

Commit 1584c73

Browse files
committed
add host name and host address to DiscoveryNode
this will allow to easily access them regardless of the transport address used, and allow to remove reverse lookup of host names from ip address extracted from inet transport address where needed
1 parent e01f8c2 commit 1584c73

File tree

20 files changed

+148
-179
lines changed

20 files changed

+148
-179
lines changed

src/main/java/org/elasticsearch/action/admin/cluster/node/info/NodeInfo.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ public class NodeInfo extends NodeOperationResponse {
4848
@Nullable
4949
private ImmutableMap<String, String> serviceAttributes;
5050

51-
@Nullable
52-
private String hostname;
53-
5451
private Version version;
5552
private Build build;
5653

@@ -84,11 +81,10 @@ public class NodeInfo extends NodeOperationResponse {
8481
NodeInfo() {
8582
}
8683

87-
public NodeInfo(@Nullable String hostname, Version version, Build build, DiscoveryNode node, @Nullable ImmutableMap<String, String> serviceAttributes, @Nullable Settings settings,
84+
public NodeInfo(Version version, Build build, DiscoveryNode node, @Nullable ImmutableMap<String, String> serviceAttributes, @Nullable Settings settings,
8885
@Nullable OsInfo os, @Nullable ProcessInfo process, @Nullable JvmInfo jvm, @Nullable ThreadPoolInfo threadPool, @Nullable NetworkInfo network,
8986
@Nullable TransportInfo transport, @Nullable HttpInfo http, @Nullable PluginsInfo plugins) {
9087
super(node);
91-
this.hostname = hostname;
9288
this.version = version;
9389
this.build = build;
9490
this.serviceAttributes = serviceAttributes;
@@ -108,7 +104,7 @@ public NodeInfo(@Nullable String hostname, Version version, Build build, Discove
108104
*/
109105
@Nullable
110106
public String getHostname() {
111-
return this.hostname;
107+
return getNode().getHostName();
112108
}
113109

114110
/**
@@ -202,9 +198,6 @@ public static NodeInfo readNodeInfo(StreamInput in) throws IOException {
202198
@Override
203199
public void readFrom(StreamInput in) throws IOException {
204200
super.readFrom(in);
205-
if (in.readBoolean()) {
206-
hostname = in.readString();
207-
}
208201
version = Version.readVersion(in);
209202
build = Build.readBuild(in);
210203
if (in.readBoolean()) {
@@ -247,12 +240,6 @@ public void readFrom(StreamInput in) throws IOException {
247240
@Override
248241
public void writeTo(StreamOutput out) throws IOException {
249242
super.writeTo(out);
250-
if (hostname == null) {
251-
out.writeBoolean(false);
252-
} else {
253-
out.writeBoolean(true);
254-
out.writeString(hostname);
255-
}
256243
out.writeVInt(version.id);
257244
Build.writeBuild(build, out);
258245
if (getServiceAttributes() == null) {

src/main/java/org/elasticsearch/action/admin/cluster/node/info/NodesInfoResponse.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
7979

8080
builder.field("name", nodeInfo.getNode().name(), XContentBuilder.FieldCaseConversion.NONE);
8181
builder.field("transport_address", nodeInfo.getNode().address().toString());
82-
83-
if (nodeInfo.getHostname() != null) {
84-
builder.field("hostname", nodeInfo.getHostname(), XContentBuilder.FieldCaseConversion.NONE);
85-
}
82+
builder.field("host", nodeInfo.getNode().getHostName(), XContentBuilder.FieldCaseConversion.NONE);
83+
builder.field("ip", nodeInfo.getNode().getHostAddress(), XContentBuilder.FieldCaseConversion.NONE);
8684

8785
builder.field("version", nodeInfo.getVersion());
8886
builder.field("build", nodeInfo.getBuild().hashShort());

src/main/java/org/elasticsearch/action/admin/cluster/node/stats/NodeStats.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ public class NodeStats extends NodeOperationResponse implements ToXContent {
4747

4848
private long timestamp;
4949

50-
@Nullable
51-
private String hostname;
52-
5350
@Nullable
5451
private NodeIndicesStats indices;
5552

@@ -83,13 +80,12 @@ public class NodeStats extends NodeOperationResponse implements ToXContent {
8380
NodeStats() {
8481
}
8582

86-
public NodeStats(DiscoveryNode node, long timestamp, @Nullable String hostname, @Nullable NodeIndicesStats indices,
83+
public NodeStats(DiscoveryNode node, long timestamp, @Nullable NodeIndicesStats indices,
8784
@Nullable OsStats os, @Nullable ProcessStats process, @Nullable JvmStats jvm, @Nullable ThreadPoolStats threadPool,
8885
@Nullable NetworkStats network, @Nullable FsStats fs, @Nullable TransportStats transport, @Nullable HttpStats http,
8986
@Nullable FieldDataBreakerStats breaker) {
9087
super(node);
9188
this.timestamp = timestamp;
92-
this.hostname = hostname;
9389
this.indices = indices;
9490
this.os = os;
9591
this.process = process;
@@ -108,7 +104,7 @@ public long getTimestamp() {
108104

109105
@Nullable
110106
public String getHostname() {
111-
return this.hostname;
107+
return getNode().getHostName();
112108
}
113109

114110
/**
@@ -192,9 +188,6 @@ public static NodeStats readNodeStats(StreamInput in) throws IOException {
192188
public void readFrom(StreamInput in) throws IOException {
193189
super.readFrom(in);
194190
timestamp = in.readVLong();
195-
if (in.readBoolean()) {
196-
hostname = in.readString();
197-
}
198191
if (in.readBoolean()) {
199192
indices = NodeIndicesStats.readIndicesStats(in);
200193
}
@@ -229,12 +222,6 @@ public void readFrom(StreamInput in) throws IOException {
229222
public void writeTo(StreamOutput out) throws IOException {
230223
super.writeTo(out);
231224
out.writeVLong(timestamp);
232-
if (hostname == null) {
233-
out.writeBoolean(false);
234-
} else {
235-
out.writeBoolean(true);
236-
out.writeString(hostname);
237-
}
238225
if (indices == null) {
239226
out.writeBoolean(false);
240227
} else {
@@ -297,10 +284,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
297284
if (!params.param("node_info_format", "default").equals("none")) {
298285
builder.field("name", getNode().name(), XContentBuilder.FieldCaseConversion.NONE);
299286
builder.field("transport_address", getNode().address().toString(), XContentBuilder.FieldCaseConversion.NONE);
300-
301-
if (getHostname() != null) {
302-
builder.field("hostname", getHostname(), XContentBuilder.FieldCaseConversion.NONE);
303-
}
287+
builder.field("host", getNode().getHostName(), XContentBuilder.FieldCaseConversion.NONE);
288+
builder.field("ip", getNode().getAddress(), XContentBuilder.FieldCaseConversion.NONE);
304289

305290
if (!getNode().attributes().isEmpty()) {
306291
builder.startObject("attributes");

src/main/java/org/elasticsearch/client/transport/TransportClientNodesService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public NodesInfoResponse newInstance() {
374374
} else if (nodeInfo.getNodes().length != 0) {
375375
// use discovered information but do keep the original transport address, so people can control which address is exactly used.
376376
DiscoveryNode nodeWithInfo = nodeInfo.getNodes()[0].getNode();
377-
newNodes.add(new DiscoveryNode(nodeWithInfo.name(), nodeWithInfo.id(), listedNode.address(), nodeWithInfo.attributes(), nodeWithInfo.version()));
377+
newNodes.add(new DiscoveryNode(nodeWithInfo.name(), nodeWithInfo.id(), nodeWithInfo.getHostName(), nodeWithInfo.getHostAddress(), listedNode.address(), nodeWithInfo.attributes(), nodeWithInfo.version()));
378378
} else {
379379
// although we asked for one node, our target may not have completed initialization yet and doesn't have cluster nodes
380380
logger.debug("node {} didn't return any discovery info, temporarily using transport discovery node", listedNode);

src/main/java/org/elasticsearch/cluster/node/DiscoveryNode.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import com.google.common.collect.ImmutableMap;
2424
import org.elasticsearch.ElasticsearchIllegalArgumentException;
2525
import org.elasticsearch.Version;
26+
import org.elasticsearch.common.Strings;
2627
import org.elasticsearch.common.io.stream.StreamInput;
2728
import org.elasticsearch.common.io.stream.StreamOutput;
2829
import org.elasticsearch.common.io.stream.Streamable;
30+
import org.elasticsearch.common.network.NetworkUtils;
2931
import org.elasticsearch.common.settings.Settings;
3032
import org.elasticsearch.common.transport.TransportAddress;
3133
import org.elasticsearch.common.transport.TransportAddressSerializers;
@@ -87,6 +89,8 @@ public static boolean dataNode(Settings settings) {
8789

8890
private String nodeName = "";
8991
private String nodeId;
92+
private String hostName;
93+
private String hostAddress;
9094
private TransportAddress address;
9195
private ImmutableMap<String, String> attributes;
9296
private Version version = Version.CURRENT;
@@ -99,6 +103,10 @@ public DiscoveryNode(String nodeId, TransportAddress address, Version version) {
99103
}
100104

101105
public DiscoveryNode(String nodeName, String nodeId, TransportAddress address, Map<String, String> attributes, Version version) {
106+
this(nodeName, nodeId, NetworkUtils.getLocalHostName(""), NetworkUtils.getLocalHostAddress(""), address, attributes, version);
107+
}
108+
109+
public DiscoveryNode(String nodeName, String nodeId, String hostName, String hostAddress, TransportAddress address, Map<String, String> attributes, Version version) {
102110
if (nodeName != null) {
103111
this.nodeName = nodeName.intern();
104112
}
@@ -108,6 +116,8 @@ public DiscoveryNode(String nodeName, String nodeId, TransportAddress address, M
108116
}
109117
this.attributes = builder.build();
110118
this.nodeId = nodeId.intern();
119+
this.hostName = hostName.intern();
120+
this.hostAddress = hostAddress.intern();
111121
this.address = address;
112122
this.version = version;
113123
}
@@ -230,6 +240,14 @@ public Version version() {
230240
return this.version;
231241
}
232242

243+
public String getHostName() {
244+
return this.hostName;
245+
}
246+
247+
public String getHostAddress() {
248+
return this.hostAddress;
249+
}
250+
233251
public Version getVersion() {
234252
return this.version;
235253
}
@@ -244,6 +262,8 @@ public static DiscoveryNode readNode(StreamInput in) throws IOException {
244262
public void readFrom(StreamInput in) throws IOException {
245263
nodeName = in.readString().intern();
246264
nodeId = in.readString().intern();
265+
hostName = in.readString().intern();
266+
hostAddress = in.readString().intern();
247267
address = TransportAddressSerializers.addressFromStream(in);
248268
int size = in.readVInt();
249269
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
@@ -258,6 +278,8 @@ public void readFrom(StreamInput in) throws IOException {
258278
public void writeTo(StreamOutput out) throws IOException {
259279
out.writeString(nodeName);
260280
out.writeString(nodeId);
281+
out.writeString(hostName);
282+
out.writeString(hostAddress);
261283
addressToStream(out, address);
262284
out.writeVInt(attributes.size());
263285
for (Map.Entry<String, String> entry : attributes.entrySet()) {
@@ -290,6 +312,9 @@ public String toString() {
290312
if (nodeId != null) {
291313
sb.append('[').append(nodeId).append(']');
292314
}
315+
if (Strings.hasLength(hostName)) {
316+
sb.append('[').append(hostName).append(']');
317+
}
293318
if (address != null) {
294319
sb.append('[').append(address).append(']');
295320
}

src/main/java/org/elasticsearch/cluster/node/DiscoveryNodeFilters.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.elasticsearch.common.Strings;
2323
import org.elasticsearch.common.regex.Regex;
2424
import org.elasticsearch.common.settings.Settings;
25-
import org.elasticsearch.common.transport.InetSocketTransportAddress;
2625

2726
import java.util.HashMap;
2827
import java.util.Map;
@@ -34,7 +33,9 @@ public class DiscoveryNodeFilters {
3433
public static enum OpType {
3534
AND,
3635
OR
37-
};
36+
}
37+
38+
;
3839

3940
public static DiscoveryNodeFilters buildFromSettings(OpType opType, String prefix, Settings settings) {
4041
return buildFromKeyValue(opType, settings.getByPrefix(prefix).getAsMap());
@@ -68,16 +69,8 @@ public boolean match(DiscoveryNode node) {
6869
String attr = entry.getKey();
6970
String[] values = entry.getValue();
7071
if ("_ip".equals(attr)) {
71-
if (!(node.address() instanceof InetSocketTransportAddress)) {
72-
if (opType == OpType.AND) {
73-
return false;
74-
} else {
75-
continue;
76-
}
77-
}
78-
InetSocketTransportAddress inetAddress = (InetSocketTransportAddress) node.address();
7972
for (String value : values) {
80-
if (Regex.simpleMatch(value, inetAddress.address().getAddress().getHostAddress())) {
73+
if (Regex.simpleMatch(value, node.getHostAddress())) {
8174
if (opType == OpType.OR) {
8275
return true;
8376
}
@@ -88,16 +81,8 @@ public boolean match(DiscoveryNode node) {
8881
}
8982
}
9083
} else if ("_host".equals(attr)) {
91-
if (!(node.address() instanceof InetSocketTransportAddress)) {
92-
if (opType == OpType.AND) {
93-
return false;
94-
} else {
95-
continue;
96-
}
97-
}
98-
InetSocketTransportAddress inetAddress = (InetSocketTransportAddress) node.address();
9984
for (String value : values) {
100-
if (Regex.simpleMatch(value, inetAddress.address().getHostName())) {
85+
if (Regex.simpleMatch(value, node.getHostName())) {
10186
if (opType == OpType.OR) {
10287
return true;
10388
}
@@ -106,7 +91,7 @@ public boolean match(DiscoveryNode node) {
10691
return false;
10792
}
10893
}
109-
if (Regex.simpleMatch(value, inetAddress.address().getAddress().getHostAddress())) {
94+
if (Regex.simpleMatch(value, node.getHostAddress())) {
11095
if (opType == OpType.OR) {
11196
return true;
11297
}

src/main/java/org/elasticsearch/cluster/node/DiscoveryNodes.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,7 @@ public boolean isAllNodes(String... nodesIds) {
287287
*
288288
* @param node id of the node to discover
289289
* @return discovered node matching the given id
290-
* @throws org.elasticsearch.ElasticsearchIllegalArgumentException
291-
* if more than one node matches the request or no nodes have been resolved
290+
* @throws org.elasticsearch.ElasticsearchIllegalArgumentException if more than one node matches the request or no nodes have been resolved
292291
*/
293292
public DiscoveryNode resolveNode(String node) {
294293
String[] resolvedNodeIds = resolveNodesIds(node);
@@ -332,7 +331,9 @@ public String[] resolveNodesIds(String... nodesIds) {
332331
}
333332
}
334333
for (DiscoveryNode node : this) {
335-
if (node.address().match(nodeId)) {
334+
if (Regex.simpleMatch(nodeId, node.getHostAddress())) {
335+
resolvedNodesIds.add(node.id());
336+
} else if (Regex.simpleMatch(nodeId, node.getHostName())) {
336337
resolvedNodesIds.add(node.id());
337338
}
338339
}

src/main/java/org/elasticsearch/cluster/routing/allocation/decider/SameShardAllocationDecider.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.cluster.routing.RoutingNode;
2424
import org.elasticsearch.cluster.routing.ShardRouting;
2525
import org.elasticsearch.cluster.routing.allocation.RoutingAllocation;
26+
import org.elasticsearch.common.Strings;
2627
import org.elasticsearch.common.inject.Inject;
2728
import org.elasticsearch.common.settings.Settings;
2829

@@ -65,12 +66,21 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
6566
continue;
6667
}
6768
// check if its on the same host as the one we want to allocate to
68-
if (!checkNode.node().address().sameHost(node.node().address())) {
69-
continue;
69+
boolean checkNodeOnSameHost = false;
70+
if (Strings.hasLength(checkNode.node().getHostAddress()) && Strings.hasLength(node.node().getHostAddress())) {
71+
if (checkNode.node().getHostAddress().equals(node.node().getHostAddress())) {
72+
checkNodeOnSameHost = true;
73+
}
74+
} else if (Strings.hasLength(checkNode.node().getHostName()) && Strings.hasLength(node.node().getHostName())) {
75+
if (checkNode.node().getHostName().equals(node.node().getHostName())) {
76+
checkNodeOnSameHost = true;
77+
}
7078
}
71-
for (MutableShardRouting assignedShard : assignedShards) {
72-
if (checkNode.nodeId().equals(assignedShard.currentNodeId())) {
73-
return Decision.NO;
79+
if (checkNodeOnSameHost) {
80+
for (MutableShardRouting assignedShard : assignedShards) {
81+
if (checkNode.nodeId().equals(assignedShard.currentNodeId())) {
82+
return Decision.NO;
83+
}
7484
}
7585
}
7686
}

src/main/java/org/elasticsearch/common/network/NetworkUtils.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,28 @@ public static InetAddress getLocalAddress() {
7878
return localAddress;
7979
}
8080

81+
public static String getLocalHostName(String defaultHostName) {
82+
if (localAddress == null) {
83+
return defaultHostName;
84+
}
85+
String hostName = localAddress.getHostName();
86+
if (hostName == null) {
87+
return defaultHostName;
88+
}
89+
return hostName;
90+
}
91+
92+
public static String getLocalHostAddress(String defaultHostAddress) {
93+
if (localAddress == null) {
94+
return defaultHostAddress;
95+
}
96+
String hostAddress = localAddress.getHostAddress();
97+
if (hostAddress == null) {
98+
return defaultHostAddress;
99+
}
100+
return hostAddress;
101+
}
102+
81103
public static InetAddress getLocalhost(StackType ip_version) throws UnknownHostException {
82104
if (ip_version == StackType.IPv4)
83105
return InetAddress.getByName("127.0.0.1");
@@ -216,7 +238,7 @@ public static boolean interfaceHasIPAddresses(NetworkInterface intf, StackType i
216238
* system properties (java.net.preferIPv4Stack and java.net.preferIPv6Addresses)
217239
*
218240
* @return StackType.IPv4 for an IPv4 only stack, StackYTypeIPv6 for an IPv6 only stack, and StackType.Unknown
219-
* if the type cannot be detected
241+
* if the type cannot be detected
220242
*/
221243
public static StackType getIpStackType() {
222244
boolean isIPv4StackAvailable = isStackAvailable(true);

0 commit comments

Comments
 (0)