From 171cc1e06ec0a10cf2383184c1441db985698858 Mon Sep 17 00:00:00 2001 From: Jacek Date: Thu, 31 Mar 2022 17:10:01 +0200 Subject: [PATCH] Fix Elasticsearch UT failing do incorrect order whne pruning data for each test case ES is performing soft deletes, which can affect query score results, recreating indexes fixes that problem --- .../java/org/sonar/server/es/EsTester.java | 11 ++++++++++- .../component/index/ComponentIndexScoreTest.java | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) 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 5ce592554f6..9496da0b328 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 @@ -45,6 +45,7 @@ import org.apache.http.HttpHost; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; import org.elasticsearch.action.bulk.BulkRequest; @@ -170,6 +171,12 @@ public class EsTester extends ExternalResource { return new EsTester(true); } + public void recreateIndexes() { + deleteIndexIfExists(ALL_INDICES.getName()); + CORE_INDICES_CREATED.set(false); + create(); + } + @Override protected void after() { if (isCustom) { @@ -185,7 +192,9 @@ public class EsTester extends ExternalResource { private void deleteAllDocumentsInIndexes() { try { - ES_REST_CLIENT.nativeClient().deleteByQuery(new DeleteByQueryRequest(ALL_INDICES.getName()).setQuery(QueryBuilders.matchAllQuery()).setRefresh(true), RequestOptions.DEFAULT); + ES_REST_CLIENT.nativeClient() + .deleteByQuery(new DeleteByQueryRequest(ALL_INDICES.getName()).setQuery(QueryBuilders.matchAllQuery()).setRefresh(true).setWaitForActiveShards(1), RequestOptions.DEFAULT); + ES_REST_CLIENT.forcemerge(new ForceMergeRequest()); } catch (IOException e) { throw new IllegalStateException("Could not delete data from _all indices", e); } diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java index 8c7a547251e..3fbf4f34622 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/component/index/ComponentIndexScoreTest.java @@ -48,6 +48,7 @@ public class ComponentIndexScoreTest extends ComponentIndexTest { @Test public void should_prefer_key_matching_over_name_matching() { + es.recreateIndexes(); ComponentDto project1 = indexProject("quality", "SonarQube"); ComponentDto project2 = indexProject("sonarqube", "Quality Product"); -- 2.39.5