You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EsClientProvider.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.es;
  21. import com.google.common.net.HostAndPort;
  22. import io.netty.util.ThreadDeathWatcher;
  23. import io.netty.util.concurrent.GlobalEventExecutor;
  24. import java.net.InetAddress;
  25. import java.net.UnknownHostException;
  26. import java.util.Arrays;
  27. import java.util.concurrent.TimeUnit;
  28. import java.util.stream.Collectors;
  29. import org.elasticsearch.client.transport.TransportClient;
  30. import org.elasticsearch.common.network.NetworkModule;
  31. import org.elasticsearch.common.settings.Settings;
  32. import org.elasticsearch.common.transport.InetSocketTransportAddress;
  33. import org.elasticsearch.common.transport.TransportAddress;
  34. import org.elasticsearch.join.ParentJoinPlugin;
  35. import org.elasticsearch.percolator.PercolatorPlugin;
  36. import org.elasticsearch.transport.Netty4Plugin;
  37. import org.picocontainer.injectors.ProviderAdapter;
  38. import org.sonar.api.ce.ComputeEngineSide;
  39. import org.sonar.api.config.Configuration;
  40. import org.sonar.api.server.ServerSide;
  41. import org.sonar.api.utils.log.Logger;
  42. import org.sonar.api.utils.log.Loggers;
  43. import org.sonar.process.cluster.NodeType;
  44. import static java.util.Collections.unmodifiableList;
  45. import static org.sonar.process.ProcessProperties.Property.CLUSTER_ENABLED;
  46. import static org.sonar.process.ProcessProperties.Property.CLUSTER_NAME;
  47. import static org.sonar.process.ProcessProperties.Property.CLUSTER_NODE_TYPE;
  48. import static org.sonar.process.ProcessProperties.Property.CLUSTER_SEARCH_HOSTS;
  49. import static org.sonar.process.ProcessProperties.Property.SEARCH_HOST;
  50. import static org.sonar.process.ProcessProperties.Property.SEARCH_PORT;
  51. import static org.sonar.process.cluster.NodeType.SEARCH;
  52. @ComputeEngineSide
  53. @ServerSide
  54. public class EsClientProvider extends ProviderAdapter {
  55. private static final Logger LOGGER = Loggers.get(EsClientProvider.class);
  56. private EsClient cache;
  57. public EsClient provide(Configuration config) {
  58. if (cache == null) {
  59. Settings.Builder esSettings = Settings.builder();
  60. // mandatory property defined by bootstrap process
  61. esSettings.put("cluster.name", config.get(CLUSTER_NAME.getKey()).get());
  62. boolean clusterEnabled = config.getBoolean(CLUSTER_ENABLED.getKey()).orElse(false);
  63. boolean searchNode = !clusterEnabled || SEARCH.equals(NodeType.parse(config.get(CLUSTER_NODE_TYPE.getKey()).orElse(null)));
  64. final TransportClient nativeClient = new MinimalTransportClient(esSettings.build());
  65. if (clusterEnabled && !searchNode) {
  66. esSettings.put("client.transport.sniff", true);
  67. Arrays.stream(config.getStringArray(CLUSTER_SEARCH_HOSTS.getKey()))
  68. .map(HostAndPort::fromString)
  69. .forEach(h -> addHostToClient(h, nativeClient));
  70. LOGGER.info("Connected to remote Elasticsearch: [{}]", displayedAddresses(nativeClient));
  71. } else {
  72. HostAndPort host = HostAndPort.fromParts(config.get(SEARCH_HOST.getKey()).get(), config.getInt(SEARCH_PORT.getKey()).get());
  73. addHostToClient(host, nativeClient);
  74. LOGGER.info("Connected to local Elasticsearch: [{}]", displayedAddresses(nativeClient));
  75. }
  76. cache = new EsClient(nativeClient);
  77. }
  78. return cache;
  79. }
  80. private static void addHostToClient(HostAndPort host, TransportClient client) {
  81. try {
  82. client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host.getHostText()), host.getPortOrDefault(9001)));
  83. } catch (UnknownHostException e) {
  84. throw new IllegalStateException("Can not resolve host [" + host + "]", e);
  85. }
  86. }
  87. private static String displayedAddresses(TransportClient nativeClient) {
  88. return nativeClient.transportAddresses().stream().map(TransportAddress::toString).collect(Collectors.joining(", "));
  89. }
  90. static class MinimalTransportClient extends TransportClient {
  91. MinimalTransportClient(Settings settings) {
  92. super(settings, unmodifiableList(Arrays.asList(Netty4Plugin.class, PercolatorPlugin.class, ParentJoinPlugin.class)));
  93. }
  94. @Override
  95. public void close() {
  96. super.close();
  97. if (!NetworkModule.TRANSPORT_TYPE_SETTING.exists(settings)
  98. || NetworkModule.TRANSPORT_TYPE_SETTING.get(settings).equals(Netty4Plugin.NETTY_TRANSPORT_NAME)) {
  99. try {
  100. GlobalEventExecutor.INSTANCE.awaitInactivity(5, TimeUnit.SECONDS);
  101. } catch (InterruptedException e) {
  102. Thread.currentThread().interrupt();
  103. }
  104. try {
  105. ThreadDeathWatcher.awaitInactivity(5, TimeUnit.SECONDS);
  106. } catch (InterruptedException e) {
  107. Thread.currentThread().interrupt();
  108. }
  109. }
  110. }
  111. }
  112. }