~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/python
#
# Copyright 2009-2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Import version control metadata from a Bazaar branch into the database."""

__metaclass__ = type

__all__ = [
    "BzrSync",
    'schedule_diff_updates',
    'schedule_translation_templates_build',
    'schedule_translation_upload',
    ]

import logging

from bzrlib.graph import DictParentsProvider
from bzrlib.revision import NULL_REVISION
import pytz
import transaction
from storm.locals import Store
from zope.component import getUtility
from zope.event import notify

from canonical.config import config

from lp.code.interfaces.branchjob import IRosettaUploadJobSource
from lp.code.interfaces.revision import IRevisionSet
from lp.code.model.branchrevision import (BranchRevision)
from lp.code.model.revision import Revision
from lp.codehosting.scanner import events
from lp.services.utils import iter_list_chunks
from lp.translations.interfaces.translationtemplatesbuildjob import (
    ITranslationTemplatesBuildJobSource,
    )


UTC = pytz.timezone('UTC')


class BzrSync:
    """Import version control metadata from a Bazaar branch into the database.
    """

    def __init__(self, branch, logger=None):
        self.db_branch = branch
        if logger is None:
            logger = logging.getLogger(self.__class__.__name__)
        self.logger = logger

    def syncBranchAndClose(self, bzr_branch=None):
        """Synchronize the database with a Bazaar branch, handling locking.
        """
        if bzr_branch is None:
            bzr_branch = self.db_branch.getBzrBranch()
        bzr_branch.lock_read()
        try:
            self.syncBranch(bzr_branch)
        finally:
            bzr_branch.unlock()

    def syncBranch(self, bzr_branch):
        """Synchronize the database view of a branch with Bazaar data.

        `bzr_branch` must be read locked.

        Several tables must be updated:

        * Revision: there must be one Revision row for each revision in the
          branch ancestry. If the row for a revision that has just been added
          to the branch is already present, it must be checked for
          consistency.

        * BranchRevision: there must be one BrancheRevision row for each
          revision in the branch ancestry. If history revisions became merged
          revisions, the corresponding rows must be changed.

        * Branch: the branch-scanner status information must be updated when
          the sync is complete.
        """
        self.logger.info("Scanning branch: %s", self.db_branch.unique_name)
        self.logger.info("    from %s", bzr_branch.base)
        # Get the history and ancestry from the branch first, to fail early
        # if something is wrong with the branch.
        self.logger.info("Retrieving history from bzrlib.")
        last_revision_info = bzr_branch.last_revision_info()
        # The BranchRevision, Revision and RevisionParent tables are only
        # written to by the branch-scanner, so they are not subject to
        # write-lock contention. Update them all in a single transaction to
        # improve the performance and allow garbage collection in the future.
        db_ancestry, db_history = self.retrieveDatabaseAncestry()

        (new_ancestry, branchrevisions_to_delete,
            revids_to_insert) = self.planDatabaseChanges(
            bzr_branch, last_revision_info, db_ancestry, db_history)
        new_db_revs = (
            new_ancestry - getUtility(IRevisionSet).onlyPresent(new_ancestry))
        self.logger.info("Adding %s new revisions.", len(new_db_revs))
        for revids in iter_list_chunks(list(new_db_revs), 1000):
            revisions = self.getBazaarRevisions(bzr_branch, revids)
            for revision in revisions:
                # This would probably go much faster if we found some way to
                # bulk-load multiple revisions at once, but as this is only
                # executed for revisions new to Launchpad, it doesn't seem
                # worth it at this stage.
                self.syncOneRevision(
                    bzr_branch, revision, revids_to_insert)
        self.deleteBranchRevisions(branchrevisions_to_delete)
        self.insertBranchRevisions(bzr_branch, revids_to_insert)
        transaction.commit()
        # Synchronize the RevisionCache for this branch.
        getUtility(IRevisionSet).updateRevisionCacheForBranch(self.db_branch)
        transaction.commit()

        # Notify any listeners that the tip of the branch has changed, but
        # before we've actually updated the database branch.
        initial_scan = (len(db_history) == 0)
        notify(events.TipChanged(self.db_branch, bzr_branch, initial_scan))

        # The Branch table is modified by other systems, including the web UI,
        # so we need to update it in a short transaction to avoid causing
        # timeouts in the webapp. This opens a small race window where the
        # revision data is updated in the database, but the Branch table has
        # not been updated. Since this has no ill-effect, and can only err on
        # the pessimistic side (tell the user the data has not yet been
        # updated although it has), the race is acceptable.
        self.updateBranchStatus(last_revision_info)
        notify(
            events.ScanCompleted(
                self.db_branch, bzr_branch, self.logger, new_ancestry))
        transaction.commit()

    def retrieveDatabaseAncestry(self):
        """Efficiently retrieve ancestry from the database."""
        self.logger.info("Retrieving ancestry from database.")
        db_ancestry, db_history = self.db_branch.getScannerData()
        return db_ancestry, db_history

    def _getRevisionGraph(self, bzr_branch, db_last):
        if bzr_branch.repository.has_revision(db_last):
            return bzr_branch.repository.get_graph()
        revisions = Store.of(self.db_branch).find(Revision,
                BranchRevision.branch_id == self.db_branch.id,
                Revision.id == BranchRevision.revision_id)
        parent_map = dict(
            (r.revision_id, r.parent_ids) for r in revisions)
        parents_provider = DictParentsProvider(parent_map)

        class PPSource:

            @staticmethod
            def _make_parents_provider():
                return parents_provider

        return bzr_branch.repository.get_graph(PPSource)

    def getAncestryDelta(self, bzr_branch, bzr_last_revinfo):
        bzr_last = bzr_last_revinfo[1]
        db_last = self.db_branch.last_scanned_id
        if db_last is None:
            db_last = NULL_REVISION
        graph = self._getRevisionGraph(bzr_branch, db_last)
        bzr_branch.lock_read()
        try:
            added_ancestry, removed_ancestry = (
                graph.find_difference(bzr_last, db_last))
        finally:
            bzr_branch.unlock()
        added_ancestry.discard(NULL_REVISION)
        return added_ancestry, removed_ancestry

    def getHistoryDelta(self, bzr_branch, bzr_last_revinfo, db_history):
        self.logger.info("Calculating history delta.")
        common_len = min(bzr_last_revinfo[0], len(db_history))
        common_revid = NULL_REVISION
        while common_len > 0:
            if db_history[common_len - 1] == bzr_branch.get_rev_id(common_len - 1):
                common_revid = db_history[common_len - 1]
                break
            common_len -= 1
        # Revision added or removed from the branch's history. These lists may
        # include revisions whose history position has merely changed.
        removed_history = db_history[common_len:]
        bzr_graph = bzr_branch.repository.get_graph()
        added_history = list(bzr_graph.iter_lefthand_ancestry(bzr_last_revinfo[1],
            (common_revid, )))
        added_history.reverse()
        return added_history, removed_history

    def planDatabaseChanges(self, bzr_branch, bzr_last_revinfo, db_ancestry,
                            db_history):
        """Plan database changes to synchronize with bzrlib data.

        Use the data retrieved by `retrieveDatabaseAncestry` and
        `retrieveBranchDetails` to plan the changes to apply to the database.
        """
        self.logger.info("Planning changes.")
        # Find the length of the common history.
        added_history, removed_history = self.getHistoryDelta(
            bzr_branch, bzr_last_revinfo, db_history)
        added_ancestry, removed_ancestry = self.getAncestryDelta(
            bzr_branch, bzr_last_revinfo)

        notify(
            events.RevisionsRemoved(
                self.db_branch, bzr_branch, removed_history))

        # We must delete BranchRevision rows for all revisions which where
        # removed from the ancestry or whose sequence value has changed.
        branchrevisions_to_delete = set(removed_history)
        branchrevisions_to_delete.update(removed_ancestry)
        branchrevisions_to_delete.update(
            set(added_history).difference(added_ancestry))

        # We must insert BranchRevision rows for all revisions which were
        # added to the ancestry or whose sequence value has changed.
        revids_to_insert = dict(
            self.revisionsToInsert(
                added_history, bzr_last_revinfo[0], added_ancestry))
        # We must remove any stray BranchRevisions that happen to already be
        # present.
        existing_branchrevisions = Store.of(self.db_branch).find(
            Revision.revision_id, BranchRevision.branch == self.db_branch,
            BranchRevision.revision_id == Revision.id,
            Revision.revision_id.is_in(revids_to_insert))
        branchrevisions_to_delete.update(existing_branchrevisions)

        return (added_ancestry, list(branchrevisions_to_delete),
                revids_to_insert)

    def getBazaarRevisions(self, bzr_branch, revisions):
        """Like ``get_revisions(revisions)`` but filter out ghosts first.

        :param revisions: the set of Bazaar revision IDs to return bzrlib
            Revision objects for.
        """
        revisions = bzr_branch.repository.get_parent_map(revisions)
        return bzr_branch.repository.get_revisions(revisions.keys())

    def syncOneRevision(self, bzr_branch, bzr_revision, revids_to_insert):
        """Import the revision with the given revision_id.

        :param bzr_branch: The Bazaar branch that's being scanned.
        :param bzr_revision: the revision to import
        :type bzr_revision: bzrlib.revision.Revision
        :param revids_to_insert: a dict of revision ids to integer
            revno. Non-mainline revisions will be mapped to None.
        """
        revision_id = bzr_revision.revision_id
        revision_set = getUtility(IRevisionSet)
        # Revision not yet in the database. Load it.
        self.logger.debug("Inserting revision: %s", revision_id)
        db_revision = revision_set.newFromBazaarRevision(bzr_revision)
        notify(
            events.NewRevision(
                self.db_branch, bzr_branch, db_revision, bzr_revision,
                revids_to_insert[revision_id]))

    @staticmethod
    def revisionsToInsert(added_history, last_revno, added_ancestry):
        """Calculate the revisions to insert and their revnos.

        :param added_history: A list of revision ids added to the revision
            history in parent-to-child order.
        :param last_revno: The revno of the last revision.
        :param added_ancestry: A set of revisions that have been added to the
            ancestry of the branch.  May overlap with added_history.
        """
        start_revno = last_revno - len(added_history) + 1
        for (revno, revision_id) in enumerate(added_history, start_revno):
            yield revision_id, revno
        for revision_id in added_ancestry.difference(added_history):
            yield revision_id, None

    def deleteBranchRevisions(self, revision_ids_to_delete):
        """Delete a batch of BranchRevision rows."""
        self.logger.info("Deleting %d branchrevision records.",
            len(revision_ids_to_delete))
        # Use a config value to work out how many to delete at a time.
        # Deleting more than one at a time is significantly more efficient
        # than doing one at a time, but the actual optimal count is a bit up
        # in the air.
        batch_size = config.branchscanner.branch_revision_delete_count
        while revision_ids_to_delete:
            batch = revision_ids_to_delete[:batch_size]
            revision_ids_to_delete[:batch_size] = []
            self.db_branch.removeBranchRevisions(batch)

    def insertBranchRevisions(self, bzr_branch, revids_to_insert):
        """Insert a batch of BranchRevision rows."""
        self.logger.info("Inserting %d branchrevision records.",
            len(revids_to_insert))
        revid_seq_pairs = revids_to_insert.items()
        for revid_seq_pair_chunk in iter_list_chunks(revid_seq_pairs, 1000):
            self.db_branch.createBranchRevisionFromIDs(revid_seq_pair_chunk)

    def updateBranchStatus(self, (revision_count, last_revision)):
        """Update the branch-scanner status in the database Branch table."""
        # Record that the branch has been updated.
        if revision_count > 0:
            revision = getUtility(IRevisionSet).getByRevisionId(last_revision)
        else:
            revision = None
        self.logger.info(
            "Updating branch scanner status: %s revs", revision_count)
        self.db_branch.updateScannedDetails(revision, revision_count)


def schedule_translation_upload(tip_changed):
    getUtility(IRosettaUploadJobSource).create(
        tip_changed.db_branch, tip_changed.old_tip_revision_id)


def schedule_translation_templates_build(tip_changed):
    utility = getUtility(ITranslationTemplatesBuildJobSource)
    utility.scheduleTranslationTemplatesBuild(tip_changed.db_branch)


def schedule_diff_updates(tip_changed):
    tip_changed.db_branch.scheduleDiffUpdates()


def update_recipes(tip_changed):
    for recipe in tip_changed.db_branch.recipes:
        recipe.is_stale = True