Search in sources :

Example 16 with EsQueueDto

use of org.sonar.db.es.EsQueueDto in project sonarqube by SonarSource.

the class OneToManyResilientIndexingListenerTest method insertInQueue.

private EsQueueDto insertInQueue(IndexType indexType, String id) {
    EsQueueDto item = EsQueueDto.create(indexType.format(), id);
    db.getDbClient().esQueueDao().insert(db.getSession(), singletonList(item));
    return item;
}
Also used : EsQueueDto(org.sonar.db.es.EsQueueDto)

Example 17 with EsQueueDto

use of org.sonar.db.es.EsQueueDto in project sonarqube by SonarSource.

the class OneToManyResilientIndexingListenerTest method ES_QUEUE_rows_are_deleted_when_all_docs_are_successfully_indexed.

@Test
public void ES_QUEUE_rows_are_deleted_when_all_docs_are_successfully_indexed() {
    EsQueueDto item1 = insertInQueue(TYPE_ISSUE, "P1");
    EsQueueDto item2 = insertInQueue(TYPE_ISSUE, "P2");
    EsQueueDto outOfScopeItem = insertInQueue(ComponentIndexDefinition.TYPE_COMPONENT, "P1");
    db.commit();
    // does not contain outOfScopeItem
    IndexingListener underTest = newListener(asList(item1, item2));
    DocId issue1 = newDocId(TYPE_ISSUE, "I1");
    DocId issue2 = newDocId(TYPE_ISSUE, "I2");
    underTest.onSuccess(asList(issue1, issue2));
    assertThatEsTableContainsOnly(item1, item2, outOfScopeItem);
    // onFinish deletes all items
    IndexingResult result = new IndexingResult();
    result.incrementSuccess().incrementRequests();
    result.incrementSuccess().incrementRequests();
    underTest.onFinish(result);
    assertThatEsTableContainsOnly(outOfScopeItem);
}
Also used : EsQueueDto(org.sonar.db.es.EsQueueDto) Test(org.junit.Test)

Example 18 with EsQueueDto

use of org.sonar.db.es.EsQueueDto in project sonarqube by SonarSource.

the class OneToOneResilientIndexingListenerTest method onSuccess_deletes_rows_from_ES_QUEUE_table.

@Test
public void onSuccess_deletes_rows_from_ES_QUEUE_table() {
    EsQueueDto item1 = insertInQueue(TYPE_ISSUE, "foo");
    EsQueueDto item2 = insertInQueue(TYPE_ISSUE, "bar");
    EsQueueDto item3 = insertInQueue(TYPE_ISSUE, "baz");
    db.commit();
    IndexingListener underTest = newListener(asList(item1, item2, item3));
    underTest.onSuccess(emptyList());
    assertThatEsTableContainsOnly(item1, item2, item3);
    underTest.onSuccess(asList(toDocId(item1), toDocId(item3)));
    assertThatEsTableContainsOnly(item2);
    // onFinish does nothing
    underTest.onFinish(new IndexingResult());
    assertThatEsTableContainsOnly(item2);
}
Also used : EsQueueDto(org.sonar.db.es.EsQueueDto) Test(org.junit.Test)

Example 19 with EsQueueDto

use of org.sonar.db.es.EsQueueDto in project sonarqube by SonarSource.

the class ActiveRuleIndexer method doIndexRuleProfiles.

private IndexingResult doIndexRuleProfiles(DbSession dbSession, Map<String, EsQueueDto> ruleProfileItems) {
    IndexingResult result = new IndexingResult();
    for (Map.Entry<String, EsQueueDto> entry : ruleProfileItems.entrySet()) {
        String ruleProfileUUid = entry.getKey();
        EsQueueDto item = entry.getValue();
        IndexingResult profileResult;
        RulesProfileDto profile = dbClient.qualityProfileDao().selectRuleProfile(dbSession, ruleProfileUUid);
        if (profile == null) {
            // profile does not exist anymore in db --> related documents must be deleted from index rules/activeRule
            SearchRequest search = EsClient.prepareSearch(TYPE_ACTIVE_RULE.getMainType()).source(new SearchSourceBuilder().query(QueryBuilders.boolQuery().must(termQuery(FIELD_ACTIVE_RULE_PROFILE_UUID, ruleProfileUUid))));
            profileResult = BulkIndexer.delete(esClient, TYPE_ACTIVE_RULE, search);
        } else {
            BulkIndexer bulkIndexer = createBulkIndexer(Size.REGULAR, IndexingListener.FAIL_ON_ERROR);
            bulkIndexer.start();
            dbClient.activeRuleDao().scrollByRuleProfileForIndexing(dbSession, ruleProfileUUid, i -> bulkIndexer.add(newIndexRequest(i)));
            profileResult = bulkIndexer.stop();
        }
        if (profileResult.isSuccess()) {
            deleteQueueDto(dbSession, item);
        }
        result.add(profileResult);
    }
    return result;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) IndexingResult(org.sonar.server.es.IndexingResult) EsQueueDto(org.sonar.db.es.EsQueueDto) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) HashMap(java.util.HashMap) Map(java.util.Map) BulkIndexer(org.sonar.server.es.BulkIndexer) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 20 with EsQueueDto

use of org.sonar.db.es.EsQueueDto in project sonarqube by SonarSource.

the class ActiveRuleIndexer method index.

/**
 * @return the number of items that have been successfully indexed
 */
@Override
public IndexingResult index(DbSession dbSession, Collection<EsQueueDto> items) {
    IndexingResult result = new IndexingResult();
    if (items.isEmpty()) {
        return result;
    }
    Map<String, EsQueueDto> activeRuleItems = new HashMap<>();
    Map<String, EsQueueDto> ruleProfileItems = new HashMap<>();
    items.forEach(i -> {
        if (ID_TYPE_RULE_PROFILE_UUID.equals(i.getDocIdType())) {
            ruleProfileItems.put(i.getDocId(), i);
        } else if (ID_TYPE_ACTIVE_RULE_UUID.equals(i.getDocIdType())) {
            activeRuleItems.put(i.getDocId(), i);
        } else {
            LOGGER.error("Unsupported es_queue.doc_id_type. Removing row from queue: " + i);
            deleteQueueDto(dbSession, i);
        }
    });
    if (!activeRuleItems.isEmpty()) {
        result.add(doIndexActiveRules(dbSession, activeRuleItems));
    }
    if (!ruleProfileItems.isEmpty()) {
        result.add(doIndexRuleProfiles(dbSession, ruleProfileItems));
    }
    return result;
}
Also used : IndexingResult(org.sonar.server.es.IndexingResult) HashMap(java.util.HashMap) EsQueueDto(org.sonar.db.es.EsQueueDto)

Aggregations

EsQueueDto (org.sonar.db.es.EsQueueDto)26 Test (org.junit.Test)13 DbSession (org.sonar.db.DbSession)7 IndexingResult (org.sonar.server.es.IndexingResult)6 HashMap (java.util.HashMap)3 BulkIndexer (org.sonar.server.es.BulkIndexer)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections.emptyList (java.util.Collections.emptyList)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 ComponentDto (org.sonar.db.component.ComponentDto)2 OneToOneResilientIndexingListener (org.sonar.server.es.OneToOneResilientIndexingListener)2 ProjectIndexer (org.sonar.server.es.ProjectIndexer)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Arrays.asList (java.util.Arrays.asList)1 Collections.emptySet (java.util.Collections.emptySet)1 Collections.singletonList (java.util.Collections.singletonList)1