]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6630 At startup, delete past compute engine tasks older than 6 months
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 17 Sep 2015 20:54:47 +0000 (22:54 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 18 Sep 2015 21:48:48 +0000 (23:48 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/PurgeCeActivities.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java
server/sonar-server/src/test/java/org/sonar/server/computation/PurgeCeActivitiesTest.java [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/ce/CeActivityDao.java
sonar-db/src/main/java/org/sonar/db/ce/CeActivityMapper.java
sonar-db/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml
sonar-db/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java

diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/PurgeCeActivities.java b/server/sonar-server/src/main/java/org/sonar/server/computation/PurgeCeActivities.java
new file mode 100644 (file)
index 0000000..00a6f7d
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * 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.server.computation;
+
+import java.util.Calendar;
+import org.sonar.api.server.ServerSide;
+import org.sonar.api.utils.System2;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+
+@ServerSide
+public class PurgeCeActivities {
+
+  private static final Logger LOGGER = Loggers.get(PurgeCeActivities.class);
+
+  private final DbClient dbClient;
+  private final System2 system2;
+
+  public PurgeCeActivities(DbClient dbClient, System2 system2) {
+    this.dbClient = dbClient;
+    this.system2 = system2;
+  }
+
+  /**
+   * Do not rename. Used at server startup.
+   */
+  public void start() {
+    DbSession dbSession = dbClient.openSession(false);
+    try {
+      Calendar sixMonthsAgo = Calendar.getInstance();
+      sixMonthsAgo.setTimeInMillis(system2.now());
+      sixMonthsAgo.add(Calendar.DATE, -180);
+
+      LOGGER.info("Delete the Compute Engine tasks created before " + sixMonthsAgo.getTime());
+      dbClient.ceActivityDao().deleteOlderThan(dbSession, sixMonthsAgo.getTimeInMillis());
+      dbSession.commit();
+
+    } finally {
+      dbClient.closeSession(dbSession);
+    }
+  }
+}
index f463289c4558f7a4d3a2c321dcb27ad7b683aee2..565b7c5a5e8d154f91f3bb0b8815c923e04f8d4e 100644 (file)
@@ -19,7 +19,7 @@
  */
 package org.sonar.server.platform.platformlevel;
 
-import org.sonar.server.computation.CeQueueInitializer;
+import org.sonar.server.computation.PurgeCeActivities;
 import org.sonar.server.issue.filter.RegisterIssueFilters;
 import org.sonar.server.platform.ServerLifecycleNotifier;
 import org.sonar.server.qualitygate.RegisterQualityGates;
@@ -64,7 +64,8 @@ public class PlatformLevelStartup extends PlatformLevel {
       RegisterServletFilters.class,
       RegisterIssueFilters.class,
       RenameIssueWidgets.class,
-      ServerLifecycleNotifier.class);
+      ServerLifecycleNotifier.class,
+      PurgeCeActivities.class);
   }
 
   @Override
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/PurgeCeActivitiesTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/PurgeCeActivitiesTest.java
new file mode 100644 (file)
index 0000000..2b9f65d
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * 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.server.computation;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.internal.TestSystem2;
+import org.sonar.db.DbTester;
+import org.sonar.db.ce.CeActivityDto;
+import org.sonar.db.ce.CeQueueDto;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class PurgeCeActivitiesTest {
+
+  TestSystem2 system2 = new TestSystem2();
+
+  @Rule
+  public DbTester dbTester = DbTester.create(system2);
+  PurgeCeActivities underTest = new PurgeCeActivities(dbTester.getDbClient(), system2);
+
+  @Test
+  public void delete_older_than_6_months() throws Exception {
+    insertWithDate("VERY_OLD", 1_000_000_000_000L);
+    insertWithDate("RECENT", 1_500_000_000_000L);
+    system2.setNow(1_500_000_000_100L);
+
+    underTest.start();
+
+    assertThat(dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), "VERY_OLD").isPresent()).isFalse();
+    assertThat(dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), "RECENT").isPresent()).isTrue();
+  }
+
+  private void insertWithDate(String uuid, long date) {
+    CeQueueDto queueDto = new CeQueueDto();
+    queueDto.setUuid(uuid);
+    queueDto.setTaskType("fake");
+
+    CeActivityDto dto = new CeActivityDto(queueDto);
+    dto.setStatus(CeActivityDto.Status.SUCCESS);
+    system2.setNow(date);
+    dbTester.getDbClient().ceActivityDao().insert(dbTester.getSession(), dto);
+    dbTester.getSession().commit();
+  }
+}
index 59b59d6e87ad09a81a3176f72ac83632a6ab4de2..fb2f47a900fd07d54b5dcce3560aa540b5cc6ad2 100644 (file)
@@ -52,6 +52,10 @@ public class CeActivityDao implements Dao {
     }
   }
 
+  public void deleteOlderThan(DbSession dbSession, long beforeDate) {
+    mapper(dbSession).deleteOlderThan(beforeDate);
+  }
+
   /**
    * Ordered by id asc -> oldest to newest
    */
index 266094669fe646c7efd75395768538d3af41fbbc..e39406d7977418f8735ee7db85072894ae7ceab7 100644 (file)
@@ -40,4 +40,6 @@ public interface CeActivityMapper {
   void updateIsLastToFalseForLastKey(@Param("isLastKey") String isLastKey, @Param("updatedAt") long updatedAt);
 
   void updateIsLastToTrueForUuid(@Param("uuid") String uuid, @Param("updatedAt") long updatedAt);
+
+  void deleteOlderThan(@Param("beforeDate") long beforeDate);
 }
index fcd588aa5e72d617dc21fbcab38da18b05742878..9add9ad45d540c07d9b4a9f04e2ce626aeb5af1c 100644 (file)
@@ -97,4 +97,9 @@
     where uuid=#{uuid}
   </update>
 
+  <delete id="deleteOlderThan" parameterType="long">
+    delete from ce_activity
+    where created_at &lt; #{beforeDate,jdbcType=BIGINT}
+  </delete>
+
 </mapper>
index 25d167afb5a92a3192a7ce434dc28c95cc241094..c36ca55a0fd87a4b6d07affd766b400a2256d6bb 100644 (file)
@@ -26,7 +26,6 @@ import org.apache.ibatis.session.RowBounds;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
-import org.sonar.api.utils.System2;
 import org.sonar.api.utils.internal.TestSystem2;
 import org.sonar.db.DbTester;
 import org.sonar.test.DbTests;
@@ -37,7 +36,7 @@ import static org.sonar.db.ce.CeTaskTypes.REPORT;
 @Category(DbTests.class)
 public class CeActivityDaoTest {
 
-  System2 system2 = new TestSystem2().setNow(1_450_000_000_000L);
+  TestSystem2 system2 = new TestSystem2().setNow(1_450_000_000_000L);
 
   @Rule
   public DbTester db = DbTester.create(system2);
@@ -111,6 +110,20 @@ public class CeActivityDaoTest {
     assertThat(dtos).extracting("uuid").containsExactly("TASK_4");
   }
 
+  @Test
+  public void deleteOlderThan() throws Exception {
+    insertWithDate("TASK_1", 1_450_000_000_000L);
+    insertWithDate("TASK_2", 1_460_000_000_000L);
+    insertWithDate("TASK_3", 1_470_000_000_000L);
+
+    underTest.deleteOlderThan(db.getSession(), 1_465_000_000_000L);
+    db.getSession().commit();
+
+    assertThat(underTest.selectByUuid(db.getSession(), "TASK_1").isPresent()).isFalse();
+    assertThat(underTest.selectByUuid(db.getSession(), "TASK_2").isPresent()).isFalse();
+    assertThat(underTest.selectByUuid(db.getSession(), "TASK_3").isPresent()).isTrue();
+  }
+
   private void insert(String uuid, String type, String componentUuid, CeActivityDto.Status status) {
     CeQueueDto queueDto = new CeQueueDto();
     queueDto.setUuid(uuid);
@@ -126,4 +139,15 @@ public class CeActivityDaoTest {
     dto.setExecutionTimeMs(500L);
     underTest.insert(db.getSession(), dto);
   }
+
+  private void insertWithDate(String uuid, long date) {
+    CeQueueDto queueDto = new CeQueueDto();
+    queueDto.setUuid(uuid);
+    queueDto.setTaskType("fake");
+
+    CeActivityDto dto = new CeActivityDto(queueDto);
+    dto.setStatus(CeActivityDto.Status.SUCCESS);
+    system2.setNow(date);
+    underTest.insert(db.getSession(), dto);
+  }
 }