From 672ae499cf6418725e63b9beb5630a304f86ae01 Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Thu, 17 Jul 2014 11:42:23 +0200 Subject: [PATCH] SONAR-5410 - Added transport mode to sonar-server --- .../java/org/sonar/server/search/ESNode.java | 72 ++++++++++--------- .../sonar/server/search/IndexProperties.java | 5 +- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/ESNode.java b/server/sonar-server/src/main/java/org/sonar/server/search/ESNode.java index f568a09c97e..b6334873455 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/ESNode.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/ESNode.java @@ -24,10 +24,12 @@ import com.google.common.annotations.VisibleForTesting; import org.apache.commons.io.FileUtils; import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus; import org.elasticsearch.client.Client; +import org.elasticsearch.client.transport.TransportClient; 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.common.transport.InetSocketTransportAddress; import org.elasticsearch.node.Node; import org.elasticsearch.node.NodeBuilder; import org.picocontainer.Startable; @@ -57,7 +59,7 @@ public class ESNode implements Startable { private final String healthTimeout; // available only after startup - private Node node; + private Client client; public ESNode(ServerFileSystem fileSystem, Settings settings) { this(fileSystem, settings, DEFAULT_HEALTH_TIMEOUT); @@ -88,30 +90,32 @@ public class ESNode implements Startable { .put("script.default_lang", "native") .put("script.native." + ListUpdate.NAME + ".type", UpdateListScriptFactory.class.getName()); - switch (type) { - case MEMORY: + initAnalysis(esSettings); + + + if (IndexProperties.ES_TYPE.TRANSPORT.equals(type)) { + client = new TransportClient(esSettings) + .addTransportAddress(new InetSocketTransportAddress("localhost", + settings.getInt(IndexProperties.NODE_PORT))); + } else { + if (IndexProperties.ES_TYPE.MEMORY.equals(type)) { initMemoryES(esSettings); - break; - case TRANSPORT: - initTransportES(esSettings); - break; - case DATA: - default: + } else if (IndexProperties.ES_TYPE.DATA.equals(type)) { initDataES(esSettings); - break; - } + } + initDirs(esSettings); + initRestConsole(esSettings); + initNetwork(esSettings); - initAnalysis(esSettings); - initDirs(esSettings); - initRestConsole(esSettings); - initNetwork(esSettings); + Node node = NodeBuilder.nodeBuilder() + .settings(esSettings) + .node(); + node.start(); - node = NodeBuilder.nodeBuilder() - .settings(esSettings) - .node(); - node.start(); + client = node.client(); + } - if (node.client().admin().cluster().prepareHealth() + if (client.admin().cluster().prepareHealth() .setWaitForYellowStatus() .setTimeout(healthTimeout) .get() @@ -156,7 +160,7 @@ public class ESNode implements Startable { } private void addIndexTemplates() { - node.client().admin().indices() + client.admin().indices() .preparePutTemplate("default") .setTemplate("*") .addMapping("_default_", "{\"dynamic\": \"strict\"}") @@ -171,40 +175,40 @@ public class ESNode implements Startable { esSettings .put("index.mapper.dynamic", false) - // Sortable text analyzer + // Sortable text analyzer .put("index.analysis.analyzer.sortable.type", "custom") .put("index.analysis.analyzer.sortable.tokenizer", "keyword") .putArray("index.analysis.analyzer.sortable.filter", "trim", "lowercase", "truncate") - // Edge NGram index-analyzer + // Edge NGram index-analyzer .put("index.analysis.analyzer.index_grams.type", "custom") .put("index.analysis.analyzer.index_grams.tokenizer", "whitespace") .putArray("index.analysis.analyzer.index_grams.filter", "trim", "lowercase", "gram_filter") - // Edge NGram search-analyzer + // Edge NGram search-analyzer .put("index.analysis.analyzer.search_grams.type", "custom") .put("index.analysis.analyzer.search_grams.tokenizer", "whitespace") .putArray("index.analysis.analyzer.search_grams.filter", "trim", "lowercase") - // Word index-analyzer + // Word index-analyzer .put("index.analysis.analyzer.index_words.type", "custom") .put("index.analysis.analyzer.index_words.tokenizer", "standard") .putArray("index.analysis.analyzer.index_words.filter", "standard", "word_filter", "lowercase", "stop", "asciifolding", "porter_stem") - // Word search-analyzer + // Word search-analyzer .put("index.analysis.analyzer.search_words.type", "custom") .put("index.analysis.analyzer.search_words.tokenizer", "standard") .putArray("index.analysis.analyzer.search_words.filter", "standard", "lowercase", "stop", "asciifolding", "porter_stem") - // Edge NGram filter + // Edge NGram filter .put("index.analysis.filter.gram_filter.type", "edgeNGram") .put("index.analysis.filter.gram_filter.min_gram", 2) .put("index.analysis.filter.gram_filter.max_gram", 15) .putArray("index.analysis.filter.gram_filter.token_chars", "letter", "digit", "punctuation", "symbol") - // Word filter + // Word filter .put("index.analysis.filter.word_filter.type", "word_delimiter") .put("index.analysis.filter.word_filter.generate_word_parts", true) .put("index.analysis.filter.word_filter.catenate_words", true) @@ -215,7 +219,7 @@ public class ESNode implements Startable { .put("index.analysis.filter.word_filter.split_on_numerics", true) .put("index.analysis.filter.word_filter.stem_english_possessive", true) - // Path Analyzer + // Path Analyzer .put("index.analysis.analyzer.path_analyzer.type", "custom") .put("index.analysis.analyzer.path_analyzer.tokenizer", "path_hierarchy"); @@ -250,16 +254,16 @@ public class ESNode implements Startable { @Override public void stop() { - if (node != null) { - node.close(); - node = null; + if (client != null) { + client.close(); + client = null; } } public Client client() { - if (node == null) { + if (client == null) { throw new IllegalStateException("Elasticsearch is not started"); } - return node.client(); + return client; } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/IndexProperties.java b/server/sonar-server/src/main/java/org/sonar/server/search/IndexProperties.java index a2f67178aca..dd6633d5708 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/IndexProperties.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/IndexProperties.java @@ -29,6 +29,7 @@ public final class IndexProperties { MEMORY, TRANSPORT, DATA } - public static final String TYPE = "sonar.es.type"; - public static final String HTTP_PORT = "sonar.es.http.port"; +public static final String TYPE = "sonar.es.type"; +public static final String HTTP_PORT = "sonar.es.http.port"; +public static final String NODE_PORT = "sonar.es.node.port"; } -- 2.39.5