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