diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-07-30 13:34:50 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2014-07-30 13:34:50 +0200 |
commit | 26bfa823f22237d7d40cb927f84c840a2d72b37e (patch) | |
tree | d1ce365d317e8f6de83ab1236c54a680fddabf96 /server/sonar-search | |
parent | 41e12aea73c6a4917d8d877c36f3421e03064e46 (diff) | |
download | sonarqube-26bfa823f22237d7d40cb927f84c840a2d72b37e.tar.gz sonarqube-26bfa823f22237d7d40cb927f84c840a2d72b37e.zip |
SONAR-4898 improve reliability
Diffstat (limited to 'server/sonar-search')
-rw-r--r-- | server/sonar-search/src/main/java/org/sonar/search/SearchServer.java (renamed from server/sonar-search/src/main/java/org/sonar/search/ElasticSearch.java) | 141 | ||||
-rw-r--r-- | server/sonar-search/src/main/resources/logback.xml | 12 | ||||
-rw-r--r-- | server/sonar-search/src/test/java/org/sonar/search/SearchServerTest.java (renamed from server/sonar-search/src/test/java/org/sonar/search/ElasticSearchTest.java) | 32 |
3 files changed, 84 insertions, 101 deletions
diff --git a/server/sonar-search/src/main/java/org/sonar/search/ElasticSearch.java b/server/sonar-search/src/main/java/org/sonar/search/SearchServer.java index bfdb5c909a8..6789aa650f7 100644 --- a/server/sonar-search/src/main/java/org/sonar/search/ElasticSearch.java +++ b/server/sonar-search/src/main/java/org/sonar/search/SearchServer.java @@ -26,13 +26,13 @@ import org.elasticsearch.node.Node; import org.elasticsearch.node.NodeBuilder; import org.slf4j.LoggerFactory; import org.sonar.process.ConfigurationUtils; -import org.sonar.process.Process; +import org.sonar.process.MonitoredProcess; import org.sonar.process.Props; import org.sonar.search.script.ListUpdate; import java.io.File; -public class ElasticSearch extends Process { +public class SearchServer extends MonitoredProcess { public static final String ES_DEBUG_PROPERTY = "esDebug"; public static final String ES_PORT_PROPERTY = "sonar.search.port"; @@ -40,84 +40,26 @@ public class ElasticSearch extends Process { private Node node; - ElasticSearch(Props props) { + SearchServer(Props props) throws Exception { super(props); } @Override - public boolean isReady() { - try { - return (node.client().admin().cluster().prepareHealth() - .setWaitForYellowStatus() - .setTimeout(TimeValue.timeValueSeconds(3L)) - .get() - .getStatus() != ClusterHealthStatus.RED); - } catch (Exception e) { - return false; - } - } - - private void initAnalysis(ImmutableSettings.Builder esSettings) { - esSettings - .put("index.mapper.dynamic", false) - - // 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 - .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 - .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 - .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 - .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 - .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 - .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) - .put("index.analysis.filter.word_filter.catenate_numbers", true) - .put("index.analysis.filter.word_filter.catenate_all", true) - .put("index.analysis.filter.word_filter.split_on_case_change", true) - .put("index.analysis.filter.word_filter.preserve_original", true) - .put("index.analysis.filter.word_filter.split_on_numerics", true) - .put("index.analysis.filter.word_filter.stem_english_possessive", true) - - // Path Analyzer - .put("index.analysis.analyzer.path_analyzer.type", "custom") - .put("index.analysis.analyzer.path_analyzer.tokenizer", "path_hierarchy"); - + protected boolean doIsReady() { + return (node.client().admin().cluster().prepareHealth() + .setWaitForYellowStatus() + .setTimeout(TimeValue.timeValueSeconds(3L)) + .get() + .getStatus() != ClusterHealthStatus.RED); } @Override - public void onStart() { + protected void doStart() { String dataDir = props.of("sonar.path.data"); Integer port = props.intOf(ES_PORT_PROPERTY); String clusterName = props.of(ES_CLUSTER_PROPERTY); - LoggerFactory.getLogger(ElasticSearch.class).info("Starting ES[{}] on port: {}", clusterName, port); + LoggerFactory.getLogger(SearchServer.class).info("Starting ES[{}] on port: {}", clusterName, port); ImmutableSettings.Builder esSettings = ImmutableSettings.settingsBuilder() .put("es.foreground", "yes") @@ -165,15 +107,70 @@ public class ElasticSearch extends Process { } } - public void onTerminate() { + private void initAnalysis(ImmutableSettings.Builder esSettings) { + esSettings + .put("index.mapper.dynamic", false) + + // 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 + .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 + .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 + .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 + .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 + .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 + .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) + .put("index.analysis.filter.word_filter.catenate_numbers", true) + .put("index.analysis.filter.word_filter.catenate_all", true) + .put("index.analysis.filter.word_filter.split_on_case_change", true) + .put("index.analysis.filter.word_filter.preserve_original", true) + .put("index.analysis.filter.word_filter.split_on_numerics", true) + .put("index.analysis.filter.word_filter.stem_english_possessive", true) + + // Path Analyzer + .put("index.analysis.analyzer.path_analyzer.type", "custom") + .put("index.analysis.analyzer.path_analyzer.tokenizer", "path_hierarchy"); + + } + + @Override + protected void doTerminate() { if (node != null && !node.isClosed()) { node.close(); node = null; } } - public static void main(String... args) throws InterruptedException { + public static void main(String... args) throws Exception { Props props = ConfigurationUtils.loadPropsFromCommandLineArgs(args); - new ElasticSearch(props).start(); + new SearchServer(props).start(); } } diff --git a/server/sonar-search/src/main/resources/logback.xml b/server/sonar-search/src/main/resources/logback.xml index 1800be76f8f..648ece82e28 100644 --- a/server/sonar-search/src/main/resources/logback.xml +++ b/server/sonar-search/src/main/resources/logback.xml @@ -27,21 +27,9 @@ </encoder> </appender> - <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> - <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> - <level>WARN</level> - </filter> - <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> - <pattern> - %d{yyyy.MM.dd HH:mm:ss} %-5level %msg%n - </pattern> - </encoder> - </appender> - <root> <level value="INFO"/> <appender-ref ref="LOGFILE"/> - <appender-ref ref="CONSOLE"/> </root> </configuration> diff --git a/server/sonar-search/src/test/java/org/sonar/search/ElasticSearchTest.java b/server/sonar-search/src/test/java/org/sonar/search/SearchServerTest.java index 4816e77bbdd..a97c51a9c62 100644 --- a/server/sonar-search/src/test/java/org/sonar/search/ElasticSearchTest.java +++ b/server/sonar-search/src/test/java/org/sonar/search/SearchServerTest.java @@ -28,26 +28,27 @@ import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.sonar.process.Process; +import org.sonar.process.JmxUtils; +import org.sonar.process.MonitoredProcess; import org.sonar.process.Props; import javax.management.InstanceNotFoundException; import javax.management.MBeanRegistrationException; import javax.management.MBeanServer; + import java.io.File; import java.io.IOException; import java.lang.management.ManagementFactory; import java.net.ServerSocket; -import java.net.SocketException; import java.util.Properties; import static org.fest.assertions.Assertions.assertThat; import static org.junit.Assert.fail; -public class ElasticSearchTest { +public class SearchServerTest { File tempDirectory; - ElasticSearch elasticSearch; + SearchServer searchServer; int freePort; int freeESPort; @@ -64,7 +65,6 @@ public class ElasticSearchTest { socket.close(); } - @After public void tearDown() throws MBeanRegistrationException, InstanceNotFoundException { resetMBeanServer(); @@ -73,32 +73,32 @@ public class ElasticSearchTest { private void resetMBeanServer() throws MBeanRegistrationException, InstanceNotFoundException { try { MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); - mbeanServer.unregisterMBean(Process.objectNameFor("ES")); + mbeanServer.unregisterMBean(JmxUtils.objectName("ES")); } catch (Exception e) { e.printStackTrace(); } } @Test - public void can_connect() throws SocketException { + public void can_connect() throws Exception { Properties properties = new Properties(); - properties.setProperty(Process.NAME_PROPERTY, "ES"); + properties.setProperty(MonitoredProcess.NAME_PROPERTY, "ES"); properties.setProperty("sonar.path.data", tempDirectory.getAbsolutePath()); properties.setProperty("sonar.path.logs", tempDirectory.getAbsolutePath()); - properties.setProperty(ElasticSearch.ES_PORT_PROPERTY, Integer.toString(freeESPort)); - properties.setProperty(ElasticSearch.ES_CLUSTER_PROPERTY, "sonarqube"); + properties.setProperty(SearchServer.ES_PORT_PROPERTY, Integer.toString(freeESPort)); + properties.setProperty(SearchServer.ES_CLUSTER_PROPERTY, "sonarqube"); - elasticSearch = new ElasticSearch(new Props(properties)); + searchServer = new SearchServer(new Props(properties)); new Thread(new Runnable() { @Override public void run() { - elasticSearch.start(); + searchServer.start(); } }).start(); - assertThat(elasticSearch.isReady()).isFalse(); + assertThat(searchServer.isReady()).isFalse(); int count = 0; - while (!elasticSearch.isReady() && count < 100) { + while (!searchServer.isReady() && count < 100) { try { Thread.sleep(500); } catch (InterruptedException e) { @@ -114,13 +114,11 @@ public class ElasticSearchTest { TransportClient client = new TransportClient(settings) .addTransportAddress(new InetSocketTransportAddress("localhost", freeESPort)); - // 0 assert that we have a OK cluster available assertThat(client.admin().cluster().prepareClusterStats().get().getStatus()).isEqualTo(ClusterHealthStatus.GREEN); - // 2 assert that we can shut down ES - elasticSearch.terminate(); + searchServer.terminate(); try { client.admin().cluster().prepareClusterStats().get().getStatus(); fail(); |