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
1diff --git a/lib/lp/scripts/garbo.py b/lib/lp/scripts/garbo.py
2index 92ab040..b8c425c 100644
3--- a/lib/lp/scripts/garbo.py
4+++ b/lib/lp/scripts/garbo.py
5@@ -1,4 +1,4 @@
6-# Copyright 2009-2020 Canonical Ltd. This software is licensed under the
7+# Copyright 2009-2023 Canonical Ltd. This software is licensed under the
8 # GNU Affero General Public License version 3 (see the file LICENSE).
9
10 """Database garbage collection."""
11@@ -1315,6 +1315,25 @@ class GitJobPruner(BulkPruner):
12 """
13
14
15+class BranchMergeProposalJobPruner(BulkPruner):
16+ """Prune `BranchMergeProposalJob`s that are in a final state and more
17+ than a month old.
18+
19+ When a BranchMergeProposalJob is completed, it gets set to a final
20+ state. These jobs should be pruned from the database after a month.
21+ """
22+
23+ target_table_class = Job
24+ ids_to_prune_query = """
25+ SELECT DISTINCT Job.id
26+ FROM Job, BranchMergeProposalJob
27+ WHERE
28+ Job.id = BranchMergeProposalJob.job
29+ AND Job.date_finished < CURRENT_TIMESTAMP AT TIME ZONE 'UTC'
30+ - CAST('30 days' AS interval)
31+ """
32+
33+
34 class SnapBuildJobPruner(BulkPruner):
35 """Prune `SnapBuildJob`s that are in a final state and more than a month
36 old.
37@@ -2557,6 +2576,7 @@ class DailyDatabaseGarbageCollector(BaseDatabaseGarbageCollector):
38 BinaryPackagePublishingHistoryFormatPopulator,
39 BinaryPackagePublishingHistorySPNPopulator,
40 BranchJobPruner,
41+ BranchMergeProposalJobPruner,
42 BugNotificationPruner,
43 BugWatchActivityPruner,
44 CodeImportEventPruner,
45diff --git a/lib/lp/scripts/tests/test_garbo.py b/lib/lp/scripts/tests/test_garbo.py
46index 796866f..cd06fcc 100644
47--- a/lib/lp/scripts/tests/test_garbo.py
48+++ b/lib/lp/scripts/tests/test_garbo.py
49@@ -1,4 +1,4 @@
50-# Copyright 2009-2020 Canonical Ltd. This software is licensed under the
51+# Copyright 2009-2023 Canonical Ltd. This software is licensed under the
52 # GNU Affero General Public License version 3 (see the file LICENSE).
53
54 """Test the database garbage collector."""
55@@ -54,6 +54,7 @@ from lp.code.interfaces.revisionstatus import (
56 IRevisionStatusReportSet,
57 )
58 from lp.code.model.branchjob import BranchJob, BranchUpgradeJob
59+from lp.code.model.branchmergeproposaljob import BranchMergeProposalJob
60 from lp.code.model.codeimportevent import CodeImportEvent
61 from lp.code.model.codeimportresult import CodeImportResult
62 from lp.code.model.diff import Diff
63@@ -978,6 +979,52 @@ class TestGarbo(FakeAdapterMixin, TestCaseWithFactory):
64 switch_dbuser("testadmin")
65 self.assertEqual(store.find(BranchJob).count(), 1)
66
67+ def test_BranchMergeProposalJobPruner(self):
68+ # Garbo should remove jobs completed over 30 days ago.
69+ switch_dbuser("testadmin")
70+ store = IPrimaryStore(Job)
71+
72+ bmp = self.factory.makeBranchMergeProposal()
73+ bmp_job = removeSecurityProxy(bmp.next_preview_diff_job)
74+ bmp_job.job.date_finished = THIRTY_DAYS_AGO
75+
76+ self.assertEqual(
77+ store.find(
78+ BranchMergeProposalJob,
79+ BranchMergeProposalJob.branch_merge_proposal == bmp.id,
80+ ).count(),
81+ 1,
82+ )
83+
84+ self.runDaily()
85+
86+ switch_dbuser("testadmin")
87+ self.assertEqual(
88+ store.find(
89+ BranchMergeProposalJob,
90+ BranchMergeProposalJob.branch_merge_proposal == bmp.id,
91+ ).count(),
92+ 0,
93+ )
94+
95+ def test_BranchMergeProposalJobPruner_doesnt_prune_recent_jobs(self):
96+ # Check to make sure the garbo doesn't remove jobs that aren't more
97+ # than thirty days old.
98+ switch_dbuser("testadmin")
99+ store = IPrimaryStore(Job)
100+
101+ bmp = self.factory.makeBranchMergeProposal()
102+ bmp_job = removeSecurityProxy(bmp.next_preview_diff_job)
103+ bmp_job.job.date_finished = THIRTY_DAYS_AGO
104+
105+ bmp2 = self.factory.makeBranchMergeProposal()
106+ self.assertIsNotNone(bmp2.next_preview_diff_job)
107+
108+ self.runDaily()
109+
110+ switch_dbuser("testadmin")
111+ self.assertEqual(store.find(BranchMergeProposalJob).count(), 1)
112+
113 def test_GitJobPruner(self):
114 # Garbo should remove jobs completed over 30 days ago.
115 switch_dbuser("testadmin")

Subscribers

People subscribed via source and target branches

to status/vote changes: