Merge ~cjwatson/launchpad:bmp-job-pruner into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 1f33e2592979a42b2ed71f0bd225520f94127dde
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:bmp-job-pruner
Merge into: launchpad:master
Diff against target: 115 lines (+69/-2)
2 files modified
lib/lp/scripts/garbo.py (+21/-1)
lib/lp/scripts/tests/test_garbo.py (+48/-1)
Reviewer Review Type Date Requested Status
Guruprasad Approve
Review via email: mp+444874@code.launchpad.net

Commit message

Prune old completed BranchMergeProposalJobs

Description of the change

This accounts for about 4 million out of the 24 million Job rows on production, so it seems worth the effort of pruning.

This is a rebased version of https://code.launchpad.net/~cjwatson/launchpad/bmp-job-pruner/+merge/362460, which couldn't be merged directly since it was from the bzr era.

To post a comment you must log in.
Revision history for this message
Guruprasad (lgp171188) wrote :

LGTM 👍

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/lib/lp/scripts/garbo.py b/lib/lp/scripts/garbo.py
index 92ab040..b8c425c 100644
--- a/lib/lp/scripts/garbo.py
+++ b/lib/lp/scripts/garbo.py
@@ -1,4 +1,4 @@
1# Copyright 2009-2020 Canonical Ltd. This software is licensed under the1# Copyright 2009-2023 Canonical Ltd. This software is licensed under the
2# GNU Affero General Public License version 3 (see the file LICENSE).2# GNU Affero General Public License version 3 (see the file LICENSE).
33
4"""Database garbage collection."""4"""Database garbage collection."""
@@ -1315,6 +1315,25 @@ class GitJobPruner(BulkPruner):
1315 """1315 """
13161316
13171317
1318class BranchMergeProposalJobPruner(BulkPruner):
1319 """Prune `BranchMergeProposalJob`s that are in a final state and more
1320 than a month old.
1321
1322 When a BranchMergeProposalJob is completed, it gets set to a final
1323 state. These jobs should be pruned from the database after a month.
1324 """
1325
1326 target_table_class = Job
1327 ids_to_prune_query = """
1328 SELECT DISTINCT Job.id
1329 FROM Job, BranchMergeProposalJob
1330 WHERE
1331 Job.id = BranchMergeProposalJob.job
1332 AND Job.date_finished < CURRENT_TIMESTAMP AT TIME ZONE 'UTC'
1333 - CAST('30 days' AS interval)
1334 """
1335
1336
1318class SnapBuildJobPruner(BulkPruner):1337class SnapBuildJobPruner(BulkPruner):
1319 """Prune `SnapBuildJob`s that are in a final state and more than a month1338 """Prune `SnapBuildJob`s that are in a final state and more than a month
1320 old.1339 old.
@@ -2557,6 +2576,7 @@ class DailyDatabaseGarbageCollector(BaseDatabaseGarbageCollector):
2557 BinaryPackagePublishingHistoryFormatPopulator,2576 BinaryPackagePublishingHistoryFormatPopulator,
2558 BinaryPackagePublishingHistorySPNPopulator,2577 BinaryPackagePublishingHistorySPNPopulator,
2559 BranchJobPruner,2578 BranchJobPruner,
2579 BranchMergeProposalJobPruner,
2560 BugNotificationPruner,2580 BugNotificationPruner,
2561 BugWatchActivityPruner,2581 BugWatchActivityPruner,
2562 CodeImportEventPruner,2582 CodeImportEventPruner,
diff --git a/lib/lp/scripts/tests/test_garbo.py b/lib/lp/scripts/tests/test_garbo.py
index 796866f..cd06fcc 100644
--- a/lib/lp/scripts/tests/test_garbo.py
+++ b/lib/lp/scripts/tests/test_garbo.py
@@ -1,4 +1,4 @@
1# Copyright 2009-2020 Canonical Ltd. This software is licensed under the1# Copyright 2009-2023 Canonical Ltd. This software is licensed under the
2# GNU Affero General Public License version 3 (see the file LICENSE).2# GNU Affero General Public License version 3 (see the file LICENSE).
33
4"""Test the database garbage collector."""4"""Test the database garbage collector."""
@@ -54,6 +54,7 @@ from lp.code.interfaces.revisionstatus import (
54 IRevisionStatusReportSet,54 IRevisionStatusReportSet,
55)55)
56from lp.code.model.branchjob import BranchJob, BranchUpgradeJob56from lp.code.model.branchjob import BranchJob, BranchUpgradeJob
57from lp.code.model.branchmergeproposaljob import BranchMergeProposalJob
57from lp.code.model.codeimportevent import CodeImportEvent58from lp.code.model.codeimportevent import CodeImportEvent
58from lp.code.model.codeimportresult import CodeImportResult59from lp.code.model.codeimportresult import CodeImportResult
59from lp.code.model.diff import Diff60from lp.code.model.diff import Diff
@@ -978,6 +979,52 @@ class TestGarbo(FakeAdapterMixin, TestCaseWithFactory):
978 switch_dbuser("testadmin")979 switch_dbuser("testadmin")
979 self.assertEqual(store.find(BranchJob).count(), 1)980 self.assertEqual(store.find(BranchJob).count(), 1)
980981
982 def test_BranchMergeProposalJobPruner(self):
983 # Garbo should remove jobs completed over 30 days ago.
984 switch_dbuser("testadmin")
985 store = IPrimaryStore(Job)
986
987 bmp = self.factory.makeBranchMergeProposal()
988 bmp_job = removeSecurityProxy(bmp.next_preview_diff_job)
989 bmp_job.job.date_finished = THIRTY_DAYS_AGO
990
991 self.assertEqual(
992 store.find(
993 BranchMergeProposalJob,
994 BranchMergeProposalJob.branch_merge_proposal == bmp.id,
995 ).count(),
996 1,
997 )
998
999 self.runDaily()
1000
1001 switch_dbuser("testadmin")
1002 self.assertEqual(
1003 store.find(
1004 BranchMergeProposalJob,
1005 BranchMergeProposalJob.branch_merge_proposal == bmp.id,
1006 ).count(),
1007 0,
1008 )
1009
1010 def test_BranchMergeProposalJobPruner_doesnt_prune_recent_jobs(self):
1011 # Check to make sure the garbo doesn't remove jobs that aren't more
1012 # than thirty days old.
1013 switch_dbuser("testadmin")
1014 store = IPrimaryStore(Job)
1015
1016 bmp = self.factory.makeBranchMergeProposal()
1017 bmp_job = removeSecurityProxy(bmp.next_preview_diff_job)
1018 bmp_job.job.date_finished = THIRTY_DAYS_AGO
1019
1020 bmp2 = self.factory.makeBranchMergeProposal()
1021 self.assertIsNotNone(bmp2.next_preview_diff_job)
1022
1023 self.runDaily()
1024
1025 switch_dbuser("testadmin")
1026 self.assertEqual(store.find(BranchMergeProposalJob).count(), 1)
1027
981 def test_GitJobPruner(self):1028 def test_GitJobPruner(self):
982 # Garbo should remove jobs completed over 30 days ago.1029 # Garbo should remove jobs completed over 30 days ago.
983 switch_dbuser("testadmin")1030 switch_dbuser("testadmin")

Subscribers

People subscribed via source and target branches

to status/vote changes: