]> source.dussan.org Git - sonarqube.git/commitdiff
Fix various code smells
authorJacek <jacek.poreda@sonarsource.com>
Fri, 4 Dec 2020 11:10:10 +0000 (12:10 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 9 Dec 2020 20:07:21 +0000 (20:07 +0000)
server/sonar-main/src/main/java/org/sonar/application/config/ClusterSettings.java
server/sonar-main/src/main/java/org/sonar/application/es/EsInstallation.java
server/sonar-main/src/main/java/org/sonar/application/es/EsSettings.java
server/sonar-main/src/test/java/org/sonar/application/config/ClusterSettingsTest.java
server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexer.java
server/sonar-server-common/src/main/java/org/sonar/server/issue/index/IssueIndexer.java
server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueQueryFactory.java

index 504f6885483dae74760fbe9ccee0fcbf80a4f414..cfa65c7824eea5e4be8aa2c98f2e1e4e8977f825 100644 (file)
@@ -83,26 +83,34 @@ public class ClusterSettings implements Consumer<Props> {
     NodeType nodeType = toNodeType(props);
     switch (nodeType) {
       case APPLICATION:
-        ensureNotH2(props);
-        requireValue(props, AUTH_JWT_SECRET);
-        Set<AddressAndPort> hzNodes = parseHosts(CLUSTER_HZ_HOSTS, requireValue(props, CLUSTER_HZ_HOSTS));
-        ensureNotLoopbackAddresses(CLUSTER_HZ_HOSTS, hzNodes);
-        checkClusterNodeHost(props);
-        checkClusterSearchHosts(props);
+        checkForApplicationNode(props);
         break;
       case SEARCH:
-        ensureNoSearchNodeForbiddenSettings(props);
-        AddressAndPort searchHost = parseAndCheckHost(CLUSTER_NODE_SEARCH_HOST, requireValue(props, CLUSTER_NODE_SEARCH_HOST));
-        ensureLocalButNotLoopbackAddress(CLUSTER_NODE_SEARCH_HOST, searchHost);
-        AddressAndPort esHost = parseAndCheckHost(CLUSTER_NODE_ES_HOST, requireValue(props, CLUSTER_NODE_ES_HOST));
-        ensureLocalButNotLoopbackAddress(CLUSTER_NODE_ES_HOST, esHost);
-        checkClusterEsHosts(props);
+        checkForSearchNode(props);
         break;
       default:
         throw new UnsupportedOperationException("Unknown value: " + nodeType);
     }
   }
 
+  private void checkForApplicationNode(Props props) {
+    ensureNotH2(props);
+    requireValue(props, AUTH_JWT_SECRET);
+    Set<AddressAndPort> hzNodes = parseHosts(CLUSTER_HZ_HOSTS, requireValue(props, CLUSTER_HZ_HOSTS));
+    ensureNotLoopbackAddresses(CLUSTER_HZ_HOSTS, hzNodes);
+    checkClusterNodeHost(props);
+    checkClusterSearchHosts(props);
+  }
+
+  private void checkForSearchNode(Props props) {
+    ensureNoSearchNodeForbiddenSettings(props);
+    AddressAndPort searchHost = parseAndCheckHost(CLUSTER_NODE_SEARCH_HOST, requireValue(props, CLUSTER_NODE_SEARCH_HOST));
+    ensureLocalButNotLoopbackAddress(CLUSTER_NODE_SEARCH_HOST, searchHost);
+    AddressAndPort esHost = parseAndCheckHost(CLUSTER_NODE_ES_HOST, requireValue(props, CLUSTER_NODE_ES_HOST));
+    ensureLocalButNotLoopbackAddress(CLUSTER_NODE_ES_HOST, esHost);
+    checkClusterEsHosts(props);
+  }
+
   private void checkClusterNodeHost(Props props) {
     AddressAndPort clusterNodeHost = parseAndCheckHost(CLUSTER_NODE_HOST, requireValue(props, CLUSTER_NODE_HOST));
     ensureLocalButNotLoopbackAddress(CLUSTER_NODE_HOST, clusterNodeHost);
index 5657f094adbe480f658a029c777deea9976c1f94..c36d2af8b3b2b7f9f6c99836991a6f7056dc71cd 100644 (file)
@@ -50,7 +50,6 @@ public class EsInstallation {
   private EsJvmOptions esJvmOptions;
   private EsYmlSettings esYmlSettings;
   private Properties log4j2Properties;
-  private String clusterName;
   private String host;
   private int httpPort;
 
index dc3b606d1c4fe0d9e7bb3c2fbf9ec849dfdfaa41..cefea6fe707b57dce9ba42610e2e38d4ba43903e 100644 (file)
@@ -58,6 +58,8 @@ public class EsSettings {
   private static final String SECCOMP_PROPERTY = "bootstrap.system_call_filter";
   private static final String ALLOW_MMAP = "node.store.allow_mmap";
 
+  private static final String JAVA_ADDITIONAL_OPS_PROPERTY = "sonar.search.javaAdditionalOpts";
+
   private final Props props;
   private final EsInstallation fileSystem;
 
@@ -114,7 +116,7 @@ public class EsSettings {
 
       int transportPort = Integer.parseInt(props.nonNullValue(ES_PORT.getKey()));
 
-      //we have no use of transport port in non-DCE editions
+      // we have no use of transport port in non-DCE editions
       builder.put(ES_TRANSPORT_HOST_KEY, this.loopbackAddress.getHostAddress());
       builder.put(ES_TRANSPORT_PORT_KEY, valueOf(transportPort));
     }
@@ -185,15 +187,15 @@ public class EsSettings {
 
   private void configureOthers(Map<String, String> builder) {
     builder.put("action.auto_create_index", String.valueOf(false));
-    if (props.value("sonar.search.javaAdditionalOpts", "").contains("-D" + SECCOMP_PROPERTY + "=false")) {
+    if (props.value(JAVA_ADDITIONAL_OPS_PROPERTY, "").contains("-D" + SECCOMP_PROPERTY + "=false")) {
       builder.put(SECCOMP_PROPERTY, "false");
     }
 
-    if (props.value("sonar.search.javaAdditionalOpts", "").contains("-Dnode.store.allow_mmapfs=false")) {
+    if (props.value(JAVA_ADDITIONAL_OPS_PROPERTY, "").contains("-Dnode.store.allow_mmapfs=false")) {
       throw new MessageException("Property 'node.store.allow_mmapfs' is no longer supported. Use 'node.store.allow_mmap' instead.");
     }
 
-    if (props.value("sonar.search.javaAdditionalOpts", "").contains("-D" + ALLOW_MMAP + "=false")) {
+    if (props.value(JAVA_ADDITIONAL_OPS_PROPERTY, "").contains("-D" + ALLOW_MMAP + "=false")) {
       builder.put(ALLOW_MMAP, "false");
     }
   }
index fa8fae1a190fbe51d87318a156fb39f07f71dc73..f91f674a4b9bc6724997cf725cf6aa2c6eed08c6 100644 (file)
@@ -96,6 +96,19 @@ public class ClusterSettingsTest {
     assertThat(ClusterSettings.getEnabledProcesses(settings)).containsOnly(ELASTICSEARCH);
   }
 
+  @Test
+  @UseDataProvider("validIPv4andIPv6Addresses")
+  public void valid_configuration_of_app_node_does_not_throw_exception(String host) {
+    mockValidHost(host);
+    mockLocalNonLoopback(host);
+    TestAppSettings settings = newSettingsForAppNode(host);
+    ClusterSettings clusterSettings = new ClusterSettings(network);
+    Props props = settings.getProps();
+
+    assertThatCode(() -> clusterSettings.accept(props))
+      .doesNotThrowAnyException();
+  }
+
   @Test
   public void accept_throws_MessageException_if_no_node_type_is_configured() {
     TestAppSettings settings = new TestAppSettings(of(CLUSTER_ENABLED.getKey(), "true"));
index 4a0334b9a65fa9644d365e7f97d4f33c0f1c618d..2bd8b93c6987ffda05e5a6e922f659b5c05d968d 100644 (file)
@@ -156,7 +156,7 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe
     bulk.stop();
   }
 
-  private void addProjectDeletionToBulkIndexer(BulkIndexer bulkIndexer, String projectUuid) {
+  private static void addProjectDeletionToBulkIndexer(BulkIndexer bulkIndexer, String projectUuid) {
     SearchRequest searchRequest = EsClient.prepareSearch(TYPE_COMPONENT.getMainType())
       .source(new SearchSourceBuilder().query(QueryBuilders.termQuery(ComponentIndexDefinition.FIELD_PROJECT_UUID, projectUuid)))
       .routing(AuthorizationDoc.idOf(projectUuid));
index 6f63c457cbdbdf30da1b4fef661391e4e54128af..cdd09f7a9f37c8f24efd08906c0416dc8c8d0dfd 100644 (file)
@@ -261,14 +261,14 @@ public class IssueIndexer implements ProjectIndexer, NeedAuthorizationIndexer {
     bulk.stop();
   }
 
-  private IndexRequest newIndexRequest(IssueDoc issue) {
+  private static IndexRequest newIndexRequest(IssueDoc issue) {
     return new IndexRequest(TYPE_ISSUE.getMainType().getIndex().getName(), TYPE_ISSUE.getMainType().getType())
       .id(issue.getId())
       .routing(issue.getRouting().orElseThrow(() -> new IllegalStateException("IssueDoc should define a routing")))
       .source(issue.getFields());
   }
 
-  private void addProjectDeletionToBulkIndexer(BulkIndexer bulkIndexer, String projectUuid) {
+  private static void addProjectDeletionToBulkIndexer(BulkIndexer bulkIndexer, String projectUuid) {
     SearchRequest search = EsClient.prepareSearch(TYPE_ISSUE.getMainType())
       .routing(AuthorizationDoc.idOf(projectUuid))
       .source(new SearchSourceBuilder().query(boolQuery().must(termQuery(FIELD_ISSUE_PROJECT_UUID, projectUuid))));
index 65b79e2c50937636b8a0bed06cf83668207c2917..b6011d6fc47f6de092113b4e1aae77616a0d8210 100644 (file)
@@ -148,7 +148,7 @@ public class IssueQueryFactory {
     }
   }
 
-  private Optional<ZoneId> parseTimeZone(@Nullable String timeZone) {
+  private static Optional<ZoneId> parseTimeZone(@Nullable String timeZone) {
     if (timeZone == null) {
       return Optional.empty();
     }