]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9133 Define the number of ES replicas
authorEric Hartmann <hartmann.eric@gmail.com>
Thu, 27 Apr 2017 15:42:47 +0000 (17:42 +0200)
committerEric Hartmann <hartmann.eric@gmail.Com>
Thu, 4 May 2017 09:07:09 +0000 (11:07 +0200)
New property added sonar.search.replicas to define the number of ES replicas
When this property is not set, in standalone mode this value will be 0 and
in cluster mode, this value will be 1

server/sonar-process/src/main/java/org/sonar/process/ProcessProperties.java
server/sonar-search/src/main/java/org/sonar/search/EsSettings.java
server/sonar-search/src/test/java/org/sonar/search/EsSettingsTest.java
server/sonar-server/src/main/java/org/sonar/server/es/NewIndex.java
server/sonar-server/src/test/java/org/sonar/server/es/NewIndexTest.java

index f579ea23038e3aa2947eb6b9ec7223f463c7c184..0f2258f5cf845c9c4825de0b2ce219d5d1827250 100644 (file)
@@ -66,6 +66,7 @@ public class ProcessProperties {
   public static final String SEARCH_HTTP_PORT = "sonar.search.httpPort";
   public static final String SEARCH_JAVA_OPTS = "sonar.search.javaOpts";
   public static final String SEARCH_JAVA_ADDITIONAL_OPTS = "sonar.search.javaAdditionalOpts";
+  public static final String SEARCH_REPLICAS = "sonar.search.replicas";
 
   public static final String WEB_JAVA_OPTS = "sonar.web.javaOpts";
 
index 9abb06eb2588d58cc750127f234703f967d74c37..aae8545f3b1112bebe7da337ca6bc90937cd8cc6 100644 (file)
@@ -158,21 +158,27 @@ public class EsSettings implements EsSettingsMBean {
   }
 
   private void configureCluster(Settings.Builder builder) {
-    int replicationFactor = 0;
+    int replicationFactor = props.valueAsInt(ProcessProperties.SEARCH_REPLICAS, 0);
+
     if (clusterEnabled) {
-      replicationFactor = 1;
+      if (!props.contains(ProcessProperties.SEARCH_REPLICAS)) {
+        // In data center edition there is 3 nodes, so if not defined, replicationFactor default value is 1
+        replicationFactor = 1;
+      }
+
       String hosts = props.value(ProcessProperties.CLUSTER_SEARCH_HOSTS, "");
       LOGGER.info("Elasticsearch cluster enabled. Connect to hosts [{}]", hosts);
       builder.put("discovery.zen.ping.unicast.hosts", hosts);
     }
-    builder.put("discovery.zen.minimum_master_nodes", 1);
-    builder.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, replicationFactor);
-    builder.put("cluster.name", getClusterName());
-    builder.put("cluster.routing.allocation.awareness.attributes", "rack_id");
-    builder.put("node.rack_id", nodeName);
-    builder.put("node.name", nodeName);
-    builder.put("node.data", true);
-    builder.put("node.master", true);
+
+    builder.put("discovery.zen.minimum_master_nodes", 1)
+      .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, replicationFactor)
+      .put("cluster.name", getClusterName())
+      .put("cluster.routing.allocation.awareness.attributes", "rack_id")
+      .put("node.rack_id", nodeName)
+      .put("node.name", nodeName)
+      .put("node.data", true)
+      .put("node.master", true);
   }
 
   private void configureMarvel(Settings.Builder builder) {
index 595b2a670e763b6ed98b14fc1d37cbb876c0bdec..b03f58e55b53cdd837ca5fabc6806b2ff916b9df 100644 (file)
@@ -25,6 +25,7 @@ import java.util.Properties;
 import org.elasticsearch.common.settings.Settings;
 import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
 import org.sonar.process.ProcessProperties;
 import org.sonar.process.Props;
@@ -36,6 +37,9 @@ public class EsSettingsTest {
   @Rule
   public TemporaryFolder temp = new TemporaryFolder();
 
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
   @Test
   public void test_default_settings() throws Exception {
     File homeDir = temp.newFolder();
@@ -94,6 +98,35 @@ public class EsSettingsTest {
     assertThat(settings.get("discovery.zen.ping.unicast.hosts")).isEqualTo("1.2.3.4:9000,1.2.3.5:8080");
   }
 
+  @Test
+  public void cluster_is_enabled_with_defined_replicas() throws Exception {
+    Props props = minProps();
+    props.set(ProcessProperties.CLUSTER_ENABLED, "true");
+    props.set(ProcessProperties.SEARCH_REPLICAS, "5");
+    Settings settings = new EsSettings(props).build();
+
+    assertThat(settings.get("index.number_of_replicas")).isEqualTo("5");
+  }
+
+  @Test
+  public void cluster_not_enabled_but_replicas_are_defined() throws Exception {
+    Props props = minProps();
+    props.set(ProcessProperties.SEARCH_REPLICAS, "5");
+    Settings settings = new EsSettings(props).build();
+
+    assertThat(settings.get("index.number_of_replicas")).isEqualTo("5");
+  }
+
+  @Test
+  public void incorrect_values_of_replicas() throws Exception {
+    Props props = minProps();
+    props.set(ProcessProperties.SEARCH_REPLICAS, "ꝱꝲꝳପ");
+
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Value of property sonar.search.replicas is not an integer:");
+    Settings settings = new EsSettings(props).build();
+  }
+
   @Test
   public void enable_marvel() throws Exception {
     Props props = minProps();
index 9e0b9d7f60adaa259fda4141fbd8db42bff775b3..bfc65cfbf270d86a0cfeae0350c9465364338b7a 100644 (file)
@@ -83,8 +83,9 @@ public class NewIndex {
     if (shards == 0) {
       shards = defaultNbOfShards;
     }
-    int replicas = settings.getInt(format("sonar.search.%s.replicas", indexName));
-    if (replicas == 0) {
+
+    int replicas = settings.getInt(ProcessProperties.SEARCH_REPLICAS);
+    if (replicas == 0 && settings.getString(ProcessProperties.SEARCH_REPLICAS) == null) {
       replicas = clusterMode ? 1 : 0;
     }
     getSettings().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, shards);
index 39c53e1c7f289c2f212ce3e6ae22edf89b972366..521ce5e37628315c67d647892144dbaafb311e47 100644 (file)
@@ -186,16 +186,50 @@ public class NewIndexTest {
   }
 
   @Test
-  public void customize_number_of_shards_and_replicas() {
+  public void default_number_of_replicas_on_standalone_instance_must_be_0() {
     NewIndex index = new NewIndex("issues");
     MapSettings settings = new MapSettings();
-    settings.setProperty("sonar.search.issues.shards", "3");
-    settings.setProperty("sonar.search.issues.replicas", "1");
     index.configureShards(settings, 5);
-    assertThat(index.getSettings().get(IndexMetaData.SETTING_NUMBER_OF_SHARDS)).isEqualTo("3");
+    assertThat(index.getSettings().get(IndexMetaData.SETTING_NUMBER_OF_REPLICAS)).isEqualTo("0");
+  }
+
+  @Test
+  public void default_number_of_replicas_on_non_enabled_cluster_must_be_0() {
+    NewIndex index = new NewIndex("issues");
+    MapSettings settings = new MapSettings();
+    settings.setProperty(ProcessProperties.CLUSTER_ENABLED, "false");
+    index.configureShards(settings, 5);
+    assertThat(index.getSettings().get(IndexMetaData.SETTING_NUMBER_OF_REPLICAS)).isEqualTo("0");
+  }
+
+  @Test
+  public void default_number_of_replicas_on_cluster_instance_must_be_1() {
+    NewIndex index = new NewIndex("issues");
+    MapSettings settings = new MapSettings();
+    settings.setProperty(ProcessProperties.CLUSTER_ENABLED, "true");
+    index.configureShards(settings, 5);
     assertThat(index.getSettings().get(IndexMetaData.SETTING_NUMBER_OF_REPLICAS)).isEqualTo("1");
   }
 
+  @Test
+  public void when_number_of_replicas_on_cluster_is_specified_to_zero_default_value_must_not_be_used() {
+    NewIndex index = new NewIndex("issues");
+    MapSettings settings = new MapSettings();
+    settings.setProperty(ProcessProperties.CLUSTER_ENABLED, "true");
+    settings.setProperty(ProcessProperties.SEARCH_REPLICAS, "0");
+    index.configureShards(settings, 5);
+    assertThat(index.getSettings().get(IndexMetaData.SETTING_NUMBER_OF_REPLICAS)).isEqualTo("0");
+  }
+
+  @Test
+  public void customize_number_of_replicas() {
+    NewIndex index = new NewIndex("issues");
+    MapSettings settings = new MapSettings();
+    settings.setProperty(ProcessProperties.SEARCH_REPLICAS, "3");
+    index.configureShards(settings, 5);
+    assertThat(index.getSettings().get(IndexMetaData.SETTING_NUMBER_OF_REPLICAS)).isEqualTo("3");
+  }
+
   @Test
   public void index_with_source() {
     NewIndex index = new NewIndex("issues");