]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7899 add cluster settings
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 26 Jul 2016 08:57:05 +0000 (10:57 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 29 Jul 2016 08:31:31 +0000 (10:31 +0200)
server/sonar-server/src/main/java/org/sonar/server/platform/cluster/Cluster.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/platform/cluster/ClusterImpl.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/platform/cluster/ClusterProperties.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/platform/cluster/package-info.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/platform/cluster/ClusterImplTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/platform/cluster/ClusterMock.java [new file with mode: 0644]

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 (file)
index 0000000..40b3642
--- /dev/null
@@ -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 (file)
index 0000000..0307cc2
--- /dev/null
@@ -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 (file)
index 0000000..4be5c21
--- /dev/null
@@ -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<PropertyDefinition> 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 (file)
index 0000000..48f939b
--- /dev/null
@@ -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 (file)
index 0000000..b4a6a69
--- /dev/null
@@ -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 (file)
index 0000000..f3d9b98
--- /dev/null
@@ -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;
+  }
+}