+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.search;
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLConnection;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.CountDownLatch;
-import java.util.stream.Collectors;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.process.MinimumViableSystem;
-import org.sonar.process.Monitored;
-import org.sonar.process.ProcessEntryPoint;
-import org.sonar.process.Props;
-
-public class SearchServer implements Monitored {
- // VisibleForTesting
- protected static Logger LOGGER = LoggerFactory.getLogger(SearchServer.class);
-
- private final EsSettings settings;
- private Process p;
- private String url;
-
- public SearchServer(Props props) {
- this.settings = new EsSettings(props);
- new MinimumViableSystem()
- .checkWritableTempDir();
- }
-
- @Override
- public void start() {
- Path path = Paths.get(getExecutable());
- if (!path.toFile().exists()) {
- throw new IllegalStateException("Cannot find elasticsearch binary");
- }
- String absolutePath = path.toAbsolutePath().toString();
-
- List<String> command = new ArrayList<>();
- command.add(absolutePath);
- Map<String, String> settingsMap = settings.build();
- settingsMap.entrySet().stream()
- .filter(entry -> !"path.home".equals(entry.getKey()))
- .forEach(entry -> command.add("-E" + entry.getKey() + "=" + entry.getValue()));
- url = "http://"+settingsMap.get("http.host") + ":" + settingsMap.get("http.port");
- System.out.println(command.stream().collect(Collectors.joining(" ")));
-
-
-
- ProcessBuilder builder = new ProcessBuilder(command)
- .directory(new File(path.getParent().toAbsolutePath().toString()));
- builder.redirectOutput(ProcessBuilder.Redirect.PIPE);
- builder.redirectErrorStream(true);
- try {
- p = builder.start();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
-
- CountDownLatch latch = new CountDownLatch(2);
-
- new Thread(() -> {
- InputStream inputStream = p.getInputStream();
- InputStreamReader reader1 = new InputStreamReader(inputStream);
- BufferedReader reader = new BufferedReader(reader1);
- String line;
- try {
- while ((line = reader.readLine()) != null) {
- System.out.println(line);
- if (line.contains(" publish_address ")) {
- latch.countDown();
- }
- if (line.contains(" started")) {
- latch.countDown();
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }).start();
-
- try {
- latch.await();
- } catch (InterruptedException e) {
- // no action required
- }
-
- String urlString = url+"/_cluster/health?wait_for_status=yellow&timeout=30s";
- try {
- URL url = new URL(urlString);
- url.openConnection();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- private static String getExecutable() {
- if (System.getProperty("os.name").startsWith("Windows")) {
- return "elasticsearch/bin/elasticsearch.bat";
- }
- return "elasticsearch/bin/elasticsearch";
- }
-
- @Override
- public Status getStatus() {
- Status status = null;
- try {
- status = getStatus2();
- System.out.println("ES STATUS "+status);
- return status;
- } catch (Exception e) {
- System.out.println("ES STATUS "+e.getMessage());
- throw new RuntimeException(e);
- }
- }
-
- private Status getStatus2() {
- String urlString = url+"/_cluster/health";
- try {
- URL url = new URL(urlString);
- URLConnection urlConnection = url.openConnection();
- InputStream inputStream = urlConnection.getInputStream();
- String line;
- BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
- while ((line = reader.readLine()) != null) {
- if (line.contains("\"status\"")) {
- if (line.contains("\"red\"")) {
- return Status.DOWN;
- }
- if (line.contains("\"yellow\"")) {
- return Status.OPERATIONAL;
- }
- if (line.contains("\"green\"")) {
- return Status.OPERATIONAL;
- }
- }
- }
- } catch (MalformedURLException e) {
- throw new RuntimeException(String.format("Cannot contact local Elasticsearch instance via url '%s'", urlString), e);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return Status.DOWN;
- }
-
- @Override
- public void awaitStop() {
- try {
- while (p != null && p.isAlive()) {
- Thread.sleep(200L);
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- if (p != null) {
- p.destroy();
- try {
- p.waitFor();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
-
- @Override
- public void stop() {
- if (p != null) {
- p.destroyForcibly();
- }
- //Jmx.unregister(EsSettingsMBean.OBJECT_NAME);
- }
-
- public static void main(String... args) {
- ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(args);
- new SearchLogging().configure(entryPoint.getProps());
- SearchServer searchServer = new SearchServer(entryPoint.getProps());
- entryPoint.launch(searchServer);
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.search;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.InetAddress;
-import java.net.URL;
-import java.net.URLConnection;
-import java.util.Properties;
-import org.junit.After;
-import org.junit.Ignore;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.DisableOnDebug;
-import org.junit.rules.TemporaryFolder;
-import org.junit.rules.TestRule;
-import org.junit.rules.Timeout;
-import org.sonar.process.Monitored;
-import org.sonar.process.NetworkUtils;
-import org.sonar.process.ProcessEntryPoint;
-import org.sonar.process.ProcessProperties;
-import org.sonar.process.Props;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
-
-@Ignore
-// FIXME enable back right now!
-public class SearchServerTest {
-
- private static final String A_CLUSTER_NAME = "a_cluster";
- private static final String A_NODE_NAME = "a_node";
-
- @Rule
- public TestRule safeguardTimeout = new DisableOnDebug(Timeout.seconds(60));
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- private int httpPort = NetworkUtils.getNextAvailablePort(InetAddress.getLoopbackAddress());
- private int port = NetworkUtils.getNextAvailablePort(InetAddress.getLoopbackAddress());
- private SearchServer underTest;
-
- @After
- public void tearDown() {
- if (underTest != null) {
- underTest.stop();
- underTest.awaitStop();
- }
- }
-
- @Test
- public void start_stop_server() throws Exception {
- underTest = new SearchServer(getClusterProperties());
-
- underTest.start();
- assertThat(underTest.getStatus()).isEqualTo(Monitored.Status.OPERATIONAL);
-
- underTest.stop();
- underTest.awaitStop();
- try {
- underTest.getStatus();
- fail();
- } catch (Exception exception) {
- // ok
- }
- }
-
- private void waitFor(String expectedStatus) throws Exception {
- String urlString = "http://localhost:" + httpPort + "/_cluster/health";
- URL url = new URL(urlString);
- URLConnection urlConnection = url.openConnection();
- InputStream inputStream = urlConnection.getInputStream();
- String line;
- BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
- while ((line = reader.readLine()) != null) {
- if (line.contains("\"status\"")) {
- if (line.contains(expectedStatus)) {
- return;
- }
- }
- }
- fail();
- }
-
- private Props getClusterProperties() throws IOException {
- Props props = new Props(new Properties());
- // the following properties have always default values (see ProcessProperties)
- InetAddress host = InetAddress.getLoopbackAddress();
- props.set(ProcessProperties.SEARCH_HOST, host.getHostAddress());
- props.set(ProcessProperties.SEARCH_HTTP_PORT, String.valueOf(httpPort));
- props.set(ProcessProperties.SEARCH_PORT, String.valueOf(port));
- props.set(ProcessProperties.CLUSTER_NAME, A_CLUSTER_NAME);
- props.set(EsSettings.CLUSTER_SEARCH_NODE_NAME, A_NODE_NAME);
- props.set(ProcessProperties.PATH_HOME, temp.newFolder().getAbsolutePath());
- props.set(ProcessEntryPoint.PROPERTY_SHARED_PATH, temp.newFolder().getAbsolutePath());
- return props;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.es;
-
-import java.io.File;
-import java.io.IOException;
-import java.net.InetAddress;
-import java.nio.file.Files;
-import java.util.Collections;
-import java.util.Properties;
-import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
-import org.elasticsearch.client.transport.TransportClient;
-import org.elasticsearch.common.settings.Settings;
-import org.elasticsearch.common.transport.InetSocketTransportAddress;
-import org.sonar.process.NetworkUtils;
-import org.sonar.process.ProcessEntryPoint;
-import org.sonar.process.ProcessProperties;
-import org.sonar.process.Props;
-import org.sonar.search.SearchServer;
-
-public class EsServerHolder {
-
- private static EsServerHolder HOLDER = null;
- private final String clusterName;
- private final InetAddress address;
- private final int port;
- private final File homeDir;
- private final SearchServer server;
-
- private EsServerHolder(SearchServer server, String clusterName, InetAddress address, int port, File homeDir) {
- this.server = server;
- this.clusterName = clusterName;
- this.address = address;
- this.port = port;
- this.homeDir = homeDir;
- }
-
- public String getClusterName() {
- return clusterName;
- }
-
- public int getPort() {
- return port;
- }
-
- public InetAddress getAddress() {
- return address;
- }
-
- public SearchServer getServer() {
- return server;
- }
-
- public File getHomeDir() {
- return homeDir;
- }
-
- private void reset() {
- TransportClient client = new TransportClient(Settings.builder()
- .put("network.bind_host", "localhost")
- .put("cluster.name", clusterName)
- .build(), Collections.emptyList()) {};
- client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getLoopbackAddress(), port));
-
- // wait for node to be ready
- client.admin().cluster().prepareHealth()
- .setWaitForGreenStatus()
- .get();
-
- // delete the indices created by previous tests
- DeleteIndexResponse response = client.admin().indices().prepareDelete("_all").get();
- if (!response.isAcknowledged()) {
- throw new IllegalStateException("Fail to delete all indices");
- }
- client.close();
- }
-
- public static synchronized EsServerHolder get() throws IOException {
- if (HOLDER == null) {
- File homeDir = Files.createTempDirectory("tmp-es-").toFile();
- homeDir.delete();
- homeDir.mkdir();
-
- String clusterName = "testCluster";
- InetAddress address = InetAddress.getLoopbackAddress();
- int port = NetworkUtils.getNextAvailablePort(address);
-
- Properties properties = new Properties();
- properties.setProperty(ProcessProperties.CLUSTER_NAME, clusterName);
- properties.setProperty(ProcessProperties.SEARCH_PORT, String.valueOf(port));
- properties.setProperty(ProcessProperties.SEARCH_HOST, address.getHostAddress());
- properties.setProperty(ProcessProperties.PATH_HOME, homeDir.getAbsolutePath());
- properties.setProperty(ProcessEntryPoint.PROPERTY_SHARED_PATH, homeDir.getAbsolutePath());
- SearchServer server = new SearchServer(new Props(properties));
- server.start();
- HOLDER = new EsServerHolder(server, clusterName, address, port, homeDir);
- }
- HOLDER.reset();
- return HOLDER;
- }
-}