From: Stephane Gamard Date: Wed, 6 Aug 2014 13:34:00 +0000 (+0200) Subject: SONAR-4907 - Added getLocalNodeInfo which gets the node on the same machine (not... X-Git-Tag: 4.5-RC1~231 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c80eba6533f33ac632eaf2b08d50929918cdf954;p=sonarqube.git SONAR-4907 - Added getLocalNodeInfo which gets the node on the same machine (not same JVM) --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/ESNode.java b/server/sonar-server/src/main/java/org/sonar/server/search/ESNode.java index de13aae4be2..05e6da16a74 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/ESNode.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/ESNode.java @@ -26,6 +26,8 @@ import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ListenableActionFuture; import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus; +import org.elasticsearch.action.admin.cluster.node.info.NodeInfo; +import org.elasticsearch.action.admin.cluster.stats.ClusterStatsNodeResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.logging.ESLoggerFactory; @@ -47,7 +49,9 @@ import org.sonar.core.profiling.StopWatch; import org.sonar.server.search.es.ListUpdate; import org.sonar.server.search.es.ListUpdate.UpdateListScriptFactory; +import javax.annotation.CheckForNull; import java.io.File; +import java.net.InetAddress; /** * ElasticSearch Node used to connect to index. @@ -131,6 +135,35 @@ public class ESNode implements Startable { LOG.info("Elasticsearch port: " + port); } + @CheckForNull + private NodeInfo getLocalNodeInfoByHostName(String hostname) { + for (ClusterStatsNodeResponse nodeResp : client().admin().cluster().prepareClusterStats().get().getNodes()) { + if (hostname.equals(nodeResp.nodeInfo().getHostname())) { + return nodeResp.nodeInfo(); + } + } + return null; + } + + @CheckForNull + private NodeInfo getLocalNodeInfoByNodeName(String nodeName) { + return client().admin().cluster().prepareClusterStats().get().getNodesMap().get(nodeName).nodeInfo(); + } + + @CheckForNull + private NodeInfo getLocalNodeInfo() { + if (settings.hasKey(IndexProperties.NODE_NAME)) { + return getLocalNodeInfoByNodeName(settings.getString(IndexProperties.NODE_NAME)); + } else { + try { + String LocalhostName = InetAddress.getLocalHost().getHostName(); + return getLocalNodeInfoByHostName(LocalhostName); + } catch (Exception exception) { + throw new IllegalStateException("Could not get localhost hostname", exception); + } + } + } + private void initLocalClient(IndexProperties.ES_TYPE type, ImmutableSettings.Builder esSettings) { if (IndexProperties.ES_TYPE.MEMORY.equals(type)) { initMemoryES(esSettings);