]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17331 Log message when Elasticsearch index requires re-indexing
authorJacek <jacek.poreda@sonarsource.com>
Fri, 21 Oct 2022 13:08:31 +0000 (15:08 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 24 Oct 2022 20:04:04 +0000 (20:04 +0000)
sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/DefaultActiveRulesLoader.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/DefaultActiveRulesLoaderTest.java

index ea1a0f6abfb07f53f50f68c5d444ba70e9eadba9..7bd76d47e03116c1c2d8e5577564f5c9b749782b 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.api.batch.rule.LoadedActiveRule;
 import org.sonar.api.impl.utils.ScannerUtils;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.utils.DateUtils;
+import org.sonar.api.utils.MessageException;
 import org.sonar.scanner.bootstrap.ScannerWsClient;
 import org.sonarqube.ws.Rules;
 import org.sonarqube.ws.Rules.Active;
@@ -96,10 +97,14 @@ public class DefaultActiveRulesLoader implements ActiveRulesLoader {
     List<LoadedActiveRule> loadedRules = new LinkedList<>();
 
     List<Rule> rulesList = response.getRulesList();
-    Map<String, ActiveList> actives = response.getActives().getActives();
+    Map<String, ActiveList> actives = response.getActives().getActivesMap();
 
     for (Rule r : rulesList) {
       ActiveList activeList = actives.get(r.getKey());
+      if (activeList == null) {
+        throw MessageException.of("Elasticsearch indices have become inconsistent. Consider re-indexing. " +
+          "Check documentation for more information https://docs.sonarqube.org/latest/setup/troubleshooting");
+      }
       Active active = activeList.getActiveList(0);
 
       LoadedActiveRule loadedRule = new LoadedActiveRule();
index 625fa4e4c5c07a5b9080bda2d54d611644761968..9902b2ed2603e43f923e2e45abb6beefd7c844ce 100644 (file)
@@ -29,6 +29,7 @@ import org.junit.Test;
 import org.sonar.api.batch.rule.LoadedActiveRule;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.Severity;
+import org.sonar.api.utils.MessageException;
 import org.sonar.scanner.WsTestUtil;
 import org.sonar.scanner.bootstrap.DefaultScannerWsClient;
 import org.sonar.scanner.scan.branch.BranchConfiguration;
@@ -41,6 +42,7 @@ import org.sonarqube.ws.Rules.SearchResponse;
 import org.sonarqube.ws.Rules.SearchResponse.Builder;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
@@ -90,6 +92,15 @@ public class DefaultActiveRulesLoaderTest {
     verifyNoMoreInteractions(wsClient);
   }
 
+  @Test
+  public void exception_thrown_when_elasticsearch_index_inconsistent() {
+    WsTestUtil.mockStream(wsClient, urlOfPage(1), prepareCorruptedResponse());
+    assertThatThrownBy(() -> loader.load("c+-test_c+-values-17445"))
+      .isInstanceOf(MessageException.class)
+      .hasMessage("Elasticsearch indices have become inconsistent. Consider re-indexing. " +
+        "Check documentation for more information https://docs.sonarqube.org/latest/setup/troubleshooting");
+  }
+
   private String urlOfPage(int page) {
     return "/api/rules/search.protobuf?f=repo,name,severity,lang,internalKey,templateKey,params,actives,createdAt,updatedAt,deprecatedKeys&activation=true"
       + ("") + "&qprofile=c%2B-test_c%2B-values-17445&ps=500&p=" + page + "";
@@ -130,4 +141,23 @@ public class DefaultActiveRulesLoaderTest {
     rules.setTotal(total);
     return new ByteArrayInputStream(rules.build().toByteArray());
   }
+
+  private InputStream prepareCorruptedResponse() {
+    Builder rules = SearchResponse.newBuilder();
+    Actives.Builder actives = Actives.newBuilder();
+
+    IntStream.rangeClosed(1, 3)
+      .mapToObj(i -> RuleKey.of("java", "S" + i))
+      .forEach(key -> {
+
+        Rule.Builder ruleBuilder = Rule.newBuilder();
+        ruleBuilder.setKey(key.toString());
+        rules.addRules(ruleBuilder);
+      });
+
+    rules.setActives(actives);
+    rules.setPs(3);
+    rules.setTotal(3);
+    return new ByteArrayInputStream(rules.build().toByteArray());
+  }
 }