]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6630 delete logs related to old reports
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 28 Sep 2015 21:28:06 +0000 (23:28 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 29 Sep 2015 09:31:42 +0000 (11:31 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/log/CeLogging.java
server/sonar-server/src/main/java/org/sonar/server/computation/log/LogFileRef.java
server/sonar-server/src/main/java/org/sonar/server/computation/queue/PurgeCeActivities.java
server/sonar-server/src/test/java/org/sonar/server/computation/PurgeCeActivitiesTest.java
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/java/org/sonar/db/ce/CeTaskTypes.java
sonar-db/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml
sonar-db/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java

index 55f76de99e89b318e03c06afd3609bfc453337d5..c4f689bb5574a50e44bd511b629bd1c665491ea9 100644 (file)
@@ -78,6 +78,11 @@ public class CeLogging {
     return Optional.absent();
   }
 
+  public void deleteIfExists(LogFileRef ref) {
+    File logFile = new File(logsDir, ref.getRelativePath());
+    logFile.delete();
+  }
+
   /**
    * Initialize logging of a Compute Engine task. Must be called
    * before first writing of log.
index d530a51e89a981da9a3d40bfe04e0e56ce93d160..3378f4805e56875a326d368be30e9b96d7855956 100644 (file)
@@ -79,7 +79,7 @@ public class LogFileRef {
     if (!taskUuid.equals(that.taskUuid)) {
       return false;
     }
-    return !(componentUuid != null ? !componentUuid.equals(that.componentUuid) : that.componentUuid != null);
+    return componentUuid == null ? that.componentUuid == null : componentUuid.equals(that.componentUuid);
 
   }
 
index e92f7d15ef3be7514097cda96d565af77dc0857c..971e50ac3ea6fb8b8147f5ec0632ccb291dd14a2 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.computation.queue;
 
 import java.util.Calendar;
+import java.util.List;
 import org.sonar.api.platform.Server;
 import org.sonar.api.platform.ServerStartHandler;
 import org.sonar.api.server.ServerSide;
@@ -28,6 +29,9 @@ import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
+import org.sonar.db.ce.CeActivityDto;
+import org.sonar.server.computation.log.CeLogging;
+import org.sonar.server.computation.log.LogFileRef;
 
 @ServerSide
 public class PurgeCeActivities implements ServerStartHandler {
@@ -36,22 +40,28 @@ public class PurgeCeActivities implements ServerStartHandler {
 
   private final DbClient dbClient;
   private final System2 system2;
+  private final CeLogging ceLogging;
 
-  public PurgeCeActivities(DbClient dbClient, System2 system2) {
+  public PurgeCeActivities(DbClient dbClient, System2 system2, CeLogging ceLogging) {
     this.dbClient = dbClient;
     this.system2 = system2;
+    this.ceLogging = ceLogging;
   }
 
   @Override
   public void onServerStart(Server server) {
-    DbSession dbSession = dbClient.openSession(false);
+    DbSession dbSession = dbClient.openSession(true);
     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());
+      LOGGER.info("Delete the Compute Engine tasks created before {}", sixMonthsAgo.getTime());
+      List<CeActivityDto> dtos = dbClient.ceActivityDao().selectOlderThan(dbSession, sixMonthsAgo.getTimeInMillis());
+      for (CeActivityDto dto : dtos) {
+        dbClient.ceActivityDao().deleteByUuid(dbSession, dto.getUuid());
+        ceLogging.deleteIfExists(LogFileRef.from(dto));
+      }
       dbSession.commit();
 
     } finally {
index b31ae60d2a6fc0b5c8a29266b1fe4ca5ad982aee..cc985811322d27c5ca7e1721f018b37908ae2e32 100644 (file)
@@ -26,10 +26,15 @@ 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 org.sonar.db.ce.CeTaskTypes;
+import org.sonar.server.computation.log.CeLogging;
+import org.sonar.server.computation.log.LogFileRef;
 import org.sonar.server.computation.queue.PurgeCeActivities;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
 
 public class PurgeCeActivitiesTest {
 
@@ -37,7 +42,8 @@ public class PurgeCeActivitiesTest {
 
   @Rule
   public DbTester dbTester = DbTester.create(system2);
-  PurgeCeActivities underTest = new PurgeCeActivities(dbTester.getDbClient(), system2);
+  CeLogging ceLogging = mock(CeLogging.class);
+  PurgeCeActivities underTest = new PurgeCeActivities(dbTester.getDbClient(), system2, ceLogging);
 
   @Test
   public void delete_older_than_6_months() throws Exception {
@@ -49,12 +55,14 @@ public class PurgeCeActivitiesTest {
 
     assertThat(dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), "VERY_OLD").isPresent()).isFalse();
     assertThat(dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), "RECENT").isPresent()).isTrue();
+    verify(ceLogging).deleteIfExists(new LogFileRef(CeTaskTypes.REPORT, "VERY_OLD", null));
+    verify(ceLogging, never()).deleteIfExists(new LogFileRef(CeTaskTypes.REPORT, "RECENT", null));
   }
 
   private void insertWithDate(String uuid, long date) {
     CeQueueDto queueDto = new CeQueueDto();
     queueDto.setUuid(uuid);
-    queueDto.setTaskType("fake");
+    queueDto.setTaskType(CeTaskTypes.REPORT);
 
     CeActivityDto dto = new CeActivityDto(queueDto);
     dto.setStatus(CeActivityDto.Status.SUCCESS);
index 58214c0c0ad9043c19d8ea65acda5d2d351513f7..f54d80f566d8e65c7e75d94a5af46b620490eb84 100644 (file)
@@ -52,8 +52,12 @@ public class CeActivityDao implements Dao {
     }
   }
 
-  public void deleteOlderThan(DbSession dbSession, long beforeDate) {
-    mapper(dbSession).deleteOlderThan(beforeDate);
+  public List<CeActivityDto> selectOlderThan(DbSession dbSession, long beforeDate) {
+    return mapper(dbSession).selectOlderThan(beforeDate);
+  }
+
+  public void deleteByUuid(DbSession dbSession, String uuid) {
+    mapper(dbSession).deleteByUuid(uuid);
   }
 
   /**
index 7c019b25eb44f164092088743d9b0c931965c271..7d9ada5c11e33ab204e73ba46002e199094e87d5 100644 (file)
@@ -35,6 +35,8 @@ public interface CeActivityMapper {
 
   List<CeActivityDto> selectByQuery(@Param("query") CeActivityQuery query, RowBounds rowBounds);
 
+  List<CeActivityDto> selectOlderThan(@Param("beforeDate") long beforeDate);
+
   int countByQuery(@Param("query") CeActivityQuery query);
 
   void insert(CeActivityDto dto);
@@ -43,5 +45,5 @@ public interface CeActivityMapper {
 
   void updateIsLastToTrueForUuid(@Param("uuid") String uuid, @Param("updatedAt") long updatedAt);
 
-  void deleteOlderThan(@Param("beforeDate") long beforeDate);
+  void deleteByUuid(@Param("uuid") String uuid);
 }
index 042ff5eec0ee7495db7e3d5e343fdf68c63f6749..3136e8295caf996a1ab2f558fee4f6ea08ff47c7 100644 (file)
@@ -19,7 +19,7 @@
  */
 package org.sonar.db.ce;
 
-public class CeTaskTypes {
+public final class CeTaskTypes {
 
   private CeTaskTypes() {
     // only statics
index d6a13c87fac13d3f0c7c26fb07dd5904f0c118e7..c466d93cbcc13e4c56cb8a631bab15d9d34f1389 100644 (file)
     </where>
   </select>
 
+  <select id="selectOlderThan" parameterType="long" resultType="org.sonar.db.ce.CeActivityDto">
+    select <include refid="columns"/>
+    from ce_activity ca
+    where ca.created_at &lt; #{beforeDate,jdbcType=BIGINT}
+  </select>
+
   <insert id="insert" parameterType="org.sonar.db.ce.CeActivityDto" useGeneratedKeys="false">
     insert into ce_activity
     (uuid, component_uuid, status, task_type, is_last, is_last_key, submitter_login, submitted_at, started_at,
     where uuid=#{uuid}
   </update>
 
-  <delete id="deleteOlderThan" parameterType="long">
+  <delete id="deleteByUuid" parameterType="string">
     delete from ce_activity
-    where created_at &lt; #{beforeDate,jdbcType=BIGINT}
+    where uuid=#{uuid}
   </delete>
-
 </mapper>
index b52f6da2272ed85229f5471399ebb6029e00a7b0..e714e688bf15dc6e87a4f019dc1eb02f050174a2 100644 (file)
@@ -80,7 +80,7 @@ public class CeActivityDaoTest {
   }
 
   @Test
-  public void test_selectByQuery() throws Exception {
+  public void test_selectByQuery() {
     insert("TASK_1", REPORT, "PROJECT_1", CeActivityDto.Status.SUCCESS);
     insert("TASK_2", REPORT, "PROJECT_1", CeActivityDto.Status.FAILED);
     insert("TASK_3", REPORT, "PROJECT_2", CeActivityDto.Status.SUCCESS);
@@ -116,7 +116,7 @@ public class CeActivityDaoTest {
   }
 
   @Test
-  public void test_countByQuery() throws Exception {
+  public void test_countByQuery() {
     insert("TASK_1", REPORT, "PROJECT_1", CeActivityDto.Status.SUCCESS);
     insert("TASK_2", REPORT, "PROJECT_1", CeActivityDto.Status.FAILED);
     insert("TASK_3", REPORT, "PROJECT_2", CeActivityDto.Status.SUCCESS);
@@ -146,7 +146,7 @@ public class CeActivityDaoTest {
   }
 
   @Test
-  public void select_and_count_by_date() throws Exception {
+  public void select_and_count_by_date() {
     insertWithDates("UUID1", 1_450_000_000_000L, 1_470_000_000_000L);
     insertWithDates("UUID2", 1_460_000_000_000L, 1_480_000_000_000L);
 
@@ -181,17 +181,33 @@ public class CeActivityDaoTest {
   }
 
   @Test
-  public void deleteOlderThan() throws Exception {
+  public void selectOlderThan() {
     insertWithCreationDate("TASK_1", 1_450_000_000_000L);
     insertWithCreationDate("TASK_2", 1_460_000_000_000L);
     insertWithCreationDate("TASK_3", 1_470_000_000_000L);
 
-    underTest.deleteOlderThan(db.getSession(), 1_465_000_000_000L);
-    db.getSession().commit();
+    List<CeActivityDto> dtos = underTest.selectOlderThan(db.getSession(), 1_465_000_000_000L);
+    assertThat(dtos).extracting("uuid").containsOnly("TASK_1", "TASK_2");
+  }
+
+  @Test
+  public void deleteByUuid() {
+    insert("TASK_1", "REPORT", "COMPONENT1", CeActivityDto.Status.SUCCESS);
+    insert("TASK_2", "REPORT", "COMPONENT1", CeActivityDto.Status.SUCCESS);
 
+    underTest.deleteByUuid(db.getSession(), "TASK_1");
     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();
+    assertThat(underTest.selectByUuid(db.getSession(), "TASK_2").isPresent()).isTrue();
+  }
+
+  @Test
+  public void deleteByUuid_does_nothing_if_uuid_does_not_exist() {
+    insert("TASK_1", "REPORT", "COMPONENT1", CeActivityDto.Status.SUCCESS);
+
+    // must not fail
+    underTest.deleteByUuid(db.getSession(), "TASK_2");
+
+    assertThat(underTest.selectByUuid(db.getSession(), "TASK_1").isPresent()).isTrue();
   }
 
   private void insert(String uuid, String type, String componentUuid, CeActivityDto.Status status) {