Skip to content

Commit

Permalink
SONAR-7330 RegisterRules is now using RuleIndexer
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Feb 29, 2016
1 parent d4087f1 commit 743fac7
Show file tree
Hide file tree
Showing 21 changed files with 663 additions and 379 deletions.
Expand Up @@ -20,16 +20,19 @@
package org.sonar.server.rule; package org.sonar.server.rule;


import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
Expand All @@ -39,6 +42,7 @@
import org.sonar.api.rule.RuleStatus; import org.sonar.api.rule.RuleStatus;
import org.sonar.api.server.debt.DebtRemediationFunction; import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.api.server.rule.RulesDefinition; import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Loggers;
import org.sonar.api.utils.log.Profiler; import org.sonar.api.utils.log.Profiler;
Expand All @@ -50,7 +54,9 @@
import org.sonar.db.rule.RuleParamDto; import org.sonar.db.rule.RuleParamDto;
import org.sonar.server.db.DbClient; import org.sonar.server.db.DbClient;
import org.sonar.server.qualityprofile.RuleActivator; import org.sonar.server.qualityprofile.RuleActivator;
import org.sonar.server.rule.index.RuleIndexer;


import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Lists.newArrayList;


/** /**
Expand All @@ -63,13 +69,18 @@ public class RegisterRules implements Startable {
private final RuleDefinitionsLoader defLoader; private final RuleDefinitionsLoader defLoader;
private final RuleActivator ruleActivator; private final RuleActivator ruleActivator;
private final DbClient dbClient; private final DbClient dbClient;
private final RuleIndexer ruleIndexer;
private final Languages languages; private final Languages languages;
private final System2 system2;


public RegisterRules(RuleDefinitionsLoader defLoader, RuleActivator ruleActivator, DbClient dbClient, Languages languages) { public RegisterRules(RuleDefinitionsLoader defLoader, RuleActivator ruleActivator, DbClient dbClient, RuleIndexer ruleIndexer,
Languages languages, System2 system2) {
this.defLoader = defLoader; this.defLoader = defLoader;
this.ruleActivator = ruleActivator; this.ruleActivator = ruleActivator;
this.dbClient = dbClient; this.dbClient = dbClient;
this.ruleIndexer = ruleIndexer;
this.languages = languages; this.languages = languages;
this.system2 = system2;
} }


@Override @Override
Expand All @@ -91,6 +102,7 @@ public void start() {
List<RuleDto> activeRules = processRemainingDbRules(allRules.values(), session); List<RuleDto> activeRules = processRemainingDbRules(allRules.values(), session);
removeActiveRulesOnStillExistingRepositories(session, activeRules, context); removeActiveRulesOnStillExistingRepositories(session, activeRules, context);
session.commit(); session.commit();
ruleIndexer.setEnabled(true).index();
profiler.stopDebug(); profiler.stopDebug();
} finally { } finally {
session.close(); session.close();
Expand Down Expand Up @@ -121,15 +133,15 @@ private void registerRule(RulesDefinition.Rule ruleDef, Map<RuleKey, RuleDto> al
} }


if (executeUpdate) { if (executeUpdate) {
dbClient.deprecatedRuleDao().update(session, rule); update(session, rule);
} }


mergeParams(ruleDef, rule, session); mergeParams(ruleDef, rule, session);
} }


private Map<RuleKey, RuleDto> loadRules(DbSession session) { private Map<RuleKey, RuleDto> loadRules(DbSession session) {
Map<RuleKey, RuleDto> rules = new HashMap<>(); Map<RuleKey, RuleDto> rules = new HashMap<>();
for (RuleDto rule : dbClient.deprecatedRuleDao().selectByNonManual(session)) { for (RuleDto rule : dbClient.ruleDao().selectByNonManual(session)) {
rules.put(rule.getKey(), rule); rules.put(rule.getKey(), rule);
} }
return rules; return rules;
Expand Down Expand Up @@ -159,7 +171,11 @@ private RuleDto createRuleDto(RulesDefinition.Rule ruleDef, DbSession session) {
.setSeverity(ruleDef.severity()) .setSeverity(ruleDef.severity())
.setStatus(ruleDef.status()) .setStatus(ruleDef.status())
.setEffortToFixDescription(ruleDef.effortToFixDescription()) .setEffortToFixDescription(ruleDef.effortToFixDescription())
.setSystemTags(ruleDef.tags()); .setSystemTags(ruleDef.tags())
.setCreatedAtInMs(system2.now())
.setUpdatedAtInMs(system2.now());
ruleDto.setCreatedAt(new Date(system2.now()));
ruleDto.setUpdatedAt(new Date(system2.now()));
if (ruleDef.htmlDescription() != null) { if (ruleDef.htmlDescription() != null) {
ruleDto.setDescription(ruleDef.htmlDescription()); ruleDto.setDescription(ruleDef.htmlDescription());
ruleDto.setDescriptionFormat(Format.HTML); ruleDto.setDescriptionFormat(Format.HTML);
Expand All @@ -168,7 +184,7 @@ private RuleDto createRuleDto(RulesDefinition.Rule ruleDef, DbSession session) {
ruleDto.setDescriptionFormat(Format.MARKDOWN); ruleDto.setDescriptionFormat(Format.MARKDOWN);
} }


dbClient.deprecatedRuleDao().insert(session, ruleDto); dbClient.ruleDao().insert(session, ruleDto);
return ruleDto; return ruleDto;
} }


Expand Down Expand Up @@ -262,17 +278,17 @@ private boolean mergeDebtDefinitions(RuleDto dto, @Nullable String remediationFu
} }


private void mergeParams(RulesDefinition.Rule ruleDef, RuleDto rule, DbSession session) { private void mergeParams(RulesDefinition.Rule ruleDef, RuleDto rule, DbSession session) {
List<RuleParamDto> paramDtos = dbClient.deprecatedRuleDao().selectRuleParamsByRuleKey(session, rule.getKey()); List<RuleParamDto> paramDtos = dbClient.ruleDao().selectRuleParamsByRuleKey(session, rule.getKey());
Map<String, RuleParamDto> existingParamsByName = Maps.newHashMap(); Map<String, RuleParamDto> existingParamsByName = Maps.newHashMap();


for (RuleParamDto paramDto : paramDtos) { for (RuleParamDto paramDto : paramDtos) {
RulesDefinition.Param paramDef = ruleDef.param(paramDto.getName()); RulesDefinition.Param paramDef = ruleDef.param(paramDto.getName());
if (paramDef == null) { if (paramDef == null) {
dbClient.activeRuleDao().deleteParamsByRuleParam(session, rule, paramDto.getName()); dbClient.activeRuleDao().deleteParamsByRuleParam(session, rule, paramDto.getName());
dbClient.deprecatedRuleDao().deleteRuleParam(session, rule, paramDto); dbClient.ruleDao().deleteRuleParam(session, paramDto.getId());
} else { } else {
if (mergeParam(paramDto, paramDef)) { if (mergeParam(paramDto, paramDef)) {
dbClient.deprecatedRuleDao().updateRuleParam(session, rule, paramDto); dbClient.ruleDao().updateRuleParam(session, rule, paramDto);
} }
existingParamsByName.put(paramDto.getName(), paramDto); existingParamsByName.put(paramDto.getName(), paramDto);
} }
Expand All @@ -287,7 +303,7 @@ private void mergeParams(RulesDefinition.Rule ruleDef, RuleDto rule, DbSession s
.setDescription(param.description()) .setDescription(param.description())
.setDefaultValue(param.defaultValue()) .setDefaultValue(param.defaultValue())
.setType(param.type().toString()); .setType(param.type().toString());
dbClient.deprecatedRuleDao().insertRuleParam(session, rule, paramDto); dbClient.ruleDao().insertRuleParam(session, rule, paramDto);
if (!StringUtils.isEmpty(param.defaultValue())) { if (!StringUtils.isEmpty(param.defaultValue())) {
// Propagate the default value to existing active rules // Propagate the default value to existing active rules
for (ActiveRuleDto activeRule : dbClient.activeRuleDao().selectByRule(session, rule)) { for (ActiveRuleDto activeRule : dbClient.activeRuleDao().selectByRule(session, rule)) {
Expand Down Expand Up @@ -346,10 +362,12 @@ private List<RuleDto> processRemainingDbRules(Collection<RuleDto> existingRules,
} }


for (RuleDto customRule : customRules) { for (RuleDto customRule : customRules) {
RuleDto template = dbClient.deprecatedRuleDao().selectTemplate(customRule, session); Integer templateId = customRule.getTemplateId();
if (template != null && template.getStatus() != RuleStatus.REMOVED) { checkNotNull(templateId, "Template id of the custom rule '%s' is null", customRule);
if (updateCustomRuleFromTemplateRule(customRule, template)) { Optional<RuleDto> template = dbClient.ruleDao().selectById(templateId, session);
dbClient.deprecatedRuleDao().update(session, customRule); if (template.isPresent() && template.get().getStatus() != RuleStatus.REMOVED) {
if (updateCustomRuleFromTemplateRule(customRule, template.get())) {
update(session, customRule);
} }
} else { } else {
removeRule(session, removedRules, customRule); removeRule(session, removedRules, customRule);
Expand All @@ -365,7 +383,7 @@ private void removeRule(DbSession session, List<RuleDto> removedRules, RuleDto r
rule.setStatus(RuleStatus.REMOVED); rule.setStatus(RuleStatus.REMOVED);
rule.setSystemTags(Collections.<String>emptySet()); rule.setSystemTags(Collections.<String>emptySet());
rule.setTags(Collections.<String>emptySet()); rule.setTags(Collections.<String>emptySet());
dbClient.deprecatedRuleDao().update(session, rule); update(session, rule);
removedRules.add(rule); removedRules.add(rule);
if (removedRules.size() % 100 == 0) { if (removedRules.size() % 100 == 0) {
session.commit(); session.commit();
Expand Down Expand Up @@ -423,7 +441,7 @@ private static boolean updateCustomRuleFromTemplateRule(RuleDto customRule, Rule
private void removeActiveRulesOnStillExistingRepositories(DbSession session, Collection<RuleDto> removedRules, RulesDefinition.Context context) { private void removeActiveRulesOnStillExistingRepositories(DbSession session, Collection<RuleDto> removedRules, RulesDefinition.Context context) {
List<String> repositoryKeys = newArrayList(Iterables.transform(context.repositories(), new Function<RulesDefinition.Repository, String>() { List<String> repositoryKeys = newArrayList(Iterables.transform(context.repositories(), new Function<RulesDefinition.Repository, String>() {
@Override @Override
public String apply(RulesDefinition.Repository input) { public String apply(@Nonnull RulesDefinition.Repository input) {
return input.key(); return input.key();
} }
} }
Expand All @@ -436,4 +454,10 @@ public String apply(RulesDefinition.Repository input) {
} }
} }
} }

private void update(DbSession session, RuleDto rule){
rule.setUpdatedAtInMs(system2.now());
rule.setUpdatedAt(new Date(system2.now()));
dbClient.ruleDao().update(session, rule);
}
} }
Expand Up @@ -20,6 +20,8 @@
package org.sonar.server.rule.db; package org.sonar.server.rule.db;


import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import java.util.List;
import javax.annotation.CheckForNull;
import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.System2; import org.sonar.api.utils.System2;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
Expand All @@ -29,10 +31,6 @@
import org.sonar.server.db.BaseDao; import org.sonar.server.db.BaseDao;
import org.sonar.server.search.IndexDefinition; import org.sonar.server.search.IndexDefinition;


import javax.annotation.CheckForNull;

import java.util.List;

public class RuleDao extends BaseDao<RuleMapper, RuleDto, RuleKey> { public class RuleDao extends BaseDao<RuleMapper, RuleDto, RuleKey> {


public RuleDao(System2 system) { public RuleDao(System2 system) {
Expand Down Expand Up @@ -65,11 +63,7 @@ protected void doDeleteByKey(DbSession session, RuleKey key) {
throw new UnsupportedOperationException("Rules cannot be deleted"); throw new UnsupportedOperationException("Rules cannot be deleted");
} }


/**
* @deprecated use keys.
*/
@CheckForNull @CheckForNull
@Deprecated
public RuleDto selectById(DbSession session, int id) { public RuleDto selectById(DbSession session, int id) {
return mapper(session).selectById(id); return mapper(session).selectById(id);
} }
Expand Down
Expand Up @@ -75,6 +75,7 @@


import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Lists.newArrayList;


@Deprecated
public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> { public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {


public static final String FACET_LANGUAGES = "languages"; public static final String FACET_LANGUAGES = "languages";
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/ */
package org.sonar.server.rule.index; package org.sonar.server.rule.index;


import com.google.common.annotations.VisibleForTesting;
import java.util.Iterator; import java.util.Iterator;
import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexRequest;
import org.sonar.db.DbClient; import org.sonar.db.DbClient;
Expand All @@ -45,7 +46,8 @@ protected long doIndex(long lastUpdatedAt) {
return doIndex(createBulkIndexer(false), lastUpdatedAt); return doIndex(createBulkIndexer(false), lastUpdatedAt);
} }


public void index(Iterator<RuleDoc> rules) { @VisibleForTesting
void index(Iterator<RuleDoc> rules) {
doIndex(createBulkIndexer(false), rules); doIndex(createBulkIndexer(false), rules);
} }


Expand Down
Expand Up @@ -27,6 +27,8 @@
import org.sonar.api.rule.RuleStatus; import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity; import org.sonar.api.rule.Severity;


import static java.util.Arrays.asList;

public class RuleQuery { public class RuleQuery {


private String key; private String key;
Expand Down Expand Up @@ -135,6 +137,13 @@ public RuleQuery setSeverities(@Nullable Collection<String> severities) {
return this; return this;
} }


public RuleQuery setSeverities(@Nullable String... severities) {
if (severities != null) {
return setSeverities(asList(severities));
}
return this;
}

@CheckForNull @CheckForNull
public Collection<RuleStatus> getStatuses() { public Collection<RuleStatus> getStatuses() {
return statuses; return statuses;
Expand Down
Expand Up @@ -22,12 +22,14 @@
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.Collections2; import com.google.common.collect.Collections2;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.annotation.Nonnull;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.math.RandomUtils; import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang.reflect.ConstructorUtils; import org.apache.commons.lang.reflect.ConstructorUtils;
Expand Down Expand Up @@ -220,6 +222,10 @@ public T apply(SearchHit input) {
})); }));
} }


public List<String> getIds(String indexName, String typeName){
return FluentIterable.from(getDocuments(indexName, typeName)).transform(SearchHitToId.INSTANCE).toList();
}

public Node node() { public Node node() {
return node; return node;
} }
Expand All @@ -228,4 +234,13 @@ public EsClient client() {
return client; return client;
} }


private enum SearchHitToId implements Function<SearchHit, String>{
INSTANCE;

@Override
public String apply(@Nonnull org.elasticsearch.search.SearchHit input) {
return input.id();
}
}

} }

0 comments on commit 743fac7

Please sign in to comment.