Merge lp:~allenap/launchpad/dynamic-batch-size-bug-546085-devel into lp:launchpad
- dynamic-batch-size-bug-546085-devel
- Merge into devel
Proposed by
Gavin Panella
Status: | Merged |
---|---|
Approved by: | Gavin Panella |
Approved revision: | no longer in the source branch. |
Merged at revision: | not available |
Proposed branch: | lp:~allenap/launchpad/dynamic-batch-size-bug-546085-devel |
Merge into: | lp:launchpad |
Diff against target: |
600 lines (+203/-124) 11 files modified
lib/lp/bugs/doc/checkwatches.txt (+6/-3) lib/lp/bugs/doc/externalbugtracker-debbugs.txt (+3/-3) lib/lp/bugs/doc/externalbugtracker.txt (+5/-2) lib/lp/bugs/externalbugtracker/__init__.py (+4/-1) lib/lp/bugs/externalbugtracker/base.py (+5/-1) lib/lp/bugs/externalbugtracker/debbugs.py (+8/-7) lib/lp/bugs/scripts/checkwatches/tests/test_scheduler.py (+110/-0) lib/lp/bugs/scripts/checkwatches/tests/test_updater.py (+15/-0) lib/lp/bugs/scripts/checkwatches/updater.py (+40/-16) lib/lp/bugs/tests/externalbugtracker.py (+4/-2) lib/lp/bugs/tests/test_bugwatch.py (+3/-89) |
To merge this branch: | bzr merge lp:~allenap/launchpad/dynamic-batch-size-bug-546085-devel |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Abel Deuring (community) | code | Approve | |
Gavin Panella (community) | code | Abstain | |
Review via email: mp+23105@code.launchpad.net |
Commit message
In checkwatches, calculate a sensible batch size at run-time based on the number of watches there are for a bug tracker. Originally landed in db-devel r9213.
Description of the change
This is a cherry-pick of db-devel r9213 into devel. I really should have landed it here in the first place. It's got a good chance of causing conflicts so I don't want it to remain only in db-devel for the whole cycle. It has already been reviewed - https:/
To post a comment you must log in.
Revision history for this message
Abel Deuring (adeuring) : | # |
review:
Approve
(rs)
Revision history for this message
Gavin Panella (allenap) : | # |
review:
Abstain
(code)
Revision history for this message
Abel Deuring (adeuring) : | # |
review:
Approve
(code)
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'lib/lp/bugs/doc/checkwatches.txt' | |||
2 | --- lib/lp/bugs/doc/checkwatches.txt 2010-03-30 17:25:52 +0000 | |||
3 | +++ lib/lp/bugs/doc/checkwatches.txt 2010-04-09 14:24:25 +0000 | |||
4 | @@ -45,7 +45,7 @@ | |||
5 | 45 | 45 | ||
6 | 46 | >>> print err | 46 | >>> print err |
7 | 47 | INFO creating lockfile | 47 | INFO creating lockfile |
9 | 48 | DEBUG Using a global batch size of None | 48 | DEBUG No global batch size specified. |
10 | 49 | DEBUG Skipping updating Ubuntu Bugzilla watches. | 49 | DEBUG Skipping updating Ubuntu Bugzilla watches. |
11 | 50 | DEBUG No watches to update on http://bugs.debian.org | 50 | DEBUG No watches to update on http://bugs.debian.org |
12 | 51 | DEBUG No watches to update on mailto:bugs@example.com | 51 | DEBUG No watches to update on mailto:bugs@example.com |
13 | @@ -274,7 +274,7 @@ | |||
14 | 274 | ... updater.log = original_log | 274 | ... updater.log = original_log |
15 | 275 | ... externalbugtracker.Roundup.batch_size = batch_size | 275 | ... externalbugtracker.Roundup.batch_size = batch_size |
16 | 276 | ... urllib2.urlopen = urlopen | 276 | ... urllib2.urlopen = urlopen |
18 | 277 | DEBUG Using a global batch size of None | 277 | DEBUG No global batch size specified. |
19 | 278 | INFO Updating 5 watches for 5 bugs on http://bugs.example.com | 278 | INFO Updating 5 watches for 5 bugs on http://bugs.example.com |
20 | 279 | ERROR Connection timed out when updating ... (OOPS-...) | 279 | ERROR Connection timed out when updating ... (OOPS-...) |
21 | 280 | 280 | ||
22 | @@ -412,7 +412,8 @@ | |||
23 | 412 | >>> from lp.bugs.interfaces.externalbugtracker import ( | 412 | >>> from lp.bugs.interfaces.externalbugtracker import ( |
24 | 413 | ... ISupportsCommentImport, ISupportsCommentPushing, | 413 | ... ISupportsCommentImport, ISupportsCommentPushing, |
25 | 414 | ... ISupportsBackLinking) | 414 | ... ISupportsBackLinking) |
27 | 415 | >>> from lp.bugs.externalbugtracker.base import ExternalBugTracker | 415 | >>> from lp.bugs.externalbugtracker.base import ( |
28 | 416 | ... BATCH_SIZE_UNLIMITED, ExternalBugTracker) | ||
29 | 416 | 417 | ||
30 | 417 | >>> nowish = datetime.now(utc) | 418 | >>> nowish = datetime.now(utc) |
31 | 418 | >>> class UselessExternalBugTracker(ExternalBugTracker): | 419 | >>> class UselessExternalBugTracker(ExternalBugTracker): |
32 | @@ -421,6 +422,8 @@ | |||
33 | 421 | ... ISupportsBackLinking, ISupportsCommentImport, | 422 | ... ISupportsBackLinking, ISupportsCommentImport, |
34 | 422 | ... ISupportsCommentPushing) | 423 | ... ISupportsCommentPushing) |
35 | 423 | ... | 424 | ... |
36 | 425 | ... batch_size = BATCH_SIZE_UNLIMITED | ||
37 | 426 | ... | ||
38 | 424 | ... def initializeRemoteBugDB(self, bug_ids): | 427 | ... def initializeRemoteBugDB(self, bug_ids): |
39 | 425 | ... # This just exists to stop errors from being raised. | 428 | ... # This just exists to stop errors from being raised. |
40 | 426 | ... pass | 429 | ... pass |
41 | 427 | 430 | ||
42 | === modified file 'lib/lp/bugs/doc/externalbugtracker-debbugs.txt' | |||
43 | --- lib/lp/bugs/doc/externalbugtracker-debbugs.txt 2010-03-26 13:48:53 +0000 | |||
44 | +++ lib/lp/bugs/doc/externalbugtracker-debbugs.txt 2010-04-09 14:24:25 +0000 | |||
45 | @@ -14,7 +14,7 @@ | |||
46 | 14 | You can specify the db_location explicitly: | 14 | You can specify the db_location explicitly: |
47 | 15 | 15 | ||
48 | 16 | >>> from lp.bugs.externalbugtracker import ( | 16 | >>> from lp.bugs.externalbugtracker import ( |
50 | 17 | ... DebBugs) | 17 | ... BATCH_SIZE_UNLIMITED, DebBugs) |
51 | 18 | >>> from canonical.testing import LaunchpadZopelessLayer | 18 | >>> from canonical.testing import LaunchpadZopelessLayer |
52 | 19 | >>> txn = LaunchpadZopelessLayer.txn | 19 | >>> txn = LaunchpadZopelessLayer.txn |
53 | 20 | >>> external_debbugs = DebBugs( | 20 | >>> external_debbugs = DebBugs( |
54 | @@ -42,8 +42,8 @@ | |||
55 | 42 | worry about batching bug watch updates for performance reasons, so | 42 | worry about batching bug watch updates for performance reasons, so |
56 | 43 | DebBugs instances don't have a batch_size limit. | 43 | DebBugs instances don't have a batch_size limit. |
57 | 44 | 44 | ||
60 | 45 | >>> print external_debbugs.batch_size | 45 | >>> external_debbugs.batch_size == BATCH_SIZE_UNLIMITED |
61 | 46 | None | 46 | True |
62 | 47 | 47 | ||
63 | 48 | 48 | ||
64 | 49 | == Retrieving bug status from the debbugs database == | 49 | == Retrieving bug status from the debbugs database == |
65 | 50 | 50 | ||
66 | === modified file 'lib/lp/bugs/doc/externalbugtracker.txt' | |||
67 | --- lib/lp/bugs/doc/externalbugtracker.txt 2010-03-26 15:12:59 +0000 | |||
68 | +++ lib/lp/bugs/doc/externalbugtracker.txt 2010-04-09 14:24:25 +0000 | |||
69 | @@ -1042,13 +1042,16 @@ | |||
70 | 1042 | 1042 | ||
71 | 1043 | _getRemoteIdsToCheck() will interpret a batch_size parameter of 0 as an | 1043 | _getRemoteIdsToCheck() will interpret a batch_size parameter of 0 as an |
72 | 1044 | instruction to ignore the batch size limitation altogether and just return all | 1044 | instruction to ignore the batch size limitation altogether and just return all |
74 | 1045 | the IDs that need checking. | 1045 | the IDs that need checking. The constant BATCH_SIZE_UNLIMITED should |
75 | 1046 | be used in place of using 0 verbatim. | ||
76 | 1047 | |||
77 | 1048 | >>> from lp.bugs.externalbugtracker import BATCH_SIZE_UNLIMITED | ||
78 | 1046 | 1049 | ||
79 | 1047 | >>> ids = bug_watch_updater._getRemoteIdsToCheck( | 1050 | >>> ids = bug_watch_updater._getRemoteIdsToCheck( |
80 | 1048 | ... external_bugtracker, | 1051 | ... external_bugtracker, |
81 | 1049 | ... unchecked_watches + watches_with_comments + old_watches, | 1052 | ... unchecked_watches + watches_with_comments + old_watches, |
82 | 1050 | ... external_bugtracker.getCurrentDBTime(), utc_now, | 1053 | ... external_bugtracker.getCurrentDBTime(), utc_now, |
84 | 1051 | ... batch_size=0) | 1054 | ... batch_size=BATCH_SIZE_UNLIMITED) |
85 | 1052 | >>> print sorted(ids['remote_ids_to_check']) | 1055 | >>> print sorted(ids['remote_ids_to_check']) |
86 | 1053 | [u'0', u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9'] | 1056 | [u'0', u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9'] |
87 | 1054 | 1057 | ||
88 | 1055 | 1058 | ||
89 | === modified file 'lib/lp/bugs/externalbugtracker/__init__.py' | |||
90 | --- lib/lp/bugs/externalbugtracker/__init__.py 2009-06-25 00:40:31 +0000 | |||
91 | +++ lib/lp/bugs/externalbugtracker/__init__.py 2010-04-09 14:24:25 +0000 | |||
92 | @@ -7,7 +7,7 @@ | |||
93 | 7 | 7 | ||
94 | 8 | __metaclass__ = type | 8 | __metaclass__ = type |
95 | 9 | __all__ = [ | 9 | __all__ = [ |
97 | 10 | 'get_external_bugtracker', | 10 | 'BATCH_SIZE_UNLIMITED', |
98 | 11 | 'BugNotFound', | 11 | 'BugNotFound', |
99 | 12 | 'BugTrackerConnectError', | 12 | 'BugTrackerConnectError', |
100 | 13 | 'BugWatchUpdateError', | 13 | 'BugWatchUpdateError', |
101 | @@ -30,6 +30,7 @@ | |||
102 | 30 | 'UnparseableBugData', | 30 | 'UnparseableBugData', |
103 | 31 | 'UnparseableBugTrackerVersion', | 31 | 'UnparseableBugTrackerVersion', |
104 | 32 | 'UnsupportedBugTrackerVersion', | 32 | 'UnsupportedBugTrackerVersion', |
105 | 33 | 'get_external_bugtracker', | ||
106 | 33 | ] | 34 | ] |
107 | 34 | 35 | ||
108 | 35 | from lp.bugs.externalbugtracker.base import * | 36 | from lp.bugs.externalbugtracker.base import * |
109 | @@ -41,6 +42,8 @@ | |||
110 | 41 | from lp.bugs.externalbugtracker.rt import * | 42 | from lp.bugs.externalbugtracker.rt import * |
111 | 42 | from lp.bugs.externalbugtracker.trac import * | 43 | from lp.bugs.externalbugtracker.trac import * |
112 | 43 | from lp.bugs.interfaces.bugtracker import BugTrackerType | 44 | from lp.bugs.interfaces.bugtracker import BugTrackerType |
113 | 45 | |||
114 | 46 | |||
115 | 44 | BUG_TRACKER_CLASSES = { | 47 | BUG_TRACKER_CLASSES = { |
116 | 45 | BugTrackerType.BUGZILLA: Bugzilla, | 48 | BugTrackerType.BUGZILLA: Bugzilla, |
117 | 46 | BugTrackerType.DEBBUGS: DebBugs, | 49 | BugTrackerType.DEBBUGS: DebBugs, |
118 | 47 | 50 | ||
119 | === modified file 'lib/lp/bugs/externalbugtracker/base.py' | |||
120 | --- lib/lp/bugs/externalbugtracker/base.py 2010-03-26 15:12:59 +0000 | |||
121 | +++ lib/lp/bugs/externalbugtracker/base.py 2010-04-09 14:24:25 +0000 | |||
122 | @@ -5,6 +5,7 @@ | |||
123 | 5 | 5 | ||
124 | 6 | __metaclass__ = type | 6 | __metaclass__ = type |
125 | 7 | __all__ = [ | 7 | __all__ = [ |
126 | 8 | 'BATCH_SIZE_UNLIMITED', | ||
127 | 8 | 'BugNotFound', | 9 | 'BugNotFound', |
128 | 9 | 'BugTrackerAuthenticationError', | 10 | 'BugTrackerAuthenticationError', |
129 | 10 | 'BugTrackerConnectError', | 11 | 'BugTrackerConnectError', |
130 | @@ -39,6 +40,9 @@ | |||
131 | 39 | # The user agent we send in our requests | 40 | # The user agent we send in our requests |
132 | 40 | LP_USER_AGENT = "Launchpad Bugscraper/0.2 (https://bugs.launchpad.net/)" | 41 | LP_USER_AGENT = "Launchpad Bugscraper/0.2 (https://bugs.launchpad.net/)" |
133 | 41 | 42 | ||
134 | 43 | # To signify that all bug watches should be checked in a single run. | ||
135 | 44 | BATCH_SIZE_UNLIMITED = 0 | ||
136 | 45 | |||
137 | 42 | 46 | ||
138 | 43 | # | 47 | # |
139 | 44 | # Errors. | 48 | # Errors. |
140 | @@ -133,7 +137,7 @@ | |||
141 | 133 | 137 | ||
142 | 134 | implements(IExternalBugTracker) | 138 | implements(IExternalBugTracker) |
143 | 135 | 139 | ||
145 | 136 | batch_size = 100 | 140 | batch_size = None |
146 | 137 | batch_query_threshold = config.checkwatches.batch_query_threshold | 141 | batch_query_threshold = config.checkwatches.batch_query_threshold |
147 | 138 | comment_template = 'default_remotecomment_template.txt' | 142 | comment_template = 'default_remotecomment_template.txt' |
148 | 139 | 143 | ||
149 | 140 | 144 | ||
150 | === modified file 'lib/lp/bugs/externalbugtracker/debbugs.py' | |||
151 | --- lib/lp/bugs/externalbugtracker/debbugs.py 2010-03-31 20:09:55 +0000 | |||
152 | +++ lib/lp/bugs/externalbugtracker/debbugs.py 2010-04-09 14:24:25 +0000 | |||
153 | @@ -22,18 +22,19 @@ | |||
154 | 22 | 22 | ||
155 | 23 | from canonical.config import config | 23 | from canonical.config import config |
156 | 24 | from canonical.database.sqlbase import commit | 24 | from canonical.database.sqlbase import commit |
157 | 25 | from canonical.launchpad.interfaces.message import IMessageSet | ||
158 | 26 | from canonical.launchpad.mail import simple_sendmail | ||
159 | 27 | from canonical.launchpad.webapp import urlsplit | ||
160 | 28 | |||
161 | 25 | from lp.bugs.externalbugtracker import ( | 29 | from lp.bugs.externalbugtracker import ( |
165 | 26 | BugNotFound, BugTrackerConnectError, ExternalBugTracker, | 30 | BATCH_SIZE_UNLIMITED, BugNotFound, BugTrackerConnectError, |
166 | 27 | InvalidBugId, UnknownRemoteStatusError) | 31 | ExternalBugTracker, InvalidBugId, UnknownRemoteStatusError) |
167 | 28 | from canonical.launchpad.interfaces.message import IMessageSet | 32 | from lp.bugs.externalbugtracker.isolation import ensure_no_transaction |
168 | 29 | from lp.bugs.interfaces.bugtask import BugTaskImportance, BugTaskStatus | 33 | from lp.bugs.interfaces.bugtask import BugTaskImportance, BugTaskStatus |
169 | 30 | from lp.bugs.interfaces.externalbugtracker import ( | 34 | from lp.bugs.interfaces.externalbugtracker import ( |
170 | 31 | ISupportsBugImport, ISupportsCommentImport, ISupportsCommentPushing, | 35 | ISupportsBugImport, ISupportsCommentImport, ISupportsCommentPushing, |
171 | 32 | UNKNOWN_REMOTE_IMPORTANCE) | 36 | UNKNOWN_REMOTE_IMPORTANCE) |
172 | 33 | from canonical.launchpad.mail import simple_sendmail | ||
173 | 34 | from lp.bugs.scripts import debbugs | 37 | from lp.bugs.scripts import debbugs |
174 | 35 | from canonical.launchpad.webapp import urlsplit | ||
175 | 36 | from lp.bugs.externalbugtracker.isolation import ensure_no_transaction | ||
176 | 37 | 38 | ||
177 | 38 | 39 | ||
178 | 39 | debbugsstatusmap = {'open': BugTaskStatus.NEW, | 40 | debbugsstatusmap = {'open': BugTaskStatus.NEW, |
179 | @@ -59,7 +60,7 @@ | |||
180 | 59 | # Because we keep a local copy of debbugs, we remove the batch_size | 60 | # Because we keep a local copy of debbugs, we remove the batch_size |
181 | 60 | # limit so that all debbugs watches that need checking will be | 61 | # limit so that all debbugs watches that need checking will be |
182 | 61 | # checked each time checkwatches runs. | 62 | # checked each time checkwatches runs. |
184 | 62 | batch_size = None | 63 | batch_size = BATCH_SIZE_UNLIMITED |
185 | 63 | 64 | ||
186 | 64 | def __init__(self, baseurl, db_location=None): | 65 | def __init__(self, baseurl, db_location=None): |
187 | 65 | super(DebBugs, self).__init__(baseurl) | 66 | super(DebBugs, self).__init__(baseurl) |
188 | 66 | 67 | ||
189 | === added directory 'lib/lp/bugs/scripts/checkwatches/tests' | |||
190 | === added file 'lib/lp/bugs/scripts/checkwatches/tests/__init__.py' | |||
191 | === added file 'lib/lp/bugs/scripts/checkwatches/tests/test_scheduler.py' | |||
192 | --- lib/lp/bugs/scripts/checkwatches/tests/test_scheduler.py 1970-01-01 00:00:00 +0000 | |||
193 | +++ lib/lp/bugs/scripts/checkwatches/tests/test_scheduler.py 2010-04-09 14:24:25 +0000 | |||
194 | @@ -0,0 +1,110 @@ | |||
195 | 1 | # Copyright 2010 Canonical Ltd. This software is licensed under the | ||
196 | 2 | # GNU Affero General Public License version 3 (see the file LICENSE). | ||
197 | 3 | |||
198 | 4 | """XXX: Module docstring goes here.""" | ||
199 | 5 | |||
200 | 6 | __metaclass__ = type | ||
201 | 7 | |||
202 | 8 | import transaction | ||
203 | 9 | import unittest | ||
204 | 10 | |||
205 | 11 | from datetime import datetime, timedelta | ||
206 | 12 | from pytz import utc | ||
207 | 13 | |||
208 | 14 | from zope.component import getUtility | ||
209 | 15 | |||
210 | 16 | from canonical.launchpad.scripts.logger import QuietFakeLogger | ||
211 | 17 | from canonical.testing import DatabaseFunctionalLayer | ||
212 | 18 | |||
213 | 19 | from lp.bugs.interfaces.bugwatch import ( | ||
214 | 20 | BugWatchActivityStatus, IBugWatchSet) | ||
215 | 21 | from lp.bugs.scripts.checkwatches.scheduler import BugWatchScheduler | ||
216 | 22 | |||
217 | 23 | from lp.testing import TestCaseWithFactory | ||
218 | 24 | |||
219 | 25 | |||
220 | 26 | class TestBugWatchScheduler(TestCaseWithFactory): | ||
221 | 27 | """Tests for the BugWatchScheduler, which runs as part of garbo.""" | ||
222 | 28 | |||
223 | 29 | layer = DatabaseFunctionalLayer | ||
224 | 30 | |||
225 | 31 | def setUp(self): | ||
226 | 32 | super(TestBugWatchScheduler, self).setUp('foo.bar@canonical.com') | ||
227 | 33 | # We'll make sure that all the other bug watches look like | ||
228 | 34 | # they've been scheduled so that only our watch gets scheduled. | ||
229 | 35 | for watch in getUtility(IBugWatchSet).search(): | ||
230 | 36 | watch.next_check = datetime.now(utc) | ||
231 | 37 | self.bug_watch = self.factory.makeBugWatch() | ||
232 | 38 | self.scheduler = BugWatchScheduler(QuietFakeLogger()) | ||
233 | 39 | transaction.commit() | ||
234 | 40 | |||
235 | 41 | def test_scheduler_schedules_unchecked_watches(self): | ||
236 | 42 | # The BugWatchScheduler will schedule a BugWatch that has never | ||
237 | 43 | # been checked to be checked immediately. | ||
238 | 44 | self.bug_watch.next_check = None | ||
239 | 45 | self.scheduler(1) | ||
240 | 46 | |||
241 | 47 | self.assertNotEqual(None, self.bug_watch.next_check) | ||
242 | 48 | self.assertTrue( | ||
243 | 49 | self.bug_watch.next_check <= datetime.now(utc)) | ||
244 | 50 | |||
245 | 51 | def test_scheduler_schedules_working_watches(self): | ||
246 | 52 | # If a watch has been checked and has never failed its next | ||
247 | 53 | # check will be scheduled for 24 hours after its last check. | ||
248 | 54 | now = datetime.now(utc) | ||
249 | 55 | # Add some succesful activity to ensure that successful activity | ||
250 | 56 | # is handled correctly. | ||
251 | 57 | self.bug_watch.addActivity() | ||
252 | 58 | self.bug_watch.lastchecked = now | ||
253 | 59 | self.bug_watch.next_check = None | ||
254 | 60 | transaction.commit() | ||
255 | 61 | self.scheduler(1) | ||
256 | 62 | |||
257 | 63 | self.assertEqual( | ||
258 | 64 | now + timedelta(hours=24), self.bug_watch.next_check) | ||
259 | 65 | |||
260 | 66 | def test_scheduler_schedules_failing_watches(self): | ||
261 | 67 | # If a watch has failed once, it will be scheduled more than 24 | ||
262 | 68 | # hours after its last check. | ||
263 | 69 | now = datetime.now(utc) | ||
264 | 70 | self.bug_watch.lastchecked = now | ||
265 | 71 | |||
266 | 72 | # The delay depends on the number of failures that the watch has | ||
267 | 73 | # had. | ||
268 | 74 | for failure_count in range(1, 6): | ||
269 | 75 | self.bug_watch.next_check = None | ||
270 | 76 | self.bug_watch.addActivity( | ||
271 | 77 | result=BugWatchActivityStatus.BUG_NOT_FOUND) | ||
272 | 78 | transaction.commit() | ||
273 | 79 | self.scheduler(1) | ||
274 | 80 | |||
275 | 81 | coefficient = self.scheduler.delay_coefficient * failure_count | ||
276 | 82 | self.assertEqual( | ||
277 | 83 | now + timedelta(days=1 + coefficient), | ||
278 | 84 | self.bug_watch.next_check) | ||
279 | 85 | |||
280 | 86 | # The scheduler only looks at the last 5 activity items, so even | ||
281 | 87 | # if there have been more failures the maximum delay will be 7 | ||
282 | 88 | # days. | ||
283 | 89 | for count in range(10): | ||
284 | 90 | self.bug_watch.addActivity( | ||
285 | 91 | result=BugWatchActivityStatus.BUG_NOT_FOUND) | ||
286 | 92 | self.bug_watch.next_check = None | ||
287 | 93 | transaction.commit() | ||
288 | 94 | self.scheduler(1) | ||
289 | 95 | self.assertEqual( | ||
290 | 96 | now + timedelta(days=7), self.bug_watch.next_check) | ||
291 | 97 | |||
292 | 98 | def test_scheduler_doesnt_schedule_scheduled_watches(self): | ||
293 | 99 | # The scheduler will ignore watches whose next_check has been | ||
294 | 100 | # set. | ||
295 | 101 | next_check_date = datetime.now(utc) + timedelta(days=1) | ||
296 | 102 | self.bug_watch.next_check = next_check_date | ||
297 | 103 | transaction.commit() | ||
298 | 104 | self.scheduler(1) | ||
299 | 105 | |||
300 | 106 | self.assertEqual(next_check_date, self.bug_watch.next_check) | ||
301 | 107 | |||
302 | 108 | |||
303 | 109 | def test_suite(): | ||
304 | 110 | return unittest.TestLoader().loadTestsFromName(__name__) | ||
305 | 0 | 111 | ||
306 | === renamed file 'lib/lp/bugs/scripts/tests/test_checkwatches.py' => 'lib/lp/bugs/scripts/checkwatches/tests/test_updater.py' | |||
307 | --- lib/lp/bugs/scripts/tests/test_checkwatches.py 2010-03-25 21:59:48 +0000 | |||
308 | +++ lib/lp/bugs/scripts/checkwatches/tests/test_updater.py 2010-04-09 14:24:25 +0000 | |||
309 | @@ -167,6 +167,21 @@ | |||
310 | 167 | # stop SQL statemnet logging. | 167 | # stop SQL statemnet logging. |
311 | 168 | clear_request_started() | 168 | clear_request_started() |
312 | 169 | 169 | ||
313 | 170 | def test_suggest_batch_size(self): | ||
314 | 171 | class RemoteSystem: pass | ||
315 | 172 | remote_system = RemoteSystem() | ||
316 | 173 | # When the batch_size is None, suggest_batch_size() will set | ||
317 | 174 | # it accordingly. | ||
318 | 175 | remote_system.batch_size = None | ||
319 | 176 | checkwatches.updater.suggest_batch_size(remote_system, 1) | ||
320 | 177 | self.failUnlessEqual(100, remote_system.batch_size) | ||
321 | 178 | remote_system.batch_size = None | ||
322 | 179 | checkwatches.updater.suggest_batch_size(remote_system, 12350) | ||
323 | 180 | self.failUnlessEqual(247, remote_system.batch_size) | ||
324 | 181 | # If the batch_size is already set, it will not be changed. | ||
325 | 182 | checkwatches.updater.suggest_batch_size(remote_system, 99999) | ||
326 | 183 | self.failUnlessEqual(247, remote_system.batch_size) | ||
327 | 184 | |||
328 | 170 | 185 | ||
329 | 171 | class TestUpdateBugsWithLinkedQuestions(unittest.TestCase): | 186 | class TestUpdateBugsWithLinkedQuestions(unittest.TestCase): |
330 | 172 | """Tests for updating bugs with linked questions.""" | 187 | """Tests for updating bugs with linked questions.""" |
331 | 173 | 188 | ||
332 | === modified file 'lib/lp/bugs/scripts/checkwatches/updater.py' | |||
333 | --- lib/lp/bugs/scripts/checkwatches/updater.py 2010-03-26 21:03:30 +0000 | |||
334 | +++ lib/lp/bugs/scripts/checkwatches/updater.py 2010-04-09 14:24:25 +0000 | |||
335 | @@ -61,16 +61,15 @@ | |||
336 | 61 | 61 | ||
337 | 62 | from lp.bugs import externalbugtracker | 62 | from lp.bugs import externalbugtracker |
338 | 63 | from lp.bugs.externalbugtracker import ( | 63 | from lp.bugs.externalbugtracker import ( |
345 | 64 | BugNotFound, BugTrackerConnectError, BugWatchUpdateError, | 64 | BATCH_SIZE_UNLIMITED, BugNotFound, BugTrackerConnectError, |
346 | 65 | BugWatchUpdateWarning, InvalidBugId, PrivateRemoteBug, | 65 | BugWatchUpdateError, BugWatchUpdateWarning, InvalidBugId, |
347 | 66 | UnknownBugTrackerTypeError, UnknownRemoteStatusError, UnparseableBugData, | 66 | PrivateRemoteBug, UnknownBugTrackerTypeError, UnknownRemoteStatusError, |
348 | 67 | UnparseableBugTrackerVersion, UnsupportedBugTrackerVersion) | 67 | UnparseableBugData, UnparseableBugTrackerVersion, |
349 | 68 | from lp.bugs.externalbugtracker.bugzilla import ( | 68 | UnsupportedBugTrackerVersion) |
350 | 69 | BugzillaAPI) | 69 | from lp.bugs.externalbugtracker.bugzilla import BugzillaAPI |
351 | 70 | from lp.bugs.externalbugtracker.isolation import check_no_transaction | 70 | from lp.bugs.externalbugtracker.isolation import check_no_transaction |
352 | 71 | from lp.bugs.interfaces.bug import IBugSet | 71 | from lp.bugs.interfaces.bug import IBugSet |
355 | 72 | from lp.bugs.interfaces.externalbugtracker import ( | 72 | from lp.bugs.interfaces.externalbugtracker import ISupportsBackLinking |
354 | 73 | ISupportsBackLinking) | ||
356 | 74 | from lp.services.limitedlist import LimitedList | 73 | from lp.services.limitedlist import LimitedList |
357 | 75 | from lp.services.scripts.base import LaunchpadCronScript | 74 | from lp.services.scripts.base import LaunchpadCronScript |
358 | 76 | 75 | ||
359 | @@ -78,6 +77,11 @@ | |||
360 | 78 | SYNCABLE_GNOME_PRODUCTS = [] | 77 | SYNCABLE_GNOME_PRODUCTS = [] |
361 | 79 | MAX_SQL_STATEMENTS_LOGGED = 10000 | 78 | MAX_SQL_STATEMENTS_LOGGED = 10000 |
362 | 80 | 79 | ||
363 | 80 | # The minimum batch size to suggest to an IExternalBugTracker. | ||
364 | 81 | SUGGESTED_BATCH_SIZE_MIN = 100 | ||
365 | 82 | # The proportion of all watches to suggest as a batch size. | ||
366 | 83 | SUGGESTED_BATCH_SIZE_PROPORTION = 0.02 | ||
367 | 84 | |||
368 | 81 | 85 | ||
369 | 82 | class TooMuchTimeSkew(BugWatchUpdateError): | 86 | class TooMuchTimeSkew(BugWatchUpdateError): |
370 | 83 | """Time difference between ourselves and the remote server is too much.""" | 87 | """Time difference between ourselves and the remote server is too much.""" |
371 | @@ -242,6 +246,22 @@ | |||
372 | 242 | yield item | 246 | yield item |
373 | 243 | 247 | ||
374 | 244 | 248 | ||
375 | 249 | def suggest_batch_size(remote_system, num_watches): | ||
376 | 250 | """Suggest a value for batch_size if it's not set. | ||
377 | 251 | |||
378 | 252 | Given the number of bug watches for a `remote_system`, this sets a | ||
379 | 253 | suggested batch size on it. If `remote_system` already has a batch | ||
380 | 254 | size set, this does not override it. | ||
381 | 255 | |||
382 | 256 | :param remote_system: An `ExternalBugTracker`. | ||
383 | 257 | :param num_watches: The number of watches for `remote_system`. | ||
384 | 258 | """ | ||
385 | 259 | if remote_system.batch_size is None: | ||
386 | 260 | remote_system.batch_size = max( | ||
387 | 261 | SUGGESTED_BATCH_SIZE_MIN, | ||
388 | 262 | int(SUGGESTED_BATCH_SIZE_PROPORTION * num_watches)) | ||
389 | 263 | |||
390 | 264 | |||
391 | 245 | class BugWatchUpdater(object): | 265 | class BugWatchUpdater(object): |
392 | 246 | """Takes responsibility for updating remote bug watches.""" | 266 | """Takes responsibility for updating remote bug watches.""" |
393 | 247 | 267 | ||
394 | @@ -381,7 +401,12 @@ | |||
395 | 381 | `BaseScheduler`. If no scheduler is given, `SerialScheduler` | 401 | `BaseScheduler`. If no scheduler is given, `SerialScheduler` |
396 | 382 | will be used, which simply runs the jobs in order. | 402 | will be used, which simply runs the jobs in order. |
397 | 383 | """ | 403 | """ |
399 | 384 | self.log.debug("Using a global batch size of %s" % batch_size) | 404 | if batch_size is None: |
400 | 405 | self.log.debug("No global batch size specified.") | ||
401 | 406 | elif batch_size == BATCH_SIZE_UNLIMITED: | ||
402 | 407 | self.log.debug("Using an unlimited global batch size.") | ||
403 | 408 | else: | ||
404 | 409 | self.log.debug("Using a global batch size of %s" % batch_size) | ||
405 | 385 | 410 | ||
406 | 386 | # Default to using the very simple SerialScheduler. | 411 | # Default to using the very simple SerialScheduler. |
407 | 387 | if scheduler is None: | 412 | if scheduler is None: |
408 | @@ -497,6 +522,7 @@ | |||
409 | 497 | def _getExternalBugTrackersAndWatches(self, bug_tracker, bug_watches): | 522 | def _getExternalBugTrackersAndWatches(self, bug_tracker, bug_watches): |
410 | 498 | """Return an `ExternalBugTracker` instance for `bug_tracker`.""" | 523 | """Return an `ExternalBugTracker` instance for `bug_tracker`.""" |
411 | 499 | with self.transaction: | 524 | with self.transaction: |
412 | 525 | num_watches = bug_tracker.watches.count() | ||
413 | 500 | remotesystem = ( | 526 | remotesystem = ( |
414 | 501 | externalbugtracker.get_external_bugtracker(bug_tracker)) | 527 | externalbugtracker.get_external_bugtracker(bug_tracker)) |
415 | 502 | # We special-case the Gnome Bugzilla. | 528 | # We special-case the Gnome Bugzilla. |
416 | @@ -506,6 +532,9 @@ | |||
417 | 506 | # Probe the remote system for additional capabilities. | 532 | # Probe the remote system for additional capabilities. |
418 | 507 | remotesystem_to_use = remotesystem.getExternalBugTrackerToUse() | 533 | remotesystem_to_use = remotesystem.getExternalBugTrackerToUse() |
419 | 508 | 534 | ||
420 | 535 | # Try to hint at how many bug watches to check each time. | ||
421 | 536 | suggest_batch_size(remotesystem_to_use, num_watches) | ||
422 | 537 | |||
423 | 509 | if (is_gnome_bugzilla and | 538 | if (is_gnome_bugzilla and |
424 | 510 | isinstance(remotesystem_to_use, BugzillaAPI) and | 539 | isinstance(remotesystem_to_use, BugzillaAPI) and |
425 | 511 | len(self._syncable_gnome_products) > 0): | 540 | len(self._syncable_gnome_products) > 0): |
426 | @@ -656,11 +685,6 @@ | |||
427 | 656 | # by the ExternalBugTracker. | 685 | # by the ExternalBugTracker. |
428 | 657 | batch_size = remotesystem.batch_size | 686 | batch_size = remotesystem.batch_size |
429 | 658 | 687 | ||
430 | 659 | if batch_size == 0: | ||
431 | 660 | # A batch_size of 0 means that there's no batch size limit | ||
432 | 661 | # for this bug tracker. | ||
433 | 662 | batch_size = None | ||
434 | 663 | |||
435 | 664 | with self.transaction: | 688 | with self.transaction: |
436 | 665 | old_bug_watches = set( | 689 | old_bug_watches = set( |
437 | 666 | bug_watch for bug_watch in bug_watches | 690 | bug_watch for bug_watch in bug_watches |
438 | @@ -693,7 +717,7 @@ | |||
439 | 693 | # are actually some bugs that we're interested in so as to | 717 | # are actually some bugs that we're interested in so as to |
440 | 694 | # avoid unnecessary network traffic. | 718 | # avoid unnecessary network traffic. |
441 | 695 | if server_time is not None and len(remote_old_ids) > 0: | 719 | if server_time is not None and len(remote_old_ids) > 0: |
443 | 696 | if batch_size is None: | 720 | if batch_size == BATCH_SIZE_UNLIMITED: |
444 | 697 | remote_old_ids_to_check = ( | 721 | remote_old_ids_to_check = ( |
445 | 698 | remotesystem.getModifiedRemoteBugs( | 722 | remotesystem.getModifiedRemoteBugs( |
446 | 699 | remote_old_ids, oldest_lastchecked)) | 723 | remote_old_ids, oldest_lastchecked)) |
447 | @@ -721,7 +745,7 @@ | |||
448 | 721 | remote_ids_to_check = chain( | 745 | remote_ids_to_check = chain( |
449 | 722 | remote_ids_with_comments, remote_new_ids, remote_old_ids_to_check) | 746 | remote_ids_with_comments, remote_new_ids, remote_old_ids_to_check) |
450 | 723 | 747 | ||
452 | 724 | if batch_size is not None: | 748 | if batch_size != BATCH_SIZE_UNLIMITED: |
453 | 725 | # Some remote bug IDs may appear in more than one list so | 749 | # Some remote bug IDs may appear in more than one list so |
454 | 726 | # we must filter the list before slicing. | 750 | # we must filter the list before slicing. |
455 | 727 | remote_ids_to_check = islice( | 751 | remote_ids_to_check = islice( |
456 | 728 | 752 | ||
457 | === modified file 'lib/lp/bugs/tests/externalbugtracker.py' | |||
458 | --- lib/lp/bugs/tests/externalbugtracker.py 2009-12-23 12:14:59 +0000 | |||
459 | +++ lib/lp/bugs/tests/externalbugtracker.py 2010-04-09 14:24:25 +0000 | |||
460 | @@ -24,8 +24,8 @@ | |||
461 | 24 | from canonical.config import config | 24 | from canonical.config import config |
462 | 25 | from canonical.database.sqlbase import commit, ZopelessTransactionManager | 25 | from canonical.database.sqlbase import commit, ZopelessTransactionManager |
463 | 26 | from lp.bugs.externalbugtracker import ( | 26 | from lp.bugs.externalbugtracker import ( |
466 | 27 | BugNotFound, BugTrackerConnectError, Bugzilla, DebBugs, | 27 | BATCH_SIZE_UNLIMITED, BugNotFound, BugTrackerConnectError, Bugzilla, |
467 | 28 | ExternalBugTracker, Mantis, RequestTracker, Roundup, SourceForge, | 28 | DebBugs, ExternalBugTracker, Mantis, RequestTracker, Roundup, SourceForge, |
468 | 29 | Trac) | 29 | Trac) |
469 | 30 | from lp.bugs.externalbugtracker.trac import ( | 30 | from lp.bugs.externalbugtracker.trac import ( |
470 | 31 | FAULT_TICKET_NOT_FOUND, LP_PLUGIN_BUG_IDS_ONLY, LP_PLUGIN_FULL, | 31 | FAULT_TICKET_NOT_FOUND, LP_PLUGIN_BUG_IDS_ONLY, LP_PLUGIN_FULL, |
471 | @@ -152,6 +152,8 @@ | |||
472 | 152 | implementation, though it doesn't actually do anything. | 152 | implementation, though it doesn't actually do anything. |
473 | 153 | """ | 153 | """ |
474 | 154 | 154 | ||
475 | 155 | batch_size = BATCH_SIZE_UNLIMITED | ||
476 | 156 | |||
477 | 155 | def __init__(self, baseurl='http://example.com/'): | 157 | def __init__(self, baseurl='http://example.com/'): |
478 | 156 | super(TestExternalBugTracker, self).__init__(baseurl) | 158 | super(TestExternalBugTracker, self).__init__(baseurl) |
479 | 157 | 159 | ||
480 | 158 | 160 | ||
481 | === modified file 'lib/lp/bugs/tests/test_bugwatch.py' | |||
482 | --- lib/lp/bugs/tests/test_bugwatch.py 2010-03-26 14:49:24 +0000 | |||
483 | +++ lib/lp/bugs/tests/test_bugwatch.py 2010-04-09 14:24:25 +0000 | |||
484 | @@ -8,27 +8,21 @@ | |||
485 | 8 | import transaction | 8 | import transaction |
486 | 9 | import unittest | 9 | import unittest |
487 | 10 | 10 | ||
488 | 11 | from datetime import datetime, timedelta | ||
489 | 12 | from pytz import utc | ||
490 | 13 | |||
491 | 14 | from urlparse import urlunsplit | 11 | from urlparse import urlunsplit |
492 | 15 | 12 | ||
493 | 16 | from zope.component import getUtility | 13 | from zope.component import getUtility |
494 | 17 | 14 | ||
495 | 18 | from canonical.launchpad.ftests import login, ANONYMOUS | 15 | from canonical.launchpad.ftests import login, ANONYMOUS |
496 | 16 | from canonical.launchpad.scripts.garbo import BugWatchActivityPruner | ||
497 | 19 | from canonical.launchpad.scripts.logger import QuietFakeLogger | 17 | from canonical.launchpad.scripts.logger import QuietFakeLogger |
498 | 20 | from canonical.launchpad.webapp import urlsplit | 18 | from canonical.launchpad.webapp import urlsplit |
499 | 21 | from canonical.launchpad.scripts.garbo import BugWatchActivityPruner | ||
500 | 22 | from canonical.launchpad.scripts.logger import QuietFakeLogger | ||
501 | 23 | from canonical.testing import ( | 19 | from canonical.testing import ( |
502 | 24 | DatabaseFunctionalLayer, LaunchpadFunctionalLayer, LaunchpadZopelessLayer) | 20 | DatabaseFunctionalLayer, LaunchpadFunctionalLayer, LaunchpadZopelessLayer) |
503 | 25 | 21 | ||
504 | 26 | from lp.bugs.interfaces.bugtracker import BugTrackerType, IBugTrackerSet | 22 | from lp.bugs.interfaces.bugtracker import BugTrackerType, IBugTrackerSet |
505 | 27 | from lp.bugs.interfaces.bugwatch import ( | 23 | from lp.bugs.interfaces.bugwatch import ( |
510 | 28 | BugWatchActivityStatus, IBugWatchSet, NoBugTrackerFound, | 24 | IBugWatchSet, NoBugTrackerFound, UnrecognizedBugTrackerURL) |
511 | 29 | UnrecognizedBugTrackerURL) | 25 | from lp.bugs.scripts.checkwatches.scheduler import MAX_SAMPLE_SIZE |
508 | 30 | from lp.bugs.scripts.checkwatches.scheduler import ( | ||
509 | 31 | BugWatchScheduler, MAX_SAMPLE_SIZE) | ||
512 | 32 | from lp.registry.interfaces.person import IPersonSet | 26 | from lp.registry.interfaces.person import IPersonSet |
513 | 33 | 27 | ||
514 | 34 | from lp.testing import TestCaseWithFactory | 28 | from lp.testing import TestCaseWithFactory |
515 | @@ -467,85 +461,5 @@ | |||
516 | 467 | self.failUnless("Activity %s" % i in messages) | 461 | self.failUnless("Activity %s" % i in messages) |
517 | 468 | 462 | ||
518 | 469 | 463 | ||
519 | 470 | class TestBugWatchScheduler(TestCaseWithFactory): | ||
520 | 471 | """Tests for the BugWatchScheduler, which runs as part of garbo.""" | ||
521 | 472 | |||
522 | 473 | layer = DatabaseFunctionalLayer | ||
523 | 474 | |||
524 | 475 | def setUp(self): | ||
525 | 476 | super(TestBugWatchScheduler, self).setUp('foo.bar@canonical.com') | ||
526 | 477 | # We'll make sure that all the other bug watches look like | ||
527 | 478 | # they've been scheduled so that only our watch gets scheduled. | ||
528 | 479 | for watch in getUtility(IBugWatchSet).search(): | ||
529 | 480 | watch.next_check = datetime.now(utc) | ||
530 | 481 | self.bug_watch = self.factory.makeBugWatch() | ||
531 | 482 | self.scheduler = BugWatchScheduler(QuietFakeLogger()) | ||
532 | 483 | transaction.commit() | ||
533 | 484 | |||
534 | 485 | def test_scheduler_schedules_unchecked_watches(self): | ||
535 | 486 | # The BugWatchScheduler will schedule a BugWatch that has never | ||
536 | 487 | # been checked to be checked immediately. | ||
537 | 488 | self.bug_watch.next_check = None | ||
538 | 489 | self.scheduler(1) | ||
539 | 490 | |||
540 | 491 | self.assertNotEqual(None, self.bug_watch.next_check) | ||
541 | 492 | self.assertTrue( | ||
542 | 493 | self.bug_watch.next_check <= datetime.now(utc)) | ||
543 | 494 | |||
544 | 495 | def test_scheduler_schedules_working_watches(self): | ||
545 | 496 | # If a watch has been checked and has never failed its next | ||
546 | 497 | # check will be scheduled for 24 hours after its last check. | ||
547 | 498 | now = datetime.now(utc) | ||
548 | 499 | self.bug_watch.lastchecked = now | ||
549 | 500 | self.bug_watch.next_check = None | ||
550 | 501 | transaction.commit() | ||
551 | 502 | self.scheduler(1) | ||
552 | 503 | |||
553 | 504 | self.assertEqual( | ||
554 | 505 | now + timedelta(hours=24), self.bug_watch.next_check) | ||
555 | 506 | |||
556 | 507 | def test_scheduler_schedules_failing_watches(self): | ||
557 | 508 | # If a watch has failed once, it will be scheduled more than 24 | ||
558 | 509 | # hours after its last check. | ||
559 | 510 | now = datetime.now(utc) | ||
560 | 511 | self.bug_watch.lastchecked = now | ||
561 | 512 | |||
562 | 513 | # The delay depends on the number of failures that the watch has | ||
563 | 514 | # had. | ||
564 | 515 | for failure_count in range(1, 6): | ||
565 | 516 | self.bug_watch.next_check = None | ||
566 | 517 | self.bug_watch.addActivity( | ||
567 | 518 | result=BugWatchActivityStatus.BUG_NOT_FOUND) | ||
568 | 519 | transaction.commit() | ||
569 | 520 | self.scheduler(1) | ||
570 | 521 | |||
571 | 522 | coefficient = self.scheduler.delay_coefficient * failure_count | ||
572 | 523 | self.assertEqual( | ||
573 | 524 | now + timedelta(days=1 + coefficient), | ||
574 | 525 | self.bug_watch.next_check) | ||
575 | 526 | |||
576 | 527 | # The scheduler only looks at the last 5 activity items, so even | ||
577 | 528 | # if there have been more failures the maximum delay will be 7 | ||
578 | 529 | # days. | ||
579 | 530 | for count in range(10): | ||
580 | 531 | self.bug_watch.addActivity( | ||
581 | 532 | result=BugWatchActivityStatus.BUG_NOT_FOUND) | ||
582 | 533 | self.bug_watch.next_check = None | ||
583 | 534 | transaction.commit() | ||
584 | 535 | self.scheduler(1) | ||
585 | 536 | self.assertEqual( | ||
586 | 537 | now + timedelta(days=7), self.bug_watch.next_check) | ||
587 | 538 | |||
588 | 539 | def test_scheduler_doesnt_schedule_scheduled_watches(self): | ||
589 | 540 | # The scheduler will ignore watches whose next_check has been | ||
590 | 541 | # set. | ||
591 | 542 | next_check_date = datetime.now(utc) + timedelta(days=1) | ||
592 | 543 | self.bug_watch.next_check = next_check_date | ||
593 | 544 | transaction.commit() | ||
594 | 545 | self.scheduler(1) | ||
595 | 546 | |||
596 | 547 | self.assertEqual(next_check_date, self.bug_watch.next_check) | ||
597 | 548 | |||
598 | 549 | |||
599 | 550 | def test_suite(): | 464 | def test_suite(): |
600 | 551 | return unittest.TestLoader().loadTestsFromName(__name__) | 465 | return unittest.TestLoader().loadTestsFromName(__name__) |