Merge lp:~gary/launchpad/bug724025 into lp:launchpad

Proposed by Gary Poster
Status: Merged
Approved by: Gary Poster
Approved revision: no longer in the source branch.
Merged at revision: 13820
Proposed branch: lp:~gary/launchpad/bug724025
Merge into: lp:launchpad
Diff against target: 107 lines (+59/-6)
3 files modified
lib/lp/bugs/browser/bugtask.py (+12/-4)
lib/lp/bugs/browser/tests/test_bugtask.py (+45/-0)
lib/lp/bugs/model/bug.py (+2/-2)
To merge this branch: bzr merge lp:~gary/launchpad/bug724025
Reviewer Review Type Date Requested Status
Robert Collins (community) Approve
Review via email: mp+72776@code.launchpad.net

Commit message

[incr] [r=lifeless][bug=724025] Reduce queries when you have several branches for a bug.

Description of the change

This branch fixes some low-hanging fruit when you have several branches: significantly reduce the queries in this case. More could be done, but I'm about to move on.

lint is happy.

Thank you

To post a comment you must log in.
Revision history for this message
Robert Collins (lifeless) wrote :

I'm a little uncomfortable with the orthogonal comments about storm cache size - this is a pattern we're using all over the place; if we need to up the storm cache size in dev to match prod, lets just do that. This isn't really an actionable review comment, of course.

review: Approve
Revision history for this message
Gary Poster (gary) wrote :

+1 on upping the storm cache on dev, at least to 1000 or so. dev is 100 right now, prod is 10000. I'll make a separate branch for that.

Thanks

Revision history for this message
Gary Poster (gary) wrote :

I had to revert this because the branch on which it relied was reverted. Resubmitting after reinstating.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/bugs/browser/bugtask.py'
2--- lib/lp/bugs/browser/bugtask.py 2011-08-25 20:50:44 +0000
3+++ lib/lp/bugs/browser/bugtask.py 2011-08-29 18:24:04 +0000
4@@ -247,6 +247,7 @@
5 from lp.bugs.interfaces.bugwatch import BugWatchActivityStatus
6 from lp.bugs.interfaces.cve import ICveSet
7 from lp.bugs.interfaces.malone import IMaloneApplication
8+from lp.code.interfaces.branchcollection import IAllBranches
9 from lp.registry.interfaces.distribution import (
10 IDistribution,
11 IDistributionSet,
12@@ -882,10 +883,17 @@
13 @cachedproperty
14 def linked_branches(self):
15 """Filter out the bug_branch links to non-visible private branches."""
16- linked_branches = []
17- for linked_branch in self.context.bug.linked_branches:
18- if check_permission('launchpad.View', linked_branch.branch):
19- linked_branches.append(linked_branch)
20+ linked_branches = list(
21+ self.context.bug.getVisibleLinkedBranches(
22+ self.user, eager_load=True))
23+ # This is an optimization for when we look at the merge proposals.
24+ # Note that, like all of these sorts of Storm cache optimizations, it
25+ # only helps if [launchpad] storm_cache_size in launchpad-lazr.conf is
26+ # pretty big--and as of this writing, it isn't for developer
27+ # instances.
28+ list(getUtility(IAllBranches).getMergeProposals(
29+ for_branches=[link.branch for link in linked_branches],
30+ eager_load=True))
31 return linked_branches
32
33 @property
34
35=== modified file 'lib/lp/bugs/browser/tests/test_bugtask.py'
36--- lib/lp/bugs/browser/tests/test_bugtask.py 2011-08-25 20:50:44 +0000
37+++ lib/lp/bugs/browser/tests/test_bugtask.py 2011-08-29 18:24:04 +0000
38@@ -124,6 +124,51 @@
39 self.factory.makeBugAttachment(bug=task.bug)
40 self.assertThat(task, browses_under_limit)
41
42+ def test_rendered_query_counts_reduced_with_branches(self):
43+ f = self.factory
44+ owner = f.makePerson()
45+ ds = f.makeDistroSeries()
46+ sourcepackagenames = [
47+ f.makeSourcePackageName('testsourcepackagename%d' % i)
48+ for i in range(10)]
49+ sourcepackages = [
50+ f.makeSourcePackage(
51+ sourcepackagename=name, distroseries=ds, publish=True)
52+ for name in sourcepackagenames]
53+ bug = f.makeBug()
54+ bugtasks = []
55+ for sp in sourcepackages:
56+ bugtask = f.makeBugTask(bug=bug, owner=owner, target=sp)
57+ bugtasks.append(bugtask)
58+ task = bugtasks[0]
59+ url = canonical_url(task)
60+ recorder = QueryCollector()
61+ recorder.register()
62+ self.addCleanup(recorder.unregister)
63+ self.invalidate_caches(task)
64+ self.getUserBrowser(url, owner)
65+ # At least 20 of these should be removed.
66+ self.assertThat(recorder, HasQueryCount(LessThan(100)))
67+ count_with_no_branches = recorder.count
68+ self.invalidate_caches(task)
69+ with person_logged_in(owner):
70+ for sp in sourcepackages:
71+ target_branch = f.makePackageBranch(
72+ sourcepackage=sp, owner=owner)
73+ source_branch = f.makeBranchTargetBranch(
74+ target_branch.target, owner=owner)
75+ bug.linkBranch(source_branch, owner)
76+ f.makeBranchMergeProposal(
77+ target_branch=target_branch,
78+ registrant=owner,
79+ source_branch=source_branch)
80+ self.getUserBrowser(url, owner)
81+ # Ideally this should be much fewer, but this tries to keep a win of
82+ # removing more than half of these.
83+ self.assertThat(recorder, HasQueryCount(
84+ LessThan(count_with_no_branches + 45),
85+ ))
86+
87 def test_interesting_activity(self):
88 # The interesting_activity property returns a tuple of interesting
89 # `BugActivityItem`s.
90
91=== modified file 'lib/lp/bugs/model/bug.py'
92--- lib/lp/bugs/model/bug.py 2011-08-25 20:50:44 +0000
93+++ lib/lp/bugs/model/bug.py 2011-08-29 18:24:04 +0000
94@@ -1295,11 +1295,11 @@
95 notify(ObjectDeletedEvent(bug_branch, user=user))
96 bug_branch.destroySelf()
97
98- def getVisibleLinkedBranches(self, user):
99+ def getVisibleLinkedBranches(self, user, eager_load=False):
100 """Return all the branches linked to the bug that `user` can see."""
101 all_branches = getUtility(IAllBranches)
102 linked_branches = list(all_branches.visibleByUser(
103- user).linkedToBugs([self]).getBranches())
104+ user).linkedToBugs([self]).getBranches(eager_load=eager_load))
105 if len(linked_branches) == 0:
106 return EmptyResultSet()
107 else: