← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~rvb/launchpad/builder-history-bug-890326 into lp:launchpad

 

Raphaël Badin has proposed merging lp:~rvb/launchpad/builder-history-bug-890326 into lp:launchpad with lp:~rvb/launchpad/builders-timeout-903827 as a prerequisite.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  Bug #890326 in Launchpad itself: "SourcePackageRecipeBuild.buildqueue_record issues one query per recipe build displayed on builder:+history page."
  https://bugs.launchpad.net/launchpad/+bug/890326

For more details, see:
https://code.launchpad.net/~rvb/launchpad/builder-history-bug-890326/+merge/86050

This branch converts sourcepackagerecipebuild.buildqueue_record into a cachedproperty and pre populate it in preloadBuildsData.

= Tests =
(modified)
./bin/test -vvc test_builder_views test_build_history_queries_count_view_recipe_builds

= Q/A =
Find a builder/+history page with recipe build records and make sure that this query http://paste.ubuntu.com/772242/ is not among the repeated statements.
-- 
https://code.launchpad.net/~rvb/launchpad/builder-history-bug-890326/+merge/86050
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/launchpad/builder-history-bug-890326 into lp:launchpad.
=== modified file 'lib/lp/code/model/sourcepackagerecipebuild.py'
--- lib/lp/code/model/sourcepackagerecipebuild.py	2011-12-16 14:51:27 +0000
+++ lib/lp/code/model/sourcepackagerecipebuild.py	2011-12-16 14:51:28 +0000
@@ -71,6 +71,10 @@
 from lp.registry.model.person import Person
 from lp.services.database.bulk import load_related
 from lp.services.job.model.job import Job
+from lp.services.propertycache import (
+    cachedproperty,
+    get_property_cache,
+    )
 from lp.soyuz.interfaces.archive import CannotUploadToArchive
 from lp.soyuz.model.archive import Archive
 from lp.soyuz.model.binarypackagebuild import BinaryPackageBuild
@@ -154,7 +158,7 @@
     requester_id = Int(name='requester', allow_none=False)
     requester = Reference(requester_id, 'Person.id')
 
-    @property
+    @cachedproperty
     def buildqueue_record(self):
         """See `IBuildFarmJob`."""
         store = Store.of(self)
@@ -164,6 +168,20 @@
             SourcePackageRecipeBuildJob.build == self.id)
         return results.one()
 
+    @staticmethod
+    def prefetchBuildqueueRecord(sourcepackagerecipebuilds):
+        ids = [sprb.id for sprb in sourcepackagerecipebuilds]
+        store = IStore(SourcePackageRecipeBuildJob)
+        results = store.find(
+            (SourcePackageRecipeBuildJob, BuildQueue),
+            SourcePackageRecipeBuildJob.job == BuildQueue.jobID,
+            SourcePackageRecipeBuildJob.build_id.is_in(ids))
+        sprb_dict = dict(
+            (result[0].build.id, result[1]) for result in results)
+        for sprb in sourcepackagerecipebuilds:
+            cache = get_property_cache(sprb)
+            cache.buildqueue_record = sprb_dict.get(sprb.id, None)
+
     @property
     def source_package_release(self):
         """See `ISourcePackageRecipeBuild`."""
@@ -292,6 +310,7 @@
     def preloadBuildsData(cls, builds):
         # Circular imports.
         from lp.code.model.sourcepackagerecipe import SourcePackageRecipe
+        SourcePackageRecipeBuild.prefetchBuildqueueRecord(builds)
         package_builds = load_related(
             PackageBuild, builds, ['package_build_id'])
         archives = load_related(Archive, package_builds, ['archive_id'])

=== modified file 'lib/lp/soyuz/browser/tests/test_builder_views.py'
--- lib/lp/soyuz/browser/tests/test_builder_views.py	2011-12-16 14:51:27 +0000
+++ lib/lp/soyuz/browser/tests/test_builder_views.py	2011-12-16 14:51:28 +0000
@@ -180,6 +180,7 @@
         build = self.factory.makeSourcePackageRecipeBuild(
             recipe=self.factory.makeSourcePackageRecipe(
                 branches=[branch1, branch2]))
+        self.factory.makeSourcePackageRecipeBuildJob(recipe_build=build)
         if private_branch:
             with celebrity_logged_in('admin'):
                 branch1.setPrivate(
@@ -231,12 +232,7 @@
             partial(self.createRecipeBuildWithBuilder, builder=self.builder),
             self.nb_objects)
 
-        # XXX: rvb 2011-11-14 bug=890326: The only query remaining is the
-        # one that results from a call to
-        # sourcepackagerecipebuild.buildqueue_record for each recipe build.
-        self.assertThat(
-            recorder2,
-            HasQueryCount(Equals(recorder1.count + 1 * self.nb_objects)))
+        self.assertThat(recorder2, HasQueryCount(Equals(recorder1.count)))
 
     def test_build_history_queries_count_binary_package_builds(self):
         # Rendering to builder's history issues a constant number of queries