import org.sonar.core.activity.Activity;
import org.sonar.core.activity.db.ActivityDto;
import org.sonar.core.cluster.WorkQueue;
-import org.sonar.core.profiling.Profiling;
-import org.sonar.core.profiling.StopWatch;
import org.sonar.server.search.BaseIndex;
import org.sonar.server.search.ESNode;
import org.sonar.server.search.IndexDefinition;
*/
public class ActivityIndex extends BaseIndex<Activity, ActivityDto, String> {
- public ActivityIndex(Profiling profiling, ActivityNormalizer normalizer, WorkQueue workQueue, ESNode node) {
- super(IndexDefinition.LOG, normalizer, workQueue, node, profiling);
+ public ActivityIndex(ActivityNormalizer normalizer, WorkQueue workQueue, ESNode node) {
+ super(IndexDefinition.LOG, normalizer, workQueue, node);
}
@Override
}
public Result<Activity> findAll() {
- StopWatch fullProfile = profiling.start("es", Profiling.Level.FULL);
- StopWatch basicProfile = profiling.start("es", Profiling.Level.BASIC);
SearchRequestBuilder request = getClient().prepareSearch(this.getIndexName())
.setQuery(QueryBuilders.matchAllQuery())
.setTypes(this.getIndexType())
.setSize(Integer.MAX_VALUE);
- basicProfile.stop(request.toString());
- SearchResponse response = request.get();
- fullProfile.stop(response.toString());
+ SearchResponse response = node.execute(request);
return new Result<Activity>(this, response);
}
public SearchResponse search(ActivityQuery query, QueryOptions options,
@Nullable FilterBuilder domainFilter) {
- StopWatch fullProfile = profiling.start("es", Profiling.Level.FULL);
- StopWatch basicProfile = profiling.start("es", Profiling.Level.BASIC);
-
// Prepare query
SearchRequestBuilder esSearch = getClient()
.prepareSearch(this.getIndexName())
esSearch.setScroll(TimeValue.timeValueMinutes(3));
}
- basicProfile.stop(esSearch.toString());
- SearchResponse response = esSearch.get();
- fullProfile.stop(response.toString());
+ SearchResponse response = node.execute(esSearch);
return response;
}
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.core.cluster.WorkQueue;
-import org.sonar.core.profiling.Profiling;
-import org.sonar.core.profiling.StopWatch;
import org.sonar.core.qualityprofile.db.ActiveRuleDto;
import org.sonar.core.qualityprofile.db.ActiveRuleKey;
import org.sonar.server.qualityprofile.ActiveRule;
public class ActiveRuleIndex extends BaseIndex<ActiveRule, ActiveRuleDto, ActiveRuleKey> {
- public ActiveRuleIndex(Profiling profiling, ActiveRuleNormalizer normalizer, WorkQueue workQueue, ESNode node) {
- super(IndexDefinition.ACTIVE_RULE, normalizer, workQueue, node, profiling);
+ public ActiveRuleIndex(ActiveRuleNormalizer normalizer, WorkQueue workQueue, ESNode node) {
+ super(IndexDefinition.ACTIVE_RULE, normalizer, workQueue, node);
}
@Override
* finder methods
*/
public List<ActiveRule> findByRule(RuleKey key) {
- StopWatch fullProfile = profiling.start("es", Profiling.Level.FULL);
- StopWatch basicProfile = profiling.start("es", Profiling.Level.BASIC);
SearchRequestBuilder request = getClient().prepareSearch(this.getIndexName())
.setQuery(QueryBuilders
.hasParentQuery(this.getParentType(),
// TODO replace by scrolling
.setSize(Integer.MAX_VALUE);
- basicProfile.stop(request.toString());
- SearchResponse response = request.get();
- fullProfile.stop(response.toString());
+ SearchResponse response = node.execute(request);
List<ActiveRule> activeRules = new ArrayList<ActiveRule>();
for (SearchHit hit : response.getHits()) {
}
public List<ActiveRule> findByProfile(String key) {
- StopWatch fullProfile = profiling.start("es", Profiling.Level.FULL);
- StopWatch basicProfile = profiling.start("es", Profiling.Level.BASIC);
SearchRequestBuilder request = getClient().prepareSearch(getIndexName())
.setQuery(QueryBuilders.termQuery(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field(), key))
.setRouting(key)
// TODO replace by scrolling
.setSize(Integer.MAX_VALUE);
- basicProfile.stop(request.toString());
- SearchResponse response = request.get();
- fullProfile.stop(response.toString());
+ SearchResponse response = node.execute(request);
List<ActiveRule> activeRules = new ArrayList<ActiveRule>();
for (SearchHit hit : response.getHits()) {
activeRules.add(toDoc(hit.getSource()));
}
public Map<String, Multimap<String, FacetValue>> getStatsByProfileKeys(List<String> keys) {
- StopWatch fullProfile = profiling.start("es", Profiling.Level.FULL);
- StopWatch basicProfile = profiling.start("es", Profiling.Level.BASIC);
SearchRequestBuilder request = getClient().prepareSearch(this.getIndexName())
.setQuery(QueryBuilders.filteredQuery(
QueryBuilders.termsQuery(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field(), keys),
.subAggregation(AggregationBuilders.count("countActiveRules")))
.setSize(0)
.setTypes(this.getIndexType());
- basicProfile.stop(request.toString());
- SearchResponse response = request.get();
- fullProfile.stop(response.toString());
+ SearchResponse response = node.execute(request);
Map<String, Multimap<String, FacetValue>> stats = new HashMap<String, Multimap<String, FacetValue>>();
Aggregation aggregation = response.getAggregations().get(ActiveRuleNormalizer.ActiveRuleField.PROFILE_KEY.field());
for (Terms.Bucket value : ((Terms) aggregation).getBuckets()) {
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.server.debt.DebtCharacteristic;
import org.sonar.core.cluster.WorkQueue;
-import org.sonar.core.profiling.Profiling;
-import org.sonar.core.profiling.StopWatch;
import org.sonar.core.rule.RuleDto;
import org.sonar.server.qualityprofile.index.ActiveRuleNormalizer;
import org.sonar.server.rule.Rule;
public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
- public RuleIndex(Profiling profiling, RuleNormalizer normalizer, WorkQueue workQueue, ESNode node) {
- super(IndexDefinition.RULE, normalizer, workQueue, node, profiling);
+ public RuleIndex(RuleNormalizer normalizer, WorkQueue workQueue, ESNode node) {
+ super(IndexDefinition.RULE, normalizer, workQueue, node);
}
protected String getKeyValue(RuleKey key) {
}
public Result<Rule> search(RuleQuery query, QueryOptions options) {
- StopWatch fullProfile = profiling.start("es", Profiling.Level.FULL);
- StopWatch basicProfile = profiling.start("es", Profiling.Level.BASIC);
-
SearchRequestBuilder esSearch = getClient()
.prepareSearch(this.getIndexName())
.setTypes(this.getIndexType())
QueryBuilder qb = this.getQuery(query, options);
esSearch.setQuery(QueryBuilders.filteredQuery(qb, fb));
- basicProfile.stop(esSearch.toString());
- SearchResponse esResult = esSearch.get();
- fullProfile.stop(esResult.toString());
+ SearchResponse esResult = node.execute(esSearch);
return new Result<Rule>(this, esResult);
}
import org.slf4j.LoggerFactory;
import org.sonar.core.cluster.WorkQueue;
import org.sonar.core.persistence.Dto;
-import org.sonar.core.profiling.Profiling;
-import org.sonar.core.profiling.StopWatch;
import javax.annotation.Nullable;
import java.io.IOException;
private static final Logger LOG = LoggerFactory.getLogger(BaseIndex.class);
- private final ESNode node;
+ protected final ESNode node;
private final BaseNormalizer<DTO, KEY> normalizer;
private final IndexDefinition indexDefinition;
- protected final Profiling profiling;
protected BaseIndex(IndexDefinition indexDefinition, BaseNormalizer<DTO, KEY> normalizer,
- WorkQueue workQueue, ESNode node, Profiling profiling) {
+ WorkQueue workQueue, ESNode node) {
this.normalizer = normalizer;
this.node = node;
this.indexDefinition = indexDefinition;
- this.profiling = profiling;
}
@Override
try {
SearchScrollRequestBuilder esRequest = getClient().prepareSearchScroll(scrollId)
.setScroll(TimeValue.timeValueMinutes(3));
- Collections.addAll(hits, esRequest.get().getHits().getHits());
+ Collections.addAll(hits, ((SearchResponse) node.execute(esRequest)).getHits().getHits());
} catch (Exception e) {
throw new IllegalStateException("Error while filling in the scroll buffer", e);
}
@Override
public Date getLastSynchronization() {
- StopWatch fullProfile = profiling.start("es", Profiling.Level.FULL);
- StopWatch basicProfile = profiling.start("es", Profiling.Level.BASIC);
Date date;
SearchRequestBuilder request = getClient().prepareSearch(this.getIndexName())
.addAggregation(AggregationBuilders.max("latest")
.field(BaseNormalizer.UPDATED_AT_FIELD));
-
- basicProfile.stop(request.toString());
- SearchResponse response = request.get();
- fullProfile.stop(response.toString());
+ SearchResponse response = node.execute(request);
Max max = (Max) response.getAggregations().get("latest");
@Override
public void refresh() {
- getClient()
+ node.execute(getClient()
.admin()
.indices()
.prepareRefresh(this.getIndexName())
.setForce(false)
- .setIndices(this.getIndexName())
- .get();
+ .setIndices(this.getIndexName()));
}
/* Base CRUD methods */
protected abstract DOMAIN toDoc(Map<String, Object> fields);
public DOMAIN getByKey(KEY key) {
- StopWatch fullProfile = profiling.start("es", Profiling.Level.FULL);
- StopWatch basicProfile = profiling.start("es", Profiling.Level.BASIC);
-
GetRequestBuilder request = getClient().prepareGet()
.setType(this.getIndexType())
.setIndex(this.getIndexName())
.setId(this.getKeyValue(key))
.setRouting(this.getKeyValue(key));
- basicProfile.stop(request.toString());
- GetResponse response = request.get();
- fullProfile.stop(response.toString());
+ GetResponse response = node.execute(request);
if (response.isExists()) {
return toDoc(response.getSource());
}
protected void updateDocument(Collection<UpdateRequest> requests, KEY key) {
- StopWatch fullProfile = profiling.start("es", Profiling.Level.FULL);
- StopWatch basicProfile = profiling.start("es", Profiling.Level.BASIC);
LOG.debug("UPDATE _id:{} in index {}", key, this.getIndexName());
BulkRequestBuilder bulkRequest = getClient().prepareBulk();
for (UpdateRequest request : requests) {
.type(this.getIndexType()));
}
}
- basicProfile.stop(bulkRequest.toString());
- BulkResponse response = bulkRequest.get();
- fullProfile.stop(response.toString());
+ BulkResponse response = node.execute(bulkRequest);
}
@Override
}
private void deleteDocument(KEY key) throws ExecutionException, InterruptedException {
- StopWatch fullProfile = profiling.start("es", Profiling.Level.FULL);
- StopWatch basicProfile = profiling.start("es", Profiling.Level.BASIC);
LOG.debug("DELETE _id:{} in index {}", key, this.getIndexName());
DeleteRequestBuilder request = getClient()
.prepareDelete()
.setIndex(this.getIndexName())
.setType(this.getIndexType())
.setId(this.getKeyValue(key));
- basicProfile.stop(request.toString());
- DeleteResponse response = request.get();
- fullProfile.stop(response.toString());
+ DeleteResponse response = node.execute(request);
}
@Override
public Map<String, Long> countByField(IndexField indexField, FilterBuilder filter) {
- StopWatch fullProfile = profiling.start("es", Profiling.Level.FULL);
- StopWatch basicProfile = profiling.start("es", Profiling.Level.BASIC);
Map<String, Long> counts = new HashMap<String, Long>();
SearchRequestBuilder request = getClient().prepareSearch(this.getIndexName())
.size(Integer.MAX_VALUE)
.minDocCount(0));
- basicProfile.stop(request.toString());
- SearchResponse response = request.get();
- fullProfile.stop(response.toString());
+ SearchResponse response = node.execute(request);
Terms values =
response.getAggregations().get(indexField.field());
import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
+import org.elasticsearch.action.ActionRequestBuilder;
+import org.elasticsearch.action.ActionResponse;
+import org.elasticsearch.action.ListenableActionFuture;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.config.Settings;
+import org.sonar.core.profiling.Profiling;
+import org.sonar.core.profiling.StopWatch;
import org.sonar.server.search.es.ListUpdate;
import org.sonar.server.search.es.ListUpdate.UpdateListScriptFactory;
// available only after startup
private Client client;
private Node node;
+ protected final Profiling profiling;
+ private final Profiling.Level profilingLevel;
+
public ESNode(Settings settings) {
this(settings, DEFAULT_HEALTH_TIMEOUT);
ESNode(Settings settings, String healthTimeout) {
this.settings = settings;
this.healthTimeout = healthTimeout;
+ this.profiling = new Profiling(settings);
+ String settingsValue = settings.getString(Profiling.CONFIG_PROFILING_LEVEL);
+ this.profilingLevel = Profiling.Level.fromConfigString(settingsValue);
}
@Override
}
return client;
}
+
+ public <K extends ActionResponse> K execute(ActionRequestBuilder action) {
+ StopWatch requestProfile = null;
+ if (profilingLevel.ordinal() >= Profiling.Level.BASIC.ordinal()) {
+ requestProfile = profiling.start("search", Profiling.Level.BASIC);
+ }
+ ListenableActionFuture acc = action.execute();
+ try {
+ K response = (K) acc.get();
+ return response;
+ } catch (Exception e) {
+ throw new IllegalStateException("ES error: ", e);
+ } finally {
+ if (requestProfile != null) {
+ requestProfile.stop(action.toString());
+ }
+ }
+ }
}
import org.junit.rules.TemporaryFolder;
import org.sonar.api.config.Settings;
import org.sonar.core.cluster.NullQueue;
-import org.sonar.core.profiling.Profiling;
import java.io.File;
import java.io.IOException;
private BaseIndex getIndex(final ESNode esNode) {
BaseIndex index = new BaseIndex(
IndexDefinition.TEST,
- null, new NullQueue(), esNode, new Profiling(new Settings())) {
+ null, new NullQueue(), esNode) {
@Override
protected String getKeyValue(Serializable key) {
return null;