package org.sonar.core.computation.dbcleaner;
+import java.util.Date;
+import javax.annotation.Nullable;
import org.sonar.api.CoreProperties;
-import org.sonar.api.server.ServerSide;
import org.sonar.api.config.Settings;
+import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.TimeUtils;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.core.purge.PurgeProfiler;
import org.sonar.server.issue.index.IssueIndex;
-import javax.annotation.Nullable;
-
-import java.util.Date;
-
import static org.sonar.core.purge.PurgeConfiguration.newDefaultPurgeConfiguration;
@ServerSide
*/
package org.sonar.server.platform.platformlevel;
+import java.util.List;
import org.sonar.api.config.EmailSettings;
import org.sonar.api.issue.action.Actions;
import org.sonar.api.profiles.AnnotationProfileParser;
import org.sonar.api.rules.AnnotationRuleParser;
import org.sonar.api.rules.XMLRuleParser;
import org.sonar.api.server.rule.RulesDefinitionXmlLoader;
-import org.sonar.core.computation.dbcleaner.DefaultPurgeTask;
import org.sonar.core.computation.dbcleaner.IndexPurgeListener;
import org.sonar.core.computation.dbcleaner.ProjectCleaner;
import org.sonar.core.computation.dbcleaner.period.DefaultPeriodCleaner;
import org.sonar.server.ws.ListingWs;
import org.sonar.server.ws.WebServiceEngine;
-import java.util.List;
-
public class PlatformLevel4 extends PlatformLevel {
private final List<Object> level4AddedComponents;
QueueAction.class,
HistoryAction.class,
DefaultPeriodCleaner.class,
- DefaultPurgeTask.class,
ProjectCleaner.class,
ProjectSettingsFactory.class,
IndexPurgeListener.class,
package org.sonar.batch.bootstrap;
import com.google.common.collect.Lists;
+import java.util.Collection;
+import java.util.List;
import org.sonar.batch.components.TimeMachineConfiguration;
import org.sonar.batch.cpd.CpdComponents;
import org.sonar.batch.debt.DebtDecorator;
import org.sonar.batch.scm.ScmSensor;
import org.sonar.batch.source.CodeColorizerSensor;
import org.sonar.batch.source.LinesSensor;
-import org.sonar.core.computation.dbcleaner.DefaultPurgeTask;
-import org.sonar.core.computation.dbcleaner.period.DefaultPeriodCleaner;
import org.sonar.core.config.CorePropertyDefinitions;
import org.sonar.core.notification.DefaultNotificationManager;
-import java.util.Collection;
-import java.util.List;
-
public class BatchComponents {
private BatchComponents() {
// only static stuff
SourceProvider.class,
RuleNameProvider.class,
- // dbcleaner
- DefaultPeriodCleaner.class,
- DefaultPurgeTask.class,
-
QualityGateVerifier.class,
// language
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.core.computation.dbcleaner;
-
-import com.google.common.annotations.VisibleForTesting;
-import org.sonar.api.CoreProperties;
-import org.sonar.api.config.Settings;
-import org.sonar.api.resources.Qualifiers;
-import org.sonar.api.utils.TimeUtils;
-import org.sonar.api.utils.log.Logger;
-import org.sonar.api.utils.log.Loggers;
-import org.sonar.core.computation.dbcleaner.period.DefaultPeriodCleaner;
-import org.sonar.core.purge.IdUuidPair;
-import org.sonar.core.purge.PurgeConfiguration;
-import org.sonar.core.purge.PurgeDao;
-import org.sonar.core.purge.PurgeListener;
-import org.sonar.core.purge.PurgeProfiler;
-import org.sonar.core.resource.ResourceDao;
-import org.sonar.core.resource.ResourceDto;
-import org.sonar.plugins.dbcleaner.api.PurgeTask;
-
-import static org.sonar.core.purge.PurgeConfiguration.newDefaultPurgeConfiguration;
-
-/**
- * @since 2.14
- */
-public class DefaultPurgeTask implements PurgeTask {
- private static final Logger LOG = Loggers.get(DefaultPurgeTask.class);
- private final PurgeProfiler profiler;
- private final PurgeDao purgeDao;
- private final ResourceDao resourceDao;
- private final Settings settings;
- private final DefaultPeriodCleaner periodCleaner;
-
- public DefaultPurgeTask(PurgeDao purgeDao, ResourceDao resourceDao, Settings settings, DefaultPeriodCleaner periodCleaner, PurgeProfiler profiler) {
- this.purgeDao = purgeDao;
- this.resourceDao = resourceDao;
- this.settings = settings;
- this.periodCleaner = periodCleaner;
- this.profiler = profiler;
- }
-
- @Override
- public DefaultPurgeTask delete(long resourceId) {
- ResourceDto resource = resourceDao.getResource(resourceId);
- if (resource != null) {
- purgeDao.deleteResourceTree(new IdUuidPair(resource.getId(), resource.getUuid()));
- }
-
- return this;
- }
-
- @VisibleForTesting
- boolean isNotViewNorSubview(String resourceQualifier) {
- return !(Qualifiers.VIEW.equals(resourceQualifier) || Qualifiers.SUBVIEW.equals(resourceQualifier));
- }
-
- @Override
- public DefaultPurgeTask purge(long resourceId) {
- long start = System.currentTimeMillis();
- ResourceDto component = resourceDao.getResource(resourceId);
- if (isNotViewNorSubview(component.getQualifier())) {
- profiler.reset();
- cleanHistoricalData(resourceId);
- doPurge(new IdUuidPair(component.getId(), component.getUuid()));
- if (settings.getBoolean(CoreProperties.PROFILING_LOG_PROPERTY)) {
- long duration = System.currentTimeMillis() - start;
- LOG.info("\n -------- Profiling for purge: " + TimeUtils.formatDuration(duration) + " --------\n");
- profiler.dump(duration, LOG);
- LOG.info("\n -------- End of profiling for purge --------\n");
- }
- }
- return this;
- }
-
- private void cleanHistoricalData(long resourceId) {
- try {
- periodCleaner.clean(resourceId);
- } catch (Exception e) {
- // purge errors must not fail the batch
- LOG.error("Fail to clean historical data [id=" + resourceId + "]", e);
- }
- }
-
- private void doPurge(IdUuidPair componentIdUuid) {
- try {
- purgeDao.purge(newPurgeConfigurationOnResource(componentIdUuid), PurgeListener.EMPTY);
- } catch (Exception e) {
- // purge errors must not fail the report analysis
- LOG.error("Fail to purge data [id=" + componentIdUuid + "]", e);
- }
- }
-
- public PurgeConfiguration newPurgeConfigurationOnResource(IdUuidPair componentIdUuid) {
- return newDefaultPurgeConfiguration(settings, componentIdUuid);
- }
-}
package org.sonar.core.computation.dbcleaner.period;
import com.google.common.annotations.VisibleForTesting;
-import org.sonar.api.batch.BatchSide;
-import org.sonar.api.server.ServerSide;
+import java.util.List;
import org.sonar.api.config.Settings;
+import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
import org.sonar.core.purge.PurgeDao;
import org.sonar.core.purge.PurgeSnapshotQuery;
import org.sonar.core.purge.PurgeableSnapshotDto;
-import java.util.List;
-
-@BatchSide
@ServerSide
public class DefaultPeriodCleaner {
private static final Logger LOG = Loggers.get(DefaultPeriodCleaner.class);
private PurgeDao purgeDao;
- private Settings settings;
- private MyBatis mybatis;
- public DefaultPeriodCleaner(PurgeDao purgeDao, Settings settings, MyBatis mybatis) {
+ public DefaultPeriodCleaner(PurgeDao purgeDao) {
this.purgeDao = purgeDao;
- this.settings = settings;
- this.mybatis = mybatis;
- }
-
- public void clean(long projectId) {
- clean(projectId, settings);
- }
-
- public void clean(long projectId, Settings settings) {
- DbSession session = mybatis.openSession(true);
- try {
- doClean(projectId, new Filters(settings).all(), session);
- } finally {
- MyBatis.closeQuietly(session);
- }
}
public void clean(DbSession session, long projectId, Settings settings) {
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.plugins.dbcleaner.api;
-
-/**
- * @deprecated (is used by the plugin views only. Will rely on the implementation when views analysis views moved on the server side)
- */
-@Deprecated
-public interface DbCleanerConstants extends org.sonar.core.computation.dbcleaner.DbCleanerConstants {
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.plugins.dbcleaner.api;
-
-import org.sonar.api.batch.BatchSide;
-import org.sonar.api.server.ServerSide;
-
-/**
- * @since 2.14
- * @deprecated (is used by the plugin views only. Will rely on the implementation when views analysis views moved on the server side)
- */
-@Deprecated
-@BatchSide
-@ServerSide
-public interface PurgeTask {
- /**
- * Purges the data related to a tree of resources.
- * <p/>
- * Exceptions are logged and are not thrown again, so this method fails only on {@link Error}s.
- *
- * @param resourceId the root of the tree
- * @return this
- */
- PurgeTask purge(long resourceId);
-
- /**
- * Completely deletes a tree of resources.
- *
- * @param resourceId the root of the tree
- * @return this
- */
- PurgeTask delete(long resourceId);
-}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.
- */
-
-@ParametersAreNonnullByDefault
-package org.sonar.plugins.dbcleaner.api;
-
-import javax.annotation.ParametersAreNonnullByDefault;
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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.core.computation.dbcleaner;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.ArgumentMatcher;
-import org.sonar.api.CoreProperties;
-import org.sonar.api.config.PropertyDefinitions;
-import org.sonar.api.config.Settings;
-import org.sonar.api.resources.Qualifiers;
-import org.sonar.api.resources.Scopes;
-import org.sonar.api.utils.log.Logger;
-import org.sonar.core.computation.dbcleaner.period.DefaultPeriodCleaner;
-import org.sonar.core.purge.IdUuidPair;
-import org.sonar.core.purge.PurgeConfiguration;
-import org.sonar.core.purge.PurgeDao;
-import org.sonar.core.purge.PurgeListener;
-import org.sonar.core.purge.PurgeProfiler;
-import org.sonar.core.resource.ResourceDao;
-import org.sonar.core.resource.ResourceDto;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.*;
-
-public class DefaultPurgeTaskTest {
-
- private DefaultPurgeTask sut;
- private ResourceDao resourceDao;
- private PurgeDao purgeDao;
- private Settings settings;
- private DefaultPeriodCleaner periodCleaner;
- private PurgeProfiler profiler;
-
- @Before
- public void before() {
- this.purgeDao = mock(PurgeDao.class);
- this.resourceDao = mock(ResourceDao.class);
- when(resourceDao.getResource(anyLong())).thenReturn(new ResourceDto().setQualifier(Qualifiers.PROJECT).setUuid("1").setId(1L));
-
- this.settings = mock(Settings.class);
- this.periodCleaner = mock(DefaultPeriodCleaner.class);
- this.profiler = mock(PurgeProfiler.class);
-
- this.sut = new DefaultPurgeTask(purgeDao, resourceDao, settings, periodCleaner, profiler);
- }
-
- @Test
- public void shouldNotDeleteHistoricalDataOfDirectories() {
- PurgeDao purgeDao = mock(PurgeDao.class);
- Settings settings = new Settings(new PropertyDefinitions(DataCleanerProperties.all()));
- settings.setProperty(DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY, "false");
- DefaultPurgeTask task = new DefaultPurgeTask(purgeDao, resourceDao, settings, mock(DefaultPeriodCleaner.class), mock(PurgeProfiler.class));
-
- task.purge(1L);
-
- verify(purgeDao).purge(argThat(new ArgumentMatcher<PurgeConfiguration>() {
- @Override
- public boolean matches(Object o) {
- PurgeConfiguration conf = (PurgeConfiguration) o;
- return conf.rootProjectIdUuid().getId() == 1L && conf.scopesWithoutHistoricalData().length == 1 && conf.scopesWithoutHistoricalData()[0].equals(Scopes.FILE);
- }
- }), any(PurgeListener.class));
- }
-
- @Test
- public void shouldDeleteHistoricalDataOfDirectoriesByDefault() {
- PurgeDao purgeDao = mock(PurgeDao.class);
- Settings settings = new Settings(new PropertyDefinitions(DataCleanerProperties.all()));
- DefaultPurgeTask task = new DefaultPurgeTask(purgeDao, resourceDao, settings, mock(DefaultPeriodCleaner.class), mock(PurgeProfiler.class));
-
- task.purge(1L);
-
- verify(purgeDao).purge(argThat(new ArgumentMatcher<PurgeConfiguration>() {
- @Override
- public boolean matches(Object o) {
- PurgeConfiguration conf = (PurgeConfiguration) o;
- return conf.rootProjectIdUuid().getId() == 1L &&
- conf.scopesWithoutHistoricalData().length == 2 &&
- conf.scopesWithoutHistoricalData()[0].equals(Scopes.DIRECTORY) &&
- conf.scopesWithoutHistoricalData()[1].equals(Scopes.FILE);
- }
- }), any(PurgeListener.class));
- }
-
- @Test
- public void shouldNotFailOnErrors() {
- PurgeDao purgeDao = mock(PurgeDao.class);
- when(purgeDao.purge(any(PurgeConfiguration.class), any(PurgeListener.class))).thenThrow(new RuntimeException());
- DefaultPurgeTask task = new DefaultPurgeTask(purgeDao, resourceDao, new Settings(), mock(DefaultPeriodCleaner.class), mock(PurgeProfiler.class));
-
- task.purge(1L);
-
- verify(purgeDao, times(1)).purge(any(PurgeConfiguration.class), any(PurgeListener.class));
- }
-
- @Test
- public void shouldDumpProfiling() {
- PurgeConfiguration conf = new PurgeConfiguration(new IdUuidPair(1L, "1"), new String[0], 30);
- PurgeDao purgeDao = mock(PurgeDao.class);
- when(purgeDao.purge(conf, PurgeListener.EMPTY)).thenThrow(new RuntimeException());
- Settings settings = new Settings(new PropertyDefinitions(DataCleanerProperties.all()));
- settings.setProperty(CoreProperties.PROFILING_LOG_PROPERTY, true);
- PurgeProfiler profiler = mock(PurgeProfiler.class);
-
- DefaultPurgeTask task = new DefaultPurgeTask(purgeDao, resourceDao, settings, mock(DefaultPeriodCleaner.class), profiler);
- task.purge(1L);
-
- verify(profiler).dump(anyLong(), any(Logger.class));
- }
-
- @Test
- public void recognize_view_and_subview() {
- boolean viewCheck = sut.isNotViewNorSubview(Qualifiers.VIEW);
- boolean subViewCheck = sut.isNotViewNorSubview(Qualifiers.SUBVIEW);
-
- assertThat(viewCheck).isFalse();
- assertThat(subViewCheck).isFalse();
- }
-
- @Test
- public void call_dao_delete_when_deleting() {
- when(resourceDao.getResource(123L)).thenReturn(new ResourceDto().setId(123L).setUuid("A"));
-
- sut.delete(123L);
-
- verify(purgeDao, times(1)).deleteResourceTree(any(IdUuidPair.class));
- }
-}
package org.sonar.core.computation.dbcleaner.period;
+import java.util.Arrays;
import org.apache.commons.lang.ObjectUtils;
import org.hamcrest.BaseMatcher;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
-import org.sonar.api.config.Settings;
import org.sonar.api.utils.System2;
import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
import org.sonar.core.purge.PurgeDao;
import org.sonar.core.purge.PurgeSnapshotQuery;
import org.sonar.core.purge.PurgeableSnapshotDto;
-import java.util.Arrays;
-
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.anyListOf;
+import static org.mockito.Mockito.argThat;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
public class DefaultPeriodCleanerTest {
Filter filter1 = newLazyFilter();
Filter filter2 = newLazyFilter();
- DefaultPeriodCleaner cleaner = new DefaultPeriodCleaner(dao, mock(Settings.class), mock(MyBatis.class));
+ DefaultPeriodCleaner cleaner = new DefaultPeriodCleaner(dao);
cleaner.doClean(123L, Arrays.asList(filter1, filter2), session);
verify(filter1).log();