You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PurgeDao.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.db.purge;
  21. import java.util.Collection;
  22. import java.util.Comparator;
  23. import java.util.Date;
  24. import java.util.List;
  25. import java.util.Optional;
  26. import java.util.Set;
  27. import java.util.function.Predicate;
  28. import java.util.stream.Collectors;
  29. import javax.annotation.Nullable;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. import org.sonar.api.utils.DateUtils;
  33. import org.sonar.api.utils.System2;
  34. import org.sonar.api.utils.TimeUtils;
  35. import org.sonar.db.Dao;
  36. import org.sonar.db.DbSession;
  37. import org.sonar.db.audit.AuditPersister;
  38. import org.sonar.db.audit.model.ComponentNewValue;
  39. import org.sonar.db.component.BranchDto;
  40. import org.sonar.db.component.BranchMapper;
  41. import org.sonar.db.component.ComponentDto;
  42. import static java.util.Collections.emptyList;
  43. import static java.util.Optional.ofNullable;
  44. import static org.sonar.api.utils.DateUtils.dateToLong;
  45. import static org.sonar.db.DatabaseUtils.executeLargeInputs;
  46. public class PurgeDao implements Dao {
  47. private static final Logger LOG = LoggerFactory.getLogger(PurgeDao.class);
  48. private static final Set<String> QUALIFIERS_PROJECT_VIEW = Set.of("TRK", "VW");
  49. private static final Set<String> QUALIFIER_SUBVIEW = Set.of("SVW");
  50. private static final String SCOPE_PROJECT = "PRJ";
  51. private final System2 system2;
  52. private final AuditPersister auditPersister;
  53. public PurgeDao(System2 system2, AuditPersister auditPersister) {
  54. this.system2 = system2;
  55. this.auditPersister = auditPersister;
  56. }
  57. public void purge(DbSession session, PurgeConfiguration conf, PurgeListener listener, PurgeProfiler profiler) {
  58. PurgeMapper mapper = session.getMapper(PurgeMapper.class);
  59. PurgeCommands commands = new PurgeCommands(session, mapper, profiler, system2);
  60. String rootUuid = conf.rootUuid();
  61. deleteAbortedAnalyses(rootUuid, commands);
  62. purgeAnalyses(commands, rootUuid);
  63. purgeDisabledComponents(commands, conf, listener);
  64. deleteOldClosedIssues(conf, mapper, listener);
  65. deleteOrphanIssues(mapper, rootUuid);
  66. purgeOldCeActivities(session, rootUuid, commands);
  67. purgeOldCeScannerContexts(session, rootUuid, commands);
  68. deleteOldAnticipatedTransitions(commands, conf, conf.projectUuid());
  69. deleteOldDisabledComponents(commands, mapper, rootUuid);
  70. purgeStaleBranches(commands, conf, mapper, rootUuid);
  71. }
  72. private static void purgeStaleBranches(PurgeCommands commands, PurgeConfiguration conf, PurgeMapper mapper, String rootUuid) {
  73. Optional<Date> maxDate = conf.maxLiveDateOfInactiveBranches();
  74. if (maxDate.isEmpty()) {
  75. // not available if branch plugin is not installed
  76. return;
  77. }
  78. LOG.debug("<- Purge stale branches");
  79. Long maxDateValue = ofNullable(dateToLong(maxDate.get())).orElseThrow(IllegalStateException::new);
  80. List<String> branchUuids = mapper.selectStaleBranchesAndPullRequests(conf.projectUuid(), maxDateValue);
  81. for (String branchUuid : branchUuids) {
  82. if (!rootUuid.equals(branchUuid)) {
  83. deleteBranch(branchUuid, commands);
  84. }
  85. }
  86. }
  87. private static void purgeAnalyses(PurgeCommands commands, String rootUuid) {
  88. List<String> analysisUuids = commands.selectSnapshotUuids(
  89. new PurgeSnapshotQuery(rootUuid)
  90. .setIslast(false)
  91. .setNotPurged(true));
  92. commands.purgeAnalyses(analysisUuids);
  93. }
  94. private static void purgeDisabledComponents(PurgeCommands commands, PurgeConfiguration conf, PurgeListener listener) {
  95. String rootUuid = conf.rootUuid();
  96. commands.purgeDisabledComponents(rootUuid, conf.getDisabledComponentUuids(), listener);
  97. }
  98. private static void deleteOrphanIssues(PurgeMapper mapper, String rootUuid) {
  99. LOG.debug("<- Delete orphan issues");
  100. List<String> issueKeys = mapper.selectBranchOrphanIssues(rootUuid);
  101. deleteIssues(mapper, issueKeys);
  102. }
  103. private static void deleteOldAnticipatedTransitions(PurgeCommands commands, PurgeConfiguration purgeConfiguration, String projectUuid) {
  104. LOG.debug("<- Delete Old Anticipated Transitions");
  105. commands.deleteAnticipatedTransitions(projectUuid, purgeConfiguration.maxLiveDateOfAnticipatedTransitions().toEpochMilli());
  106. }
  107. private static void deleteOldClosedIssues(PurgeConfiguration conf, PurgeMapper mapper, PurgeListener listener) {
  108. Date toDate = conf.maxLiveDateOfClosedIssues();
  109. String rootUuid = conf.rootUuid();
  110. List<String> issueKeys = mapper.selectOldClosedIssueKeys(rootUuid, dateToLong(toDate));
  111. deleteIssues(mapper, issueKeys);
  112. listener.onIssuesRemoval(conf.projectUuid(), issueKeys);
  113. }
  114. private static void deleteIssues(PurgeMapper mapper, Collection<String> issueKeys) {
  115. executeLargeInputs(issueKeys, input -> {
  116. mapper.deleteIssueChangesFromIssueKeys(input);
  117. return emptyList();
  118. });
  119. executeLargeInputs(issueKeys, input -> {
  120. mapper.deleteNewCodeReferenceIssuesFromKeys(input);
  121. return emptyList();
  122. });
  123. executeLargeInputs(issueKeys, input -> {
  124. mapper.deleteIssuesImpactsFromKeys(input);
  125. return emptyList();
  126. });
  127. executeLargeInputs(issueKeys, input -> {
  128. mapper.deleteIssuesFromKeys(input);
  129. return emptyList();
  130. });
  131. }
  132. private static void deleteAbortedAnalyses(String rootUuid, PurgeCommands commands) {
  133. LOG.debug("<- Delete aborted builds");
  134. commands.deleteAbortedAnalyses(rootUuid);
  135. }
  136. private static void deleteOldDisabledComponents(PurgeCommands commands, PurgeMapper mapper, String rootUuid) {
  137. List<String> disabledComponentsWithoutIssue = mapper.selectDisabledComponentsWithoutIssues(rootUuid);
  138. commands.deleteDisabledComponentsWithoutIssues(disabledComponentsWithoutIssue);
  139. }
  140. public List<PurgeableAnalysisDto> selectPurgeableAnalyses(String componentUuid, DbSession session) {
  141. PurgeMapper mapper = mapper(session);
  142. return mapper.selectPurgeableAnalyses(componentUuid).stream()
  143. .filter(new NewCodePeriodAnalysisFilter(mapper, componentUuid))
  144. .sorted()
  145. .toList();
  146. }
  147. public void purgeCeActivities(DbSession session, PurgeProfiler profiler) {
  148. PurgeMapper mapper = session.getMapper(PurgeMapper.class);
  149. PurgeCommands commands = new PurgeCommands(session, mapper, profiler, system2);
  150. purgeOldCeActivities(session, null, commands);
  151. }
  152. private void purgeOldCeActivities(DbSession session, @Nullable String rootUuid, PurgeCommands commands) {
  153. String entityUuidToPurge = getEntityUuidToPurge(session, rootUuid);
  154. Date sixMonthsAgo = DateUtils.addDays(new Date(system2.now()), -180);
  155. commands.deleteCeActivityBefore(rootUuid, entityUuidToPurge, sixMonthsAgo.getTime());
  156. }
  157. /**
  158. * When the rootUuid is the main branch of a project, we also want to clean the old activities and context of other branches.
  159. * This is probably to ensure that the cleanup happens regularly on branch that are not as active as the main branch.
  160. */
  161. @Nullable
  162. private static String getEntityUuidToPurge(DbSession session, @Nullable String rootUuid) {
  163. if (rootUuid == null) {
  164. return null;
  165. }
  166. BranchDto branch = session.getMapper(BranchMapper.class).selectByUuid(rootUuid);
  167. String entityUuidToPurge = null;
  168. if (branch != null && branch.isMain()) {
  169. entityUuidToPurge = branch.getProjectUuid();
  170. }
  171. return entityUuidToPurge;
  172. }
  173. public void purgeCeScannerContexts(DbSession session, PurgeProfiler profiler) {
  174. PurgeMapper mapper = session.getMapper(PurgeMapper.class);
  175. PurgeCommands commands = new PurgeCommands(session, mapper, profiler, system2);
  176. purgeOldCeScannerContexts(session, null, commands);
  177. }
  178. private void purgeOldCeScannerContexts(DbSession session, @Nullable String rootUuid, PurgeCommands commands) {
  179. Date fourWeeksAgo = DateUtils.addDays(new Date(system2.now()), -28);
  180. String entityUuidToPurge = getEntityUuidToPurge(session, rootUuid);
  181. commands.deleteCeScannerContextBefore(rootUuid, entityUuidToPurge, fourWeeksAgo.getTime());
  182. }
  183. private static final class NewCodePeriodAnalysisFilter implements Predicate<PurgeableAnalysisDto> {
  184. @Nullable
  185. private final String analysisUuid;
  186. private NewCodePeriodAnalysisFilter(PurgeMapper mapper, String componentUuid) {
  187. this.analysisUuid = mapper.selectSpecificAnalysisNewCodePeriod(componentUuid);
  188. }
  189. @Override
  190. public boolean test(PurgeableAnalysisDto purgeableAnalysisDto) {
  191. return analysisUuid == null || !analysisUuid.equals(purgeableAnalysisDto.getAnalysisUuid());
  192. }
  193. }
  194. public void deleteBranch(DbSession session, String uuid) {
  195. PurgeProfiler profiler = new PurgeProfiler();
  196. PurgeCommands purgeCommands = new PurgeCommands(session, profiler, system2);
  197. deleteBranch(uuid, purgeCommands);
  198. }
  199. public void deleteProject(DbSession session, String uuid, String qualifier, String name, String key) {
  200. PurgeProfiler profiler = new PurgeProfiler();
  201. PurgeMapper purgeMapper = mapper(session);
  202. PurgeCommands purgeCommands = new PurgeCommands(session, profiler, system2);
  203. long start = System2.INSTANCE.now();
  204. List<String> branchUuids = session.getMapper(BranchMapper.class).selectByProjectUuid(uuid).stream()
  205. // Main branch is deleted last
  206. .sorted(Comparator.comparing(BranchDto::isMain))
  207. .map(BranchDto::getUuid)
  208. .toList();
  209. branchUuids.forEach(id -> deleteBranch(id, purgeCommands));
  210. deleteProject(uuid, purgeMapper, purgeCommands);
  211. auditPersister.deleteComponent(session, new ComponentNewValue(uuid, name, key, qualifier));
  212. logProfiling(profiler, start);
  213. }
  214. private static void logProfiling(PurgeProfiler profiler, long start) {
  215. if (!LOG.isDebugEnabled()) {
  216. return;
  217. }
  218. long duration = System.currentTimeMillis() - start;
  219. LOG.debug("");
  220. LOG.atDebug().setMessage(" -------- Profiling for project deletion: {} --------").addArgument(() -> TimeUtils.formatDuration(duration)).log();
  221. LOG.debug("");
  222. for (String line : profiler.getProfilingResult(duration)) {
  223. LOG.debug(line);
  224. }
  225. LOG.debug("");
  226. LOG.debug(" -------- End of profiling for project deletion--------");
  227. LOG.debug("");
  228. }
  229. private static void deleteBranch(String branchUuid, PurgeCommands commands) {
  230. commands.deleteScannerCache(branchUuid);
  231. commands.deleteAnalyses(branchUuid);
  232. commands.deleteIssues(branchUuid);
  233. commands.deleteFileSources(branchUuid);
  234. commands.deleteCeActivity(branchUuid);
  235. commands.deleteCeQueue(branchUuid);
  236. commands.deleteLiveMeasures(branchUuid);
  237. commands.deleteNewCodePeriodsForBranch(branchUuid);
  238. commands.deleteBranch(branchUuid);
  239. commands.deleteApplicationBranchProjects(branchUuid);
  240. commands.deleteComponents(branchUuid);
  241. commands.deleteReportSchedules(branchUuid);
  242. commands.deleteReportSubscriptions(branchUuid);
  243. }
  244. private static void deleteProject(String projectUuid, PurgeMapper mapper, PurgeCommands commands) {
  245. List<String> rootAndSubviews = mapper.selectRootAndSubviewsByProjectUuid(projectUuid);
  246. commands.deleteLinks(projectUuid);
  247. commands.deleteScannerCache(projectUuid);
  248. commands.deleteEventComponentChanges(projectUuid);
  249. commands.deleteAnalyses(projectUuid);
  250. commands.deleteByRootAndSubviews(rootAndSubviews);
  251. commands.deleteIssues(projectUuid);
  252. commands.deleteFileSources(projectUuid);
  253. commands.deleteCeActivity(projectUuid);
  254. commands.deleteCeQueue(projectUuid);
  255. commands.deleteWebhooks(projectUuid);
  256. commands.deleteWebhookDeliveries(projectUuid);
  257. commands.deleteLiveMeasures(projectUuid);
  258. commands.deleteProjectAlmSettings(projectUuid);
  259. commands.deletePermissions(projectUuid);
  260. commands.deleteNewCodePeriodsForProject(projectUuid);
  261. commands.deleteBranch(projectUuid);
  262. commands.deleteApplicationBranchProjects(projectUuid);
  263. commands.deleteApplicationProjects(projectUuid);
  264. commands.deleteApplicationProjectsByProject(projectUuid);
  265. commands.deleteProjectInPortfolios(projectUuid);
  266. commands.deleteComponents(projectUuid);
  267. commands.deleteNonMainBranchComponentsByProjectUuid(projectUuid);
  268. commands.deleteProjectBadgeToken(projectUuid);
  269. commands.deleteProject(projectUuid);
  270. commands.deleteUserDismissedMessages(projectUuid);
  271. commands.deleteOutdatedProperties(projectUuid);
  272. commands.deleteReportSchedules(projectUuid);
  273. commands.deleteReportSubscriptions(projectUuid);
  274. }
  275. /**
  276. * Delete the non root components (ie. sub-view, application or project copy) from the specified collection of {@link ComponentDto}
  277. * and data from their child tables.
  278. * <p>
  279. * This method has no effect when passed an empty collection or only root components.
  280. * </p>
  281. */
  282. public void deleteNonRootComponentsInView(DbSession dbSession, Collection<ComponentDto> components) {
  283. Set<ComponentDto> nonRootComponents = components.stream().filter(PurgeDao::isNotRoot).collect(Collectors.toSet());
  284. if (nonRootComponents.isEmpty()) {
  285. return;
  286. }
  287. PurgeProfiler profiler = new PurgeProfiler();
  288. PurgeCommands purgeCommands = new PurgeCommands(dbSession, profiler, system2);
  289. deleteNonRootComponentsInView(nonRootComponents, purgeCommands);
  290. }
  291. private static void deleteNonRootComponentsInView(Set<ComponentDto> nonRootComponents, PurgeCommands purgeCommands) {
  292. List<String> subviewsOrProjectCopies = nonRootComponents.stream()
  293. .filter(PurgeDao::isSubview)
  294. .map(ComponentDto::uuid)
  295. .toList();
  296. purgeCommands.deleteByRootAndSubviews(subviewsOrProjectCopies);
  297. List<String> nonRootComponentUuids = nonRootComponents.stream().map(ComponentDto::uuid).toList();
  298. purgeCommands.deleteComponentMeasures(nonRootComponentUuids);
  299. purgeCommands.deleteComponents(nonRootComponentUuids);
  300. }
  301. private static boolean isNotRoot(ComponentDto dto) {
  302. return !(SCOPE_PROJECT.equals(dto.scope()) && QUALIFIERS_PROJECT_VIEW.contains(dto.qualifier()));
  303. }
  304. private static boolean isSubview(ComponentDto dto) {
  305. return SCOPE_PROJECT.equals(dto.scope()) && QUALIFIER_SUBVIEW.contains(dto.qualifier());
  306. }
  307. public void deleteAnalyses(DbSession session, PurgeProfiler profiler, List<String> analysisUuids) {
  308. new PurgeCommands(session, profiler, system2).deleteAnalyses(analysisUuids);
  309. }
  310. private static PurgeMapper mapper(DbSession session) {
  311. return session.getMapper(PurgeMapper.class);
  312. }
  313. }