diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2019-05-27 10:53:05 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-05-31 10:17:08 +0200 |
commit | aefa1e5ffb6e9348a5545872b48a81d470c87690 (patch) | |
tree | 7474c8d65d229b71ff3ce5ec1129c81f1c11c95d /server/sonar-ce | |
parent | 1c430b3667afe03087e03ab796d1613c41ec0483 (diff) | |
download | sonarqube-aefa1e5ffb6e9348a5545872b48a81d470c87690.tar.gz sonarqube-aefa1e5ffb6e9348a5545872b48a81d470c87690.zip |
SONAR-11675 delete scan contexts and CE activities on project purge
Diffstat (limited to 'server/sonar-ce')
-rw-r--r-- | server/sonar-ce/src/main/java/org/sonar/ce/queue/PurgeCeActivities.java | 39 | ||||
-rw-r--r-- | server/sonar-ce/src/test/java/org/sonar/ce/queue/PurgeCeActivitiesTest.java | 138 |
2 files changed, 26 insertions, 151 deletions
diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/queue/PurgeCeActivities.java b/server/sonar-ce/src/main/java/org/sonar/ce/queue/PurgeCeActivities.java index e61d5b35ed5..09a865f197a 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/queue/PurgeCeActivities.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/queue/PurgeCeActivities.java @@ -19,55 +19,28 @@ */ package org.sonar.ce.queue; -import java.util.Date; -import java.util.Set; import org.sonar.api.Startable; import org.sonar.api.ce.ComputeEngineSide; -import org.sonar.api.utils.DateUtils; -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; -import org.sonar.db.ce.CeActivityDto; - -import static java.util.stream.Stream.concat; -import static org.sonar.core.util.stream.MoreCollectors.toSet; +import org.sonar.db.purge.PurgeProfiler; @ComputeEngineSide public class PurgeCeActivities implements Startable { - private static final Logger LOGGER = Loggers.get(PurgeCeActivities.class); - private final DbClient dbClient; - private final System2 system2; + private final PurgeProfiler profiler; - public PurgeCeActivities(DbClient dbClient, System2 system2) { + public PurgeCeActivities(DbClient dbClient, PurgeProfiler profiler) { this.dbClient = dbClient; - this.system2 = system2; + this.profiler = profiler; } @Override public void start() { try (DbSession dbSession = dbClient.openSession(false)) { - Date sixMonthsAgo = DateUtils.addDays(new Date(system2.now()), -180); - - LOGGER.info("Delete the Compute Engine tasks created before {}", sixMonthsAgo.getTime()); - Set<String> ceActivityUuids = dbClient.ceActivityDao().selectOlderThan(dbSession, sixMonthsAgo.getTime()) - .stream() - .map(CeActivityDto::getUuid) - .collect(toSet()); - dbClient.ceActivityDao().deleteByUuids(dbSession, ceActivityUuids); - dbClient.ceTaskCharacteristicsDao().deleteByTaskUuids(dbSession, ceActivityUuids); - dbClient.ceTaskInputDao().deleteByUuids(dbSession, ceActivityUuids); - - Date fourWeeksAgo = DateUtils.addDays(new Date(system2.now()), -28); - - LOGGER.info("Delete the Scanner contexts tasks created before {}", fourWeeksAgo.getTime()); - Set<String> scannerContextUuids = dbClient.ceScannerContextDao().selectOlderThan(dbSession, fourWeeksAgo.getTime()); - dbClient.ceScannerContextDao().deleteByUuids( - dbSession, - concat(ceActivityUuids.stream(), scannerContextUuids.stream()).collect(toSet())); + dbClient.purgeDao().purgeCeActivities(dbSession, profiler); + dbClient.purgeDao().purgeCeScannerContexts(dbSession, profiler); dbSession.commit(); } } diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/queue/PurgeCeActivitiesTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/queue/PurgeCeActivitiesTest.java index 21cf35cfb1c..4a1bf21d4ed 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/queue/PurgeCeActivitiesTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/queue/PurgeCeActivitiesTest.java @@ -19,135 +19,37 @@ */ package org.sonar.ce.queue; -import java.nio.charset.Charset; -import java.time.LocalDateTime; -import java.time.ZoneOffset; -import java.util.Collections; -import java.util.List; -import java.util.Optional; -import org.apache.commons.io.IOUtils; -import org.junit.Rule; import org.junit.Test; -import org.sonar.api.utils.System2; -import org.sonar.core.util.UuidFactoryFast; -import org.sonar.db.DbTester; -import org.sonar.db.ce.CeActivityDto; -import org.sonar.db.ce.CeQueueDto; -import org.sonar.db.ce.CeTaskCharacteristicDto; -import org.sonar.db.ce.CeTaskInputDao; -import org.sonar.db.ce.CeTaskTypes; +import org.mockito.InOrder; +import org.mockito.Mockito; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.purge.PurgeDao; +import org.sonar.db.purge.PurgeProfiler; -import static java.time.ZoneOffset.UTC; -import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; -import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class PurgeCeActivitiesTest { - private System2 system2 = mock(System2.class); - - @Rule - public DbTester dbTester = DbTester.create(system2); - - private PurgeCeActivities underTest = new PurgeCeActivities(dbTester.getDbClient(), system2); - - @Test - public void delete_activity_older_than_180_days_and_their_scanner_context() { - LocalDateTime now = LocalDateTime.now(); - insertWithDate("VERY_OLD", now.minusDays(180).minusMonths(10)); - insertWithDate("JUST_OLD_ENOUGH", now.minusDays(180).minusDays(1)); - insertWithDate("NOT_OLD_ENOUGH", now.minusDays(180)); - insertWithDate("RECENT", now.minusDays(1)); - when(system2.now()).thenReturn(now.toInstant(ZoneOffset.UTC).toEpochMilli()); - - underTest.start(); - - assertThat(selectActivity("VERY_OLD").isPresent()).isFalse(); - assertThat(selectTaskInput("VERY_OLD").isPresent()).isFalse(); - assertThat(selectTaskCharecteristic("VERY_OLD")).hasSize(0); - assertThat(scannerContextExists("VERY_OLD")).isFalse(); - - assertThat(selectActivity("JUST_OLD_ENOUGH").isPresent()).isFalse(); - assertThat(selectTaskInput("JUST_OLD_ENOUGH").isPresent()).isFalse(); - assertThat(selectTaskCharecteristic("JUST_OLD_ENOUGH")).hasSize(0); - assertThat(scannerContextExists("JUST_OLD_ENOUGH")).isFalse(); - - assertThat(selectActivity("NOT_OLD_ENOUGH").isPresent()).isTrue(); - assertThat(selectTaskInput("NOT_OLD_ENOUGH").isPresent()).isTrue(); - assertThat(selectTaskCharecteristic("NOT_OLD_ENOUGH")).hasSize(1); - assertThat(scannerContextExists("NOT_OLD_ENOUGH")).isFalse(); // because more than 4 weeks old - - assertThat(selectActivity("RECENT").isPresent()).isTrue(); - assertThat(selectTaskInput("RECENT").isPresent()).isTrue(); - assertThat(selectTaskCharecteristic("RECENT")).hasSize(1); - assertThat(scannerContextExists("RECENT")).isTrue(); - - } + private DbClient dbClient = mock(DbClient.class); + private PurgeDao purgeDao = mock(PurgeDao.class); + private DbSession dbSession = mock(DbSession.class); + private PurgeProfiler profiler = mock(PurgeProfiler.class); + private PurgeCeActivities underTest = new PurgeCeActivities(dbClient, profiler); @Test - public void delete_ce_scanner_context_older_than_28_days() { - LocalDateTime now = LocalDateTime.now(); - insertWithDate("VERY_OLD", now.minusDays(28).minusMonths(12)); - insertWithDate("JUST_OLD_ENOUGH", now.minusDays(28).minusDays(1)); - insertWithDate("NOT_OLD_ENOUGH", now.minusDays(28)); - insertWithDate("RECENT", now.minusDays(1)); - when(system2.now()).thenReturn(now.toInstant(ZoneOffset.UTC).toEpochMilli()); + public void starts_calls_purgeDao_and_commit() { + when(dbClient.purgeDao()).thenReturn(purgeDao); + when(dbClient.openSession(false)).thenReturn(dbSession); underTest.start(); - assertThat(scannerContextExists("VERY_OLD")).isFalse(); - assertThat(scannerContextExists("JUST_OLD_ENOUGH")).isFalse(); - assertThat(scannerContextExists("NOT_OLD_ENOUGH")).isTrue(); - assertThat(scannerContextExists("RECENT")).isTrue(); - } - - private Optional<CeActivityDto> selectActivity(String taskUuid) { - return dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), taskUuid); - } - - private List<CeTaskCharacteristicDto> selectTaskCharecteristic(String taskUuid) { - return dbTester.getDbClient().ceTaskCharacteristicsDao().selectByTaskUuids(dbTester.getSession(), Collections.singletonList(taskUuid)); - } - - private Optional<CeTaskInputDao.DataStream> selectTaskInput(String taskUuid) { - return dbTester.getDbClient().ceTaskInputDao().selectData(dbTester.getSession(), taskUuid); - } - - private boolean scannerContextExists(String uuid) { - return dbTester.countSql("select count(1) from ce_scanner_context where task_uuid = '" + uuid + "'") == 1; - } - - private void insertWithDate(String uuid, LocalDateTime dateTime) { - long date = dateTime.toInstant(UTC).toEpochMilli(); - CeQueueDto queueDto = new CeQueueDto(); - queueDto.setUuid(uuid); - queueDto.setTaskType(CeTaskTypes.REPORT); - - CeActivityDto dto = new CeActivityDto(queueDto); - dto.setStatus(CeActivityDto.Status.SUCCESS); - when(system2.now()).thenReturn(date); - CeTaskCharacteristicDto ceTaskCharacteristicDto = new CeTaskCharacteristicDto() - .setUuid(UuidFactoryFast.getInstance().create()) - .setValue(randomAlphanumeric(10)) - .setKey(randomAlphanumeric(10)) - .setTaskUuid(dto.getUuid()); - - dbTester.getDbClient().ceTaskInputDao().insert(dbTester.getSession(), dto.getUuid(), IOUtils.toInputStream(randomAlphanumeric(10), Charset.forName("UTF-8"))); - dbTester.getDbClient().ceActivityDao().insert(dbTester.getSession(), dto); - dbTester.getDbClient().ceTaskCharacteristicsDao().insert(dbTester.getSession(), Collections.singletonList(ceTaskCharacteristicDto)); - dbTester.getSession().commit(); - - insertScannerContext(uuid, date); - } - - private void insertScannerContext(String uuid, long createdAt) { - dbTester.executeInsert( - "CE_SCANNER_CONTEXT", - "task_uuid", uuid, - "created_at", createdAt, - "updated_at", 1, - "context_data", "YoloContent".getBytes()); - dbTester.commit(); + InOrder inOrder = Mockito.inOrder(purgeDao, dbSession); + inOrder.verify(purgeDao).purgeCeActivities(dbSession, profiler); + inOrder.verify(purgeDao).purgeCeScannerContexts(dbSession, profiler); + inOrder.verify(dbSession).commit(); + inOrder.verify(dbSession).close(); + inOrder.verifyNoMoreInteractions(); } } |