Search in sources :

Example 21 with IndexingResult

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

the class ViewIndexer method index.

/**
 * This is based on the fact that a WebService is only calling {@link ViewIndexer#delete(DbSession, Collection)}
 * So the resiliency is only taking in account a deletion of view component
 * A safety check is done by not deleting any component that still exist in database.
 *
 * This should not occur but prevent any misuse on this resiliency
 */
@Override
public IndexingResult index(DbSession dbSession, Collection<EsQueueDto> items) {
    if (items.isEmpty()) {
        return new IndexingResult();
    }
    Set<String> views = items.stream().map(EsQueueDto::getDocId).collect(toHashSet(items.size()));
    BulkIndexer bulkIndexer = newBulkIndexer(Size.REGULAR, new OneToOneResilientIndexingListener(dbClient, dbSession, items));
    bulkIndexer.start();
    // Safety check to remove all views that may not have been deleted
    views.removeAll(dbClient.componentDao().selectExistingUuids(dbSession, views));
    views.forEach(v -> bulkIndexer.addDeletion(TYPE_VIEW, v));
    return bulkIndexer.stop();
}
Also used : IndexingResult(org.sonar.server.es.IndexingResult) BulkIndexer(org.sonar.server.es.BulkIndexer) OneToOneResilientIndexingListener(org.sonar.server.es.OneToOneResilientIndexingListener)

Example 22 with IndexingResult

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

the class UserIndexer method index.

/**
 * @return the number of items that have been successfully indexed
 */
@Override
public IndexingResult index(DbSession dbSession, Collection<EsQueueDto> items) {
    if (items.isEmpty()) {
        return new IndexingResult();
    }
    Set<String> uuids = items.stream().map(EsQueueDto::getDocId).collect(toHashSet(items.size()));
    BulkIndexer bulkIndexer = newBulkIndexer(Size.REGULAR, new OneToOneResilientIndexingListener(dbClient, dbSession, items));
    bulkIndexer.start();
    dbClient.userDao().scrollByUuids(dbSession, uuids, // Deactivated users are not deleted but updated.
    u -> {
        uuids.remove(u.getUuid());
        bulkIndexer.add(newIndexRequest(u));
    });
    // the remaining uuids reference rows that don't exist in db. They must
    // be deleted from index.
    uuids.forEach(uuid -> bulkIndexer.addDeletion(TYPE_USER, uuid));
    return bulkIndexer.stop();
}
Also used : IndexingResult(org.sonar.server.es.IndexingResult) BulkIndexer(org.sonar.server.es.BulkIndexer) OneToOneResilientIndexingListener(org.sonar.server.es.OneToOneResilientIndexingListener)

Example 23 with IndexingResult

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

the class IssueIndexer method doIndexProjectItems.

private IndexingResult doIndexProjectItems(DbSession dbSession, ListMultimap<String, EsQueueDto> itemsByProjectUuid) {
    if (itemsByProjectUuid.isEmpty()) {
        return new IndexingResult();
    }
    // one project, referenced by es_queue.doc_id = many issues
    IndexingListener listener = new OneToManyResilientIndexingListener(dbClient, dbSession, itemsByProjectUuid.values());
    BulkIndexer bulkIndexer = createBulkIndexer(Size.REGULAR, listener);
    bulkIndexer.start();
    for (String projectUuid : itemsByProjectUuid.keySet()) {
        // TODO support loading of multiple projects in a single SQL request
        try (IssueIterator issues = issueIteratorFactory.createForProject(projectUuid)) {
            if (issues.hasNext()) {
                do {
                    IssueDoc doc = issues.next();
                    bulkIndexer.add(newIndexRequest(doc));
                } while (issues.hasNext());
            } else {
                // project does not exist or has no issues. In both case
                // all the documents related to this project are deleted.
                addProjectDeletionToBulkIndexer(bulkIndexer, projectUuid);
            }
        }
    }
    return bulkIndexer.stop();
}
Also used : IndexingListener(org.sonar.server.es.IndexingListener) OneToOneResilientIndexingListener(org.sonar.server.es.OneToOneResilientIndexingListener) OneToManyResilientIndexingListener(org.sonar.server.es.OneToManyResilientIndexingListener) OneToManyResilientIndexingListener(org.sonar.server.es.OneToManyResilientIndexingListener) IndexingResult(org.sonar.server.es.IndexingResult) BulkIndexer(org.sonar.server.es.BulkIndexer)

Example 24 with IndexingResult

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

the class IssueIndexer method doIndexIssueItems.

private IndexingResult doIndexIssueItems(DbSession dbSession, ListMultimap<String, EsQueueDto> itemsByIssueKey) {
    if (itemsByIssueKey.isEmpty()) {
        return new IndexingResult();
    }
    IndexingListener listener = new OneToOneResilientIndexingListener(dbClient, dbSession, itemsByIssueKey.values());
    BulkIndexer bulkIndexer = createBulkIndexer(Size.REGULAR, listener);
    bulkIndexer.start();
    try (IssueIterator issues = issueIteratorFactory.createForIssueKeys(itemsByIssueKey.keySet())) {
        while (issues.hasNext()) {
            IssueDoc issue = issues.next();
            bulkIndexer.add(newIndexRequest(issue));
            itemsByIssueKey.removeAll(issue.getId());
        }
    }
    // the remaining uuids reference issues that don't exist in db. They must
    // be deleted from index.
    itemsByIssueKey.values().forEach(item -> bulkIndexer.addDeletion(TYPE_ISSUE.getMainType(), item.getDocId(), item.getDocRouting()));
    return bulkIndexer.stop();
}
Also used : IndexingListener(org.sonar.server.es.IndexingListener) OneToOneResilientIndexingListener(org.sonar.server.es.OneToOneResilientIndexingListener) OneToManyResilientIndexingListener(org.sonar.server.es.OneToManyResilientIndexingListener) IndexingResult(org.sonar.server.es.IndexingResult) BulkIndexer(org.sonar.server.es.BulkIndexer) OneToOneResilientIndexingListener(org.sonar.server.es.OneToOneResilientIndexingListener)

Example 25 with IndexingResult

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

the class ProjectMeasuresIndexerTest method update_index_when_project_tags_are_updated.

@Test
public void update_index_when_project_tags_are_updated() {
    ComponentDto project = db.components().insertPrivateProject(defaults(), p -> p.setTagsString("foo"));
    indexProject(project, PROJECT_CREATION);
    assertThatProjectHasTag(project, "foo");
    ProjectDto projectDto = db.components().getProjectDto(project);
    projectDto.setTagsString("bar");
    db.getDbClient().projectDao().updateTags(db.getSession(), projectDto);
    // TODO change indexing?
    IndexingResult result = indexProject(project, PROJECT_TAGS_UPDATE);
    assertThatProjectHasTag(project, "bar");
    assertThat(result.getTotal()).isOne();
    assertThat(result.getSuccess()).isOne();
}
Also used : ComponentTesting.newPrivateProjectDto(org.sonar.db.component.ComponentTesting.newPrivateProjectDto) ProjectDto(org.sonar.db.project.ProjectDto) IndexingResult(org.sonar.server.es.IndexingResult) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

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