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";
}
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) {
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;
@Rule
public TemporaryFolder temp = new TemporaryFolder();
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
@Test
public void test_default_settings() throws Exception {
File homeDir = temp.newFolder();
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();
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);
}
@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");