--- /dev/null
+/*
+ * 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);
+ }
+ }
+}
*/
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;
RegisterServletFilters.class,
RegisterIssueFilters.class,
RenameIssueWidgets.class,
- ServerLifecycleNotifier.class);
+ ServerLifecycleNotifier.class,
+ PurgeCeActivities.class);
}
@Override
--- /dev/null
+/*
+ * 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();
+ }
+}
}
}
+ public void deleteOlderThan(DbSession dbSession, long beforeDate) {
+ mapper(dbSession).deleteOlderThan(beforeDate);
+ }
+
/**
* Ordered by id asc -> oldest to newest
*/
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);
}
where uuid=#{uuid}
</update>
+ <delete id="deleteOlderThan" parameterType="long">
+ delete from ce_activity
+ where created_at < #{beforeDate,jdbcType=BIGINT}
+ </delete>
+
</mapper>
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;
@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);
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);
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);
+ }
}