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
|
# Copyright 2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Script code relating to team participations."""
__metaclass__ = type
__all__ = [
"check_teamparticipation_circular",
"check_teamparticipation_consistency",
"fetch_team_participation_info",
"fix_teamparticipation_consistency",
]
from collections import (
defaultdict,
namedtuple,
)
from functools import partial
from itertools import (
chain,
count,
imap,
izip,
)
import transaction
from zope.component import getUtility
from lp.registry.interfaces.teammembership import ACTIVE_STATES
from lp.services.database.sqlbase import (
quote,
sqlvalues,
)
from lp.services.scripts.base import LaunchpadScriptFailure
from lp.services.webapp.interfaces import (
IStoreSelector,
MAIN_STORE,
MASTER_FLAVOR,
SLAVE_FLAVOR,
)
def get_master_store():
"""Return a master store.
Errors in `TeamPartipation` must be fixed in the master.
"""
return getUtility(IStoreSelector).get(MAIN_STORE, MASTER_FLAVOR)
def get_slave_store():
"""Return a slave store.
Errors in `TeamPartipation` can be detected using a replicated copy.
"""
return getUtility(IStoreSelector).get(MAIN_STORE, SLAVE_FLAVOR)
def check_teamparticipation_circular(log):
"""Check circular references.
There can be no mutual participation between teams.
"""
query = """
SELECT tp.team, tp2.team
FROM TeamParticipation AS tp,
TeamParticipation AS tp2
WHERE tp.team = tp2.person
AND tp.person = tp2.team
AND tp.id != tp2.id;
"""
circular_references = list(get_slave_store().execute(query))
if len(circular_references) > 0:
raise LaunchpadScriptFailure(
"Circular references found: %s" % circular_references)
ConsistencyError = namedtuple(
"ConsistencyError", ("type", "team", "people"))
def report_progress(log, interval, results, what):
"""Iterate through `results`, reporting on progress.
:param log: A logger.
:param interval: How many results to report progress about.
:param results: An iterable of things.
:param what: A string descriping what the results are.
"""
for num, result in izip(count(1), results):
if num % interval == 0:
log.debug("%d %s", num, what)
yield result
log.debug("%d %s", num, what)
def execute_long_query(store, log, interval, query):
"""Execute the given query, reporting as results are fetched.
The query is logged, then every `interval` rows a message is logged with
the total number of rows fetched thus far.
"""
log.debug(query)
results = store.execute(query)
# Hackish; the default is 10 which seems fairly low.
results._raw_cursor.arraysize = interval
return report_progress(log, interval, results, "rows")
def fetch_team_participation_info(log):
"""Fetch people, teams, memberships and participations."""
slurp = partial(execute_long_query, get_slave_store(), log, 10000)
people = dict(
slurp(
"SELECT id, name FROM Person"
" WHERE teamowner IS NULL"
" AND merged IS NULL"))
teams = dict(
slurp(
"SELECT id, name FROM Person"
" WHERE teamowner IS NOT NULL"
" AND merged IS NULL"))
team_memberships = defaultdict(set)
results = slurp(
"SELECT team, person FROM TeamMembership"
" WHERE status in %s" % quote(ACTIVE_STATES))
for (team, person) in results:
team_memberships[team].add(person)
team_participations = defaultdict(set)
results = slurp(
"SELECT team, person FROM TeamParticipation")
for (team, person) in results:
team_participations[team].add(person)
# Don't hold any locks.
transaction.commit()
return people, teams, team_memberships, team_participations
def check_teamparticipation_consistency(log, info):
"""Check for missing or spurious participations.
For example, participations for people who are not members, or missing
participations for people who are members.
"""
people, teams, team_memberships, team_participations = info
# set.intersection() with a dict is slow.
people_set = frozenset(people)
teams_set = frozenset(teams)
def get_participants(team):
"""Recurse through membership records to get participants."""
member_people = team_memberships[team].intersection(people_set)
member_people.add(team) # Teams always participate in themselves.
member_teams = team_memberships[team].intersection(teams_set)
return member_people.union(
chain.from_iterable(imap(get_participants, member_teams)))
def check_participants(person, expected, observed):
spurious = observed - expected
missing = expected - observed
if len(spurious) > 0:
yield ConsistencyError("spurious", person, sorted(spurious))
if len(missing) > 0:
yield ConsistencyError("missing", person, sorted(missing))
errors = []
log.debug("Checking consistency of %d people", len(people))
for person in report_progress(log, 50000, people, "people"):
participants_expected = set((person,))
participants_observed = team_participations[person]
errors.extend(
check_participants(
person, participants_expected, participants_observed))
log.debug("Checking consistency of %d teams", len(teams))
for team in report_progress(log, 1000, teams, "teams"):
participants_expected = get_participants(team)
participants_observed = team_participations[team]
errors.extend(
check_participants(
team, participants_expected, participants_observed))
def get_repr(id):
if id in people:
name = people[id]
elif id in teams:
name = teams[id]
else:
name = "<unknown>"
return "%s (%d)" % (name, id)
for error in errors:
people_repr = ", ".join(imap(get_repr, error.people))
log.warn(
"%s: %s TeamParticipation entries for %s.",
get_repr(error.team), error.type, people_repr)
return errors
def fix_teamparticipation_consistency(log, errors):
"""Fix missing or spurious participations.
This function does not consult `TeamMembership` at all, so it /may/
introduce another participation inconsistency if the records that are the
subject of the given errors have been modified since being checked.
:param errors: An iterable of `ConsistencyError` tuples.
"""
sql_missing = (
"""
INSERT INTO TeamParticipation (team, person)
SELECT %(team)s, %(person)s
EXCEPT
SELECT team, person
FROM TeamParticipation
WHERE team = %(team)s
AND person = %(person)s
""")
sql_spurious = (
"""
DELETE FROM TeamParticipation
WHERE team = %(team)s
AND person IN %(people)s
""")
store = get_master_store()
for error in errors:
if error.type == "missing":
for person in error.people:
statement = sql_missing % sqlvalues(
team=error.team, person=person)
log.debug(statement)
store.execute(statement)
transaction.commit()
elif error.type == "spurious":
statement = sql_spurious % sqlvalues(
team=error.team, people=error.people)
log.debug(statement)
store.execute(statement)
transaction.commit()
else:
log.warn("Unrecognized error: %r", error)
|