Search in sources :

Example 1 with OneToOneResilientIndexingListener

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

the class ProjectMeasuresIndexer method index.

@Override
public IndexingResult index(DbSession dbSession, Collection<EsQueueDto> items) {
    if (items.isEmpty()) {
        return new IndexingResult();
    }
    OneToOneResilientIndexingListener listener = new OneToOneResilientIndexingListener(dbClient, dbSession, items);
    BulkIndexer bulkIndexer = createBulkIndexer(Size.REGULAR, listener);
    bulkIndexer.start();
    List<String> projectUuids = items.stream().map(EsQueueDto::getDocId).collect(MoreCollectors.toArrayList(items.size()));
    Iterator<String> it = projectUuids.iterator();
    while (it.hasNext()) {
        String projectUuid = it.next();
        try (ProjectMeasuresIndexerIterator rowIt = ProjectMeasuresIndexerIterator.create(dbSession, projectUuid)) {
            while (rowIt.hasNext()) {
                bulkIndexer.add(toProjectMeasuresDoc(rowIt.next()).toIndexRequest());
                it.remove();
            }
        }
    }
    // the remaining uuids reference projects that don't exist in db. They must be deleted from index.
    projectUuids.forEach(projectUuid -> bulkIndexer.addDeletion(TYPE_PROJECT_MEASURES, projectUuid, AuthorizationDoc.idOf(projectUuid)));
    return bulkIndexer.stop();
}
Also used : IndexingResult(org.sonar.server.es.IndexingResult) ProjectMeasuresIndexerIterator(org.sonar.db.measure.ProjectMeasuresIndexerIterator) BulkIndexer(org.sonar.server.es.BulkIndexer) OneToOneResilientIndexingListener(org.sonar.server.es.OneToOneResilientIndexingListener)

Example 2 with OneToOneResilientIndexingListener

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

the class RuleIndexer method doIndexRules.

private Optional<IndexingResult> doIndexRules(DbSession dbSession, List<EsQueueDto> items) {
    if (items.isEmpty()) {
        return Optional.empty();
    }
    BulkIndexer bulkIndexer = createBulkIndexer(Size.REGULAR, new OneToOneResilientIndexingListener(dbClient, dbSession, items));
    bulkIndexer.start();
    Set<String> ruleUuids = items.stream().map(EsQueueDto::getDocId).collect(toHashSet(items.size()));
    dbClient.ruleDao().scrollIndexingRulesByKeys(dbSession, ruleUuids, r -> {
        bulkIndexer.add(ruleDocOf(r).toIndexRequest());
        ruleUuids.remove(r.getUuid());
    });
    // the remaining items reference rows that don't exist in db. They must be deleted from index.
    ruleUuids.forEach(ruleUuid -> bulkIndexer.addDeletion(TYPE_RULE, ruleUuid, ruleUuid));
    return Optional.of(bulkIndexer.stop());
}
Also used : BulkIndexer(org.sonar.server.es.BulkIndexer) OneToOneResilientIndexingListener(org.sonar.server.es.OneToOneResilientIndexingListener)

Example 3 with OneToOneResilientIndexingListener

use of org.sonar.server.es.OneToOneResilientIndexingListener 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 4 with OneToOneResilientIndexingListener

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

the class ActiveRuleIndexer method doIndexActiveRules.

private IndexingResult doIndexActiveRules(DbSession dbSession, Map<String, EsQueueDto> activeRuleItems) {
    OneToOneResilientIndexingListener listener = new OneToOneResilientIndexingListener(dbClient, dbSession, activeRuleItems.values());
    BulkIndexer bulkIndexer = createBulkIndexer(Size.REGULAR, listener);
    bulkIndexer.start();
    Map<String, EsQueueDto> remaining = new HashMap<>(activeRuleItems);
    dbClient.activeRuleDao().scrollByUuidsForIndexing(dbSession, toActiveRuleUuids(activeRuleItems), i -> {
        remaining.remove(docIdOf(i.getUuid()));
        bulkIndexer.add(newIndexRequest(i));
    });
    // the remaining ids reference rows that don't exist in db. They must
    // be deleted from index.
    remaining.values().forEach(item -> bulkIndexer.addDeletion(TYPE_ACTIVE_RULE, item.getDocId(), item.getDocRouting()));
    return bulkIndexer.stop();
}
Also used : HashMap(java.util.HashMap) EsQueueDto(org.sonar.db.es.EsQueueDto) BulkIndexer(org.sonar.server.es.BulkIndexer) OneToOneResilientIndexingListener(org.sonar.server.es.OneToOneResilientIndexingListener)

Example 5 with OneToOneResilientIndexingListener

use of org.sonar.server.es.OneToOneResilientIndexingListener 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)

Aggregations

BulkIndexer (org.sonar.server.es.BulkIndexer)7 OneToOneResilientIndexingListener (org.sonar.server.es.OneToOneResilientIndexingListener)7 IndexingResult (org.sonar.server.es.IndexingResult)5 EsQueueDto (org.sonar.db.es.EsQueueDto)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 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 MoreCollectors (org.sonar.core.util.stream.MoreCollectors)1 MoreCollectors.toArrayList (org.sonar.core.util.stream.MoreCollectors.toArrayList)1 DbClient (org.sonar.db.DbClient)1 DbSession (org.sonar.db.DbSession)1