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