From 116560af620b498a562c73be5c2b8a3464fc4fb3 Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Tue, 27 May 2014 18:24:11 +0200 Subject: [PATCH] DAOv.2 - Implemented multiple methods for ESNode (DATA and MEMORY) --- .../java/org/sonar/server/search/ESNode.java | 52 ++++++++++++++++--- .../sonar/server/search/IndexProperties.java | 33 ++++++++++++ 2 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 sonar-server/src/main/java/org/sonar/server/search/IndexProperties.java diff --git a/sonar-server/src/main/java/org/sonar/server/search/ESNode.java b/sonar-server/src/main/java/org/sonar/server/search/ESNode.java index 5ab1e56201d..fe5b20375e6 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/ESNode.java +++ b/sonar-server/src/main/java/org/sonar/server/search/ESNode.java @@ -27,6 +27,7 @@ import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus; import org.elasticsearch.client.Client; import org.elasticsearch.common.logging.ESLoggerFactory; import org.elasticsearch.common.logging.slf4j.Slf4jESLoggerFactory; +import org.elasticsearch.common.network.NetworkUtils; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.node.Node; import org.elasticsearch.node.NodeBuilder; @@ -71,8 +72,47 @@ public class ESNode implements Startable { @Override public void start() { - LOG.info("Start Elasticsearch..."); + IndexProperties.ES_TYPE type = IndexProperties.ES_TYPE.valueOf(settings.getString(IndexProperties.TYPE)); + + switch (type) { + case MEMORY: + initMemoryES(); + break; + case TRANSPORT: + initTransportES(); + break; + case DATA: + default: + initDataES(); + break; + } + } + + private void initMemoryES(){ + ImmutableSettings.Builder builder = ImmutableSettings.settingsBuilder() + .put("node.name", "node-test-" + System.currentTimeMillis()) + .put("node.data", true) + .put("cluster.name", "cluster-test-" + NetworkUtils.getLocalAddress().getHostName()) + .put("index.store.type", "memory") + .put("index.store.fs.memory.enabled", "true") + .put("gateway.type", "none") + .put("index.number_of_shards", "1") + .put("index.number_of_replicas", "0") + .put("cluster.routing.schedule", "50ms") + .put("node.local", true); + + node = NodeBuilder.nodeBuilder() + .settings(builder) + .node(); + node.start(); + } + + private void initTransportES(){ + throw new IllegalStateException("Not implemented yet"); + } + + private void initDataES() { initLogging(); ImmutableSettings.Builder esSettings = ImmutableSettings.builder() .loadFromUrl(getClass().getResource("config/elasticsearch.json")); @@ -89,10 +129,10 @@ public class ESNode implements Startable { if ( node.client().admin().cluster().prepareHealth() - .setWaitForYellowStatus() - .setTimeout(healthTimeout) - .execute().actionGet() - .getStatus() == ClusterHealthStatus.RED) { + .setWaitForYellowStatus() + .setTimeout(healthTimeout) + .execute().actionGet() + .getStatus() == ClusterHealthStatus.RED) { throw new IllegalStateException( String.format("Elasticsearch index is corrupt, please delete directory '%s/%s' and relaunch the SonarQube server.", fileSystem.getHomeDir().getAbsolutePath(), DATA_DIR)); } @@ -119,7 +159,7 @@ public class ESNode implements Startable { } private void initRestConsole(ImmutableSettings.Builder esSettings) { - int httpPort = settings.getInt("sonar.es.http.port"); + int httpPort = settings.getInt(IndexProperties.HTTP_PORT); if (httpPort > 0) { LOG.warn("Elasticsearch HTTP console enabled on port {}. Only for debugging purpose.", httpPort); esSettings.put(HTTP_ENABLED, true); diff --git a/sonar-server/src/main/java/org/sonar/server/search/IndexProperties.java b/sonar-server/src/main/java/org/sonar/server/search/IndexProperties.java new file mode 100644 index 00000000000..87a68428806 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/search/IndexProperties.java @@ -0,0 +1,33 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.search; + +/** + * @since 4.4 + */ +public final class IndexProperties { + + public static enum ES_TYPE { + MEMORY, TRANSPORT, DATA + } + + public static final String TYPE = "sonar.es.type"; + public static final String HTTP_PORT = "sonar.es.http.port"; +} -- 2.39.5