From: Simon Brandhof Date: Tue, 26 Jul 2016 08:57:05 +0000 (+0200) Subject: SONAR-7899 add cluster settings X-Git-Tag: 6.1-RC1~490 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=13c05cb112bb10b2706d97b432d8208cd503208f;p=sonarqube.git SONAR-7899 add cluster settings --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/cluster/Cluster.java b/server/sonar-server/src/main/java/org/sonar/server/platform/cluster/Cluster.java new file mode 100644 index 00000000000..40b3642f4e2 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/cluster/Cluster.java @@ -0,0 +1,37 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.platform.cluster; + +public interface Cluster { + + /** + * Cluster is enabled when property {@link ClusterProperties#ENABLED} is {@code true} + */ + boolean isEnabled(); + + /** + * The startup leader is the first node to be started in a cluster. It's the only one + * to create and populate datastores. + * A standard node is automatically marked as "startup leader" when cluster + * is disabled (default). + */ + boolean isStartupLeader(); + +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/cluster/ClusterImpl.java b/server/sonar-server/src/main/java/org/sonar/server/platform/cluster/ClusterImpl.java new file mode 100644 index 00000000000..0307cc25223 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/cluster/ClusterImpl.java @@ -0,0 +1,49 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.platform.cluster; + +import org.sonar.api.config.Settings; +import org.sonar.api.utils.log.Loggers; + +public class ClusterImpl implements Cluster { + + private final boolean enabled; + private final boolean startupLeader; + + public ClusterImpl(Settings settings) { + this.enabled = settings.getBoolean(ClusterProperties.ENABLED); + if (this.enabled) { + this.startupLeader = settings.getBoolean(ClusterProperties.STARTUP_LEADER); + Loggers.get(ClusterImpl.class).info("Cluster enabled (startup {})", startupLeader ? "leader" : "follower"); + } else { + this.startupLeader = true; + } + } + + @Override + public boolean isEnabled() { + return enabled; + } + + @Override + public boolean isStartupLeader() { + return startupLeader; + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/cluster/ClusterProperties.java b/server/sonar-server/src/main/java/org/sonar/server/platform/cluster/ClusterProperties.java new file mode 100644 index 00000000000..4be5c21f58b --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/cluster/ClusterProperties.java @@ -0,0 +1,27 @@ +package org.sonar.server.platform.cluster; + +import com.google.common.collect.ImmutableList; +import java.util.List; +import org.sonar.api.PropertyType; +import org.sonar.api.config.PropertyDefinition; + +public class ClusterProperties { + + public static final String ENABLED = "sonar.cluster.enabled"; + public static final String STARTUP_LEADER = "sonar.cluster.startupLeader"; + + public static List definitions() { + return ImmutableList.of( + PropertyDefinition.builder(ENABLED) + .type(PropertyType.BOOLEAN) + .defaultValue(String.valueOf(false)) + .hidden() + .build(), + + PropertyDefinition.builder(STARTUP_LEADER) + .type(PropertyType.BOOLEAN) + .defaultValue(String.valueOf(false)) + .hidden() + .build()); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/cluster/package-info.java b/server/sonar-server/src/main/java/org/sonar/server/platform/cluster/package-info.java new file mode 100644 index 00000000000..48f939bbeae --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/cluster/package-info.java @@ -0,0 +1,23 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact 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.server.platform.cluster; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/cluster/ClusterImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/cluster/ClusterImplTest.java new file mode 100644 index 00000000000..b4a6a69ce7a --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/cluster/ClusterImplTest.java @@ -0,0 +1,58 @@ +package org.sonar.server.platform.cluster; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.config.PropertyDefinitions; +import org.sonar.api.config.Settings; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ClusterImplTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private Settings settings = new Settings(new PropertyDefinitions(ClusterProperties.definitions())); + + @Test + public void cluster_is_disabled_by_default() { + ClusterImpl underTest = new ClusterImpl(settings); + + assertThat(underTest.isEnabled()).isFalse(); + assertThat(underTest.isStartupLeader()).isTrue(); + } + + @Test + public void node_is_startup_leader_in_cluster() { + settings.setProperty("sonar.cluster.enabled", "true"); + settings.setProperty("sonar.cluster.startupLeader", "true"); + + ClusterImpl underTest = new ClusterImpl(settings); + + assertThat(underTest.isEnabled()).isTrue(); + assertThat(underTest.isStartupLeader()).isTrue(); + } + + @Test + public void node_is_startup_follower_by_default_in_cluster() { + settings.setProperty("sonar.cluster.enabled", "true"); + + ClusterImpl underTest = new ClusterImpl(settings); + + assertThat(underTest.isEnabled()).isTrue(); + assertThat(underTest.isStartupLeader()).isFalse(); + } + + @Test + public void node_is_startup_follower_in_cluster() { + settings.setProperty("sonar.cluster.enabled", "true"); + settings.setProperty("sonar.cluster.startupLeader", "false"); + + ClusterImpl underTest = new ClusterImpl(settings); + + assertThat(underTest.isEnabled()).isTrue(); + assertThat(underTest.isStartupLeader()).isFalse(); + } + +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/cluster/ClusterMock.java b/server/sonar-server/src/test/java/org/sonar/server/platform/cluster/ClusterMock.java new file mode 100644 index 00000000000..f3d9b98ffb8 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/cluster/ClusterMock.java @@ -0,0 +1,27 @@ +package org.sonar.server.platform.cluster; + +public class ClusterMock implements Cluster { + + private boolean enabled = false; + private boolean startupLeader = false; + + public ClusterMock setEnabled(boolean b) { + this.enabled = b; + return this; + } + + @Override + public boolean isEnabled() { + return enabled; + } + + public ClusterMock setStartupLeader(boolean b) { + this.startupLeader = b; + return this; + } + + @Override + public boolean isStartupLeader() { + return startupLeader; + } +}