summaryrefslogtreecommitdiffstats
path: root/server/sonar-server
diff options
context:
space:
mode:
authorStephane Gamard <stephane.gamard@searchbox.com>2014-08-06 15:34:00 +0200
committerStephane Gamard <stephane.gamard@searchbox.com>2014-08-06 15:34:00 +0200
commitc80eba6533f33ac632eaf2b08d50929918cdf954 (patch)
tree971aa3a20fc313add009d956f69586f9c9b1afd4 /server/sonar-server
parent36bf1c9ec419315e478b247baea9bc1468f305b9 (diff)
downloadsonarqube-c80eba6533f33ac632eaf2b08d50929918cdf954.tar.gz
sonarqube-c80eba6533f33ac632eaf2b08d50929918cdf954.zip
SONAR-4907 - Added getLocalNodeInfo which gets the node on the same machine (not same JVM)
Diffstat (limited to 'server/sonar-server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/search/ESNode.java33
1 files changed, 33 insertions, 0 deletions
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);