]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7130 group deletion and purges of snapshots all together 731/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 21 Jan 2016 12:41:43 +0000 (13:41 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 26 Jan 2016 15:18:41 +0000 (16:18 +0100)
avoid doing select requests between deletion/purges of snapshots

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/period/DefaultPeriodCleaner.java
sonar-db/src/test/java/org/sonar/db/purge/period/DefaultPeriodCleanerTest.java

index 1d5111a6a01274820a06bd1c2644e8ec764ee488..806703ebf6a2e349ad11c18f1ccd4f14a1c33dd0 100644 (file)
 package org.sonar.db.purge;
 
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Function;
+import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import java.util.LinkedHashSet;
 import java.util.List;
+import javax.annotation.Nullable;
 import org.apache.ibatis.session.SqlSession;
 
+import static com.google.common.collect.FluentIterable.from;
+import static java.util.Arrays.asList;
+
 class PurgeCommands {
 
   private static final int MAX_SNAPSHOTS_PER_QUERY = 1000;
@@ -32,6 +40,13 @@ class PurgeCommands {
   private final SqlSession session;
   private final PurgeMapper purgeMapper;
   private final PurgeProfiler profiler;
+  private final Function<PurgeSnapshotQuery, Iterable<Long>> purgeSnapshotQueryToSnapshotIds = new Function<PurgeSnapshotQuery, Iterable<Long>>() {
+    @Nullable
+    @Override
+    public Iterable<Long> apply(PurgeSnapshotQuery query) {
+      return purgeMapper.selectSnapshotIds(query);
+    }
+  };
 
   PurgeCommands(SqlSession session, PurgeMapper purgeMapper, PurgeProfiler profiler) {
     this.session = session;
@@ -146,8 +161,10 @@ class PurgeCommands {
     profiler.stop();
   }
 
-  void deleteSnapshots(final PurgeSnapshotQuery query) {
-    deleteSnapshots(purgeMapper.selectSnapshotIds(query));
+  void deleteSnapshots(PurgeSnapshotQuery... queries) {
+    List<Long> snapshotIds = from(asList(queries))
+      .transformAndConcat(purgeSnapshotQueryToSnapshotIds).toList();
+    deleteSnapshots(snapshotIds);
   }
 
   @VisibleForTesting
@@ -179,14 +196,16 @@ class PurgeCommands {
     profiler.stop();
   }
 
-  void purgeSnapshots(final PurgeSnapshotQuery query) {
-    purgeSnapshots(purgeMapper.selectSnapshotIds(query));
+  void purgeSnapshots(PurgeSnapshotQuery... queries) {
+    // use LinkedHashSet to keep order by remove duplicated ids
+    LinkedHashSet<Long> snapshotIds = Sets.newLinkedHashSet(from(asList(queries)).transformAndConcat(purgeSnapshotQueryToSnapshotIds));
+    purgeSnapshots(snapshotIds);
   }
 
   @VisibleForTesting
-  protected void purgeSnapshots(final List<Long> snapshotIds) {
+  protected void purgeSnapshots(Iterable<Long> snapshotIds) {
     // note that events are not deleted
-    List<List<Long>> snapshotIdsPartition = Lists.partition(snapshotIds, MAX_SNAPSHOTS_PER_QUERY);
+    Iterable<List<Long>> snapshotIdsPartition = Iterables.partition(snapshotIds, MAX_SNAPSHOTS_PER_QUERY);
 
     deleteSnapshotDuplications(snapshotIdsPartition);
 
@@ -206,7 +225,7 @@ class PurgeCommands {
     profiler.stop();
   }
 
-  private void deleteSnapshotDuplications(final List<List<Long>> snapshotIdsPartition) {
+  private void deleteSnapshotDuplications(Iterable<List<Long>> snapshotIdsPartition) {
     profiler.start("deleteSnapshotDuplications (duplications_index)");
     for (List<Long> partSnapshotIds : snapshotIdsPartition) {
       purgeMapper.deleteSnapshotDuplications(partSnapshotIds);
index d1c206a7452bf168407d3d552b92d25d397ce3ac..a0d8923513184ced193d1b824838e33cc8e64ea5 100644 (file)
@@ -46,6 +46,8 @@ import static org.sonar.api.utils.DateUtils.dateToLong;
  */
 public class PurgeDao implements Dao {
   private static final Logger LOG = Loggers.get(PurgeDao.class);
+  private static final String[] UNPROCESSED_STATUS = new String[] {"U"};
+
   private final MyBatis mybatis;
   private final ResourceDao resourceDao;
   private final System2 system2;
@@ -92,7 +94,7 @@ public class PurgeDao implements Dao {
     LOG.debug("<- Delete aborted builds");
     PurgeSnapshotQuery query = PurgeSnapshotQuery.create()
       .setIslast(false)
-      .setStatus(new String[] {"U"})
+      .setStatus(UNPROCESSED_STATUS)
       .setRootProjectId(project.getId());
     commands.deleteSnapshots(query);
   }
@@ -114,11 +116,10 @@ public class PurgeDao implements Dao {
         purgeCommands.deleteSnapshots(query);
       }
 
-      PurgeSnapshotQuery query = PurgeSnapshotQuery.create().setRootSnapshotId(projectSnapshotId).setNotPurged(true);
-      purgeCommands.purgeSnapshots(query);
-
       // must be executed at the end for reentrance
-      purgeCommands.purgeSnapshots(PurgeSnapshotQuery.create().setId(projectSnapshotId).setNotPurged(true));
+      purgeCommands.purgeSnapshots(
+        PurgeSnapshotQuery.create().setRootSnapshotId(projectSnapshotId).setNotPurged(true),
+        PurgeSnapshotQuery.create().setId(projectSnapshotId).setNotPurged(true));
     }
   }
 
@@ -186,15 +187,15 @@ public class PurgeDao implements Dao {
   public PurgeDao deleteSnapshots(PurgeSnapshotQuery query, PurgeProfiler profiler) {
     final DbSession session = mybatis.openSession(true);
     try {
-      return deleteSnapshots(query, session, profiler);
+      return deleteSnapshots(session, profiler, query);
 
     } finally {
       MyBatis.closeQuietly(session);
     }
   }
 
-  public PurgeDao deleteSnapshots(PurgeSnapshotQuery query, DbSession session, PurgeProfiler profiler) {
-    new PurgeCommands(session, profiler).deleteSnapshots(query);
+  public PurgeDao deleteSnapshots(DbSession session, PurgeProfiler profiler, PurgeSnapshotQuery... queries) {
+    new PurgeCommands(session, profiler).deleteSnapshots(queries);
     return this;
   }
 
index fd34d37476e11089e2d742f66d8889894f789734..8d41e91fbe4533a99483caadb48f2ce97aa2929b 100644 (file)
@@ -60,8 +60,10 @@ public class DefaultPeriodCleaner {
   private void delete(List<PurgeableSnapshotDto> snapshots, DbSession session) {
     for (PurgeableSnapshotDto snapshot : snapshots) {
       LOG.debug("<- Delete snapshot: {} [{}]", DateUtils.formatDateTime(snapshot.getDate()), snapshot.getSnapshotId());
-      purgeDao.deleteSnapshots(PurgeSnapshotQuery.create().setRootSnapshotId(snapshot.getSnapshotId()), session, profiler);
-      purgeDao.deleteSnapshots(PurgeSnapshotQuery.create().setId(snapshot.getSnapshotId()), session, profiler);
+      purgeDao.deleteSnapshots(
+        session, profiler,
+        PurgeSnapshotQuery.create().setRootSnapshotId(snapshot.getSnapshotId()),
+        PurgeSnapshotQuery.create().setId(snapshot.getSnapshotId()));
     }
   }
 
index 9a6add0ca0697a8923c3a34ae833584f5f866628..785504ba07322f819561ad9b9395a85de9495803 100644 (file)
@@ -58,8 +58,7 @@ public class DefaultPeriodCleanerTest {
 
     verify(filter1).log();
     verify(filter2).log();
-    verify(dao, times(2)).deleteSnapshots(argThat(newRootSnapshotQuery()), eq(session), any(PurgeProfiler.class));
-    verify(dao, times(2)).deleteSnapshots(argThat(newSnapshotIdQuery()), eq(session), any(PurgeProfiler.class));
+    verify(dao, times(2)).deleteSnapshots(eq(session), any(PurgeProfiler.class), argThat(newRootSnapshotQuery()), argThat(newSnapshotIdQuery()));
   }
 
   private BaseMatcher<PurgeSnapshotQuery> newRootSnapshotQuery() {