##// END OF EJS Templates
setdiscovery: avoid calling any sample building if the undecided set is small...
Pierre-Yves David -
r23808:07d0f59e default
parent child Browse files
Show More
@@ -1,242 +1,243 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 48 def _updatesample(dag, nodes, sample, always, quicksamplesize=0):
49 49 # if nodes is empty we scan the entire graph
50 50 if nodes:
51 51 heads = dag.headsetofconnecteds(nodes)
52 52 else:
53 53 heads = dag.heads()
54 54 dist = {}
55 55 visit = util.deque(heads)
56 56 seen = set()
57 57 factor = 1
58 58 while visit:
59 59 curr = visit.popleft()
60 60 if curr in seen:
61 61 continue
62 62 d = dist.setdefault(curr, 1)
63 63 if d > factor:
64 64 factor *= 2
65 65 if d == factor:
66 66 if curr not in always: # need this check for the early exit below
67 67 sample.add(curr)
68 68 if quicksamplesize and (len(sample) >= quicksamplesize):
69 69 return
70 70 seen.add(curr)
71 71 for p in dag.parents(curr):
72 72 if not nodes or p in nodes:
73 73 dist.setdefault(p, d + 1)
74 74 visit.append(p)
75 75
76 76 def _setupsample(dag, nodes, size):
77 if len(nodes) <= size:
78 return set(nodes), None, 0
79 77 always = dag.headsetofconnecteds(nodes)
80 78 desiredlen = size - len(always)
81 79 if desiredlen <= 0:
82 80 # This could be bad if there are very many heads, all unknown to the
83 81 # server. We're counting on long request support here.
84 82 return always, None, desiredlen
85 83 return always, set(), desiredlen
86 84
87 85 def _takequicksample(dag, nodes, size):
88 86 always, sample, desiredlen = _setupsample(dag, nodes, size)
89 87 if sample is None:
90 88 return always
91 89 _updatesample(dag, None, sample, always, quicksamplesize=desiredlen)
92 90 sample.update(always)
93 91 return sample
94 92
95 93 def _takefullsample(dag, nodes, size):
96 94 always, sample, desiredlen = _setupsample(dag, nodes, size)
97 95 if sample is None:
98 96 return always
99 97 # update from heads
100 98 _updatesample(dag, nodes, sample, always)
101 99 # update from roots
102 100 _updatesample(dag.inverse(), nodes, sample, always)
103 101 assert sample
104 102 sample = _limitsample(sample, desiredlen)
105 103 if len(sample) < desiredlen:
106 104 more = desiredlen - len(sample)
107 105 sample.update(random.sample(list(nodes - sample - always), more))
108 106 sample.update(always)
109 107 return sample
110 108
111 109 def _limitsample(sample, desiredlen):
112 110 """return a random subset of sample of at most desiredlen item"""
113 111 if len(sample) > desiredlen:
114 112 sample = set(random.sample(sample, desiredlen))
115 113 return sample
116 114
117 115 def findcommonheads(ui, local, remote,
118 116 initialsamplesize=100,
119 117 fullsamplesize=200,
120 118 abortwhenunrelated=True):
121 119 '''Return a tuple (common, anyincoming, remoteheads) used to identify
122 120 missing nodes from or in remote.
123 121 '''
124 122 roundtrips = 0
125 123 cl = local.changelog
126 124 dag = dagutil.revlogdag(cl)
127 125
128 126 # early exit if we know all the specified remote heads already
129 127 ui.debug("query 1; heads\n")
130 128 roundtrips += 1
131 129 ownheads = dag.heads()
132 130 sample = _limitsample(ownheads, initialsamplesize)
133 131 # indices between sample and externalized version must match
134 132 sample = list(sample)
135 133 if remote.local():
136 134 # stopgap until we have a proper localpeer that supports batch()
137 135 srvheadhashes = remote.heads()
138 136 yesno = remote.known(dag.externalizeall(sample))
139 137 elif remote.capable('batch'):
140 138 batch = remote.batch()
141 139 srvheadhashesref = batch.heads()
142 140 yesnoref = batch.known(dag.externalizeall(sample))
143 141 batch.submit()
144 142 srvheadhashes = srvheadhashesref.value
145 143 yesno = yesnoref.value
146 144 else:
147 145 # compatibility with pre-batch, but post-known remotes during 1.9
148 146 # development
149 147 srvheadhashes = remote.heads()
150 148 sample = []
151 149
152 150 if cl.tip() == nullid:
153 151 if srvheadhashes != [nullid]:
154 152 return [nullid], True, srvheadhashes
155 153 return [nullid], False, []
156 154
157 155 # start actual discovery (we note this before the next "if" for
158 156 # compatibility reasons)
159 157 ui.status(_("searching for changes\n"))
160 158
161 159 srvheads = dag.internalizeall(srvheadhashes, filterunknown=True)
162 160 if len(srvheads) == len(srvheadhashes):
163 161 ui.debug("all remote heads known locally\n")
164 162 return (srvheadhashes, False, srvheadhashes,)
165 163
166 164 if sample and len(ownheads) <= initialsamplesize and util.all(yesno):
167 165 ui.note(_("all local heads known remotely\n"))
168 166 ownheadhashes = dag.externalizeall(ownheads)
169 167 return (ownheadhashes, True, srvheadhashes,)
170 168
171 169 # full blown discovery
172 170
173 171 # own nodes I know we both know
174 172 # treat remote heads (and maybe own heads) as a first implicit sample
175 173 # response
176 174 common = cl.incrementalmissingrevs(srvheads)
177 175 commoninsample = set(n for i, n in enumerate(sample) if yesno[i])
178 176 common.addbases(commoninsample)
179 177 # own nodes where I don't know if remote knows them
180 178 undecided = set(common.missingancestors(ownheads))
181 179 # own nodes I know remote lacks
182 180 missing = set()
183 181
184 182 full = False
185 183 while undecided:
186 184
187 185 if sample:
188 186 missinginsample = [n for i, n in enumerate(sample) if not yesno[i]]
189 187 missing.update(dag.descendantset(missinginsample, missing))
190 188
191 189 undecided.difference_update(missing)
192 190
193 191 if not undecided:
194 192 break
195 193
196 194 if full or common.hasbases():
197 195 if full:
198 196 ui.note(_("sampling from both directions\n"))
199 197 else:
200 198 ui.debug("taking initial sample\n")
201 199 samplefunc = _takefullsample
202 200 targetsize = fullsamplesize
203 201 else:
204 202 # use even cheaper initial sample
205 203 ui.debug("taking quick initial sample\n")
206 204 samplefunc = _takequicksample
207 205 targetsize = initialsamplesize
206 if len(undecided) < targetsize:
207 sample = list(undecided)
208 else:
208 209 sample = samplefunc(dag, undecided, targetsize)
209 210 sample = _limitsample(sample, targetsize)
210 211
211 212 roundtrips += 1
212 213 ui.progress(_('searching'), roundtrips, unit=_('queries'))
213 214 ui.debug("query %i; still undecided: %i, sample size is: %i\n"
214 215 % (roundtrips, len(undecided), len(sample)))
215 216 # indices between sample and externalized version must match
216 217 sample = list(sample)
217 218 yesno = remote.known(dag.externalizeall(sample))
218 219 full = True
219 220
220 221 if sample:
221 222 commoninsample = set(n for i, n in enumerate(sample) if yesno[i])
222 223 common.addbases(commoninsample)
223 224 common.removeancestorsfrom(undecided)
224 225
225 226 # heads(common) == heads(common.bases) since common represents common.bases
226 227 # and all its ancestors
227 228 result = dag.headsetofconnecteds(common.bases)
228 229 # common.bases can include nullrev, but our contract requires us to not
229 230 # return any heads in that case, so discard that
230 231 result.discard(nullrev)
231 232 ui.progress(_('searching'), None)
232 233 ui.debug("%d total queries\n" % roundtrips)
233 234
234 235 if not result and srvheadhashes != [nullid]:
235 236 if abortwhenunrelated:
236 237 raise util.Abort(_("repository is unrelated"))
237 238 else:
238 239 ui.warn(_("warning: repository is unrelated\n"))
239 240 return (set([nullid]), True, srvheadhashes,)
240 241
241 242 anyincoming = (srvheadhashes != [nullid])
242 243 return dag.externalizeall(result), anyincoming, srvheadhashes
General Comments 0
You need to be logged in to leave comments. Login now