diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2017-10-17 18:16:28 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk@users.noreply.github.com> | 2017-10-19 12:12:11 +0200 |
commit | 8cbb514261e2af99b4ee7f79a5c1210e1cee3714 (patch) | |
tree | 021da2b2a022f581649e37f71f281231a8e0a6f8 /server | |
parent | d9564eed6baf772c31d3ee2888dffc7d8c51fca2 (diff) | |
download | sonarqube-8cbb514261e2af99b4ee7f79a5c1210e1cee3714.tar.gz sonarqube-8cbb514261e2af99b4ee7f79a5c1210e1cee3714.zip |
SONAR-7309 DB Cleaner strategy to keep only analyses with a version
Diffstat (limited to 'server')
9 files changed, 128 insertions, 12 deletions
diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java index 9f02e6aa47c..e70437c795b 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java @@ -114,7 +114,7 @@ public class ComputeEngineContainerImplTest { + 26 // level 1 + 52 // content of DaoModule + 3 // content of EsSearchModule - + 66 // content of CorePropertyDefinitions + + 67 // content of CorePropertyDefinitions + 1 // StopFlagContainer ); assertThat( diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeableAnalysisDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeableAnalysisDto.java index 1d91f886174..1d8f8116651 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeableAnalysisDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeableAnalysisDto.java @@ -20,16 +20,19 @@ package org.sonar.db.purge; import java.util.Date; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; /** - * Represents an analysis, aka. root snapshot, aka. snapshot of a project, developer or view + * Represents an analysis, aka. root snapshot, aka. snapshot of a project or portfolio */ public class PurgeableAnalysisDto implements Comparable<PurgeableAnalysisDto> { private Date date; private long analysisId; private String analysisUuid; + private String version; private boolean hasEvents; private boolean isLast; @@ -78,6 +81,16 @@ public class PurgeableAnalysisDto implements Comparable<PurgeableAnalysisDto> { return this; } + @CheckForNull + public String getVersion() { + return version; + } + + public PurgeableAnalysisDto setVersion(@Nullable String version) { + this.version = version; + return this; + } + @Override public int compareTo(PurgeableAnalysisDto other) { return date.compareTo(other.date); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/Filters.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/Filters.java index 8ed4a3c7a6b..8c550e30760 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/Filters.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/Filters.java @@ -34,11 +34,13 @@ class Filters { Date dateToStartKeepingOneSnapshotByDay = getDateFromHours(config, PurgeConstants.HOURS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_DAY); Date dateToStartKeepingOneSnapshotByWeek = getDateFromWeeks(config, PurgeConstants.WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_WEEK); Date dateToStartKeepingOneSnapshotByMonth = getDateFromWeeks(config, PurgeConstants.WEEKS_BEFORE_KEEPING_ONLY_ONE_SNAPSHOT_BY_MONTH); + Date dateToStartKeepingOnlyAnalysisWithVersion = getDateFromWeeks(config, PurgeConstants.WEEKS_BEFORE_KEEPING_ONLY_ANALYSES_WITH_VERSION); Date dateToStartDeletingAllSnapshots = getDateFromWeeks(config, PurgeConstants.WEEKS_BEFORE_DELETING_ALL_SNAPSHOTS); all.add(new KeepOneFilter(dateToStartKeepingOneSnapshotByWeek, dateToStartKeepingOneSnapshotByDay, Calendar.DAY_OF_YEAR, "day")); all.add(new KeepOneFilter(dateToStartKeepingOneSnapshotByMonth, dateToStartKeepingOneSnapshotByWeek, Calendar.WEEK_OF_YEAR, "week")); all.add(new KeepOneFilter(dateToStartDeletingAllSnapshots, dateToStartKeepingOneSnapshotByMonth, Calendar.MONTH, "month")); + all.add(new KeepWithVersionFilter(dateToStartKeepingOnlyAnalysisWithVersion)); all.add(new DeleteAllFilter(dateToStartDeletingAllSnapshots)); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepOneFilter.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepOneFilter.java index e67a4c9657a..cc5054cbbec 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepOneFilter.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepOneFilter.java @@ -57,7 +57,7 @@ class KeepOneFilter implements Filter { Loggers.get(getClass()).debug("-> Keep one snapshot per {} between {} and {}", label, DateUtils.formatDate(start), DateUtils.formatDate(end)); } - private void appendSnapshotsToDelete(Interval interval, List<PurgeableAnalysisDto> toDelete) { + private static void appendSnapshotsToDelete(Interval interval, List<PurgeableAnalysisDto> toDelete) { if (interval.count() > 1) { List<PurgeableAnalysisDto> deletables = Lists.newArrayList(); List<PurgeableAnalysisDto> toKeep = Lists.newArrayList(); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepWithVersionFilter.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepWithVersionFilter.java new file mode 100644 index 00000000000..d692bc6c298 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/period/KeepWithVersionFilter.java @@ -0,0 +1,55 @@ +/* + * 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.db.purge.period; + +import com.google.common.base.Strings; +import java.util.Date; +import java.util.List; +import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.log.Loggers; +import org.sonar.core.util.stream.MoreCollectors; +import org.sonar.db.purge.PurgeableAnalysisDto; + +class KeepWithVersionFilter implements Filter { + + private final Date before; + + KeepWithVersionFilter(Date before) { + this.before = before; + } + + @Override + public List<PurgeableAnalysisDto> filter(List<PurgeableAnalysisDto> history) { + return history.stream() + .filter(analysis -> analysis.getDate().before(before)) + .filter(KeepWithVersionFilter::isDeletable) + .collect(MoreCollectors.toList()); + } + + @Override + public void log() { + Loggers.get(getClass()).debug("-> Keep analyses with a version prior to {}", DateUtils.formatDate(before)); + } + + private static boolean isDeletable(PurgeableAnalysisDto snapshot) { + return !snapshot.isLast() && Strings.isNullOrEmpty(snapshot.getVersion()); + } + +} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml index 1316351dced..36f668e5626 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml @@ -27,9 +27,9 @@ <select id="selectPurgeableAnalysesWithEvents" parameterType="String" resultType="PurgeableAnalysis"> select - s.id as "analysisId", s.uuid as "analysisUuid", s.created_at as "date", ${_true} as "hasEvents", islast as "isLast" - from - snapshots s + s.id as "analysisId", s.uuid as "analysisUuid", s.created_at as "date", ${_true} as "hasEvents", islast as "isLast", ve.name as "version" + from snapshots s + left outer join events ve on ve.analysis_uuid=s.uuid and ve.category='Version' where s.component_uuid=#{componentUuid,jdbcType=VARCHAR} and s.status='P' @@ -38,9 +38,8 @@ <select id="selectPurgeableAnalysesWithoutEvents" parameterType="String" resultType="PurgeableAnalysis"> select - s.id as "analysisId", s.uuid as "analysisUuid", s.created_at as "date", ${_false} as "hasEvents", islast as "isLast" - from - snapshots s + s.id as "analysisId", s.uuid as "analysisUuid", s.created_at as "date", ${_false} as "hasEvents", islast as "isLast", NULL as "version" + from snapshots s where s.component_uuid=#{componentUuid,jdbcType=VARCHAR} and s.status='P' diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java index 39698bf8df4..e48f5c90b70 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/PurgeDaoTest.java @@ -161,17 +161,20 @@ public class PurgeDaoTest { } @Test - public void shouldSelectPurgeableAnalysis() { + public void selectPurgeableAnalyses() { dbTester.prepareDbUnit(getClass(), "shouldSelectPurgeableAnalysis.xml"); List<PurgeableAnalysisDto> analyses = underTest.selectPurgeableAnalyses(THE_PROJECT_UUID, dbSession); assertThat(analyses).hasSize(3); assertThat(getById(analyses, "u1").isLast()).isTrue(); assertThat(getById(analyses, "u1").hasEvents()).isFalse(); + assertThat(getById(analyses, "u1").getVersion()).isNull(); assertThat(getById(analyses, "u4").isLast()).isFalse(); assertThat(getById(analyses, "u4").hasEvents()).isFalse(); + assertThat(getById(analyses, "u4").getVersion()).isNull(); assertThat(getById(analyses, "u5").isLast()).isFalse(); assertThat(getById(analyses, "u5").hasEvents()).isTrue(); + assertThat(getById(analyses, "u5").getVersion()).isEqualTo("V5"); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/purge/period/KeepWithVersionFilterTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/period/KeepWithVersionFilterTest.java new file mode 100644 index 00000000000..5d30b47172e --- /dev/null +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/purge/period/KeepWithVersionFilterTest.java @@ -0,0 +1,44 @@ +/* + * 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.db.purge.period; + +import java.util.Arrays; +import java.util.List; +import org.junit.Test; +import org.sonar.db.purge.DbCleanerTestUtils; +import org.sonar.db.purge.PurgeableAnalysisDto; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.api.utils.DateUtils.parseDate; + +public class KeepWithVersionFilterTest { + + @Test + public void keep_only_analyses_with_a_version() { + Filter underTest = new KeepWithVersionFilter(parseDate("2015-10-18")); + + List<PurgeableAnalysisDto> result = underTest.filter(Arrays.asList( + DbCleanerTestUtils.createAnalysisWithDate("u1", "2015-10-17").setVersion("V1"), + DbCleanerTestUtils.createAnalysisWithDate("u2", "2015-10-17").setVersion(null), + DbCleanerTestUtils.createAnalysisWithDate("u3", "2015-10-19").setVersion(null))); + + assertThat(result).extracting(PurgeableAnalysisDto::getAnalysisUuid).containsExactlyInAnyOrder("u2"); + } +} diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldSelectPurgeableAnalysis.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldSelectPurgeableAnalysis.xml index 19482e742cb..e1c341d36b0 100644 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldSelectPurgeableAnalysis.xml +++ b/server/sonar-db-dao/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldSelectPurgeableAnalysis.xml @@ -132,7 +132,7 @@ period5_date="[null]" created_at="1228222680000" build_date="1228222680000" - version="[null]" + version="V5" /> <events id="2" @@ -141,7 +141,7 @@ component_uuid="1" category="Version" description="[null]" - name="Version 1.0" + name="V5" event_date="1228222680000" created_at="1228222680000" event_data="[null]"/> |