aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-07-05 10:42:12 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-07-17 10:52:47 +0200
commitcffa304e688ed72619b3a88f1e4fcb1cd14f04d4 (patch)
tree1cacfc3bf330cedc3d285373a41187b23ee832cb
parentb2697bca004c5d72d5539233b7780faa9fe06df6 (diff)
downloadsonarqube-cffa304e688ed72619b3a88f1e4fcb1cd14f04d4.tar.gz
sonarqube-cffa304e688ed72619b3a88f1e4fcb1cd14f04d4.zip
SONAR-9508 CE gets number of workers from WorkerCountProvider
rather than from internal properties directly
-rw-r--r--server/sonar-ce/src/main/java/org/sonar/ce/configuration/CeConfigurationImpl.java39
-rw-r--r--server/sonar-ce/src/test/java/org/sonar/ce/configuration/CeConfigurationImplTest.java93
-rw-r--r--server/sonar-server/src/main/java/org/sonar/ce/configuration/WorkerCountProvider.java36
-rw-r--r--server/sonar-server/src/main/java/org/sonar/ce/configuration/package-info.java23
4 files changed, 110 insertions, 81 deletions
diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/configuration/CeConfigurationImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/configuration/CeConfigurationImpl.java
index 3489c453c1a..94d0abf9c55 100644
--- a/server/sonar-ce/src/main/java/org/sonar/ce/configuration/CeConfigurationImpl.java
+++ b/server/sonar-ce/src/main/java/org/sonar/ce/configuration/CeConfigurationImpl.java
@@ -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
diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/configuration/CeConfigurationImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/configuration/CeConfigurationImplTest.java
index 1f80b0b8eaa..654c09f68aa 100644
--- a/server/sonar-ce/src/test/java/org/sonar/ce/configuration/CeConfigurationImplTest.java
+++ b/server/sonar-ce/src/test/java/org/sonar/ce/configuration/CeConfigurationImplTest.java
@@ -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
index 00000000000..c66e4dce6fe
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/ce/configuration/WorkerCountProvider.java
@@ -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
index 00000000000..5805ec5bddf
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/ce/configuration/package-info.java
@@ -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;