diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-10-21 11:36:52 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-10-21 16:47:41 +0200 |
commit | 9388dfa0d2826d4b7ddc905a46ef514a5136aa1c (patch) | |
tree | a66eb03ed83d4f349b8748606bb8b2147d094824 | |
parent | dd15f598e92b44189952cd6a1f5ad02f937c862d (diff) | |
download | sonarqube-9388dfa0d2826d4b7ddc905a46ef514a5136aa1c.tar.gz sonarqube-9388dfa0d2826d4b7ddc905a46ef514a5136aa1c.zip |
SONAR-8176 do not fail when ES is not available in api/system/info
-rw-r--r-- | server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/EsMonitor.java | 21 | ||||
-rw-r--r-- | server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/EsMonitorTest.java | 39 |
2 files changed, 53 insertions, 7 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/EsMonitor.java b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/EsMonitor.java index 69f757c0f23..11bebb80cca 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/EsMonitor.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/EsMonitor.java @@ -21,6 +21,7 @@ package org.sonar.server.platform.monitoring; import java.util.LinkedHashMap; import java.util.Map; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.admin.cluster.node.stats.NodeStats; import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse; import org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse; @@ -29,6 +30,7 @@ import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.common.breaker.CircuitBreaker; +import org.sonar.api.utils.log.Loggers; import org.sonar.server.es.EsClient; import static org.apache.commons.io.FileUtils.byteCountToDisplaySize; @@ -66,12 +68,19 @@ public class EsMonitor extends BaseMonitorMBean implements EsMonitorMBean { @Override public Map<String, Object> attributes() { - Map<String, Object> attributes = new LinkedHashMap<>(); - attributes.put("State", getStateAsEnum()); - attributes.put("Indices", indexAttributes()); - attributes.put("Number of Nodes", getNumberOfNodes()); - attributes.put("Nodes", nodeAttributes()); - return attributes; + try { + Map<String, Object> attributes = new LinkedHashMap<>(); + attributes.put("State", getStateAsEnum()); + attributes.put("Indices", indexAttributes()); + attributes.put("Number of Nodes", getNumberOfNodes()); + attributes.put("Nodes", nodeAttributes()); + return attributes; + } catch (Exception es) { + Loggers.get(EsMonitor.class).warn("Failed to retrieve ES attributes. There will be only a single \"state\" attribute.", es); + Map<String, Object> attributes = new LinkedHashMap<>(); + attributes.put("State", (es.getCause() instanceof ElasticsearchException ? es.getCause().getMessage() : es.getMessage())); + return attributes; + } } private LinkedHashMap<String, LinkedHashMap<String, Object>> indexAttributes() { diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/EsMonitorTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/EsMonitorTest.java index 5ccbbd00015..830bad000d8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/EsMonitorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/EsMonitorTest.java @@ -20,21 +20,25 @@ package org.sonar.server.platform.monitoring; import java.util.Map; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.junit.Rule; import org.junit.Test; import org.sonar.api.config.MapSettings; +import org.sonar.server.es.EsClient; import org.sonar.server.es.EsTester; import org.sonar.server.issue.index.IssueIndexDefinition; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class EsMonitorTest { @Rule public EsTester esTester = new EsTester(new IssueIndexDefinition(new MapSettings())); - EsMonitor underTest = new EsMonitor(esTester.client()); + private EsMonitor underTest = new EsMonitor(esTester.client()); @Test public void name() { @@ -73,4 +77,37 @@ public class EsMonitorTest { assertThat((int) indexAttributes.get("Shards")).isGreaterThan(0); assertThat(indexAttributes.get("Store Size")).isNotNull(); } + + @Test + public void attributes_displays_exception_message_when_cause_null_when_client_fails() { + EsClient esClientMock = mock(EsClient.class); + EsMonitor underTest = new EsMonitor(esClientMock); + when(esClientMock.prepareClusterStats()).thenThrow(new RuntimeException("RuntimeException with no cause")); + + Map<String, Object> attributes = underTest.attributes(); + assertThat(attributes).hasSize(1); + assertThat(attributes.get("State")).isEqualTo("RuntimeException with no cause"); + } + + @Test + public void attributes_displays_exception_message_when_cause_is_not_ElasticSearchException_when_client_fails() { + EsClient esClientMock = mock(EsClient.class); + EsMonitor underTest = new EsMonitor(esClientMock); + when(esClientMock.prepareClusterStats()).thenThrow(new RuntimeException("RuntimeException with cause not ES", new IllegalArgumentException("some cause message"))); + + Map<String, Object> attributes = underTest.attributes(); + assertThat(attributes).hasSize(1); + assertThat(attributes.get("State")).isEqualTo("RuntimeException with cause not ES"); + } + + @Test + public void attributes_displays_cause_message_when_cause_is_ElasticSearchException_when_client_fails() { + EsClient esClientMock = mock(EsClient.class); + EsMonitor underTest = new EsMonitor(esClientMock); + when(esClientMock.prepareClusterStats()).thenThrow(new RuntimeException("RuntimeException with ES cause", new ElasticsearchException("some cause message"))); + + Map<String, Object> attributes = underTest.attributes(); + assertThat(attributes).hasSize(1); + assertThat(attributes.get("State")).isEqualTo("some cause message"); + } } |