Search in sources :

Example 11 with IndexingResult

use of org.sonar.server.es.IndexingResult in project sonarqube by SonarSource.

the class ProjectMeasuresIndexerTest method update_index_when_project_is_created.

@Test
public void update_index_when_project_is_created() {
    ComponentDto project = db.components().insertPrivateProject();
    IndexingResult result = indexProject(project, PROJECT_CREATION);
    assertThatIndexContainsOnly(project);
    assertThat(result.getTotal()).isOne();
    assertThat(result.getSuccess()).isOne();
}
Also used : IndexingResult(org.sonar.server.es.IndexingResult) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 12 with IndexingResult

use of org.sonar.server.es.IndexingResult in project sonarqube by SonarSource.

the class ProjectMeasuresIndexerTest method delete_doc_from_index_when_project_is_deleted.

@Test
public void delete_doc_from_index_when_project_is_deleted() {
    ComponentDto project = db.components().insertPrivateProject();
    indexProject(project, PROJECT_CREATION);
    assertThatIndexContainsOnly(project);
    db.getDbClient().purgeDao().deleteProject(db.getSession(), project.uuid(), Qualifiers.PROJECT, project.name(), project.getKey());
    IndexingResult result = indexProject(project, PROJECT_DELETION);
    assertThat(es.countDocuments(TYPE_PROJECT_MEASURES)).isZero();
    assertThat(result.getTotal()).isOne();
    assertThat(result.getSuccess()).isOne();
}
Also used : IndexingResult(org.sonar.server.es.IndexingResult) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 13 with IndexingResult

use of org.sonar.server.es.IndexingResult in project sonarqube by SonarSource.

the class ProjectMeasuresIndexerTest method errors_during_indexing_are_recovered.

@Test
public void errors_during_indexing_are_recovered() {
    ComponentDto project = db.components().insertPrivateProject();
    es.lockWrites(TYPE_PROJECT_MEASURES);
    IndexingResult result = indexProject(project, PROJECT_CREATION);
    assertThat(result.getTotal()).isOne();
    assertThat(result.getFailures()).isOne();
    // index is still read-only, fail to recover
    result = recover();
    assertThat(result.getTotal()).isOne();
    assertThat(result.getFailures()).isOne();
    assertThat(es.countDocuments(TYPE_PROJECT_MEASURES)).isZero();
    assertThatEsQueueTableHasSize(1);
    es.unlockWrites(TYPE_PROJECT_MEASURES);
    result = recover();
    assertThat(result.getTotal()).isOne();
    assertThat(result.getFailures()).isZero();
    assertThatEsQueueTableHasSize(0);
    assertThatIndexContainsOnly(project);
}
Also used : IndexingResult(org.sonar.server.es.IndexingResult) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 14 with IndexingResult

use of org.sonar.server.es.IndexingResult in project sonarqube by SonarSource.

the class PermissionIndexer method index.

@Override
public IndexingResult index(DbSession dbSession, Collection<EsQueueDto> items) {
    IndexingResult result = new IndexingResult();
    List<BulkIndexer> bulkIndexers = items.stream().map(EsQueueDto::getDocType).distinct().map(indexTypeByFormat::get).filter(Objects::nonNull).map(indexType -> new BulkIndexer(esClient, indexType, Size.REGULAR, new OneToOneResilientIndexingListener(dbClient, dbSession, items))).collect(Collectors.toList());
    if (bulkIndexers.isEmpty()) {
        return result;
    }
    bulkIndexers.forEach(BulkIndexer::start);
    PermissionIndexerDao permissionIndexerDao = new PermissionIndexerDao();
    Set<String> remainingProjectUuids = items.stream().map(EsQueueDto::getDocId).map(AuthorizationDoc::projectUuidOf).collect(MoreCollectors.toHashSet());
    permissionIndexerDao.selectByUuids(dbClient, dbSession, remainingProjectUuids).forEach(p -> {
        remainingProjectUuids.remove(p.getProjectUuid());
        bulkIndexers.forEach(bi -> bi.add(AuthorizationDoc.fromDto(bi.getIndexType(), p).toIndexRequest()));
    });
    // the remaining references on projects that don't exist in db. They must
    // be deleted from index.
    remainingProjectUuids.forEach(projectUuid -> bulkIndexers.forEach(bi -> {
        String authorizationDocId = AuthorizationDoc.idOf(projectUuid);
        bi.addDeletion(bi.getIndexType(), authorizationDocId, authorizationDocId);
    }));
    bulkIndexers.forEach(b -> result.add(b.stop()));
    return result;
}
Also used : Arrays(java.util.Arrays) ImmutableSet(com.google.common.collect.ImmutableSet) EsQueueDto(org.sonar.db.es.EsQueueDto) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Set(java.util.Set) Autowired(org.springframework.beans.factory.annotation.Autowired) BulkIndexer(org.sonar.server.es.BulkIndexer) Collectors(java.util.stream.Collectors) DbSession(org.sonar.db.DbSession) EsClient(org.sonar.server.es.EsClient) Objects(java.util.Objects) OneToOneResilientIndexingListener(org.sonar.server.es.OneToOneResilientIndexingListener) DbClient(org.sonar.db.DbClient) List(java.util.List) Stream(java.util.stream.Stream) MoreCollectors.toArrayList(org.sonar.core.util.stream.MoreCollectors.toArrayList) IndexingResult(org.sonar.server.es.IndexingResult) Map(java.util.Map) Size(org.sonar.server.es.BulkIndexer.Size) ProjectIndexer(org.sonar.server.es.ProjectIndexer) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) IndexType(org.sonar.server.es.IndexType) IndexingResult(org.sonar.server.es.IndexingResult) EsQueueDto(org.sonar.db.es.EsQueueDto) Objects(java.util.Objects) BulkIndexer(org.sonar.server.es.BulkIndexer) OneToOneResilientIndexingListener(org.sonar.server.es.OneToOneResilientIndexingListener)

Example 15 with IndexingResult

use of org.sonar.server.es.IndexingResult in project sonarqube by SonarSource.

the class ComponentIndexer method index.

@Override
public IndexingResult index(DbSession dbSession, Collection<EsQueueDto> items) {
    if (items.isEmpty()) {
        return new IndexingResult();
    }
    OneToManyResilientIndexingListener listener = new OneToManyResilientIndexingListener(dbClient, dbSession, items);
    BulkIndexer bulkIndexer = new BulkIndexer(esClient, TYPE_COMPONENT, Size.REGULAR, listener);
    bulkIndexer.start();
    Set<String> branchUuids = items.stream().map(EsQueueDto::getDocId).collect(MoreCollectors.toHashSet(items.size()));
    Set<String> remaining = new HashSet<>(branchUuids);
    for (String branchUuid : branchUuids) {
        // TODO allow scrolling multiple projects at the same time
        dbClient.componentDao().scrollForIndexing(dbSession, branchUuid, context -> {
            ComponentDto dto = context.getResultObject();
            bulkIndexer.add(toDocument(dto).toIndexRequest());
            remaining.remove(dto.projectUuid());
        });
    }
    // the remaining uuids reference projects that don't exist in db. They must
    // be deleted from index.
    remaining.forEach(projectUuid -> addProjectDeletionToBulkIndexer(bulkIndexer, projectUuid));
    return bulkIndexer.stop();
}
Also used : OneToManyResilientIndexingListener(org.sonar.server.es.OneToManyResilientIndexingListener) IndexingResult(org.sonar.server.es.IndexingResult) ComponentDto(org.sonar.db.component.ComponentDto) BulkIndexer(org.sonar.server.es.BulkIndexer) HashSet(java.util.HashSet)

Aggregations

IndexingResult (org.sonar.server.es.IndexingResult)26 Test (org.junit.Test)15 ComponentDto (org.sonar.db.component.ComponentDto)11 BulkIndexer (org.sonar.server.es.BulkIndexer)8 OneToOneResilientIndexingListener (org.sonar.server.es.OneToOneResilientIndexingListener)6 EsQueueDto (org.sonar.db.es.EsQueueDto)5 IssueDto (org.sonar.db.issue.IssueDto)5 OneToManyResilientIndexingListener (org.sonar.server.es.OneToManyResilientIndexingListener)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)2 IndexingListener (org.sonar.server.es.IndexingListener)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections.emptyList (java.util.Collections.emptyList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Objects (java.util.Objects)1