diff options
author | Daniel Schwarz <bartfastiel@users.noreply.github.com> | 2017-02-21 11:09:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-21 11:09:35 +0100 |
commit | 3716bfea7ebed5a9f50e11157c2adfbfce40d6e5 (patch) | |
tree | 5a7f9b761c5c2e35443d745b6a173690b1c84586 /server/sonar-server | |
parent | eb089aeca110589de27bce9cf8e1fb4635d18ecc (diff) | |
download | sonarqube-3716bfea7ebed5a9f50e11157c2adfbfce40d6e5.tar.gz sonarqube-3716bfea7ebed5a9f50e11157c2adfbfce40d6e5.zip |
SONAR-8809 always update component index, instead of drop+reindex
Diffstat (limited to 'server/sonar-server')
4 files changed, 43 insertions, 12 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java index d0a4b9ffc4e..93654370790 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/index/ComponentIndexer.java @@ -23,6 +23,7 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Throwables; import com.google.common.util.concurrent.Uninterruptibles; import java.util.Arrays; +import java.util.Collection; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; @@ -88,7 +89,6 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe case PROJECT_CREATION: case PROJECT_KEY_UPDATE: case NEW_ANALYSIS: - deleteProject(projectUuid); doIndexByProjectUuid(projectUuid); break; default: @@ -131,6 +131,13 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe termQuery(ComponentIndexDefinition.FIELD_PROJECT_UUID, projectUuid)))); } + public void delete(String projectUuid, Collection<String> disabledComponentUuids) { + BulkIndexer bulk = new BulkIndexer(esClient, INDEX_COMPONENTS); + bulk.start(); + disabledComponentUuids.stream().forEach(uuid -> bulk.addDeletion(INDEX_COMPONENTS, TYPE_COMPONENT, uuid, projectUuid)); + bulk.stop(); + } + void index(ComponentDto... docs) { Future<?> submit = executor.submit(() -> indexNow(docs)); try { diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/dbcleaner/IndexPurgeListener.java b/server/sonar-server/src/main/java/org/sonar/server/computation/dbcleaner/IndexPurgeListener.java index 55909ffd52d..bc288db7380 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/dbcleaner/IndexPurgeListener.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/dbcleaner/IndexPurgeListener.java @@ -19,9 +19,11 @@ */ package org.sonar.server.computation.dbcleaner; +import java.util.Collection; import java.util.List; import org.sonar.api.server.ServerSide; import org.sonar.db.purge.PurgeListener; +import org.sonar.server.component.index.ComponentIndexer; import org.sonar.server.issue.index.IssueIndexer; import org.sonar.server.test.index.TestIndexer; @@ -29,14 +31,21 @@ import org.sonar.server.test.index.TestIndexer; public class IndexPurgeListener implements PurgeListener { private final TestIndexer testIndexer; private final IssueIndexer issueIndexer; + private final ComponentIndexer componentIndexer; - public IndexPurgeListener(TestIndexer testIndexer, IssueIndexer issueIndexer) { + public IndexPurgeListener(TestIndexer testIndexer, IssueIndexer issueIndexer, ComponentIndexer componentIndexer) { this.testIndexer = testIndexer; this.issueIndexer = issueIndexer; + this.componentIndexer = componentIndexer; } @Override - public void onComponentDisabling(String uuid) { + public void onComponentsDisabling(String projectUuid, Collection<String> disabledComponentUuids) { + componentIndexer.delete(projectUuid, disabledComponentUuids); + disabledComponentUuids.forEach(this::onComponentDisabling); + } + + private void onComponentDisabling(String uuid) { testIndexer.deleteByFile(uuid); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/es/BulkIndexer.java b/server/sonar-server/src/main/java/org/sonar/server/es/BulkIndexer.java index b3cb11a5ae6..0aaf343869b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/es/BulkIndexer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/es/BulkIndexer.java @@ -32,7 +32,6 @@ import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.bulk.BulkResponse; -import org.elasticsearch.action.delete.DeleteRequestBuilder; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.cluster.metadata.IndexMetaData; @@ -128,7 +127,7 @@ public class BulkIndexer implements Startable { progress.start(); } - public void add(ActionRequest request) { + public void add(ActionRequest<?> request) { bulkRequest.request().add(request); if (bulkRequest.request().estimatedSizeInBytes() >= flushByteSize) { executeBulk(); @@ -152,12 +151,12 @@ public class BulkIndexer implements Startable { while (true) { SearchHit[] hits = searchResponse.getHits().getHits(); for (SearchHit hit : hits) { - DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete(hit.index(), hit.type(), hit.getId()); SearchHitField routing = hit.field("_routing"); - if (routing != null) { - deleteRequestBuilder.setRouting(routing.getValue()); + if (routing == null) { + addDeletion(hit.index(), hit.type(), hit.getId()); + } else { + addDeletion(hit.index(), hit.type(), hit.getId(), routing.getValue()); } - add(deleteRequestBuilder.request()); } String scrollId = searchResponse.getScrollId(); @@ -169,6 +168,14 @@ public class BulkIndexer implements Startable { } } + public void addDeletion(String index, String type, String id) { + add(client.prepareDelete(index, type, id).request()); + } + + public void addDeletion(String index, String type, String id, String routing) { + add(client.prepareDelete(index, type, id).setRouting(routing).request()); + } + /** * Delete all the documents matching the given search request. This method is blocking. * Index is refreshed, so docs are not searchable as soon as method is executed. diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/dbcleaner/IndexPurgeListenerTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/dbcleaner/IndexPurgeListenerTest.java index b79895cfcee..5ac5980c7c1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/dbcleaner/IndexPurgeListenerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/dbcleaner/IndexPurgeListenerTest.java @@ -19,7 +19,10 @@ */ package org.sonar.server.computation.dbcleaner; +import java.util.Arrays; +import java.util.List; import org.junit.Test; +import org.sonar.server.component.index.ComponentIndexer; import org.sonar.server.issue.index.IssueIndexer; import org.sonar.server.test.index.TestIndexer; @@ -31,14 +34,19 @@ public class IndexPurgeListenerTest { TestIndexer testIndexer = mock(TestIndexer.class); IssueIndexer issueIndexer = mock(IssueIndexer.class); + ComponentIndexer componentIndexer = mock(ComponentIndexer.class); - IndexPurgeListener underTest = new IndexPurgeListener(testIndexer, issueIndexer); + IndexPurgeListener underTest = new IndexPurgeListener(testIndexer, issueIndexer, componentIndexer); @Test public void test_onComponentDisabling() { - underTest.onComponentDisabling("123456"); + String uuid = "123456"; + String projectUuid = "P789"; + List<String> uuids = Arrays.asList(uuid); + underTest.onComponentsDisabling(projectUuid, uuids); - verify(testIndexer).deleteByFile("123456"); + verify(testIndexer).deleteByFile(uuid); + verify(componentIndexer).delete(projectUuid, uuids); } @Test |