Skip to content

Commit

Permalink
Speed-up the unit tests relying on Elasticsearch
Browse files Browse the repository at this point in the history
Creation of an index is slow in Elasticsearch. It should not executed
before each test.
  • Loading branch information
Simon Brandhof authored and SonarTech committed Apr 12, 2018
1 parent 9552d58 commit 72be5b2
Show file tree
Hide file tree
Showing 126 changed files with 645 additions and 1,804 deletions.
Expand Up @@ -56,8 +56,8 @@
/** /**
* Helper to bulk requests in an efficient way : * Helper to bulk requests in an efficient way :
* <ul> * <ul>
* <li>bulk request is sent on the wire when its size is higher than 5Mb</li> * <li>bulk request is sent on the wire when its size is higher than 5Mb</li>
* <li>on large table indexing, replicas and automatic refresh can be temporarily disabled</li> * <li>on large table indexing, replicas and automatic refresh can be temporarily disabled</li>
* </ul> * </ul>
*/ */
public class BulkIndexer { public class BulkIndexer {
Expand Down Expand Up @@ -160,6 +160,9 @@ public void addDeletion(SearchRequestBuilder searchRequest) {
} }


String scrollId = searchResponse.getScrollId(); String scrollId = searchResponse.getScrollId();
if (scrollId == null) {
break;
}
searchResponse = client.prepareSearchScroll(scrollId).setScroll(TimeValue.timeValueMinutes(5)).get(); searchResponse = client.prepareSearchScroll(scrollId).setScroll(TimeValue.timeValueMinutes(5)).get();
if (hits.length == 0) { if (hits.length == 0) {
client.nativeClient().prepareClearScroll().addScrollId(scrollId).get(); client.nativeClient().prepareClearScroll().addScrollId(scrollId).get();
Expand All @@ -179,7 +182,7 @@ public void addDeletion(IndexType indexType, String id, @Nullable String routing
/** /**
* Delete all the documents matching the given search request. This method is blocking. * Delete all the documents matching the given search request. This method is blocking.
* Index is refreshed, so docs are not searchable as soon as method is executed. * Index is refreshed, so docs are not searchable as soon as method is executed.
* * <p>
* Note that the parameter indexType could be removed if progress logs are not needed. * Note that the parameter indexType could be removed if progress logs are not needed.
*/ */
public static IndexingResult delete(EsClient client, IndexType indexType, SearchRequestBuilder searchRequest) { public static IndexingResult delete(EsClient client, IndexType indexType, SearchRequestBuilder searchRequest) {
Expand Down Expand Up @@ -216,7 +219,9 @@ public void afterBulk(long executionId, BulkRequest req, Throwable e) {
} }


public enum Size { public enum Size {
/** Use this size for a limited number of documents. */ /**
* Use this size for a limited number of documents.
*/
REGULAR { REGULAR {
@Override @Override
SizeHandler createHandler(Runtime2 runtime2) { SizeHandler createHandler(Runtime2 runtime2) {
Expand Down
Expand Up @@ -20,8 +20,10 @@
package org.sonar.server.es; package org.sonar.server.es;


import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
Expand Down Expand Up @@ -74,7 +76,7 @@ public void start() {
createIndex(new Index(index), false); createIndex(new Index(index), false);
} }


checkDbCompatibility(); checkDbCompatibility(definitions.getIndices().values());


// create indices that do not exist or that have a new definition (different mapping, cluster enabled, ...) // create indices that do not exist or that have a new definition (different mapping, cluster enabled, ...)
for (Index index : definitions.getIndices().values()) { for (Index index : definitions.getIndices().values()) {
Expand Down Expand Up @@ -140,13 +142,13 @@ private boolean hasDefinitionChange(Index index) {
}).orElse(true); }).orElse(true);
} }


private void checkDbCompatibility() { private void checkDbCompatibility(Collection<Index> definitions) {
boolean disabledCheck = configuration.getBoolean(PROPERY_DISABLE_CHECK).orElse(false); boolean disabledCheck = configuration.getBoolean(PROPERY_DISABLE_CHECK).orElse(false);
if (disabledCheck) { if (disabledCheck) {
LOGGER.warn("Automatic drop of search indices in turned off (see property " + PROPERY_DISABLE_CHECK + ")"); LOGGER.warn("Automatic drop of search indices in turned off (see property " + PROPERY_DISABLE_CHECK + ")");
} }


List<String> existingIndices = loadExistingIndicesExceptMetadata(); List<String> existingIndices = loadExistingIndicesExceptMetadata(definitions);
if (!disabledCheck && !existingIndices.isEmpty()) { if (!disabledCheck && !existingIndices.isEmpty()) {
boolean delete = false; boolean delete = false;
if (!esDbCompatibility.hasSameDbVendor()) { if (!esDbCompatibility.hasSameDbVendor()) {
Expand All @@ -163,8 +165,10 @@ private void checkDbCompatibility() {
esDbCompatibility.markAsCompatible(); esDbCompatibility.markAsCompatible();
} }


private List<String> loadExistingIndicesExceptMetadata() { private List<String> loadExistingIndicesExceptMetadata(Collection<Index> definitions) {
Set<String> definedNames = definitions.stream().map(Index::getName).collect(Collectors.toSet());
return Arrays.stream(client.nativeClient().admin().indices().prepareGetIndex().get().getIndices()) return Arrays.stream(client.nativeClient().admin().indices().prepareGetIndex().get().getIndices())
.filter(definedNames::contains)
.filter(index -> !MetadataIndexDefinition.INDEX_TYPE_METADATA.getIndex().equals(index)) .filter(index -> !MetadataIndexDefinition.INDEX_TYPE_METADATA.getIndex().equals(index))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
Expand Down
Expand Up @@ -19,8 +19,8 @@
*/ */
package org.sonar.server.issue.index; package org.sonar.server.issue.index;


import com.google.common.annotations.VisibleForTesting;
import org.sonar.api.config.Configuration; import org.sonar.api.config.Configuration;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.server.es.IndexDefinition; import org.sonar.server.es.IndexDefinition;
import org.sonar.server.es.IndexType; import org.sonar.server.es.IndexType;
import org.sonar.server.es.NewIndex; import org.sonar.server.es.NewIndex;
Expand Down Expand Up @@ -109,9 +109,8 @@ private IssueIndexDefinition(Configuration config, boolean enableSource) {
* Keep the document sources in index so that indexer tests can verify content * Keep the document sources in index so that indexer tests can verify content
* of indexed documents. * of indexed documents.
*/ */
@VisibleForTesting public static IssueIndexDefinition createForTest() {
public static IssueIndexDefinition createForTest(Configuration config) { return new IssueIndexDefinition(new MapSettings().asConfig(), true);
return new IssueIndexDefinition(config, true);
} }


@Override @Override
Expand Down
Expand Up @@ -19,11 +19,11 @@
*/ */
package org.sonar.server.rule.index; package org.sonar.server.rule.index;


import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import java.util.Set; import java.util.Set;
import org.sonar.api.config.Configuration; import org.sonar.api.config.Configuration;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.server.es.IndexDefinition; import org.sonar.server.es.IndexDefinition;
import org.sonar.server.es.IndexType; import org.sonar.server.es.IndexType;
import org.sonar.server.es.NewIndex; import org.sonar.server.es.NewIndex;
Expand All @@ -39,7 +39,7 @@
*/ */
public class RuleIndexDefinition implements IndexDefinition { public class RuleIndexDefinition implements IndexDefinition {


static final String INDEX = "rules"; private static final String INDEX = "rules";


public static final IndexType INDEX_TYPE_RULE = new IndexType(INDEX, "rule"); public static final IndexType INDEX_TYPE_RULE = new IndexType(INDEX, "rule");
public static final String FIELD_RULE_ID = "id"; public static final String FIELD_RULE_ID = "id";
Expand All @@ -66,7 +66,9 @@ public class RuleIndexDefinition implements IndexDefinition {


// Rule extension fields // Rule extension fields
public static final IndexType INDEX_TYPE_RULE_EXTENSION = new IndexType(INDEX, "ruleExtension"); public static final IndexType INDEX_TYPE_RULE_EXTENSION = new IndexType(INDEX, "ruleExtension");
/** The uuid of a {@link RuleExtensionScope} */ /**
* The uuid of a {@link RuleExtensionScope}
*/
public static final String FIELD_RULE_EXTENSION_SCOPE = "scope"; public static final String FIELD_RULE_EXTENSION_SCOPE = "scope";
public static final String FIELD_RULE_EXTENSION_RULE_ID = "ruleId"; public static final String FIELD_RULE_EXTENSION_RULE_ID = "ruleId";
public static final String FIELD_RULE_EXTENSION_TAGS = "tags"; public static final String FIELD_RULE_EXTENSION_TAGS = "tags";
Expand Down Expand Up @@ -95,9 +97,8 @@ private RuleIndexDefinition(Configuration config, boolean enableSource) {
* Keep the document sources in index so that indexer tests can verify content * Keep the document sources in index so that indexer tests can verify content
* of indexed documents. * of indexed documents.
*/ */
@VisibleForTesting public static RuleIndexDefinition createForTest() {
public static RuleIndexDefinition createForTest(Configuration config) { return new RuleIndexDefinition(new MapSettings().asConfig(), true);
return new RuleIndexDefinition(config, true);
} }


@Override @Override
Expand Down

0 comments on commit 72be5b2

Please sign in to comment.