diff options
author | Daniel Schwarz <bartfastiel@users.noreply.github.com> | 2017-02-16 15:49:18 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-16 15:49:18 +0100 |
commit | b4b1940277e877c853fbe2b32696c3cb1d50f816 (patch) | |
tree | 1ac68ea775e2644cd0e812d4bb281c0493b907b7 /server/sonar-server/src | |
parent | cdc1c25040fccdf84b46250fa5ad84bb1dde252a (diff) | |
download | sonarqube-b4b1940277e877c853fbe2b32696c3cb1d50f816.tar.gz sonarqube-b4b1940277e877c853fbe2b32696c3cb1d50f816.zip |
SONAR-7282 only reindex components if the index is empty
Diffstat (limited to 'server/sonar-server/src')
2 files changed, 61 insertions, 1 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 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; @@ -59,11 +60,25 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe } /** + * 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. * <p> * <b>Warning</b>: 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); } |