]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7705 replace purge by cpt of scope PRJ by purge by analyses
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 30 Jun 2016 11:07:12 +0000 (13:07 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 4 Jul 2016 13:20:30 +0000 (15:20 +0200)
sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java
sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java
sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java
sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/disable_resources_without_last_snapshot-result.xml
sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/disable_resources_without_last_snapshot.xml
sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldPurgeProject-result.xml
sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/shouldPurgeProject.xml

index 3273834f56acd3b40f6e5b3669b23950918b3694..7e728f84ca0746a1ba1749e68c167eac238e7911 100644 (file)
@@ -54,6 +54,10 @@ class PurgeCommands {
     return purgeMapper.selectSnapshotIdsAndUuids(query).stream().map(IdUuidPair::getUuid).collect(Collectors.toList());
   }
 
+  List<IdUuidPair> selectSnapshotIdUuids(PurgeSnapshotQuery query) {
+    return purgeMapper.selectSnapshotIdsAndUuids(query);
+  }
+
   void deleteAnalyses(String rootUuid) {
     deleteAnalyses(purgeMapper.selectSnapshotIdsAndUuids(PurgeSnapshotQuery.create().setComponentUuid(rootUuid)));
   }
@@ -233,7 +237,6 @@ class PurgeCommands {
 
   @VisibleForTesting
   protected void purgeSnapshots(Iterable<IdUuidPair> snapshotIdUuidPairs) {
-    // note that events are not deleted
     List<List<Long>> snapshotIdsPartitions = Lists.partition(IdUuidPairs.ids(snapshotIdUuidPairs), MAX_SNAPSHOTS_PER_QUERY);
     List<List<String>> snapshotUuidsPartitions = Lists.partition(IdUuidPairs.uuids(snapshotIdUuidPairs), MAX_SNAPSHOTS_PER_QUERY);
 
@@ -247,7 +250,27 @@ class PurgeCommands {
     profiler.stop();
 
     profiler.start("updatePurgeStatusToOne (snapshots)");
-    snapshotIdUuidPairs.iterator().forEachRemaining(idUuidPair -> purgeMapper.updatePurgeStatusToOne(idUuidPair.getUuid()));
+    snapshotUuidsPartitions.forEach(snapshotUuidsPartition -> purgeMapper.updatePurgeStatusToOne(snapshotUuidsPartition));
+    session.commit();
+    profiler.stop();
+  }
+
+  public void purgeAnalyses(List<IdUuidPair> analysisUuids) {
+    List<List<Long>> analysisIdsPartitions = Lists.partition(IdUuidPairs.ids(analysisUuids), MAX_SNAPSHOTS_PER_QUERY);
+    List<List<String>> analysisUuidsPartitions = Lists.partition(IdUuidPairs.uuids(analysisUuids), MAX_SNAPSHOTS_PER_QUERY);
+
+    deleteSnapshotDuplications(analysisUuidsPartitions);
+
+    profiler.start("deleteSnapshotWastedMeasures (project_measures)");
+    List<Long> metricIdsWithoutHistoricalData = purgeMapper.selectMetricIdsWithoutHistoricalData();
+    analysisUuidsPartitions.stream()
+        .forEach(analysisUuidsPartition -> purgeMapper.deleteAnalysisWastedMeasures(analysisUuidsPartition, metricIdsWithoutHistoricalData));
+    session.commit();
+    profiler.stop();
+
+    profiler.start("updatePurgeStatusToOne (snapshots)");
+    analysisUuidsPartitions.forEach(purgeMapper::updatePurgeStatusToOne);
+    analysisIdsPartitions.forEach(purgeMapper::updateDescendantPurgeStatusToOne);
     session.commit();
     profiler.stop();
   }
index 79fdd11aed4ceb5ff703a0e9d15558f55bfd3976..b15c88048bd43ba14f2df9c39362618fef718350 100644 (file)
@@ -64,14 +64,19 @@ public class PurgeDao implements Dao {
   public void purge(DbSession session, PurgeConfiguration conf, PurgeListener listener, PurgeProfiler profiler) {
     PurgeMapper mapper = session.getMapper(PurgeMapper.class);
     PurgeCommands commands = new PurgeCommands(session, mapper, profiler);
-    deleteAbortedAnalyses(conf.rootProjectIdUuid().getUuid(), commands);
+    String rootUuid = conf.rootProjectIdUuid().getUuid();
+    deleteAbortedAnalyses(rootUuid, commands);
     deleteDataOfComponentsWithoutHistoricalData(session, conf.rootProjectIdUuid().getUuid(), conf.scopesWithoutHistoricalData(), commands);
+
+    List<IdUuidPair> analysisUuids = commands.selectSnapshotIdUuids(
+        PurgeSnapshotQuery.create()
+            .setComponentUuid(rootUuid)
+            .setIslast(false)
+            .setNotPurged(true));
+    commands.purgeAnalyses(analysisUuids);
+
     // retrieve all nodes in the tree (including root) with scope=PROJECT
     List<ResourceDto> projects = getProjects(conf.rootProjectIdUuid().getId(), session);
-    for (ResourceDto project : projects) {
-      LOG.debug("-> Clean " + project.getLongName() + " [id=" + project.getId() + "]");
-      purge(project.getUuid(), commands);
-    }
     for (ResourceDto project : projects) {
       disableOrphanResources(project, session, mapper, listener);
     }
@@ -140,22 +145,6 @@ public class PurgeDao implements Dao {
       .setSortFields(UUID_FIELD_SORT);
   }
 
-  private static void purge(String componentUuid, PurgeCommands purgeCommands) {
-    List<String> projectSnapshotUuids = purgeCommands.selectSnapshotUuids(
-      PurgeSnapshotQuery.create()
-        .setComponentUuid(componentUuid)
-        .setIslast(false)
-        .setNotPurged(true));
-    for (String snapshotUuid : projectSnapshotUuids) {
-      LOG.debug("<- Clean analysis " + snapshotUuid);
-
-      // must be executed at the end for reentrance
-      purgeCommands.purgeSnapshots(
-        PurgeSnapshotQuery.create().setAnalysisUuid(snapshotUuid).setNotPurged(true),
-        PurgeSnapshotQuery.create().setSnapshotUuid(snapshotUuid).setNotPurged(true));
-    }
-  }
-
   private void disableOrphanResources(ResourceDto project, SqlSession session, PurgeMapper purgeMapper, PurgeListener purgeListener) {
     List<String> componentUuids = new ArrayList<>();
     session.select("org.sonar.db.purge.PurgeMapper.selectComponentUuidsToDisable", project.getUuid(),
index 5ebe7ded3016db14fd474225b028c82806fe72c4..82215fdfa21abc550d8b6faf567a8eef7578b91e 100644 (file)
@@ -52,7 +52,11 @@ public interface PurgeMapper {
 
   void deleteSnapshotWastedMeasures(@Param("snapshotIds") List<Long> snapshotIds, @Param("mids") List<Long> metricIds);
 
-  void updatePurgeStatusToOne(String snapshotUuid);
+  void deleteAnalysisWastedMeasures(@Param("analysisUuids") List<String> analysisUuids, @Param("metricIds") List<Long> metricIds);
+
+  void updatePurgeStatusToOne(@Param("analysisUuids") List<String> analysisUuid);
+
+  void updateDescendantPurgeStatusToOne(@Param("analysisIds") List<Long> analysisIds);
 
   void disableComponent(String componentUuid);
 
index b087ab53da1da2a7bd03c17ab49810044c65f0a0..ebd055cde76ebd2e16ff0f638a56aa01f8ca30da 100644 (file)
     </where>
   </delete>
 
-  <update id="updatePurgeStatusToOne" parameterType="string">
+  <delete id="deleteAnalysisWastedMeasures" parameterType="map">
+    delete from project_measures
+    <where>
+      analysis_uuid in
+      <foreach collection="analysisUuids" open="(" close=")" item="analysisUuid" separator=",">
+        #{analysisUuid}
+      </foreach>
+      and (person_id is not null
+      <if test="metricIds.size()>0">
+        or metric_id in
+        <foreach collection="metricIds" open="(" item="metricId" separator="," close=")">
+          #{metricId}
+        </foreach>
+      </if>
+      )
+    </where>
+  </delete>
+
+  <update id="updatePurgeStatusToOne" parameterType="map">
     update
       snapshots
     set
       purge_status = 1
     where
-      uuid = #{snapshotUuid}
+      uuid in
+      <foreach collection="analysisUuids" open="(" close=")" item="analysisUuid" separator=",">
+        #{analysisUuid}
+      </foreach>
+  </update>
+
+  <update id="updateDescendantPurgeStatusToOne" parameterType="map">
+    update
+      snapshots
+    set
+      purge_status = 1
+    where
+      root_snapshot_id in
+      <foreach collection="analysisIds" open="(" close=")" item="analysisId" separator=",">
+        #{analysisId}
+      </foreach>
   </update>
 
   <update id="disableComponent" parameterType="string">
index 053b937a8b665fae2a4cbf81a5bd5ac00c3f82ba..4497f0a6ae4c7f3c9f234c9e3496a715f7381ba3 100644 (file)
@@ -9,9 +9,9 @@ What has been changed :
 <dataset>
 
   <!-- the project -->
-  <projects uuid="ABCD"
+  <projects uuid="P1"
             uuid_path="NOT_USED"
-            project_uuid="ABCD"
+            project_uuid="P1"
             module_uuid="[null]"
             module_uuid_path="."
             created_at="[null]"
@@ -29,13 +29,13 @@ What has been changed :
             authorization_updated_at="[null]"
             id="1"
             enabled="[false]"
-            root_uuid="ABCD"/>
+            root_uuid="P1"/>
 
   <!-- the directory -->
   <projects uuid="EFGH"
             uuid_path="NOT_USED"
-            project_uuid="ABCD"
-            module_uuid="ABCD"
+            project_uuid="P1"
+            module_uuid="P1"
             module_uuid_path="."
             created_at="[null]"
             long_name="[null]"
@@ -52,14 +52,14 @@ What has been changed :
             authorization_updated_at="[null]"
             id="2"
             enabled="[false]"
-            root_uuid="ABCD"/>
+            root_uuid="P1"/>
 
   <!-- the file -->
   <projects uuid="GHIJ"
             uuid_path="NOT_USED"
-            project_uuid="ABCD"
-            module_uuid="ABCD"
-            module_uuid_path=".ABCD."
+            project_uuid="P1"
+            module_uuid="P1"
+            module_uuid_path=".P1."
             created_at="[null]"
             long_name="[null]"
             scope="FIL"
@@ -75,13 +75,13 @@ What has been changed :
             authorization_updated_at="[null]"
             id="3"
             enabled="[false]"
-            root_uuid="ABCD"/>
+            root_uuid="P1"/>
 
   <snapshots id="1"
              uuid="u1"
-             component_uuid="ABCD"
+             component_uuid="P1"
              parent_snapshot_id="[null]"
-             root_component_uuid="ABCD"
+             root_component_uuid="P1"
              root_snapshot_id="[null]"
              status="P"
              islast="[false]"
@@ -113,7 +113,7 @@ What has been changed :
              uuid="u2"
              component_uuid="EFGH"
              parent_snapshot_id="1"
-             root_component_uuid="ABCD"
+             root_component_uuid="P1"
              root_snapshot_id="1"
              status="P"
              islast="[false]"
@@ -146,7 +146,7 @@ What has been changed :
              uuid="u3"
              component_uuid="GHIJ"
              parent_snapshot_id="2"
-             root_component_uuid="ABCD"
+             root_component_uuid="P1"
              root_snapshot_id="1"
              status="P"
              islast="[false]"
@@ -178,7 +178,7 @@ What has been changed :
   <issues id="1"
           kee="ISSUE-1"
           component_uuid="GHIJ"
-          project_uuid="ABCD"
+          project_uuid="P1"
           status="CLOSED"
           issue_close_date="1396994400000"
           resolution="REMOVED"
@@ -208,7 +208,7 @@ What has been changed :
   <issues id="2"
           kee="ISSUE-2"
           component_uuid="EFGH"
-          project_uuid="ABCD"
+          project_uuid="P1"
           status="CLOSED"
           issue_close_date="1396994400000"
           resolution="REMOVED"
@@ -236,8 +236,8 @@ What has been changed :
   <!-- Open issue on project -->
   <issues id="3"
           kee="ISSUE-3"
-          component_uuid="ABCD"
-          project_uuid="ABCD"
+          component_uuid="P1"
+          project_uuid="P1"
           status="CLOSED"
           issue_close_date="1396994400000"
           resolution="REMOVED"
@@ -266,7 +266,7 @@ What has been changed :
   <issues id="4"
           kee="ISSUE-4"
           component_uuid="GHIJ"
-          project_uuid="ABCD"
+          project_uuid="P1"
           status="CLOSED"
           issue_close_date="1449529200000"
           resolution="FIXED"
index eb5e979cd4274a270ff080e6903fdbcfe7c9b00f..09d38781bf2c06da091212bd9f7eff12d3b711c2 100644 (file)
@@ -1,9 +1,9 @@
 <dataset>
 
   <!-- the project -->
-  <projects uuid="ABCD"
+  <projects uuid="P1"
             uuid_path="NOT_USED"
-            project_uuid="ABCD"
+            project_uuid="P1"
             module_uuid="[null]"
             module_uuid_path="."
             created_at="[null]"
             authorization_updated_at="[null]"
             id="1"
             enabled="[true]"
-            root_uuid="ABCD"/>
+            root_uuid="P1"/>
 
   <!-- the directory -->
   <projects uuid="EFGH"
             uuid_path="NOT_USED"
-            project_uuid="ABCD"
-            module_uuid="ABCD"
+            project_uuid="P1"
+            module_uuid="P1"
             module_uuid_path="."
             created_at="[null]"
             long_name="[null]"
             authorization_updated_at="[null]"
             id="2"
             enabled="[true]"
-            root_uuid="ABCD"/>
+            root_uuid="P1"/>
 
   <!-- the file -->
   <projects uuid="GHIJ"
             uuid_path="NOT_USED"
-            project_uuid="ABCD"
-            module_uuid="ABCD"
-            module_uuid_path=".ABCD."
+            project_uuid="P1"
+            module_uuid="P1"
+            module_uuid_path=".P1."
             created_at="[null]"
             long_name="[null]"
             scope="FIL"
             authorization_updated_at="[null]"
             id="3"
             enabled="[true]"
-            root_uuid="ABCD"/>
+            root_uuid="P1"/>
 
   <snapshots id="1"
              uuid="u1"
-             component_uuid="ABCD"
+             component_uuid="P1"
              parent_snapshot_id="[null]"
-             root_component_uuid="ABCD"
+             root_component_uuid="P1"
              root_snapshot_id="[null]"
              status="P"
              islast="[false]"
              uuid="u2"
              component_uuid="EFGH"
              parent_snapshot_id="1"
-             root_component_uuid="ABCD"
+             root_component_uuid="P1"
              root_snapshot_id="1"
              status="P"
              islast="[false]"
              uuid="u3"
              component_uuid="GHIJ"
              parent_snapshot_id="2"
-             root_component_uuid="ABCD"
+             root_component_uuid="P1"
              root_snapshot_id="1"
              status="P"
              islast="[false]"
   <issues id="1"
           kee="ISSUE-1"
           component_uuid="GHIJ"
-          project_uuid="ABCD"
+          project_uuid="P1"
           status="OPEN"
           issue_close_date="[null]"
           resolution="[null]"
   <issues id="2"
           kee="ISSUE-2"
           component_uuid="EFGH"
-          project_uuid="ABCD"
+          project_uuid="P1"
           status="OPEN"
           issue_close_date="[null]"
           resolution="[null]"
   <!-- Open issue on project -->
   <issues id="3"
           kee="ISSUE-3"
-          component_uuid="ABCD"
-          project_uuid="ABCD"
+          component_uuid="P1"
+          project_uuid="P1"
           status="CONFIRM"
           issue_close_date="[null]"
           resolution="[null]"
   <issues id="4"
           kee="ISSUE-4"
           component_uuid="GHIJ"
-          project_uuid="ABCD"
+          project_uuid="P1"
           status="CLOSED"
           issue_close_date="1449529200000"
           resolution="FIXED"
index 23f9534eb9ba70dc6f6d4c94358022ca7e3311d6..b089cb32df7d00971c343620eb957f7836316e95 100644 (file)
@@ -1,9 +1,9 @@
 <dataset>
 
   <!-- the project -->
-  <projects uuid="ABCD"
+  <projects uuid="P1"
             uuid_path="NOT_USED"
-            project_uuid="ABCD"
+            project_uuid="P1"
             module_uuid="[null]"
             module_uuid_path="."
             enabled="[true]"
@@ -13,7 +13,7 @@
             qualifier="TRK"
             kee="project"
             name="project"
-            root_uuid="ABCD"
+            root_uuid="P1"
             description="[null]"
             language="java"
             copy_component_uuid="[null]"
@@ -27,9 +27,9 @@
   <!-- snapshot already purged -->
   <snapshots id="1"
              uuid="u1"
-             component_uuid="ABCD"
+             component_uuid="P1"
              parent_snapshot_id="[null]"
-             root_component_uuid="ABCD"
+             root_component_uuid="P1"
              root_snapshot_id="[null]"
              status="P"
              islast="[false]"
@@ -61,9 +61,9 @@
   <!-- do not purge snapshot with islast=true-->
   <snapshots id="2"
              uuid="u2"
-             component_uuid="ABCD"
+             component_uuid="P1"
              parent_snapshot_id="[null]"
-             root_component_uuid="ABCD"
+             root_component_uuid="P1"
              root_snapshot_id="[null]"
              status="P"
              islast="[true]"
@@ -94,9 +94,9 @@
   <!-- snapshot to be purged -->
   <snapshots id="3"
              uuid="u3"
-             component_uuid="ABCD"
+             component_uuid="P1"
              parent_snapshot_id="[null]"
-             root_component_uuid="ABCD"
+             root_component_uuid="P1"
              root_snapshot_id="[null]"
              status="P"
              islast="[false]"
index 026c46cea17f9430b7665481e6f4177929effad2..2118edd2e6841b4c1138d390e84f43c99037ea3c 100644 (file)
@@ -1,9 +1,9 @@
 <dataset>
 
   <!-- the project -->
-  <projects uuid="ABCD"
+  <projects uuid="P1"
             uuid_path="NOT_USED"
-            project_uuid="ABCD"
+            project_uuid="P1"
             module_uuid="[null]"
             module_uuid_path="."
             enabled="[true]"
@@ -13,7 +13,7 @@
             qualifier="TRK"
             kee="project"
             name="project"
-            root_uuid="ABCD"
+            root_uuid="P1"
             description="[null]"
             language="java"
             copy_component_uuid="[null]"
@@ -27,9 +27,9 @@
   <!-- snapshot already purged -->
   <snapshots id="1"
              uuid="u1"
-             component_uuid="ABCD"
+             component_uuid="P1"
              parent_snapshot_id="[null]"
-             root_component_uuid="ABCD"
+             root_component_uuid="P1"
              root_snapshot_id="[null]"
              status="P"
              islast="[false]"
@@ -61,9 +61,9 @@
   <!-- do not purge snapshot with islast=true-->
   <snapshots id="2"
              uuid="u2"
-             component_uuid="ABCD"
+             component_uuid="P1"
              parent_snapshot_id="[null]"
-             root_component_uuid="ABCD"
+             root_component_uuid="P1"
              root_snapshot_id="[null]"
              status="P"
              islast="[true]"
@@ -94,9 +94,9 @@
   <!-- snapshot to be purged -->
   <snapshots id="3"
              uuid="u3"
-             component_uuid="ABCD"
+             component_uuid="P1"
              parent_snapshot_id="[null]"
-             root_component_uuid="ABCD"
+             root_component_uuid="P1"
              root_snapshot_id="[null]"
              status="P"
              islast="[false]"