From a8377cdc47390e932da20c2f644206a7adba3070 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 30 Jun 2016 13:07:12 +0200 Subject: [PATCH] SONAR-7705 replace purge by cpt of scope PRJ by purge by analyses --- .../org/sonar/db/purge/PurgeCommands.java | 27 ++++++++++++- .../java/org/sonar/db/purge/PurgeDao.java | 31 +++++---------- .../java/org/sonar/db/purge/PurgeMapper.java | 6 ++- .../org/sonar/db/purge/PurgeMapper.xml | 37 +++++++++++++++++- ...resources_without_last_snapshot-result.xml | 38 +++++++++---------- ...isable_resources_without_last_snapshot.xml | 38 +++++++++---------- .../shouldPurgeProject-result.xml | 18 ++++----- .../purge/PurgeDaoTest/shouldPurgeProject.xml | 18 ++++----- 8 files changed, 131 insertions(+), 82 deletions(-) diff --git a/sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java b/sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java index 3273834f56a..7e728f84ca0 100644 --- a/sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java +++ b/sonar-db/src/main/java/org/sonar/db/purge/PurgeCommands.java @@ -54,6 +54,10 @@ class PurgeCommands { return purgeMapper.selectSnapshotIdsAndUuids(query).stream().map(IdUuidPair::getUuid).collect(Collectors.toList()); } + List 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 snapshotIdUuidPairs) { - // note that events are not deleted List> snapshotIdsPartitions = Lists.partition(IdUuidPairs.ids(snapshotIdUuidPairs), MAX_SNAPSHOTS_PER_QUERY); List> 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 analysisUuids) { + List> analysisIdsPartitions = Lists.partition(IdUuidPairs.ids(analysisUuids), MAX_SNAPSHOTS_PER_QUERY); + List> analysisUuidsPartitions = Lists.partition(IdUuidPairs.uuids(analysisUuids), MAX_SNAPSHOTS_PER_QUERY); + + deleteSnapshotDuplications(analysisUuidsPartitions); + + profiler.start("deleteSnapshotWastedMeasures (project_measures)"); + List 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(); } diff --git a/sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java b/sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java index 79fdd11aed4..b15c88048bd 100644 --- a/sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java +++ b/sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java @@ -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 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 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 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 componentUuids = new ArrayList<>(); session.select("org.sonar.db.purge.PurgeMapper.selectComponentUuidsToDisable", project.getUuid(), diff --git a/sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java b/sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java index 5ebe7ded301..82215fdfa21 100644 --- a/sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/purge/PurgeMapper.java @@ -52,7 +52,11 @@ public interface PurgeMapper { void deleteSnapshotWastedMeasures(@Param("snapshotIds") List snapshotIds, @Param("mids") List metricIds); - void updatePurgeStatusToOne(String snapshotUuid); + void deleteAnalysisWastedMeasures(@Param("analysisUuids") List analysisUuids, @Param("metricIds") List metricIds); + + void updatePurgeStatusToOne(@Param("analysisUuids") List analysisUuid); + + void updateDescendantPurgeStatusToOne(@Param("analysisIds") List analysisIds); void disableComponent(String componentUuid); diff --git a/sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml b/sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml index b087ab53da1..ebd055cde76 100644 --- a/sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/purge/PurgeMapper.xml @@ -179,13 +179,46 @@ - + + delete from project_measures + + analysis_uuid in + + #{analysisUuid} + + and (person_id is not null + + or metric_id in + + #{metricId} + + + ) + + + + update snapshots set purge_status = 1 where - uuid = #{snapshotUuid} + uuid in + + #{analysisUuid} + + + + + update + snapshots + set + purge_status = 1 + where + root_snapshot_id in + + #{analysisId} + diff --git a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/disable_resources_without_last_snapshot-result.xml b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/disable_resources_without_last_snapshot-result.xml index 053b937a8b6..4497f0a6ae4 100644 --- a/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/disable_resources_without_last_snapshot-result.xml +++ b/sonar-db/src/test/resources/org/sonar/db/purge/PurgeDaoTest/disable_resources_without_last_snapshot-result.xml @@ -9,9 +9,9 @@ What has been changed : - + root_uuid="P1"/> + root_uuid="P1"/> + root_uuid="P1"/> - + root_uuid="P1"/> + root_uuid="P1"/> + root_uuid="P1"/> - -