From 86f2c502104fbc03eb5aac19598e9b5c2e7290b8 Mon Sep 17 00:00:00 2001 From: Jacek Date: Wed, 8 Sep 2021 11:14:57 +0200 Subject: [PATCH] SONAR-15375 Fix SSF-191 --- build.gradle | 7 +++-- gradle.properties | 4 +-- .../sonar/application/es/EsConnectorImpl.java | 2 +- .../application/process/EsManagedProcess.java | 8 ++++++ .../application/es/EsConnectorImplTest.java | 26 +++++++++++++++++- .../process/EsManagedProcessTest.java | 27 +++++++++++++++---- .../java/org/sonar/server/es/BulkIndexer.java | 2 +- .../java/org/sonar/server/es/EsUtils.java | 2 +- .../sonar/server/rule/index/RuleIndex.java | 2 +- .../sonar/server/view/index/ViewIndex.java | 2 +- .../sonar/server/es/EsRequestDetailsTest.java | 2 +- .../java/org/sonar/server/es/EsTester.java | 2 +- sonar-application/build.gradle | 4 +-- 13 files changed, 69 insertions(+), 21 deletions(-) diff --git a/build.gradle b/build.gradle index 221ace3f811..b0358ae1cee 100644 --- a/build.gradle +++ b/build.gradle @@ -393,13 +393,12 @@ subprojects { entry 'jetty-server' entry 'jetty-servlet' } - dependency('org.elasticsearch.client:elasticsearch-rest-high-level-client:7.13.3') { + dependency('org.elasticsearch.client:elasticsearch-rest-high-level-client:7.14.1') { exclude 'commons-logging:commons-logging' } - dependency 'org.elasticsearch.plugin:transport-netty4-client:7.13.3' + dependency 'org.elasticsearch.plugin:transport-netty4-client:7.14.1' dependency 'org.elasticsearch:mocksocket:1.0' - //analysis-common need to stay at 7.10.2 as it hasn't been published to mvn central - dependency 'org.codelibs.elasticsearch.module:analysis-common:7.10.2' + dependency 'org.codelibs.elasticsearch.module:analysis-common:7.14.1' dependency 'org.eclipse.jgit:org.eclipse.jgit:5.11.0.202103091610-r' dependency 'org.tmatesoft.svnkit:svnkit:1.10.1' dependency 'org.hamcrest:hamcrest-all:1.3' diff --git a/gradle.properties b/gradle.properties index ffff9478d13..4b3df4d87c1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,5 +11,5 @@ org.gradle.vfs.watch=true # https://www.elastic.co/downloads/elasticsearch-no-jdk elasticsearchDownloadUrlPath=https://artifacts.elastic.co/downloads/elasticsearch/ elasticsearchDownloadRepoxUrlPath=https://repox.jfrog.io/artifactory/sonarsource-bucket/sonarqube/elasticsearch/ -elasticsearchDownloadUrlFile=elasticsearch-7.13.3-no-jdk-linux-x86_64.tar.gz -elasticsearchDownloadSha512=b79c249b7fa181d56a62ae9997f14dac8a1bd64199573bc08a75dd344241d762f05405f391746492ad3843f097a79980a21ef99173c6c1eebe8d51541f20c01b +elasticsearchDownloadUrlFile=elasticsearch-7.14.1-no-jdk-linux-x86_64.tar.gz +elasticsearchDownloadSha512=77dca78ba865ae74863b3b2a3cd61e8a8e4478cd02eb020184dbf89fa32cf145a6bbd1d11a1cb88c2236a3b8cdb8b0047e3c0f1a40f609f31b898c905b2c211d diff --git a/server/sonar-main/src/main/java/org/sonar/application/es/EsConnectorImpl.java b/server/sonar-main/src/main/java/org/sonar/application/es/EsConnectorImpl.java index 1c5a3263df1..59bc2f9b75d 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/es/EsConnectorImpl.java +++ b/server/sonar-main/src/main/java/org/sonar/application/es/EsConnectorImpl.java @@ -41,7 +41,7 @@ import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds; +import static org.elasticsearch.core.TimeValue.timeValueSeconds; public class EsConnectorImpl implements EsConnector { private static final String ES_USERNAME = "elastic"; diff --git a/server/sonar-main/src/main/java/org/sonar/application/process/EsManagedProcess.java b/server/sonar-main/src/main/java/org/sonar/application/process/EsManagedProcess.java index bf5bad5d183..2917485d8df 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/process/EsManagedProcess.java +++ b/server/sonar-main/src/main/java/org/sonar/application/process/EsManagedProcess.java @@ -20,6 +20,7 @@ package org.sonar.application.process; import java.util.concurrent.atomic.AtomicBoolean; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.rest.RestStatus; @@ -96,6 +97,13 @@ public class EsManagedProcess extends AbstractManagedProcess { } } return KO; + } catch (ElasticsearchException e) { + if (e.status() == RestStatus.INTERNAL_SERVER_ERROR && e.getMessage().contains("Connection refused")) { + return CONNECTION_REFUSED; + } else { + LOG.error("Failed to check status", e); + } + return KO; } catch (Exception e) { LOG.error("Failed to check status", e); return KO; diff --git a/server/sonar-main/src/test/java/org/sonar/application/es/EsConnectorImplTest.java b/server/sonar-main/src/test/java/org/sonar/application/es/EsConnectorImplTest.java index 9c5e3d0d914..a9d473d9cc8 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/es/EsConnectorImplTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/es/EsConnectorImplTest.java @@ -56,6 +56,24 @@ public class EsConnectorImplTest { " \"error\" : \"i-have-a-bad-feelings-about-this\"" + "}"; + private static final String ES_INFO_RESPONSE = "{" + + " \"name\" : \"sonarqube\"," + + " \"cluster_name\" : \"sonarqube\"," + + " \"cluster_uuid\" : \"6Oj9lFIyQVa_d5HgQWqQpA\"," + + " \"version\" : {" + + " \"number\" : \"7.14.1\"," + + " \"build_flavor\" : \"default\"," + + " \"build_type\" : \"tar\"," + + " \"build_hash\" : \"66b55ebfa59c92c15db3f69a335d500018b3331e\"," + + " \"build_date\" : \"2021-08-26T09:01:05.390870785Z\"," + + " \"build_snapshot\" : false," + + " \"lucene_version\" : \"8.9.0\"," + + " \"minimum_wire_compatibility_version\" : \"6.8.0\"," + + " \"minimum_index_compatibility_version\" : \"6.0.0-beta1\"" + + " }," + + " \"tagline\" : \"You Know, for Search\"" + + "}"; + @Rule public MockWebServer mockWebServer = new MockWebServer(); @@ -95,10 +113,16 @@ public class EsConnectorImplTest { } private void mockServerResponse(int httpCode, String jsonResponse) { + mockWebServer.enqueue(new MockResponse() + .setResponseCode(200) + .setBody(ES_INFO_RESPONSE) + .setHeader("Content-Type", "application/json") + .setHeader("X-elastic-product", "Elasticsearch")); mockWebServer.enqueue(new MockResponse() .setResponseCode(httpCode) .setBody(jsonResponse) - .setHeader("Content-Type", "application/json")); + .setHeader("Content-Type", "application/json") + .setHeader("X-elastic-product", "Elasticsearch")); } } diff --git a/server/sonar-main/src/test/java/org/sonar/application/process/EsManagedProcessTest.java b/server/sonar-main/src/test/java/org/sonar/application/process/EsManagedProcessTest.java index d097fa39f53..72c475118dc 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/process/EsManagedProcessTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/process/EsManagedProcessTest.java @@ -26,6 +26,7 @@ import ch.qos.logback.core.AppenderBase; import java.util.ArrayList; import java.util.List; import java.util.Optional; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.rest.RestStatus; @@ -103,6 +104,24 @@ public class EsManagedProcessTest { assertThat(underTest.isOperational()).isFalse(); } + @Test + public void isOperational_should_return_false_if_ElasticsearchException_with_connection_refused_thrown() { + EsConnector esConnector = mock(EsConnector.class); + when(esConnector.getClusterHealthStatus()) + .thenThrow(new ElasticsearchException("Connection refused")); + EsManagedProcess underTest = new EsManagedProcess(mock(Process.class), ProcessId.ELASTICSEARCH, esConnector); + assertThat(underTest.isOperational()).isFalse(); + } + + @Test + public void isOperational_should_return_false_if_ElasticsearchException_thrown() { + EsConnector esConnector = mock(EsConnector.class); + when(esConnector.getClusterHealthStatus()) + .thenThrow(new ElasticsearchException("test")); + EsManagedProcess underTest = new EsManagedProcess(mock(Process.class), ProcessId.ELASTICSEARCH, esConnector); + assertThat(underTest.isOperational()).isFalse(); + } + @Test public void isOperational_must_log_once_when_master_is_not_elected() { MemoryAppender memoryAppender = new MemoryAppender<>(); @@ -122,20 +141,18 @@ public class EsManagedProcessTest { assertThat(memoryAppender.events) .extracting(ILoggingEvent::getLevel, ILoggingEvent::getMessage) .containsOnlyOnce( - tuple(Level.INFO, "Elasticsearch is waiting for a master to be elected. Did you start all the search nodes ?") - ); + tuple(Level.INFO, "Elasticsearch is waiting for a master to be elected. Did you start all the search nodes ?")); // Second call must not log another message assertThat(underTest.isOperational()).isFalse(); assertThat(memoryAppender.events) .extracting(ILoggingEvent::getLevel, ILoggingEvent::getMessage) .containsOnlyOnce( - tuple(Level.INFO, "Elasticsearch is waiting for a master to be elected. Did you start all the search nodes ?") - ); + tuple(Level.INFO, "Elasticsearch is waiting for a master to be elected. Did you start all the search nodes ?")); } private static class MemoryAppender extends AppenderBase { - private final List events = new ArrayList(); + private final List events = new ArrayList<>(); @Override protected void append(E eventObject) { diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/es/BulkIndexer.java b/server/sonar-server-common/src/main/java/org/sonar/server/es/BulkIndexer.java index 063d35cbfb1..b73ce9d0e4b 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/es/BulkIndexer.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/es/BulkIndexer.java @@ -52,7 +52,7 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.document.DocumentField; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; -import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.sort.SortOrder; import org.sonar.api.utils.log.Logger; diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/es/EsUtils.java b/server/sonar-server-common/src/main/java/org/sonar/server/es/EsUtils.java index e5966a90c6a..204d7966a58 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/es/EsUtils.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/es/EsUtils.java @@ -35,7 +35,7 @@ import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchScrollRequest; -import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.bucket.terms.Terms; diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndex.java b/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndex.java index d618bcc6923..73c7d7a0f80 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndex.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndex.java @@ -33,7 +33,7 @@ import org.apache.commons.lang.StringUtils; import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.Operator; import org.elasticsearch.index.query.QueryBuilder; diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/view/index/ViewIndex.java b/server/sonar-server-common/src/main/java/org/sonar/server/view/index/ViewIndex.java index 773c63fd757..b2567ef8685 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/view/index/ViewIndex.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/view/index/ViewIndex.java @@ -25,7 +25,7 @@ import org.elasticsearch.action.search.ClearScrollRequest; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchScrollRequest; -import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.SortOrder; diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/es/EsRequestDetailsTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/es/EsRequestDetailsTest.java index b0987394f89..8cb3ca582f3 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/es/EsRequestDetailsTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/es/EsRequestDetailsTest.java @@ -31,7 +31,7 @@ import org.elasticsearch.action.search.SearchScrollRequest; import org.elasticsearch.client.Requests; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.GetIndexRequest; -import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.core.TimeValue; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; diff --git a/server/sonar-server-common/src/testFixtures/java/org/sonar/server/es/EsTester.java b/server/sonar-server-common/src/testFixtures/java/org/sonar/server/es/EsTester.java index 611a89c3c47..006254c7c4e 100644 --- a/server/sonar-server-common/src/testFixtures/java/org/sonar/server/es/EsTester.java +++ b/server/sonar-server-common/src/testFixtures/java/org/sonar/server/es/EsTester.java @@ -65,7 +65,7 @@ import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.core.TimeValue; import org.elasticsearch.discovery.DiscoveryModule; import org.elasticsearch.env.Environment; import org.elasticsearch.env.NodeEnvironment; diff --git a/sonar-application/build.gradle b/sonar-application/build.gradle index 652f8febbbe..edaecf7a4c0 100644 --- a/sonar-application/build.gradle +++ b/sonar-application/build.gradle @@ -224,8 +224,8 @@ zip.doFirst { } // Check the size of the archive zip.doLast { - def minLength = 274000000 - def maxLength = 295000000 + def minLength = 295000000 + def maxLength = 310000000 def length = archiveFile.get().asFile.length() if (length < minLength) -- 2.39.5