]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9507 make sonar.ce.workerCount an internal property
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 4 Jul 2017 15:36:56 +0000 (17:36 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 17 Jul 2017 08:52:47 +0000 (10:52 +0200)
server/sonar-ce/src/main/java/org/sonar/ce/configuration/CeConfigurationImpl.java
server/sonar-ce/src/test/java/org/sonar/ce/configuration/CeConfigurationImplTest.java
server/sonar-server/src/main/java/org/sonar/server/property/MapInternalProperties.java [new file with mode: 0644]
sonar-application/src/main/assembly/conf/sonar.properties

index be00dbc3d088584e988eb8e0c68a36a939e2c7c1..3489c453c1a9f89ead76cfffab98c1adf50c57d0 100644 (file)
 package org.sonar.ce.configuration;
 
 import org.picocontainer.Startable;
-import org.sonar.api.config.Configuration;
 import org.sonar.api.utils.MessageException;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
+import org.sonar.server.property.InternalProperties;
 
 import static java.lang.String.format;
 
@@ -47,13 +47,12 @@ public class CeConfigurationImpl implements CeConfiguration, Startable {
 
   private final int workerCount;
 
-  public CeConfigurationImpl(Configuration config) {
-    String workerCountAsStr = config.get(CE_WORKERS_COUNT_PROPERTY).orElse(null);
-    if (workerCountAsStr == null || workerCountAsStr.isEmpty()) {
-      this.workerCount = DEFAULT_WORKER_COUNT;
-    } else {
-      this.workerCount = parseStringValue(workerCountAsStr);
-    }
+  public CeConfigurationImpl(InternalProperties internalProperties) {
+    this.workerCount = internalProperties.read(CE_WORKERS_COUNT_PROPERTY)
+      .map(String::trim)
+      .filter(s -> !s.isEmpty())
+      .map(CeConfigurationImpl::parseStringValue)
+      .orElse(DEFAULT_WORKER_COUNT);
   }
 
   private static int parseStringValue(String workerCountAsStr) {
index 2f501716b2dcb73b300f96f6647b5702a64e43e7..1f80b0b8eaa39c312c8d76ca898e0f7ed0df165f 100644 (file)
@@ -23,8 +23,9 @@ import java.util.Random;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
-import org.sonar.api.config.internal.MapSettings;
 import org.sonar.api.utils.MessageException;
+import org.sonar.server.property.InternalProperties;
+import org.sonar.server.property.MapInternalProperties;
 
 import static java.lang.Math.abs;
 import static org.assertj.core.api.Assertions.assertThat;
@@ -35,89 +36,89 @@ public class CeConfigurationImplTest {
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
-  private MapSettings settings = new MapSettings();
+  private InternalProperties settings = new MapInternalProperties();
 
   @Test
   public void getWorkerCount_returns_1_when_worker_property_is_not_defined() {
-    assertThat(new CeConfigurationImpl(settings.asConfig()).getWorkerCount()).isEqualTo(1);
+    assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(1);
   }
 
   @Test
   public void getWorkerCount_returns_1_when_worker_property_is_empty() {
-    settings.setProperty(CE_WORKERS_COUNT_PROPERTY, "");
+    settings.write(CE_WORKERS_COUNT_PROPERTY, "");
 
-    assertThat(new CeConfigurationImpl(settings.asConfig()).getWorkerCount()).isEqualTo(1);
+    assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(1);
   }
 
   @Test
   public void getWorkerCount_returns_1_when_worker_property_is_space_chars() {
-    settings.setProperty(CE_WORKERS_COUNT_PROPERTY, "  \n  ");
+    settings.write(CE_WORKERS_COUNT_PROPERTY, "  \n  ");
 
-    assertThat(new CeConfigurationImpl(settings.asConfig()).getWorkerCount()).isEqualTo(1);
+    assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(1);
   }
 
   @Test
   public void getWorkerCount_returns_1_when_worker_property_is_1() {
-    settings.setProperty(CE_WORKERS_COUNT_PROPERTY, 1);
+    settings.write(CE_WORKERS_COUNT_PROPERTY, String.valueOf(1));
 
-    assertThat(new CeConfigurationImpl(settings.asConfig()).getWorkerCount()).isEqualTo(1);
+    assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(1);
   }
 
   @Test
   public void getWorkerCount_returns_value_when_worker_property_is_integer_greater_than_1() {
     int value = abs(new Random().nextInt()) + 2;
-    settings.setProperty(CE_WORKERS_COUNT_PROPERTY, value);
+    settings.write(CE_WORKERS_COUNT_PROPERTY, String.valueOf(value));
 
-    assertThat(new CeConfigurationImpl(settings.asConfig()).getWorkerCount()).isEqualTo(value);
+    assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(value);
   }
 
   @Test
   public void constructor_throws_MessageException_when_worker_property_is_0() {
     int value = 0;
-    settings.setProperty(CE_WORKERS_COUNT_PROPERTY, String.valueOf(value));
+    settings.write(CE_WORKERS_COUNT_PROPERTY, String.valueOf(value));
 
     expectMessageException(value);
 
-    new CeConfigurationImpl(settings.asConfig());
+    new CeConfigurationImpl(settings);
   }
 
   @Test
   public void constructor_throws_MessageException_when_worker_property_is_less_than_0() {
     int value = -1 * abs(new Random().nextInt());
-    settings.setProperty(CE_WORKERS_COUNT_PROPERTY, String.valueOf(value));
+    settings.write(CE_WORKERS_COUNT_PROPERTY, String.valueOf(value));
 
     expectMessageException(value);
 
-    new CeConfigurationImpl(settings.asConfig());
+    new CeConfigurationImpl(settings);
   }
 
   @Test
-  public void constructor_throws_MessageException_when_worker_property_is_not_an_double() {
+  public void constructor_throws_MessageException_when_worker_property_is_a_double() {
     double value = 3.5;
-    settings.setProperty(CE_WORKERS_COUNT_PROPERTY, String.valueOf(value));
+    settings.write(CE_WORKERS_COUNT_PROPERTY, String.valueOf(value));
 
     expectedException.expect(MessageException.class);
     expectedException.expectMessage("value '" + value + "' of property " + CE_WORKERS_COUNT_PROPERTY + " is invalid. " +
-      "It must an integer strictly greater than 0");
+        "It must an integer strictly greater than 0");
 
-    new CeConfigurationImpl(settings.asConfig());
+    new CeConfigurationImpl(settings);
   }
 
   private void expectMessageException(int value) {
     expectedException.expect(MessageException.class);
     expectedException.expectMessage("value '" + value + "' of property " + CE_WORKERS_COUNT_PROPERTY + " is invalid. " +
-      "It must an integer strictly greater than 0");
+        "It must an integer strictly greater than 0");
   }
 
   @Test
   public void getCleanCeTasksInitialDelay_returns_1() {
-    assertThat(new CeConfigurationImpl(settings.asConfig()).getCleanCeTasksInitialDelay())
-      .isEqualTo(1L);
+    assertThat(new CeConfigurationImpl(settings).getCleanCeTasksInitialDelay())
+        .isEqualTo(1L);
   }
 
   @Test
   public void getCleanCeTasksDelay_returns_10() {
-    assertThat(new CeConfigurationImpl(settings.asConfig()).getCleanCeTasksDelay())
-      .isEqualTo(10L);
+    assertThat(new CeConfigurationImpl(settings).getCleanCeTasksDelay())
+        .isEqualTo(10L);
   }
 }
diff --git a/server/sonar-server/src/main/java/org/sonar/server/property/MapInternalProperties.java b/server/sonar-server/src/main/java/org/sonar/server/property/MapInternalProperties.java
new file mode 100644 (file)
index 0000000..6f52593
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.property;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import javax.annotation.Nullable;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * Map based implementation of {@link InternalProperties} to be used for unit testing.
+ */
+public class MapInternalProperties implements InternalProperties {
+  private final Map<String, String> values = new HashMap<>(1);
+
+  @Override
+  public Optional<String> read(String propertyKey) {
+    checkPropertyKey(propertyKey);
+    return Optional.ofNullable(values.get(propertyKey));
+  }
+
+  @Override
+  public void write(String propertyKey, @Nullable String value) {
+    checkPropertyKey(propertyKey);
+    values.put(propertyKey, value);
+  }
+
+  private static void checkPropertyKey(@Nullable String propertyKey) {
+    checkArgument(propertyKey != null && !propertyKey.isEmpty(), "property key can't be null nor empty");
+  }
+}
index b9b8f53893a17510fd9cd613c3033d96251e0af3..4b167d014e7916be07fdc0425e5973ac89d71565 100644 (file)
 
 # Same as previous property, but allows to not repeat all other settings like -Xmx
 #sonar.ce.javaAdditionalOpts=
-# The number of workers in the Compute Engine. Value must be greater than zero.
-# By default the Compute Engine uses a single worker and therefore processes tasks one at a time.
-#    Recommendations:
-#
-#    Using N workers will require N times as much Heap memory (see property
-#    sonar.ce.javaOpts to tune heap) and produce N times as much IOs on disk, database and
-#    Elasticsearch. The number of workers must suit your environment.
-#sonar.ce.workerCount=1
 
 
 #--------------------------------------------------------------------------------------------------