1
# (C) 2005, 2006 Canonical Limited.
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Reconcilers are able to fix some potential data errors in a branch."""
20
__all__ = ['reconcile', 'Reconciler', 'RepoReconciler', 'KnitReconciler']
24
import bzrlib.errors as errors
25
import bzrlib.progress
26
from bzrlib.trace import mutter
27
from bzrlib.tsort import TopoSorter
28
import bzrlib.ui as ui
31
def reconcile(dir, other=None):
32
"""Reconcile the data in dir.
34
Currently this is limited to a inventory 'reweave'.
36
This is a convenience method, for using a Reconciler object.
38
Directly using Reconciler is recommended for library users that
39
desire fine grained control or analysis of the found issues.
41
:param other: another bzrdir to reconcile against.
43
reconciler = Reconciler(dir, other=other)
44
reconciler.reconcile()
47
class Reconciler(object):
48
"""Reconcilers are used to reconcile existing data."""
50
def __init__(self, dir, other=None):
51
"""Create a Reconciler."""
55
"""Perform reconciliation.
57
After reconciliation the following attributes document found issues:
58
inconsistent_parents: The number of revisions in the repository whose
59
ancestry was being reported incorrectly.
60
garbage_inventories: The number of inventory objects without revisions
61
that were garbage collected.
63
self.pb = ui.ui_factory.nested_progress_bar()
70
"""Helper function for performing reconciliation."""
71
self.repo = self.bzrdir.find_repository()
72
self.pb.note('Reconciling repository %s',
73
self.repo.bzrdir.root_transport.base)
74
repo_reconciler = self.repo.reconcile(thorough=True)
75
self.inconsistent_parents = repo_reconciler.inconsistent_parents
76
self.garbage_inventories = repo_reconciler.garbage_inventories
77
self.pb.note('Reconciliation complete.')
80
class RepoReconciler(object):
81
"""Reconciler that reconciles a repository.
83
Currently this consists of an inventory reweave with revision cross-checks.
86
def __init__(self, repo, other=None, thorough=False):
87
"""Construct a RepoReconciler.
89
:param thorough: perform a thorough check which may take longer but
90
will correct non-data loss issues such as incorrect
93
self.garbage_inventories = 0
94
self.inconsistent_parents = 0
96
self.thorough = thorough
99
"""Perform reconciliation.
101
After reconciliation the following attributes document found issues:
102
inconsistent_parents: The number of revisions in the repository whose
103
ancestry was being reported incorrectly.
104
garbage_inventories: The number of inventory objects without revisions
105
that were garbage collected.
107
self.repo.lock_write()
109
self.pb = ui.ui_factory.nested_progress_bar()
111
self._reconcile_steps()
117
def _reconcile_steps(self):
118
"""Perform the steps to reconcile this repository."""
120
self._reweave_inventory()
122
def _reweave_inventory(self):
123
"""Regenerate the inventory weave for the repository from scratch."""
124
# local because its really a wart we want to hide
125
from bzrlib.weave import WeaveFile, Weave
126
transaction = self.repo.get_transaction()
127
self.pb.update('Reading inventory data.')
128
self.inventory = self.repo.get_inventory_weave()
129
# the total set of revisions to process
130
self.pending = set([rev_id for rev_id in self.repo._revision_store.all_revision_ids(transaction)])
132
# mapping from revision_id to parents
134
# errors that we detect
135
self.inconsistent_parents = 0
136
# we need the revision id of each revision and its available parents list
137
self._setup_steps(len(self.pending))
138
for rev_id in self.pending:
139
# put a revision into the graph.
140
self._graph_revision(rev_id)
141
self._check_garbage_inventories()
142
if not self.inconsistent_parents and not self.garbage_inventories:
143
self.pb.note('Inventory ok.')
145
self.pb.update('Backing up inventory...', 0, 0)
146
self.repo.control_weaves.copy(self.inventory, 'inventory.backup', self.repo.get_transaction())
147
self.pb.note('Backup Inventory created.')
148
# asking for '' should never return a non-empty weave
149
new_inventory_vf = self.repo.control_weaves.get_empty('inventory.new',
150
self.repo.get_transaction())
152
# we have topological order of revisions and non ghost parents ready.
153
self._setup_steps(len(self._rev_graph))
154
for rev_id in TopoSorter(self._rev_graph.items()).iter_topo_order():
155
parents = self._rev_graph[rev_id]
156
# double check this really is in topological order.
157
unavailable = [p for p in parents if p not in new_inventory_vf]
158
assert len(unavailable) == 0
159
# this entry has all the non ghost parents in the inventory
161
self._reweave_step('adding inventories')
162
if isinstance(new_inventory_vf, WeaveFile):
163
# It's really a WeaveFile, but we call straight into the
164
# Weave's add method to disable the auto-write-out behaviour.
165
# This is done to avoid a revision_count * time-to-write additional overhead on
167
new_inventory_vf._check_write_ok()
168
Weave._add_lines(new_inventory_vf, rev_id, parents, self.inventory.get_lines(rev_id),
171
new_inventory_vf.add_lines(rev_id, parents, self.inventory.get_lines(rev_id))
173
if isinstance(new_inventory_vf, WeaveFile):
174
new_inventory_vf._save()
175
# if this worked, the set of new_inventory_vf.names should equal
177
assert set(new_inventory_vf.versions()) == self.pending
178
self.pb.update('Writing weave')
179
self.repo.control_weaves.copy(new_inventory_vf, 'inventory', self.repo.get_transaction())
180
self.repo.control_weaves.delete('inventory.new', self.repo.get_transaction())
181
self.inventory = None
182
self.pb.note('Inventory regenerated.')
184
def _setup_steps(self, new_total):
185
"""Setup the markers we need to control the progress bar."""
186
self.total = new_total
189
def _graph_revision(self, rev_id):
190
"""Load a revision into the revision graph."""
191
# pick a random revision
192
# analyse revision id rev_id and put it in the stack.
193
self._reweave_step('loading revisions')
194
rev = self.repo.get_revision_reconcile(rev_id)
195
assert rev.revision_id == rev_id
197
for parent in rev.parent_ids:
198
if self._parent_is_available(parent):
199
parents.append(parent)
201
mutter('found ghost %s', parent)
202
self._rev_graph[rev_id] = parents
203
if set(self.inventory.get_parents(rev_id)) != set(parents):
204
self.inconsistent_parents += 1
205
mutter('Inconsistent inventory parents: id {%s} '
206
'inventory claims %r, '
207
'available parents are %r, '
208
'unavailable parents are %r',
210
set(self.inventory.get_parents(rev_id)),
212
set(rev.parent_ids).difference(set(parents)))
214
def _check_garbage_inventories(self):
215
"""Check for garbage inventories which we cannot trust
217
We cant trust them because their pre-requisite file data may not
218
be present - all we know is that their revision was not installed.
220
inventories = set(self.inventory.versions())
221
revisions = set(self._rev_graph.keys())
222
garbage = inventories.difference(revisions)
223
self.garbage_inventories = len(garbage)
224
for revision_id in garbage:
225
mutter('Garbage inventory {%s} found.', revision_id)
227
def _parent_is_available(self, parent):
228
"""True if parent is a fully available revision
230
A fully available revision has a inventory and a revision object in the
233
return (parent in self._rev_graph or
234
(parent in self.inventory and self.repo.has_revision(parent)))
236
def _reweave_step(self, message):
237
"""Mark a single step of regeneration complete."""
238
self.pb.update(message, self.count, self.total)
242
class KnitReconciler(RepoReconciler):
243
"""Reconciler that reconciles a knit format repository.
245
This will detect garbage inventories and remove them.
247
Inconsistent parentage is checked for in the revision weave.
250
def _reconcile_steps(self):
251
"""Perform the steps to reconcile this repository."""
254
# knits never suffer this
257
def _load_indexes(self):
258
"""Load indexes for the reconciliation."""
259
self.transaction = self.repo.get_transaction()
260
self.pb.update('Reading indexes.', 0, 2)
261
self.inventory = self.repo.get_inventory_weave()
262
self.pb.update('Reading indexes.', 1, 2)
263
self.revisions = self.repo._revision_store.get_revision_file(self.transaction)
264
self.pb.update('Reading indexes.', 2, 2)
266
def _gc_inventory(self):
267
"""Remove inventories that are not referenced from the revision store."""
268
self.pb.update('Checking unused inventories.', 0, 1)
269
self._check_garbage_inventories()
270
self.pb.update('Checking unused inventories.', 1, 3)
271
if not self.garbage_inventories:
272
self.pb.note('Inventory ok.')
274
self.pb.update('Backing up inventory...', 0, 0)
275
self.repo.control_weaves.copy(self.inventory, 'inventory.backup', self.transaction)
276
self.pb.note('Backup Inventory created.')
277
# asking for '' should never return a non-empty weave
278
new_inventory_vf = self.repo.control_weaves.get_empty('inventory.new',
281
# we have topological order of revisions and non ghost parents ready.
282
self._setup_steps(len(self.revisions))
283
for rev_id in TopoSorter(self.revisions.get_graph().items()).iter_topo_order():
284
parents = self.revisions.get_parents(rev_id)
285
# double check this really is in topological order.
286
unavailable = [p for p in parents if p not in new_inventory_vf]
287
assert len(unavailable) == 0
288
# this entry has all the non ghost parents in the inventory
290
self._reweave_step('adding inventories')
291
# ugly but needed, weaves are just way tooooo slow else.
292
new_inventory_vf.add_lines(rev_id, parents, self.inventory.get_lines(rev_id))
294
# if this worked, the set of new_inventory_vf.names should equal
296
assert set(new_inventory_vf.versions()) == set(self.revisions.versions())
297
self.pb.update('Writing weave')
298
self.repo.control_weaves.copy(new_inventory_vf, 'inventory', self.transaction)
299
self.repo.control_weaves.delete('inventory.new', self.transaction)
300
self.inventory = None
301
self.pb.note('Inventory regenerated.')
303
def _graph_revision(self, rev_id):
304
"""Load a revision into the revision graph."""
305
# pick a random revision
306
# analyse revision id rev_id and put it in the stack.
307
self._reweave_step('loading revisions')
308
rev = self.repo._revision_store.get_revision(rev_id, self.transaction)
309
assert rev.revision_id == rev_id
310
parents = list(rev.parent_ids)
311
for parent in rev.parent_ids:
312
# debug info only : not a problem for knits.
313
mutter('found ghost %s', parent)
314
self._rev_graph[rev_id] = parents
315
if set(self.revisions.get_parents(rev_id)) != set(parents):
316
self.inconsistent_parents += 1
317
mutter('Inconsistent inventory parents: id {%s} '
318
'inventory claims %r, '
319
'available parents are %r, '
320
'unavailable parents are %r',
322
set(self.inventory.get_parents(rev_id)),
324
set(rev.parent_ids).difference(set(parents)))
326
def _check_garbage_inventories(self):
327
"""Check for garbage inventories which we cannot trust
329
We cant trust them because their pre-requisite file data may not
330
be present - all we know is that their revision was not installed.
332
inventories = set(self.inventory.versions())
333
revisions = set(self.revisions.versions())
334
garbage = inventories.difference(revisions)
335
self.garbage_inventories = len(garbage)
336
for revision_id in garbage:
337
mutter('Garbage inventory {%s} found.', revision_id)