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