aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-core/src
diff options
context:
space:
mode:
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>2024-10-31 10:59:48 +0100
committersonartech <sonartech@sonarsource.com>2024-11-05 20:03:02 +0000
commit6b04bfc5da9698344566ee9637a66c9483e967d7 (patch)
treefdd7e87a50ab9fb93891a939466ac4c5beeb9cd9 /server/sonar-webserver-core/src
parent3995d636eca4780bed3b4b891ee3228b2450097b (diff)
downloadsonarqube-6b04bfc5da9698344566ee9637a66c9483e967d7.tar.gz
sonarqube-6b04bfc5da9698344566ee9637a66c9483e967d7.zip
SONAR-23299 Add missing portfolios and views metrics and reindex project measure
Diffstat (limited to 'server/sonar-webserver-core/src')
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/es/MigrationEsClientImpl.java57
-rw-r--r--server/sonar-webserver-core/src/test/java/org/sonar/server/es/MigrationEsClientImplTest.java91
2 files changed, 148 insertions, 0 deletions
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/es/MigrationEsClientImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/es/MigrationEsClientImpl.java
new file mode 100644
index 00000000000..eb8c4e2ae8f
--- /dev/null
+++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/es/MigrationEsClientImpl.java
@@ -0,0 +1,57 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.es;
+
+import java.util.Arrays;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
+import org.elasticsearch.client.indices.GetIndexRequest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.server.platform.db.migration.es.MigrationEsClient;
+
+public class MigrationEsClientImpl implements MigrationEsClient {
+ private static final Logger LOG = LoggerFactory.getLogger(MigrationEsClientImpl.class);
+ private final EsClient client;
+
+ public MigrationEsClientImpl(EsClient client) {
+ this.client = client;
+ }
+
+ @Override
+ public void deleteIndexes(String name, String... otherNames) {
+ String[] indices = client.getIndex(new GetIndexRequest("_all")).getIndices();
+ Set<String> existingIndices = Arrays.stream(indices).collect(Collectors.toSet());
+ String[] toDelete = Stream.concat(Stream.of(name), Arrays.stream(otherNames))
+ .distinct()
+ .filter(existingIndices::contains)
+ .toArray(String[]::new);
+ if (toDelete.length > 0) {
+ deleteIndex(toDelete);
+ }
+ }
+
+ private void deleteIndex(String... indices) {
+ LOG.info("Drop Elasticsearch indices [{}]", String.join(",", indices));
+ client.deleteIndex(new DeleteIndexRequest(indices));
+ }
+}
diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/es/MigrationEsClientImplTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/es/MigrationEsClientImplTest.java
new file mode 100644
index 00000000000..4b42b6d6c47
--- /dev/null
+++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/es/MigrationEsClientImplTest.java
@@ -0,0 +1,91 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.es;
+
+import java.util.Iterator;
+import org.elasticsearch.client.indices.GetMappingsRequest;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.slf4j.event.Level;
+import org.sonar.api.config.internal.MapSettings;
+import org.sonar.api.testfixtures.log.LogTesterJUnit5;
+import org.sonar.server.platform.db.migration.es.MigrationEsClient;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.server.es.newindex.SettingsConfiguration.newBuilder;
+
+class MigrationEsClientImplTest {
+ @RegisterExtension
+ public LogTesterJUnit5 logTester = new LogTesterJUnit5();
+
+ @RegisterExtension
+ public EsTester es = EsTester.createCustom(
+ new SimpleIndexDefinition("as"),
+ new SimpleIndexDefinition("bs"),
+ new SimpleIndexDefinition("cs"));
+
+ private final MigrationEsClient underTest = new MigrationEsClientImpl(es.client());
+
+ @Test
+ void delete_existing_index() {
+ underTest.deleteIndexes("as");
+
+ assertThat(loadExistingIndices())
+ .toIterable()
+ .doesNotContain("as")
+ .contains("bs", "cs");
+ assertThat(logTester.logs(Level.INFO))
+ .contains("Drop Elasticsearch indices [as]");
+ }
+
+ @Test
+ void delete_index_that_does_not_exist() {
+ underTest.deleteIndexes("as", "xxx", "cs");
+
+ assertThat(loadExistingIndices())
+ .toIterable()
+ .doesNotContain("as", "cs")
+ .contains("bs");
+ assertThat(logTester.logs(Level.INFO))
+ .contains("Drop Elasticsearch indices [as,cs]")
+ .doesNotContain("Drop Elasticsearch indices [xxx]");
+ }
+
+ private Iterator<String> loadExistingIndices() {
+ return es.client().getMapping(new GetMappingsRequest()).mappings().keySet().iterator();
+ }
+
+ private static class SimpleIndexDefinition implements IndexDefinition {
+ private final String indexName;
+
+ public SimpleIndexDefinition(String indexName) {
+ this.indexName = indexName;
+ }
+
+ @Override
+ public void define(IndexDefinitionContext context) {
+ IndexType.IndexMainType mainType = IndexType.main(Index.simple(indexName), indexName.substring(1));
+ context.create(
+ mainType.getIndex(),
+ newBuilder(new MapSettings().asConfig()).build())
+ .createTypeMapping(mainType);
+ }
+ }
+}