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) {
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;
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;
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;
@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;
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);
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);
@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);