]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7899 Fix sonar.cluster.startupLeader when local ES is enabled
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 2 Aug 2016 11:52:56 +0000 (13:52 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 3 Aug 2016 15:58:52 +0000 (17:58 +0200)
server/sonar-server/src/main/java/org/sonar/server/es/EsClientProvider.java
server/sonar-server/src/test/java/org/sonar/server/es/EsClientProviderTest.java

index 2a4f9640fcb163e9b803c5ee5b42777e557e44da..a9cf4b8b3db358fe3818f9260f6d1c32d2372032 100644 (file)
@@ -22,19 +22,25 @@ package org.sonar.server.es;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.Arrays;
+import java.util.stream.Collectors;
 import javax.annotation.concurrent.Immutable;
 import org.elasticsearch.client.transport.TransportClient;
 import org.elasticsearch.common.transport.InetSocketTransportAddress;
+import org.elasticsearch.common.transport.TransportAddress;
 import org.picocontainer.injectors.ProviderAdapter;
 import org.sonar.api.ce.ComputeEngineSide;
 import org.sonar.api.config.Settings;
 import org.sonar.api.server.ServerSide;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
 import org.sonar.process.ProcessProperties;
 
 @ComputeEngineSide
 @ServerSide
 public class EsClientProvider extends ProviderAdapter {
 
+  private static final Logger LOGGER = Loggers.get(EsClientProvider.class);
+
   private EsClient cache;
 
   public EsClient provide(Settings settings) {
@@ -46,22 +52,29 @@ public class EsClientProvider extends ProviderAdapter {
       esSettings.put("cluster.name", settings.getString(ProcessProperties.SEARCH_CLUSTER_NAME));
 
       boolean clusterEnabled = settings.getBoolean(ProcessProperties.CLUSTER_ENABLED);
-      if (clusterEnabled) {
+      if (clusterEnabled && settings.getBoolean(ProcessProperties.CLUSTER_SEARCH_DISABLED)) {
         esSettings.put("client.transport.sniff", true);
         nativeClient = TransportClient.builder().settings(esSettings).build();
         Arrays.stream(settings.getStringArray(ProcessProperties.CLUSTER_SEARCH_HOSTS))
           .map(Host::parse)
           .forEach(h -> h.addTo(nativeClient));
+        LOGGER.info("Connected to remote Elasticsearch: [{}]", displayedAddresses(nativeClient));
       } else {
         nativeClient = TransportClient.builder().settings(esSettings).build();
         Host host = new Host(settings.getString(ProcessProperties.SEARCH_HOST), settings.getInt(ProcessProperties.SEARCH_PORT));
         host.addTo(nativeClient);
+        LOGGER.info("Connected to local Elasticsearch: [{}]", displayedAddresses(nativeClient));
       }
+
       cache = new EsClient(nativeClient);
     }
     return cache;
   }
 
+  private static String displayedAddresses(TransportClient nativeClient) {
+    return nativeClient.transportAddresses().stream().map(TransportAddress::toString).collect(Collectors.joining(", "));
+  }
+
   @Immutable
   private static class Host {
     private final String ip;
index 27e33425ebc72c4792e7f1319c0c0d5f397c257d..644bae28a0306c533e5583b1b5cec67001fc3de3 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.es;
 
 import java.net.InetAddress;
+import org.assertj.core.api.Condition;
 import org.elasticsearch.client.transport.TransportClient;
 import org.elasticsearch.common.transport.TransportAddress;
 import org.junit.Before;
@@ -27,6 +28,8 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.sonar.api.config.Settings;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
 import org.sonar.process.ProcessProperties;
 
 import static java.lang.String.format;
@@ -37,6 +40,9 @@ public class EsClientProviderTest {
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
+  @Rule
+  public LogTester logTester = new LogTester();
+
   private Settings settings = new Settings();
   private EsClientProvider underTest = new EsClientProvider();
   private String localhost;
@@ -61,14 +67,16 @@ public class EsClientProviderTest {
     TransportAddress address = transportClient.transportAddresses().get(0);
     assertThat(address.getAddress()).isEqualTo(localhost);
     assertThat(address.getPort()).isEqualTo(8080);
+    assertThat(logTester.logs(LoggerLevel.INFO)).has(new Condition<>(s -> s.contains("Connected to local Elasticsearch: [" + localhost + ":8080]"), ""));
 
     // keep in cache
     assertThat(underTest.provide(settings)).isSameAs(client);
   }
 
   @Test
-  public void connection_to_remote_es_nodes_when_cluster_mode_is_enabled() throws Exception {
+  public void connection_to_remote_es_nodes_when_cluster_mode_is_enabled_and_local_es_is_disabled() throws Exception {
     settings.setProperty(ProcessProperties.CLUSTER_ENABLED, true);
+    settings.setProperty(ProcessProperties.CLUSTER_SEARCH_DISABLED, true);
     settings.setProperty(ProcessProperties.CLUSTER_SEARCH_HOSTS, format("%s:8080,%s:8081", localhost, localhost));
 
     EsClient client = underTest.provide(settings);
@@ -80,6 +88,7 @@ public class EsClientProviderTest {
     address = transportClient.transportAddresses().get(1);
     assertThat(address.getAddress()).isEqualTo(localhost);
     assertThat(address.getPort()).isEqualTo(8081);
+    assertThat(logTester.logs(LoggerLevel.INFO)).has(new Condition<>(s -> s.contains("Connected to remote Elasticsearch: [" + localhost + ":8080, " + localhost + ":8081]"), ""));
 
     // keep in cache
     assertThat(underTest.provide(settings)).isSameAs(client);
@@ -88,6 +97,7 @@ public class EsClientProviderTest {
   @Test
   public void fail_if_cluster_host_is_badly_formatted() throws Exception {
     settings.setProperty(ProcessProperties.CLUSTER_ENABLED, true);
+    settings.setProperty(ProcessProperties.CLUSTER_SEARCH_DISABLED, true);
     settings.setProperty(ProcessProperties.CLUSTER_SEARCH_HOSTS, "missing_colon");
 
     expectedException.expect(IllegalArgumentException.class);