From: Daniel Schwarz Date: Thu, 16 Feb 2017 14:49:18 +0000 (+0100) Subject: SONAR-7282 only reindex components if the index is empty X-Git-Tag: 6.3.0.18587~7 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b4b1940277e877c853fbe2b32696c3cb1d50f816;p=sonarqube.git SONAR-7282 only reindex components if the index is empty --- 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 285e285cc66..d0a4b9ffc4e 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 @@ -19,6 +19,7 @@ */ package org.sonar.server.component.index; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Throwables; import com.google.common.util.concurrent.Uninterruptibles; import java.util.Arrays; @@ -58,12 +59,26 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe this.esClient = esClient; } + /** + * Make sure, that the component index is filled. + */ + public void index() { + if (isEmpty()) { + reindex(); + } + } + + @VisibleForTesting + boolean isEmpty() { + return esClient.prepareSearch(INDEX_COMPONENTS).setTypes(TYPE_COMPONENT).setSize(0).get().getHits().getTotalHits() <= 0; + } + /** * Copy all components of all projects to the elastic search index. *

* Warning: This should only be called on an empty index and it should only be called during startup. */ - public void index() { + private void reindex() { doIndexByProjectUuid(null); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java index 1d1add24b1f..9175e225d5e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/index/ComponentIndexerTest.java @@ -32,11 +32,17 @@ import org.sonar.db.component.ComponentTesting; import org.sonar.db.component.ComponentUpdateDto; import org.sonar.db.organization.OrganizationDto; import org.sonar.db.organization.OrganizationTesting; +import org.sonar.server.es.EsClient; import org.sonar.server.es.EsTester; import org.sonar.server.es.ProjectIndexer; import static org.assertj.core.api.Assertions.assertThat; import static org.elasticsearch.index.query.QueryBuilders.termQuery; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_NAME; import static org.sonar.server.component.index.ComponentIndexDefinition.INDEX_COMPONENTS; import static org.sonar.server.component.index.ComponentIndexDefinition.TYPE_COMPONENT; @@ -143,6 +149,45 @@ public class ComponentIndexerTest { assertMatches("NewFile", 1); } + @Test + public void full_reindexing_on_empty_index() { + + // insert + ComponentDto project = dbTester.components().insertProject(); + dbTester.components().insertComponent(ComponentTesting.newFileDto(project).setName("OldFile")); + + // verify insert + index(); + assertMatches("OldFile", 1); + } + + @Test + public void full_reindexing_should_not_do_anything_if_index_is_not_empty() { + EsClient esMock = mock(EsClient.class); + + // attempt to start indexing + ComponentIndexer indexer = spy(new ComponentIndexer(dbClient, esMock)); + doReturn(false).when(indexer).isEmpty(); + indexer.index(); + + // verify, that index has not been altered + verify(indexer).index(); + verify(indexer).isEmpty(); + verifyNoMoreInteractions(indexer); + verifyNoMoreInteractions(esMock); + } + + @Test + public void isEmpty_should_return_true_if_index_is_empty() { + assertThat(createIndexer().isEmpty()).isTrue(); + } + + @Test + public void isEmpty_should_return_false_if_index_is_not_empty() { + index_one_project(); + assertThat(createIndexer().isEmpty()).isFalse(); + } + private void insert(ComponentDto component) { dbTester.components().insertComponent(component); }