##// END OF EJS Templates
setdiscovery: drop the 'always' argument to '_updatesample'...
Pierre-Yves David -
r23814:6a5877a7 default
parent child Browse files
Show More
@@ -1,255 +1,252 b''
1 1 # setdiscovery.py - improved discovery of common nodeset for mercurial
2 2 #
3 3 # Copyright 2010 Benoit Boissinot <bboissin@gmail.com>
4 4 # and Peter Arrenbrecht <peter@arrenbrecht.ch>
5 5 #
6 6 # This software may be used and distributed according to the terms of the
7 7 # GNU General Public License version 2 or any later version.
8 8 """
9 9 Algorithm works in the following way. You have two repository: local and
10 10 remote. They both contains a DAG of changelists.
11 11
12 12 The goal of the discovery protocol is to find one set of node *common*,
13 13 the set of nodes shared by local and remote.
14 14
15 15 One of the issue with the original protocol was latency, it could
16 16 potentially require lots of roundtrips to discover that the local repo was a
17 17 subset of remote (which is a very common case, you usually have few changes
18 18 compared to upstream, while upstream probably had lots of development).
19 19
20 20 The new protocol only requires one interface for the remote repo: `known()`,
21 21 which given a set of changelists tells you if they are present in the DAG.
22 22
23 23 The algorithm then works as follow:
24 24
25 25 - We will be using three sets, `common`, `missing`, `unknown`. Originally
26 26 all nodes are in `unknown`.
27 27 - Take a sample from `unknown`, call `remote.known(sample)`
28 28 - For each node that remote knows, move it and all its ancestors to `common`
29 29 - For each node that remote doesn't know, move it and all its descendants
30 30 to `missing`
31 31 - Iterate until `unknown` is empty
32 32
33 33 There are a couple optimizations, first is instead of starting with a random
34 34 sample of missing, start by sending all heads, in the case where the local
35 35 repo is a subset, you computed the answer in one round trip.
36 36
37 37 Then you can do something similar to the bisecting strategy used when
38 38 finding faulty changesets. Instead of random samples, you can try picking
39 39 nodes that will maximize the number of nodes that will be
40 40 classified with it (since all ancestors or descendants will be marked as well).
41 41 """
42 42
43 43 from node import nullid, nullrev
44 44 from i18n import _
45 45 import random
46 46 import util, dagutil
47 47
48 def _updatesample(dag, nodes, sample, always, quicksamplesize=0):
48 def _updatesample(dag, nodes, sample, quicksamplesize=0):
49 49 """update an existing sample to match the expected size
50 50
51 51 The sample is updated with nodes exponentially distant from each head of the
52 52 <nodes> set. (H~1, H~2, H~4, H~8, etc).
53 53
54 54 If a target size is specified, the sampling will stop once this size is
55 55 reached. Otherwise sampling will happen until roots of the <nodes> set are
56 56 reached.
57 57
58 58 :dag: a dag object from dagutil
59 59 :nodes: set of nodes we want to discover (if None, assume the whole dag)
60 60 :sample: a sample to update
61 :always: set of notable nodes that will be part of the sample anyway
62 61 :quicksamplesize: optional target size of the sample"""
63 62 # if nodes is empty we scan the entire graph
64 63 if nodes:
65 64 heads = dag.headsetofconnecteds(nodes)
66 65 else:
67 66 heads = dag.heads()
68 67 dist = {}
69 68 visit = util.deque(heads)
70 69 seen = set()
71 70 factor = 1
72 71 while visit:
73 72 curr = visit.popleft()
74 73 if curr in seen:
75 74 continue
76 75 d = dist.setdefault(curr, 1)
77 76 if d > factor:
78 77 factor *= 2
79 78 if d == factor:
80 if curr not in always: # need this check for the early exit below
81 79 sample.add(curr)
82 80 if quicksamplesize and (len(sample) >= quicksamplesize):
83 81 return
84 82 seen.add(curr)
85 83 for p in dag.parents(curr):
86 84 if not nodes or p in nodes:
87 85 dist.setdefault(p, d + 1)
88 86 visit.append(p)
89 87
90 88 def _setupsample(dag, nodes, size):
91 89 always = dag.headsetofconnecteds(nodes)
92 90 desiredlen = size - len(always)
93 91 if desiredlen <= 0:
94 92 # This could be bad if there are very many heads, all unknown to the
95 93 # server. We're counting on long request support here.
96 94 return always, None, desiredlen
97 95 return always, set(), desiredlen
98 96
99 97 def _takequicksample(dag, nodes, size):
100 98 always, sample, desiredlen = _setupsample(dag, nodes, size)
101 99 if sample is None:
102 100 return always
103 _updatesample(dag, None, sample, always, quicksamplesize=desiredlen)
104 sample.update(always)
101 sample = always
102 _updatesample(dag, None, sample, quicksamplesize=size)
105 103 return sample
106 104
107 105 def _takefullsample(dag, nodes, size):
108 sample = always = dag.headsetofconnecteds(nodes)
106 sample = dag.headsetofconnecteds(nodes)
109 107 # update from heads
110 _updatesample(dag, nodes, sample, always)
108 _updatesample(dag, nodes, sample)
111 109 # update from roots
112 _updatesample(dag.inverse(), nodes, sample, always)
110 _updatesample(dag.inverse(), nodes, sample)
113 111 assert sample
114 sample.update(always)
115 112 sample = _limitsample(sample, size)
116 113 if len(sample) < size:
117 114 more = size - len(sample)
118 115 sample.update(random.sample(list(nodes - sample), more))
119 116 return sample
120 117
121 118 def _limitsample(sample, desiredlen):
122 119 """return a random subset of sample of at most desiredlen item"""
123 120 if len(sample) > desiredlen:
124 121 sample = set(random.sample(sample, desiredlen))
125 122 return sample
126 123
127 124 def findcommonheads(ui, local, remote,
128 125 initialsamplesize=100,
129 126 fullsamplesize=200,
130 127 abortwhenunrelated=True):
131 128 '''Return a tuple (common, anyincoming, remoteheads) used to identify
132 129 missing nodes from or in remote.
133 130 '''
134 131 roundtrips = 0
135 132 cl = local.changelog
136 133 dag = dagutil.revlogdag(cl)
137 134
138 135 # early exit if we know all the specified remote heads already
139 136 ui.debug("query 1; heads\n")
140 137 roundtrips += 1
141 138 ownheads = dag.heads()
142 139 sample = _limitsample(ownheads, initialsamplesize)
143 140 # indices between sample and externalized version must match
144 141 sample = list(sample)
145 142 if remote.local():
146 143 # stopgap until we have a proper localpeer that supports batch()
147 144 srvheadhashes = remote.heads()
148 145 yesno = remote.known(dag.externalizeall(sample))
149 146 elif remote.capable('batch'):
150 147 batch = remote.batch()
151 148 srvheadhashesref = batch.heads()
152 149 yesnoref = batch.known(dag.externalizeall(sample))
153 150 batch.submit()
154 151 srvheadhashes = srvheadhashesref.value
155 152 yesno = yesnoref.value
156 153 else:
157 154 # compatibility with pre-batch, but post-known remotes during 1.9
158 155 # development
159 156 srvheadhashes = remote.heads()
160 157 sample = []
161 158
162 159 if cl.tip() == nullid:
163 160 if srvheadhashes != [nullid]:
164 161 return [nullid], True, srvheadhashes
165 162 return [nullid], False, []
166 163
167 164 # start actual discovery (we note this before the next "if" for
168 165 # compatibility reasons)
169 166 ui.status(_("searching for changes\n"))
170 167
171 168 srvheads = dag.internalizeall(srvheadhashes, filterunknown=True)
172 169 if len(srvheads) == len(srvheadhashes):
173 170 ui.debug("all remote heads known locally\n")
174 171 return (srvheadhashes, False, srvheadhashes,)
175 172
176 173 if sample and len(ownheads) <= initialsamplesize and util.all(yesno):
177 174 ui.note(_("all local heads known remotely\n"))
178 175 ownheadhashes = dag.externalizeall(ownheads)
179 176 return (ownheadhashes, True, srvheadhashes,)
180 177
181 178 # full blown discovery
182 179
183 180 # own nodes I know we both know
184 181 # treat remote heads (and maybe own heads) as a first implicit sample
185 182 # response
186 183 common = cl.incrementalmissingrevs(srvheads)
187 184 commoninsample = set(n for i, n in enumerate(sample) if yesno[i])
188 185 common.addbases(commoninsample)
189 186 # own nodes where I don't know if remote knows them
190 187 undecided = set(common.missingancestors(ownheads))
191 188 # own nodes I know remote lacks
192 189 missing = set()
193 190
194 191 full = False
195 192 while undecided:
196 193
197 194 if sample:
198 195 missinginsample = [n for i, n in enumerate(sample) if not yesno[i]]
199 196 missing.update(dag.descendantset(missinginsample, missing))
200 197
201 198 undecided.difference_update(missing)
202 199
203 200 if not undecided:
204 201 break
205 202
206 203 if full or common.hasbases():
207 204 if full:
208 205 ui.note(_("sampling from both directions\n"))
209 206 else:
210 207 ui.debug("taking initial sample\n")
211 208 samplefunc = _takefullsample
212 209 targetsize = fullsamplesize
213 210 else:
214 211 # use even cheaper initial sample
215 212 ui.debug("taking quick initial sample\n")
216 213 samplefunc = _takequicksample
217 214 targetsize = initialsamplesize
218 215 if len(undecided) < targetsize:
219 216 sample = list(undecided)
220 217 else:
221 218 sample = samplefunc(dag, undecided, targetsize)
222 219 sample = _limitsample(sample, targetsize)
223 220
224 221 roundtrips += 1
225 222 ui.progress(_('searching'), roundtrips, unit=_('queries'))
226 223 ui.debug("query %i; still undecided: %i, sample size is: %i\n"
227 224 % (roundtrips, len(undecided), len(sample)))
228 225 # indices between sample and externalized version must match
229 226 sample = list(sample)
230 227 yesno = remote.known(dag.externalizeall(sample))
231 228 full = True
232 229
233 230 if sample:
234 231 commoninsample = set(n for i, n in enumerate(sample) if yesno[i])
235 232 common.addbases(commoninsample)
236 233 common.removeancestorsfrom(undecided)
237 234
238 235 # heads(common) == heads(common.bases) since common represents common.bases
239 236 # and all its ancestors
240 237 result = dag.headsetofconnecteds(common.bases)
241 238 # common.bases can include nullrev, but our contract requires us to not
242 239 # return any heads in that case, so discard that
243 240 result.discard(nullrev)
244 241 ui.progress(_('searching'), None)
245 242 ui.debug("%d total queries\n" % roundtrips)
246 243
247 244 if not result and srvheadhashes != [nullid]:
248 245 if abortwhenunrelated:
249 246 raise util.Abort(_("repository is unrelated"))
250 247 else:
251 248 ui.warn(_("warning: repository is unrelated\n"))
252 249 return (set([nullid]), True, srvheadhashes,)
253 250
254 251 anyincoming = (srvheadhashes != [nullid])
255 252 return dag.externalizeall(result), anyincoming, srvheadhashes
General Comments 0
You need to be logged in to leave comments. Login now