]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9508 CE gets number of workers from WorkerCountProvider
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 5 Jul 2017 08:42:12 +0000 (10:42 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 17 Jul 2017 08:52:47 +0000 (10:52 +0200)
rather than from internal properties directly

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/ce/configuration/WorkerCountProvider.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/ce/configuration/package-info.java [new file with mode: 0644]

index 3489c453c1a9f89ead76cfffab98c1adf50c57d0..94d0abf9c556d58c8dab257b16e1bf18e7edf718 100644 (file)
@@ -23,18 +23,16 @@ import org.picocontainer.Startable;
 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;
 
 /**
- * Immutable implementation of {@link CeConfiguration} which takes value returned by
- * {@link CeConfiguration#getWorkerCount()} from property {@link CeConfigurationImpl#CE_WORKERS_COUNT_PROPERTY} and
- * always returns {@link #DEFAULT_QUEUE_POLLING_DELAY} when {@link CeConfiguration#getQueuePollingDelay()} is called.
+ * Immutable implementation of {@link CeConfiguration} which takes value returned by an implementation of
+ * {@link WorkerCountProvider}, if any is available, or use the {@link #DEFAULT_WORKER_COUNT default worker count}.
+ * In addition, it always returns {@link #DEFAULT_QUEUE_POLLING_DELAY} when
+ * {@link CeConfiguration#getQueuePollingDelay()} is called.
  */
 public class CeConfigurationImpl implements CeConfiguration, Startable {
-  private static final String CE_WORKERS_COUNT_PROPERTY = "sonar.ce.workerCount";
-
   private static final Logger LOG = Loggers.get(CeConfigurationImpl.class);
 
   private static final int DEFAULT_WORKER_COUNT = 1;
@@ -47,31 +45,22 @@ public class CeConfigurationImpl implements CeConfiguration, Startable {
 
   private final int workerCount;
 
-  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);
+  public CeConfigurationImpl() {
+    this.workerCount = DEFAULT_WORKER_COUNT;
   }
 
-  private static int parseStringValue(String workerCountAsStr) {
-    try {
-      int value = Integer.parseInt(workerCountAsStr);
-      if (value < 1) {
-        throw parsingError(workerCountAsStr);
-      }
-      return value;
-    } catch (NumberFormatException e) {
-      throw parsingError(workerCountAsStr);
+  public CeConfigurationImpl(WorkerCountProvider workerCountProvider) {
+    int value = workerCountProvider.get();
+    if (value < 1) {
+      throw parsingError(value);
     }
+    this.workerCount = value;
   }
 
-  private static MessageException parsingError(String workerCountAsStr) {
+  private static MessageException parsingError(int value) {
     return MessageException.of(format(
-      "value '%s' of property %s is invalid. It must an integer strictly greater than 0.",
-      workerCountAsStr,
-      CE_WORKERS_COUNT_PROPERTY));
+        "Worker count '%s' is invalid. It must an integer strictly greater than 0.",
+        value));
   }
 
   @Override
index 1f80b0b8eaa39c312c8d76ca898e0f7ed0df165f..654c09f68aa4c331c7ad7892d8a0f5a5eb819dfc 100644 (file)
@@ -24,101 +24,82 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 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;
 
 public class CeConfigurationImplTest {
-  private static final String CE_WORKERS_COUNT_PROPERTY = "sonar.ce.workerCount";
-
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
-  private InternalProperties settings = new MapInternalProperties();
-
-  @Test
-  public void getWorkerCount_returns_1_when_worker_property_is_not_defined() {
-    assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(1);
-  }
-
-  @Test
-  public void getWorkerCount_returns_1_when_worker_property_is_empty() {
-    settings.write(CE_WORKERS_COUNT_PROPERTY, "");
-
-    assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(1);
-  }
+  private SimpleWorkerCountProvider workerCountProvider = new SimpleWorkerCountProvider();
 
   @Test
-  public void getWorkerCount_returns_1_when_worker_property_is_space_chars() {
-    settings.write(CE_WORKERS_COUNT_PROPERTY, "  \n  ");
-
-    assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(1);
+  public void getWorkerCount_returns_1_when_there_is_no_WorkerCountProvider() {
+    assertThat(new CeConfigurationImpl().getWorkerCount()).isEqualTo(1);
   }
 
   @Test
-  public void getWorkerCount_returns_1_when_worker_property_is_1() {
-    settings.write(CE_WORKERS_COUNT_PROPERTY, String.valueOf(1));
+  public void getWorkerCount_returns_value_returned_by_WorkerCountProvider_when_available() {
+    int value = 1 + Math.abs(new Random().nextInt());
+    workerCountProvider.set(value);
 
-    assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(1);
+    assertThat(new CeConfigurationImpl(workerCountProvider).getWorkerCount()).isEqualTo(value);
   }
 
   @Test
-  public void getWorkerCount_returns_value_when_worker_property_is_integer_greater_than_1() {
-    int value = abs(new Random().nextInt()) + 2;
-    settings.write(CE_WORKERS_COUNT_PROPERTY, String.valueOf(value));
-
-    assertThat(new CeConfigurationImpl(settings).getWorkerCount()).isEqualTo(value);
-  }
+  public void constructor_throws_MessageException_when_WorkerCountProvider_returns_0() {
+    workerCountProvider.set(0);
 
-  @Test
-  public void constructor_throws_MessageException_when_worker_property_is_0() {
-    int value = 0;
-    settings.write(CE_WORKERS_COUNT_PROPERTY, String.valueOf(value));
-
-    expectMessageException(value);
+    expectMessageException(0);
 
-    new CeConfigurationImpl(settings);
+    new CeConfigurationImpl(workerCountProvider);
   }
 
   @Test
-  public void constructor_throws_MessageException_when_worker_property_is_less_than_0() {
-    int value = -1 * abs(new Random().nextInt());
-    settings.write(CE_WORKERS_COUNT_PROPERTY, String.valueOf(value));
+  public void constructor_throws_MessageException_when_WorkerCountProvider_returns_less_than_0() {
+    int value = -1 - abs(new Random().nextInt());
+    workerCountProvider.set(value);
 
     expectMessageException(value);
 
-    new CeConfigurationImpl(settings);
-  }
-
-  @Test
-  public void constructor_throws_MessageException_when_worker_property_is_a_double() {
-    double value = 3.5;
-    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");
-
-    new CeConfigurationImpl(settings);
+    new CeConfigurationImpl(workerCountProvider);
   }
 
   private void expectMessageException(int value) {
     expectedException.expect(MessageException.class);
-    expectedException.expectMessage("value '" + value + "' of property " + CE_WORKERS_COUNT_PROPERTY + " is invalid. " +
+    expectedException.expectMessage("Worker count '" + value + "' is invalid. " +
         "It must an integer strictly greater than 0");
   }
 
   @Test
   public void getCleanCeTasksInitialDelay_returns_1() {
-    assertThat(new CeConfigurationImpl(settings).getCleanCeTasksInitialDelay())
+    assertThat(new CeConfigurationImpl().getCleanCeTasksInitialDelay())
+        .isEqualTo(1L);
+    workerCountProvider.set(1);
+    assertThat(new CeConfigurationImpl(workerCountProvider).getCleanCeTasksInitialDelay())
         .isEqualTo(1L);
   }
 
   @Test
   public void getCleanCeTasksDelay_returns_10() {
-    assertThat(new CeConfigurationImpl(settings).getCleanCeTasksDelay())
+    assertThat(new CeConfigurationImpl().getCleanCeTasksDelay())
+        .isEqualTo(10L);
+    workerCountProvider.set(1);
+    assertThat(new CeConfigurationImpl(workerCountProvider).getCleanCeTasksDelay())
         .isEqualTo(10L);
   }
+
+  private static final class SimpleWorkerCountProvider implements WorkerCountProvider {
+    private int value = 0;
+
+    void set(int value) {
+      this.value = value;
+    }
+
+    @Override
+    public int get() {
+      return value;
+    }
+  }
 }
diff --git a/server/sonar-server/src/main/java/org/sonar/ce/configuration/WorkerCountProvider.java b/server/sonar-server/src/main/java/org/sonar/ce/configuration/WorkerCountProvider.java
new file mode 100644 (file)
index 0000000..c66e4dc
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * 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.ce.configuration;
+
+import org.sonar.api.ce.ComputeEngineSide;
+import org.sonar.api.server.ServerSide;
+
+/**
+ * When an implementation of this interface is available in Pico, the Compute Engine will use the value returned by
+ * {@link #get()} as the number of worker the Compute Engine should run on.
+ */
+@ComputeEngineSide
+@ServerSide
+public interface WorkerCountProvider {
+  /**
+   * @return an integer strictly greater than 0
+   */
+  int get();
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/ce/configuration/package-info.java b/server/sonar-server/src/main/java/org/sonar/ce/configuration/package-info.java
new file mode 100644 (file)
index 0000000..5805ec5
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.ce.configuration;
+
+import javax.annotation.ParametersAreNonnullByDefault;