diff options
4 files changed, 82 insertions, 40 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 be00dbc3d08..3489c453c1a 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 @@ -20,10 +20,10 @@ 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) { 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 2f501716b2d..1f80b0b8eaa 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 @@ -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 index 00000000000..6f5259325b2 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/property/MapInternalProperties.java @@ -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"); + } +} diff --git a/sonar-application/src/main/assembly/conf/sonar.properties b/sonar-application/src/main/assembly/conf/sonar.properties index b9b8f53893a..4b167d014e7 100644 --- a/sonar-application/src/main/assembly/conf/sonar.properties +++ b/sonar-application/src/main/assembly/conf/sonar.properties @@ -179,14 +179,6 @@ # 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 #-------------------------------------------------------------------------------------------------- |