]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8176 do not fail when ES is not available in api/system/info 1325/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 21 Oct 2016 09:36:52 +0000 (11:36 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 21 Oct 2016 14:47:41 +0000 (16:47 +0200)
server/sonar-server/src/main/java/org/sonar/server/platform/monitoring/EsMonitor.java
server/sonar-server/src/test/java/org/sonar/server/platform/monitoring/EsMonitorTest.java

index 69f757c0f231e4565736d0afba03bb50b818e15d..11bebb80ccadf05153ec1f715122becc5b8d8603 100644 (file)
@@ -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() {
index 5ccbbd0001543a850876e44d303b6a1cfe8e85ad..830bad000d88200bcbe009dac4324e48708917aa 100644 (file)
 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");
+  }
 }