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