diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-09-17 22:54:47 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-09-18 23:48:48 +0200 |
commit | d254d8ef61f2793a079f3b04afc4dc0a798e6df2 (patch) | |
tree | b01cbf8e88cbd93da78f77cb73627551eb6c8d07 /sonar-db | |
parent | 024672b883d5b81af0838106fe53df31365444f2 (diff) | |
download | sonarqube-d254d8ef61f2793a079f3b04afc4dc0a798e6df2.tar.gz sonarqube-d254d8ef61f2793a079f3b04afc4dc0a798e6df2.zip |
SONAR-6630 At startup, delete past compute engine tasks older than 6 months
Diffstat (limited to 'sonar-db')
4 files changed, 37 insertions, 2 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/ce/CeActivityDao.java b/sonar-db/src/main/java/org/sonar/db/ce/CeActivityDao.java index 59b59d6e87a..fb2f47a900f 100644 --- a/sonar-db/src/main/java/org/sonar/db/ce/CeActivityDao.java +++ b/sonar-db/src/main/java/org/sonar/db/ce/CeActivityDao.java @@ -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 */ diff --git a/sonar-db/src/main/java/org/sonar/db/ce/CeActivityMapper.java b/sonar-db/src/main/java/org/sonar/db/ce/CeActivityMapper.java index 266094669fe..e39406d7977 100644 --- a/sonar-db/src/main/java/org/sonar/db/ce/CeActivityMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/ce/CeActivityMapper.java @@ -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); } diff --git a/sonar-db/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml b/sonar-db/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml index fcd588aa5e7..9add9ad45d5 100644 --- a/sonar-db/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml @@ -97,4 +97,9 @@ where uuid=#{uuid} </update> + <delete id="deleteOlderThan" parameterType="long"> + delete from ce_activity + where created_at < #{beforeDate,jdbcType=BIGINT} + </delete> + </mapper> diff --git a/sonar-db/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java b/sonar-db/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java index 25d167afb5a..c36ca55a0fd 100644 --- a/sonar-db/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java @@ -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); + } } |