import org.sonar.server.platform.ServerLifecycleNotifier;
import org.sonar.server.platform.web.RegisterServletFilters;
import org.sonar.server.qualitygate.RegisterQualityGates;
+import org.sonar.server.qualityprofile.CachingRuleActivator;
import org.sonar.server.qualityprofile.RegisterQualityProfiles;
import org.sonar.server.rule.RegisterRules;
import org.sonar.server.startup.ClearRulesOverloadedDebt;
IndexerStartupTask.class,
RegisterMetrics.class,
RegisterQualityGates.class,
+ CachingRuleActivator.class,
RegisterRules.class,
RegisterQualityProfiles.class,
RegisterPermissionTemplates.class,
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.qualityprofile;
+
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import java.util.List;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.qualityprofile.QualityProfileDto;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
+import org.sonar.server.rule.index.RuleIndex;
+import org.sonar.server.user.UserSession;
+import org.sonar.server.util.TypeValidations;
+
+public class CachingRuleActivator extends RuleActivator {
+ private final Cache<String, List<QualityProfileDto>> childrenByParentKey = CacheBuilder.newBuilder()
+ .maximumSize(10_000)
+ .build();
+
+ public CachingRuleActivator(System2 system2, DbClient db, RuleIndex ruleIndex, RuleActivatorContextFactory contextFactory, TypeValidations typeValidations,
+ ActiveRuleIndexer activeRuleIndexer, UserSession userSession) {
+ super(system2, db, ruleIndex, contextFactory, typeValidations, activeRuleIndexer, userSession);
+ }
+
+ @Override
+ protected List<QualityProfileDto> getChildren(DbSession session, String qualityProfileKey) {
+ List<QualityProfileDto> res = childrenByParentKey.getIfPresent(qualityProfileKey);
+ if (res != null) {
+ return res;
+ }
+ res = super.getChildren(session, qualityProfileKey);
+ childrenByParentKey.put(qualityProfileKey, res);
+ return res;
+ }
+
+}
* To be kept when no ProfileDefinition are injected
*/
public RegisterQualityProfiles(DbClient dbClient,
- QProfileFactory profileFactory, RuleActivator ruleActivator, Languages languages, ActiveRuleIndexer activeRuleIndexer,
+ QProfileFactory profileFactory, CachingRuleActivator ruleActivator, Languages languages, ActiveRuleIndexer activeRuleIndexer,
DefaultOrganizationProvider defaultOrganizationProvider) {
this(dbClient, profileFactory, ruleActivator, Collections.emptyList(), languages, activeRuleIndexer, defaultOrganizationProvider);
}
public RegisterQualityProfiles(DbClient dbClient,
- QProfileFactory profileFactory, RuleActivator ruleActivator,
+ QProfileFactory profileFactory, CachingRuleActivator ruleActivator,
List<ProfileDefinition> definitions, Languages languages, ActiveRuleIndexer activeRuleIndexer,
DefaultOrganizationProvider defaultOrganizationProvider) {
this.dbClient = dbClient;
List<ActiveRuleChange> changes = Lists.newArrayList();
// get all inherited profiles
- List<QualityProfileDto> children = db.qualityProfileDao().selectChildren(session, qualityProfileDto.getKey());
+ String qualityProfileKey = qualityProfileDto.getKey();
+ List<QualityProfileDto> children = getChildren(session, qualityProfileKey);
for (QualityProfileDto child : children) {
RuleActivation childActivation = new RuleActivation(activation).setCascade(true);
changes.addAll(activate(session, childActivation, child));
return changes;
}
+ protected List<QualityProfileDto> getChildren(DbSession session, String qualityProfileKey) {
+ return db.qualityProfileDao().selectChildren(session, qualityProfileKey);
+ }
+
private ActiveRuleDto persist(ActiveRuleChange change, RuleActivatorContext context, DbSession dbSession) {
ActiveRuleDto activeRule = null;
if (change.getType() == ActiveRuleChange.Type.ACTIVATED) {
persist(change, context, dbSession);
// get all inherited profiles
- List<QualityProfileDto> profiles = db.qualityProfileDao().selectChildren(dbSession, key.qProfile());
+ List<QualityProfileDto> profiles = getChildren(dbSession, key.qProfile());
for (QualityProfileDto profile : profiles) {
ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), key.ruleKey());
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.qualityprofile;
+
+import java.util.Arrays;
+import java.util.stream.Collectors;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.qualityprofile.QualityProfileDao;
+import org.sonar.db.qualityprofile.QualityProfileDto;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Matchers.anyBoolean;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+public class CachingRuleActivatorTest {
+ private DbClient dbClient = mock(DbClient.class);
+ private DbSession dbSession = mock(DbSession.class);
+ private QualityProfileDao qualityProfileDao = mock(QualityProfileDao.class);
+ private CachingRuleActivator underTest = new CachingRuleActivator(null, dbClient, null, null, null, null, null);
+
+ @Before
+ public void wire_mocks() throws Exception {
+ when(dbClient.openSession(anyBoolean())).thenReturn(dbSession);
+ when(dbClient.qualityProfileDao()).thenReturn(qualityProfileDao);
+ }
+
+ @Test
+ public void getChildren_caches_that_qp_has_no_children() {
+ mockSelectChildrenForKey("no_children");
+
+ assertThat(underTest.getChildren(dbSession, "no_children"))
+ .isEmpty();
+ assertThat(underTest.getChildren(dbSession, "no_children"))
+ .isEmpty();
+ assertThat(underTest.getChildren(dbSession, "no_children"))
+ .isEmpty();
+ verify(qualityProfileDao, times(1)).selectChildren(eq(dbSession), anyString());
+ }
+
+ @Test
+ public void getChildren_caches_that_sq_has_one_or_more_children() {
+ mockSelectChildrenForKey("0", "1");
+ mockSelectChildrenForKey("1", "2", "3");
+
+ assertThat(underTest.getChildren(dbSession, "0"))
+ .extracting(QualityProfileDto::getKey)
+ .containsExactly("1");
+ assertThat(underTest.getChildren(dbSession, "0"))
+ .extracting(QualityProfileDto::getKey)
+ .containsExactly("1");
+ assertThat(underTest.getChildren(dbSession, "0"))
+ .extracting(QualityProfileDto::getKey)
+ .containsExactly("1");
+ assertThat(underTest.getChildren(dbSession, "1"))
+ .extracting(QualityProfileDto::getKey)
+ .containsExactly("2", "3");
+ assertThat(underTest.getChildren(dbSession, "1"))
+ .extracting(QualityProfileDto::getKey)
+ .containsExactly("2", "3");
+ assertThat(underTest.getChildren(dbSession, "1"))
+ .extracting(QualityProfileDto::getKey)
+ .containsExactly("2", "3");
+ verify(qualityProfileDao, times(1)).selectChildren(dbSession, "0");
+ verify(qualityProfileDao, times(1)).selectChildren(dbSession, "1");
+ verifyNoMoreInteractions(qualityProfileDao);
+ }
+
+ private void mockSelectChildrenForKey(String key, String... children) {
+ when(qualityProfileDao.selectChildren(dbSession, key))
+ .thenReturn(Arrays.stream(children).map(this::dto).collect(Collectors.toList()))
+ .thenThrow(new IllegalStateException("selectChildren should be called only once for key " + key));
+ }
+
+ private QualityProfileDto dto(String key) {
+ return new QualityProfileDto() {
+ @Override
+ public String toString() {
+ return getKey();
+ }
+ }.setKey(key);
+ }
+}