Mercurial > public > mercurial-scm > hg
view mercurial/httppeer.py @ 45892:06b64fabf91c
copies: cache the ancestor checking call when tracing copy
A good share of the time spent in this function is spent doing ancestors
checking. To avoid spending time in duplicated call, we cache the result of
calls.
In the slower case, this provide a quite significant performance boost. Below
are the result for a set of selected pairs (many of them pathological):
(And further down is another table that summarize the current state of filelog
based vs changeset base copy tracing)
The benchmark have been configured to be killed after 6 minutes of runtime,
which mean that any detect slower than 2 minutes will be marked as "killed".
This drop some useful information about how much slower these case are? but also
prevent 99% of the benchmark time to be spent on case that can be labelled "very
slow" anyway.
Repo Case Source-Rev Dest-Rev Old-Time New-Time Difference Factor
------------------------------------------------------------------------------------------------------------------------------------
mercurial x_revs_x_added_0_copies ad6b123de1c7 39cfcef4f463 : 0.000044 s, 0.000044 s, +0.000000 s, ? 1.0000
mercurial x_revs_x_added_x_copies 2b1c78674230 0c1d10351869 : 0.000138 s, 0.000138 s, +0.000000 s, ? 1.0000
mercurial x000_revs_x000_added_x_copies 81f8ff2a9bf2 dd3267698d84 : 0.005067 s, 0.005052 s, -0.000015 s, ? 0.9970
pypy x_revs_x_added_0_copies aed021ee8ae8 099ed31b181b : 0.000218 s, 0.000219 s, +0.000001 s, ? 1.0046
pypy x_revs_x000_added_0_copies 4aa4e1f8e19a 359343b9ac0e : 0.000053 s, 0.000055 s, +0.000002 s, ? 1.0377
pypy x_revs_x_added_x_copies ac52eb7bbbb0 72e022663155 : 0.000125 s, 0.000128 s, +0.000003 s, ? 1.0240
pypy x_revs_x00_added_x_copies c3b14617fbd7 ace7255d9a26 : 0.001098 s, 0.001089 s, -0.000009 s, ? 0.9918
pypy x_revs_x000_added_x000_copies df6f7a526b60 a83dc6a2d56f : 0.017546 s, 0.017407 s, -0.000139 s, ? 0.9921
pypy x000_revs_xx00_added_0_copies 89a76aede314 2f22446ff07e : 0.096723 s, 0.094175 s, -0.002548 s, ? 0.9737
pypy x000_revs_x000_added_x_copies 8a3b5bfd266e 2c68e87c3efe : 0.271796 s, 0.238009 s, -0.033787 s, ? 0.8757
pypy x000_revs_x000_added_x000_copies 89a76aede314 7b3dda341c84 : 0.128602 s, 0.125876 s, -0.002726 s, ? 0.9788
pypy x0000_revs_x_added_0_copies d1defd0dc478 c9cb1334cc78 : 7.086742 s, 3.581556 s, -3.505186 s, ? 0.5054
pypy x0000_revs_xx000_added_0_copies bf2c629d0071 4ffed77c095c : 0.016634 s, 0.016721 s, +0.000087 s, ? 1.0052
pypy x0000_revs_xx000_added_x000_copies 08ea3258278e d9fa043f30c0 : 0.254225 s, 0.242367 s, -0.011858 s, ? 0.9534
netbeans x_revs_x_added_0_copies fb0955ffcbcd a01e9239f9e7 : 0.000166 s, 0.000165 s, -0.000001 s, ? 0.9940
netbeans x_revs_x000_added_0_copies 6f360122949f 20eb231cc7d0 : 0.000118 s, 0.000114 s, -0.000004 s, ? 0.9661
netbeans x_revs_x_added_x_copies 1ada3faf6fb6 5a39d12eecf4 : 0.000296 s, 0.000296 s, +0.000000 s, ? 1.0000
netbeans x_revs_x00_added_x_copies 35be93ba1e2c 9eec5e90c05f : 0.001137 s, 0.001124 s, -0.000013 s, ? 0.9886
netbeans x000_revs_xx00_added_0_copies eac3045b4fdd 51d4ae7f1290 : 0.014133 s, 0.013060 s, -0.001073 s, ? 0.9241
netbeans x000_revs_x000_added_x_copies e2063d266acd 6081d72689dc : 0.016988 s, 0.017112 s, +0.000124 s, ? 1.0073
netbeans x000_revs_x000_added_x000_copies ff453e9fee32 411350406ec2 : 0.676361 s, 0.660350 s, -0.016011 s, ? 0.9763
netbeans x0000_revs_xx000_added_x000_copies 588c2d1ced70 1aad62e59ddd : 12.515149 s, 10.032499 s, -2.482650 s, ? 0.8016
mozilla-central x_revs_x_added_0_copies 3697f962bb7b 7015fcdd43a2 : 0.000186 s, 0.000189 s, +0.000003 s, ? 1.0161
mozilla-central x_revs_x000_added_0_copies dd390860c6c9 40d0c5bed75d : 0.000459 s, 0.000462 s, +0.000003 s, ? 1.0065
mozilla-central x_revs_x_added_x_copies 8d198483ae3b 14207ffc2b2f : 0.000273 s, 0.000270 s, -0.000003 s, ? 0.9890
mozilla-central x_revs_x00_added_x_copies 98cbc58cc6bc 446a150332c3 : 0.001503 s, 0.001474 s, -0.000029 s, ? 0.9807
mozilla-central x_revs_x000_added_x000_copies 3c684b4b8f68 0a5e72d1b479 : 0.004862 s, 0.004806 s, -0.000056 s, ? 0.9885
mozilla-central x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 0.088291 s, 0.085150 s, -0.003141 s, ? 0.9644
mozilla-central x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.007113 s, 0.007064 s, -0.000049 s, ? 0.9931
mozilla-central x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.004687 s, 0.004741 s, +0.000054 s, ? 1.0115
mozilla-central x000_revs_x000_added_x000_copies 7c97034feb78 4407bd0c6330 : 0.198710 s, 0.190133 s, -0.008577 s, ? 0.9568
mozilla-central x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 0.036068 s, 0.035651 s, -0.000417 s, ? 0.9884
mozilla-central x0000_revs_xx000_added_x000_copies f78c615a656c 96a38b690156 : 0.465362 s, 0.440694 s, -0.024668 s, ? 0.9470
mozilla-central x00000_revs_x0000_added_x0000_copies 6832ae71433c 4c222a1d9a00 : 24.519684 s, 18.454163 s, -6.065521 s, ? 0.7526
mozilla-central x00000_revs_x00000_added_x000_copies 76caed42cf7c 1daa622bbe42 : 42.711897 s, 31.562719 s, -11.149178 s, ? 0.7390
mozilla-try x_revs_x_added_0_copies aaf6dde0deb8 9790f499805a : 0.001201 s, 0.001189 s, -0.000012 s, ? 0.9900
mozilla-try x_revs_x000_added_0_copies d8d0222927b4 5bb8ce8c7450 : 0.001216 s, 0.001204 s, -0.000012 s, ? 0.9901
mozilla-try x_revs_x_added_x_copies 092fcca11bdb 936255a0384a : 0.000595 s, 0.000586 s, -0.000009 s, ? 0.9849
mozilla-try x_revs_x00_added_x_copies b53d2fadbdb5 017afae788ec : 0.001856 s, 0.001845 s, -0.000011 s, ? 0.9941
mozilla-try x_revs_x000_added_x000_copies 20408ad61ce5 6f0ee96e21ad : 0.064936 s, 0.063822 s, -0.001114 s, ? 0.9828
mozilla-try x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 0.090601 s, 0.088038 s, -0.002563 s, ? 0.9717
mozilla-try x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.007510 s, 0.007389 s, -0.000121 s, ? 0.9839
mozilla-try x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.004911 s, 0.004868 s, -0.000043 s, ? 0.9912
mozilla-try x000_revs_x000_added_x000_copies 1346fd0130e4 4c65cbdabc1f : 0.233231 s, 0.222450 s, -0.010781 s, ? 0.9538
mozilla-try x0000_revs_x_added_0_copies 63519bfd42ee a36a2a865d92 : 0.419989 s, 0.370675 s, -0.049314 s, ? 0.8826
mozilla-try x0000_revs_x_added_x_copies 9fe69ff0762d bcabf2a78927 : 0.401521 s, 0.358020 s, -0.043501 s, ? 0.8917
mozilla-try x0000_revs_xx000_added_x_copies 156f6e2674f2 4d0f2c178e66 : 0.179555 s, 0.145235 s, -0.034320 s, ? 0.8089
mozilla-try x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 0.038004 s, 0.037606 s, -0.000398 s, ? 0.9895
mozilla-try x0000_revs_xx000_added_x000_copies 89294cd501d9 7ccb2fc7ccb5 : 52.838482 s, 7.382439 s, -45.456043 s, ? 0.1397
mozilla-try x0000_revs_x0000_added_x0000_copies e928c65095ed e951f4ad123a : 8.705874 s, 7.273506 s, -1.432368 s, ? 0.8355
mozilla-try x00000_revs_x00000_added_0_copies dc8a3ca7010e d16fde900c9c : 1.126708 s, 1.074593 s, -0.052115 s, ? 0.9537
mozilla-try x00000_revs_x0000_added_x0000_copies 8d3fafa80d4b eb884023b810 : 83.854020 s, 27.746195 s, -56.107825 s, ? 0.3309
Below is a table comparing the runtime of the current "filelog centric"
algorithm, with the "changeset centric" one, we just modified.
The changeset centric algorithm is a significant win in many scenario, but they
are still various cases where it is quite slower. When many revision has to be
considered the cost of retrieving the copy information, creating new
dictionaries, merging dictionaries and checking if revision are ancestors of
each other can slow things down.
The rest of this series, will introduce a rust version of the copy tracing code
to deal with most of theses issues.
Repo Case Source-Rev Dest-Rev filelog sidedata Difference Factor
---------------------------------------------------------------------------------------------------------------------------------------
mercurial x_revs_x_added_0_copies ad6b123de1c7 39cfcef4f463 : 0.000914 s, 0.000044 s, - 0.000870 s, ? 0.048140
mercurial x_revs_x_added_x_copies 2b1c78674230 0c1d10351869 : 0.001812 s, 0.000138 s, - 0.001674 s, ? 0.076159
mercurial x000_revs_x000_added_x_copies 81f8ff2a9bf2 dd3267698d84 : 0.017954 s, 0.005052 s, - 0.012902 s, ? 0.281386
pypy x_revs_x_added_0_copies aed021ee8ae8 099ed31b181b : 0.001509 s, 0.000219 s, - 0.001290 s, ? 0.145129
pypy x_revs_x000_added_0_copies 4aa4e1f8e19a 359343b9ac0e : 0.206881 s, 0.000055 s, - 0.206826 s, ? 0.000266
pypy x_revs_x_added_x_copies ac52eb7bbbb0 72e022663155 : 0.016951 s, 0.000128 s, - 0.016823 s, ? 0.007551
pypy x_revs_x00_added_x_copies c3b14617fbd7 ace7255d9a26 : 0.019096 s, 0.001089 s, - 0.018007 s, ? 0.057028
pypy x_revs_x000_added_x000_copies df6f7a526b60 a83dc6a2d56f : 0.762506 s, 0.017407 s, - 0.745099 s, ? 0.022829
pypy x000_revs_xx00_added_0_copies 89a76aede314 2f22446ff07e : 1.179211 s, 0.094175 s, - 1.085036 s, ? 0.079863
pypy x000_revs_x000_added_x_copies 8a3b5bfd266e 2c68e87c3efe : 1.249058 s, 0.238009 s, - 1.011049 s, ? 0.190551
pypy x000_revs_x000_added_x000_copies 89a76aede314 7b3dda341c84 : 1.614107 s, 0.125876 s, - 1.488231 s, ? 0.077985
pypy x0000_revs_x_added_0_copies d1defd0dc478 c9cb1334cc78 : 0.001064 s, 3.581556 s, + 3.580492 s, ? 3366.124060
pypy x0000_revs_xx000_added_0_copies bf2c629d0071 4ffed77c095c : 1.061275 s, 0.016721 s, - 1.044554 s, ? 0.015756
pypy x0000_revs_xx000_added_x000_copies 08ea3258278e d9fa043f30c0 : 1.341119 s, 0.242367 s, - 1.098752 s, ? 0.180720
netbeans x_revs_x_added_0_copies fb0955ffcbcd a01e9239f9e7 : 0.027803 s, 0.000165 s, - 0.027638 s, ? 0.005935
netbeans x_revs_x000_added_0_copies 6f360122949f 20eb231cc7d0 : 0.130014 s, 0.000114 s, - 0.129900 s, ? 0.000877
netbeans x_revs_x_added_x_copies 1ada3faf6fb6 5a39d12eecf4 : 0.024990 s, 0.000296 s, - 0.024694 s, ? 0.011845
netbeans x_revs_x00_added_x_copies 35be93ba1e2c 9eec5e90c05f : 0.052201 s, 0.001124 s, - 0.051077 s, ? 0.021532
netbeans x000_revs_xx00_added_0_copies eac3045b4fdd 51d4ae7f1290 : 0.037642 s, 0.013060 s, - 0.024582 s, ? 0.346953
netbeans x000_revs_x000_added_x_copies e2063d266acd 6081d72689dc : 0.197086 s, 0.017112 s, - 0.179974 s, ? 0.086825
netbeans x000_revs_x000_added_x000_copies ff453e9fee32 411350406ec2 : 0.935148 s, 0.660350 s, - 0.274798 s, ? 0.706145
netbeans x0000_revs_xx000_added_x000_copies 588c2d1ced70 1aad62e59ddd : 3.920674 s, 10.032499 s, + 6.111825 s, ? 2.558871
mozilla-central x_revs_x_added_0_copies 3697f962bb7b 7015fcdd43a2 : 0.024232 s, 0.000189 s, - 0.024043 s, ? 0.007800
mozilla-central x_revs_x000_added_0_copies dd390860c6c9 40d0c5bed75d : 0.141483 s, 0.000462 s, - 0.141021 s, ? 0.003265
mozilla-central x_revs_x_added_x_copies 8d198483ae3b 14207ffc2b2f : 0.025775 s, 0.000270 s, - 0.025505 s, ? 0.010475
mozilla-central x_revs_x00_added_x_copies 98cbc58cc6bc 446a150332c3 : 0.084922 s, 0.001474 s, - 0.083448 s, ? 0.017357
mozilla-central x_revs_x000_added_x000_copies 3c684b4b8f68 0a5e72d1b479 : 0.194784 s, 0.004806 s, - 0.189978 s, ? 0.024673
mozilla-central x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 2.161103 s, 0.085150 s, - 2.075953 s, ? 0.039401
mozilla-central x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.089347 s, 0.007064 s, - 0.082283 s, ? 0.079063
mozilla-central x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.732171 s, 0.004741 s, - 0.727430 s, ? 0.006475
mozilla-central x000_revs_x000_added_x000_copies 7c97034feb78 4407bd0c6330 : 1.157287 s, 0.190133 s, - 0.967154 s, ? 0.164292
mozilla-central x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 6.726568 s, 0.035651 s, - 6.690917 s, ? 0.005300
mozilla-central x0000_revs_xx000_added_x000_copies f78c615a656c 96a38b690156 : 3.266229 s, 0.440694 s, - 2.825535 s, ? 0.134924
mozilla-central x00000_revs_x0000_added_x0000_copies 6832ae71433c 4c222a1d9a00 : 15.860534 s, 18.454163 s, + 2.593629 s, ? 1.163527
mozilla-central x00000_revs_x00000_added_x000_copies 76caed42cf7c 1daa622bbe42 : 20.450475 s, 31.562719 s, +11.112244 s, ? 1.543373
mozilla-try x_revs_x_added_0_copies aaf6dde0deb8 9790f499805a : 0.080442 s, 0.001189 s, - 0.079253 s, ? 0.014781
mozilla-try x_revs_x000_added_0_copies d8d0222927b4 5bb8ce8c7450 : 0.497672 s, 0.001204 s, - 0.496468 s, ? 0.002419
mozilla-try x_revs_x_added_x_copies 092fcca11bdb 936255a0384a : 0.021183 s, 0.000586 s, - 0.020597 s, ? 0.027664
mozilla-try x_revs_x00_added_x_copies b53d2fadbdb5 017afae788ec : 0.230991 s, 0.001845 s, - 0.229146 s, ? 0.007987
mozilla-try x_revs_x000_added_x000_copies 20408ad61ce5 6f0ee96e21ad : 1.118461 s, 0.063822 s, - 1.054639 s, ? 0.057062
mozilla-try x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 2.206083 s, 0.088038 s, - 2.118045 s, ? 0.039907
mozilla-try x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.089404 s, 0.007389 s, - 0.082015 s, ? 0.082647
mozilla-try x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.733043 s, 0.004868 s, - 0.728175 s, ? 0.006641
mozilla-try x000_revs_x000_added_x000_copies 1346fd0130e4 4c65cbdabc1f : 1.163367 s, 0.222450 s, - 0.940917 s, ? 0.191212
mozilla-try x0000_revs_x_added_0_copies 63519bfd42ee a36a2a865d92 : 0.085456 s, 0.370675 s, + 0.285219 s, ? 4.337612
mozilla-try x0000_revs_x_added_x_copies 9fe69ff0762d bcabf2a78927 : 0.083601 s, 0.358020 s, + 0.274419 s, ? 4.282485
mozilla-try x0000_revs_xx000_added_x_copies 156f6e2674f2 4d0f2c178e66 : 7.366614 s, 0.145235 s, - 7.221379 s, ? 0.019715
mozilla-try x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 6.664464 s, 0.037606 s, - 6.626858 s, ? 0.005643
mozilla-try x0000_revs_xx000_added_x000_copies 89294cd501d9 7ccb2fc7ccb5 : 7.467836 s, 7.382439 s, - 0.085397 s, ? 0.988565
mozilla-try x0000_revs_x0000_added_x0000_copies e928c65095ed e951f4ad123a : 9.801294 s, 7.273506 s, - 2.527788 s, ? 0.742097
mozilla-try x00000_revs_x_added_0_copies 6a320851d377 1ebb79acd503 : 0.091886 s, killed
mozilla-try x00000_revs_x00000_added_0_copies dc8a3ca7010e d16fde900c9c : 26.491140 s, 1.074593 s, -25.416547 s, ? 0.040564
mozilla-try x00000_revs_x_added_x_copies 5173c4b6f97c 95d83ee7242d : 0.092863 s, killed
mozilla-try x00000_revs_x000_added_x_copies 9126823d0e9c ca82787bb23c : 0.226823 s, killed
mozilla-try x00000_revs_x0000_added_x0000_copies 8d3fafa80d4b eb884023b810 : 18.914630 s, 27.746195 s, + 8.831565 s, ? 1.466917
mozilla-try x00000_revs_x00000_added_x0000_copies 1b661134e2ca 1ae03d022d6d : 21.198903 s, killed
mozilla-try x00000_revs_x00000_added_x000_copies 9b2a99adc05e 8e29777b48e6 : 24.952268 s, killed
Differential Revision: https://phab.mercurial-scm.org/D9296
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Mon, 02 Nov 2020 11:03:56 +0100 |
parents | 9f70512ae2cf |
children | 89a2afe31e82 |
line wrap: on
line source
# httppeer.py - HTTP repository proxy classes for mercurial # # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com> # # This software may be used and distributed according to the terms of the # GNU General Public License version 2 or any later version. from __future__ import absolute_import import errno import io import os import socket import struct import weakref from .i18n import _ from .pycompat import getattr from . import ( bundle2, error, httpconnection, pycompat, statichttprepo, url as urlmod, util, wireprotoframing, wireprototypes, wireprotov1peer, wireprotov2peer, wireprotov2server, ) from .interfaces import ( repository, util as interfaceutil, ) from .utils import ( cborutil, stringutil, ) httplib = util.httplib urlerr = util.urlerr urlreq = util.urlreq def encodevalueinheaders(value, header, limit): """Encode a string value into multiple HTTP headers. ``value`` will be encoded into 1 or more HTTP headers with the names ``header-<N>`` where ``<N>`` is an integer starting at 1. Each header name + value will be at most ``limit`` bytes long. Returns an iterable of 2-tuples consisting of header names and values as native strings. """ # HTTP Headers are ASCII. Python 3 requires them to be unicodes, # not bytes. This function always takes bytes in as arguments. fmt = pycompat.strurl(header) + r'-%s' # Note: it is *NOT* a bug that the last bit here is a bytestring # and not a unicode: we're just getting the encoded length anyway, # and using an r-string to make it portable between Python 2 and 3 # doesn't work because then the \r is a literal backslash-r # instead of a carriage return. valuelen = limit - len(fmt % '000') - len(b': \r\n') result = [] n = 0 for i in pycompat.xrange(0, len(value), valuelen): n += 1 result.append((fmt % str(n), pycompat.strurl(value[i : i + valuelen]))) return result class _multifile(object): def __init__(self, *fileobjs): for f in fileobjs: if not util.safehasattr(f, b'length'): raise ValueError( b'_multifile only supports file objects that ' b'have a length but this one does not:', type(f), f, ) self._fileobjs = fileobjs self._index = 0 @property def length(self): return sum(f.length for f in self._fileobjs) def read(self, amt=None): if amt <= 0: return b''.join(f.read() for f in self._fileobjs) parts = [] while amt and self._index < len(self._fileobjs): parts.append(self._fileobjs[self._index].read(amt)) got = len(parts[-1]) if got < amt: self._index += 1 amt -= got return b''.join(parts) def seek(self, offset, whence=os.SEEK_SET): if whence != os.SEEK_SET: raise NotImplementedError( b'_multifile does not support anything other' b' than os.SEEK_SET for whence on seek()' ) if offset != 0: raise NotImplementedError( b'_multifile only supports seeking to start, but that ' b'could be fixed if you need it' ) for f in self._fileobjs: f.seek(0) self._index = 0 def makev1commandrequest( ui, requestbuilder, caps, capablefn, repobaseurl, cmd, args ): """Make an HTTP request to run a command for a version 1 client. ``caps`` is a set of known server capabilities. The value may be None if capabilities are not yet known. ``capablefn`` is a function to evaluate a capability. ``cmd``, ``args``, and ``data`` define the command, its arguments, and raw data to pass to it. """ if cmd == b'pushkey': args[b'data'] = b'' data = args.pop(b'data', None) headers = args.pop(b'headers', {}) ui.debug(b"sending %s command\n" % cmd) q = [(b'cmd', cmd)] headersize = 0 # Important: don't use self.capable() here or else you end up # with infinite recursion when trying to look up capabilities # for the first time. postargsok = caps is not None and b'httppostargs' in caps # Send arguments via POST. if postargsok and args: strargs = urlreq.urlencode(sorted(args.items())) if not data: data = strargs else: if isinstance(data, bytes): i = io.BytesIO(data) i.length = len(data) data = i argsio = io.BytesIO(strargs) argsio.length = len(strargs) data = _multifile(argsio, data) headers['X-HgArgs-Post'] = len(strargs) elif args: # Calling self.capable() can infinite loop if we are calling # "capabilities". But that command should never accept wire # protocol arguments. So this should never happen. assert cmd != b'capabilities' httpheader = capablefn(b'httpheader') if httpheader: headersize = int(httpheader.split(b',', 1)[0]) # Send arguments via HTTP headers. if headersize > 0: # The headers can typically carry more data than the URL. encargs = urlreq.urlencode(sorted(args.items())) for header, value in encodevalueinheaders( encargs, b'X-HgArg', headersize ): headers[header] = value # Send arguments via query string (Mercurial <1.9). else: q += sorted(args.items()) qs = b'?%s' % urlreq.urlencode(q) cu = b"%s%s" % (repobaseurl, qs) size = 0 if util.safehasattr(data, b'length'): size = data.length elif data is not None: size = len(data) if data is not None and 'Content-Type' not in headers: headers['Content-Type'] = 'application/mercurial-0.1' # Tell the server we accept application/mercurial-0.2 and multiple # compression formats if the server is capable of emitting those # payloads. # Note: Keep this set empty by default, as client advertisement of # protocol parameters should only occur after the handshake. protoparams = set() mediatypes = set() if caps is not None: mt = capablefn(b'httpmediatype') if mt: protoparams.add(b'0.1') mediatypes = set(mt.split(b',')) protoparams.add(b'partial-pull') if b'0.2tx' in mediatypes: protoparams.add(b'0.2') if b'0.2tx' in mediatypes and capablefn(b'compression'): # We /could/ compare supported compression formats and prune # non-mutually supported or error if nothing is mutually supported. # For now, send the full list to the server and have it error. comps = [ e.wireprotosupport().name for e in util.compengines.supportedwireengines(util.CLIENTROLE) ] protoparams.add(b'comp=%s' % b','.join(comps)) if protoparams: protoheaders = encodevalueinheaders( b' '.join(sorted(protoparams)), b'X-HgProto', headersize or 1024 ) for header, value in protoheaders: headers[header] = value varyheaders = [] for header in headers: if header.lower().startswith('x-hg'): varyheaders.append(header) if varyheaders: headers['Vary'] = ','.join(sorted(varyheaders)) req = requestbuilder(pycompat.strurl(cu), data, headers) if data is not None: ui.debug(b"sending %d bytes\n" % size) req.add_unredirected_header('Content-Length', '%d' % size) return req, cu, qs def _reqdata(req): """Get request data, if any. If no data, returns None.""" if pycompat.ispy3: return req.data if not req.has_data(): return None return req.get_data() def sendrequest(ui, opener, req): """Send a prepared HTTP request. Returns the response object. """ dbg = ui.debug if ui.debugflag and ui.configbool(b'devel', b'debug.peer-request'): line = b'devel-peer-request: %s\n' dbg( line % b'%s %s' % ( pycompat.bytesurl(req.get_method()), pycompat.bytesurl(req.get_full_url()), ) ) hgargssize = None for header, value in sorted(req.header_items()): header = pycompat.bytesurl(header) value = pycompat.bytesurl(value) if header.startswith(b'X-hgarg-'): if hgargssize is None: hgargssize = 0 hgargssize += len(value) else: dbg(line % b' %s %s' % (header, value)) if hgargssize is not None: dbg( line % b' %d bytes of commands arguments in headers' % hgargssize ) data = _reqdata(req) if data is not None: length = getattr(data, 'length', None) if length is None: length = len(data) dbg(line % b' %d bytes of data' % length) start = util.timer() res = None try: res = opener.open(req) except urlerr.httperror as inst: if inst.code == 401: raise error.Abort(_(b'authorization failed')) raise except httplib.HTTPException as inst: ui.debug( b'http error requesting %s\n' % util.hidepassword(req.get_full_url()) ) ui.traceback() raise IOError(None, inst) finally: if ui.debugflag and ui.configbool(b'devel', b'debug.peer-request'): code = res.code if res else -1 dbg( line % b' finished in %.4f seconds (%d)' % (util.timer() - start, code) ) # Insert error handlers for common I/O failures. urlmod.wrapresponse(res) return res class RedirectedRepoError(error.RepoError): def __init__(self, msg, respurl): super(RedirectedRepoError, self).__init__(msg) self.respurl = respurl def parsev1commandresponse( ui, baseurl, requrl, qs, resp, compressible, allowcbor=False ): # record the url we got redirected to redirected = False respurl = pycompat.bytesurl(resp.geturl()) if respurl.endswith(qs): respurl = respurl[: -len(qs)] qsdropped = False else: qsdropped = True if baseurl.rstrip(b'/') != respurl.rstrip(b'/'): redirected = True if not ui.quiet: ui.warn(_(b'real URL is %s\n') % respurl) try: proto = pycompat.bytesurl(resp.getheader('content-type', '')) except AttributeError: proto = pycompat.bytesurl(resp.headers.get('content-type', '')) safeurl = util.hidepassword(baseurl) if proto.startswith(b'application/hg-error'): raise error.OutOfBandError(resp.read()) # Pre 1.0 versions of Mercurial used text/plain and # application/hg-changegroup. We don't support such old servers. if not proto.startswith(b'application/mercurial-'): ui.debug(b"requested URL: '%s'\n" % util.hidepassword(requrl)) msg = _( b"'%s' does not appear to be an hg repository:\n" b"---%%<--- (%s)\n%s\n---%%<---\n" ) % (safeurl, proto or b'no content-type', resp.read(1024)) # Some servers may strip the query string from the redirect. We # raise a special error type so callers can react to this specially. if redirected and qsdropped: raise RedirectedRepoError(msg, respurl) else: raise error.RepoError(msg) try: subtype = proto.split(b'-', 1)[1] # Unless we end up supporting CBOR in the legacy wire protocol, # this should ONLY be encountered for the initial capabilities # request during handshake. if subtype == b'cbor': if allowcbor: return respurl, proto, resp else: raise error.RepoError( _(b'unexpected CBOR response from server') ) version_info = tuple([int(n) for n in subtype.split(b'.')]) except ValueError: raise error.RepoError( _(b"'%s' sent a broken Content-Type header (%s)") % (safeurl, proto) ) # TODO consider switching to a decompression reader that uses # generators. if version_info == (0, 1): if compressible: resp = util.compengines[b'zlib'].decompressorreader(resp) elif version_info == (0, 2): # application/mercurial-0.2 always identifies the compression # engine in the payload header. elen = struct.unpack(b'B', util.readexactly(resp, 1))[0] ename = util.readexactly(resp, elen) engine = util.compengines.forwiretype(ename) resp = engine.decompressorreader(resp) else: raise error.RepoError( _(b"'%s' uses newer protocol %s") % (safeurl, subtype) ) return respurl, proto, resp class httppeer(wireprotov1peer.wirepeer): def __init__(self, ui, path, url, opener, requestbuilder, caps): self.ui = ui self._path = path self._url = url self._caps = caps self.limitedarguments = caps is not None and b'httppostargs' not in caps self._urlopener = opener self._requestbuilder = requestbuilder def __del__(self): for h in self._urlopener.handlers: h.close() getattr(h, "close_all", lambda: None)() # Begin of ipeerconnection interface. def url(self): return self._path def local(self): return None def peer(self): return self def canpush(self): return True def close(self): try: reqs, sent, recv = ( self._urlopener.requestscount, self._urlopener.sentbytescount, self._urlopener.receivedbytescount, ) except AttributeError: return self.ui.note( _( b'(sent %d HTTP requests and %d bytes; ' b'received %d bytes in responses)\n' ) % (reqs, sent, recv) ) # End of ipeerconnection interface. # Begin of ipeercommands interface. def capabilities(self): return self._caps # End of ipeercommands interface. def _callstream(self, cmd, _compressible=False, **args): args = pycompat.byteskwargs(args) req, cu, qs = makev1commandrequest( self.ui, self._requestbuilder, self._caps, self.capable, self._url, cmd, args, ) resp = sendrequest(self.ui, self._urlopener, req) self._url, ct, resp = parsev1commandresponse( self.ui, self._url, cu, qs, resp, _compressible ) return resp def _call(self, cmd, **args): fp = self._callstream(cmd, **args) try: return fp.read() finally: # if using keepalive, allow connection to be reused fp.close() def _callpush(self, cmd, cg, **args): # have to stream bundle to a temp file because we do not have # http 1.1 chunked transfer. types = self.capable(b'unbundle') try: types = types.split(b',') except AttributeError: # servers older than d1b16a746db6 will send 'unbundle' as a # boolean capability. They only support headerless/uncompressed # bundles. types = [b""] for x in types: if x in bundle2.bundletypes: type = x break tempname = bundle2.writebundle(self.ui, cg, None, type) fp = httpconnection.httpsendfile(self.ui, tempname, b"rb") headers = {'Content-Type': 'application/mercurial-0.1'} try: r = self._call(cmd, data=fp, headers=headers, **args) vals = r.split(b'\n', 1) if len(vals) < 2: raise error.ResponseError(_(b"unexpected response:"), r) return vals except urlerr.httperror: # Catch and re-raise these so we don't try and treat them # like generic socket errors. They lack any values in # .args on Python 3 which breaks our socket.error block. raise except socket.error as err: if err.args[0] in (errno.ECONNRESET, errno.EPIPE): raise error.Abort(_(b'push failed: %s') % err.args[1]) raise error.Abort(err.args[1]) finally: fp.close() os.unlink(tempname) def _calltwowaystream(self, cmd, fp, **args): filename = None try: # dump bundle to disk fd, filename = pycompat.mkstemp(prefix=b"hg-bundle-", suffix=b".hg") with os.fdopen(fd, "wb") as fh: d = fp.read(4096) while d: fh.write(d) d = fp.read(4096) # start http push with httpconnection.httpsendfile(self.ui, filename, b"rb") as fp_: headers = {'Content-Type': 'application/mercurial-0.1'} return self._callstream(cmd, data=fp_, headers=headers, **args) finally: if filename is not None: os.unlink(filename) def _callcompressable(self, cmd, **args): return self._callstream(cmd, _compressible=True, **args) def _abort(self, exception): raise exception def sendv2request( ui, opener, requestbuilder, apiurl, permission, requests, redirect ): wireprotoframing.populatestreamencoders() uiencoders = ui.configlist(b'experimental', b'httppeer.v2-encoder-order') if uiencoders: encoders = [] for encoder in uiencoders: if encoder not in wireprotoframing.STREAM_ENCODERS: ui.warn( _( b'wire protocol version 2 encoder referenced in ' b'config (%s) is not known; ignoring\n' ) % encoder ) else: encoders.append(encoder) else: encoders = wireprotoframing.STREAM_ENCODERS_ORDER reactor = wireprotoframing.clientreactor( ui, hasmultiplesend=False, buffersends=True, clientcontentencoders=encoders, ) handler = wireprotov2peer.clienthandler( ui, reactor, opener=opener, requestbuilder=requestbuilder ) url = b'%s/%s' % (apiurl, permission) if len(requests) > 1: url += b'/multirequest' else: url += b'/%s' % requests[0][0] ui.debug(b'sending %d commands\n' % len(requests)) for command, args, f in requests: ui.debug( b'sending command %s: %s\n' % (command, stringutil.pprint(args, indent=2)) ) assert not list( handler.callcommand(command, args, f, redirect=redirect) ) # TODO stream this. body = b''.join(map(bytes, handler.flushcommands())) # TODO modify user-agent to reflect v2 headers = { 'Accept': wireprotov2server.FRAMINGTYPE, 'Content-Type': wireprotov2server.FRAMINGTYPE, } req = requestbuilder(pycompat.strurl(url), body, headers) req.add_unredirected_header('Content-Length', '%d' % len(body)) try: res = opener.open(req) except urlerr.httperror as e: if e.code == 401: raise error.Abort(_(b'authorization failed')) raise except httplib.HTTPException as e: ui.traceback() raise IOError(None, e) return handler, res class queuedcommandfuture(pycompat.futures.Future): """Wraps result() on command futures to trigger submission on call.""" def result(self, timeout=None): if self.done(): return pycompat.futures.Future.result(self, timeout) self._peerexecutor.sendcommands() # sendcommands() will restore the original __class__ and self.result # will resolve to Future.result. return self.result(timeout) @interfaceutil.implementer(repository.ipeercommandexecutor) class httpv2executor(object): def __init__( self, ui, opener, requestbuilder, apiurl, descriptor, redirect ): self._ui = ui self._opener = opener self._requestbuilder = requestbuilder self._apiurl = apiurl self._descriptor = descriptor self._redirect = redirect self._sent = False self._closed = False self._neededpermissions = set() self._calls = [] self._futures = weakref.WeakSet() self._responseexecutor = None self._responsef = None def __enter__(self): return self def __exit__(self, exctype, excvalue, exctb): self.close() def callcommand(self, command, args): if self._sent: raise error.ProgrammingError( b'callcommand() cannot be used after commands are sent' ) if self._closed: raise error.ProgrammingError( b'callcommand() cannot be used after close()' ) # The service advertises which commands are available. So if we attempt # to call an unknown command or pass an unknown argument, we can screen # for this. if command not in self._descriptor[b'commands']: raise error.ProgrammingError( b'wire protocol command %s is not available' % command ) cmdinfo = self._descriptor[b'commands'][command] unknownargs = set(args.keys()) - set(cmdinfo.get(b'args', {})) if unknownargs: raise error.ProgrammingError( b'wire protocol command %s does not accept argument: %s' % (command, b', '.join(sorted(unknownargs))) ) self._neededpermissions |= set(cmdinfo[b'permissions']) # TODO we /could/ also validate types here, since the API descriptor # includes types... f = pycompat.futures.Future() # Monkeypatch it so result() triggers sendcommands(), otherwise result() # could deadlock. f.__class__ = queuedcommandfuture f._peerexecutor = self self._futures.add(f) self._calls.append((command, args, f)) return f def sendcommands(self): if self._sent: return if not self._calls: return self._sent = True # Unhack any future types so caller sees a clean type and so we # break reference cycle. for f in self._futures: if isinstance(f, queuedcommandfuture): f.__class__ = pycompat.futures.Future f._peerexecutor = None # Mark the future as running and filter out cancelled futures. calls = [ (command, args, f) for command, args, f in self._calls if f.set_running_or_notify_cancel() ] # Clear out references, prevent improper object usage. self._calls = None if not calls: return permissions = set(self._neededpermissions) if b'push' in permissions and b'pull' in permissions: permissions.remove(b'pull') if len(permissions) > 1: raise error.RepoError( _(b'cannot make request requiring multiple permissions: %s') % _(b', ').join(sorted(permissions)) ) permission = {b'push': b'rw', b'pull': b'ro',}[permissions.pop()] handler, resp = sendv2request( self._ui, self._opener, self._requestbuilder, self._apiurl, permission, calls, self._redirect, ) # TODO we probably want to validate the HTTP code, media type, etc. self._responseexecutor = pycompat.futures.ThreadPoolExecutor(1) self._responsef = self._responseexecutor.submit( self._handleresponse, handler, resp ) def close(self): if self._closed: return self.sendcommands() self._closed = True if not self._responsef: return # TODO ^C here may not result in immediate program termination. try: self._responsef.result() finally: self._responseexecutor.shutdown(wait=True) self._responsef = None self._responseexecutor = None # If any of our futures are still in progress, mark them as # errored, otherwise a result() could wait indefinitely. for f in self._futures: if not f.done(): f.set_exception( error.ResponseError(_(b'unfulfilled command response')) ) self._futures = None def _handleresponse(self, handler, resp): # Called in a thread to read the response. while handler.readdata(resp): pass @interfaceutil.implementer(repository.ipeerv2) class httpv2peer(object): limitedarguments = False def __init__( self, ui, repourl, apipath, opener, requestbuilder, apidescriptor ): self.ui = ui self.apidescriptor = apidescriptor if repourl.endswith(b'/'): repourl = repourl[:-1] self._url = repourl self._apipath = apipath self._apiurl = b'%s/%s' % (repourl, apipath) self._opener = opener self._requestbuilder = requestbuilder self._redirect = wireprotov2peer.supportedredirects(ui, apidescriptor) # Start of ipeerconnection. def url(self): return self._url def local(self): return None def peer(self): return self def canpush(self): # TODO change once implemented. return False def close(self): self.ui.note( _( b'(sent %d HTTP requests and %d bytes; ' b'received %d bytes in responses)\n' ) % ( self._opener.requestscount, self._opener.sentbytescount, self._opener.receivedbytescount, ) ) # End of ipeerconnection. # Start of ipeercapabilities. def capable(self, name): # The capabilities used internally historically map to capabilities # advertised from the "capabilities" wire protocol command. However, # version 2 of that command works differently. # Maps to commands that are available. if name in ( b'branchmap', b'getbundle', b'known', b'lookup', b'pushkey', ): return True # Other concepts. if name in b'bundle2': return True # Alias command-* to presence of command of that name. if name.startswith(b'command-'): return name[len(b'command-') :] in self.apidescriptor[b'commands'] return False def requirecap(self, name, purpose): if self.capable(name): return raise error.CapabilityError( _( b'cannot %s; client or remote repository does not support the ' b'\'%s\' capability' ) % (purpose, name) ) # End of ipeercapabilities. def _call(self, name, **args): with self.commandexecutor() as e: return e.callcommand(name, args).result() def commandexecutor(self): return httpv2executor( self.ui, self._opener, self._requestbuilder, self._apiurl, self.apidescriptor, self._redirect, ) # Registry of API service names to metadata about peers that handle it. # # The following keys are meaningful: # # init # Callable receiving (ui, repourl, servicepath, opener, requestbuilder, # apidescriptor) to create a peer. # # priority # Integer priority for the service. If we could choose from multiple # services, we choose the one with the highest priority. API_PEERS = { wireprototypes.HTTP_WIREPROTO_V2: {b'init': httpv2peer, b'priority': 50,}, } def performhandshake(ui, url, opener, requestbuilder): # The handshake is a request to the capabilities command. caps = None def capable(x): raise error.ProgrammingError(b'should not be called') args = {} # The client advertises support for newer protocols by adding an # X-HgUpgrade-* header with a list of supported APIs and an # X-HgProto-* header advertising which serializing formats it supports. # We only support the HTTP version 2 transport and CBOR responses for # now. advertisev2 = ui.configbool(b'experimental', b'httppeer.advertise-v2') if advertisev2: args[b'headers'] = { 'X-HgProto-1': 'cbor', } args[b'headers'].update( encodevalueinheaders( b' '.join(sorted(API_PEERS)), b'X-HgUpgrade', # We don't know the header limit this early. # So make it small. 1024, ) ) req, requrl, qs = makev1commandrequest( ui, requestbuilder, caps, capable, url, b'capabilities', args ) resp = sendrequest(ui, opener, req) # The server may redirect us to the repo root, stripping the # ?cmd=capabilities query string from the URL. The server would likely # return HTML in this case and ``parsev1commandresponse()`` would raise. # We catch this special case and re-issue the capabilities request against # the new URL. # # We should ideally not do this, as a redirect that drops the query # string from the URL is arguably a server bug. (Garbage in, garbage out). # However, Mercurial clients for several years appeared to handle this # issue without behavior degradation. And according to issue 5860, it may # be a longstanding bug in some server implementations. So we allow a # redirect that drops the query string to "just work." try: respurl, ct, resp = parsev1commandresponse( ui, url, requrl, qs, resp, compressible=False, allowcbor=advertisev2 ) except RedirectedRepoError as e: req, requrl, qs = makev1commandrequest( ui, requestbuilder, caps, capable, e.respurl, b'capabilities', args ) resp = sendrequest(ui, opener, req) respurl, ct, resp = parsev1commandresponse( ui, url, requrl, qs, resp, compressible=False, allowcbor=advertisev2 ) try: rawdata = resp.read() finally: resp.close() if not ct.startswith(b'application/mercurial-'): raise error.ProgrammingError(b'unexpected content-type: %s' % ct) if advertisev2: if ct == b'application/mercurial-cbor': try: info = cborutil.decodeall(rawdata)[0] except cborutil.CBORDecodeError: raise error.Abort( _(b'error decoding CBOR from remote server'), hint=_( b'try again and consider contacting ' b'the server operator' ), ) # We got a legacy response. That's fine. elif ct in (b'application/mercurial-0.1', b'application/mercurial-0.2'): info = {b'v1capabilities': set(rawdata.split())} else: raise error.RepoError( _(b'unexpected response type from server: %s') % ct ) else: info = {b'v1capabilities': set(rawdata.split())} return respurl, info def makepeer(ui, path, opener=None, requestbuilder=urlreq.request): """Construct an appropriate HTTP peer instance. ``opener`` is an ``url.opener`` that should be used to establish connections, perform HTTP requests. ``requestbuilder`` is the type used for constructing HTTP requests. It exists as an argument so extensions can override the default. """ u = util.url(path) if u.query or u.fragment: raise error.Abort( _(b'unsupported URL component: "%s"') % (u.query or u.fragment) ) # urllib cannot handle URLs with embedded user or passwd. url, authinfo = u.authinfo() ui.debug(b'using %s\n' % url) opener = opener or urlmod.opener(ui, authinfo) respurl, info = performhandshake(ui, url, opener, requestbuilder) # Given the intersection of APIs that both we and the server support, # sort by their advertised priority and pick the first one. # # TODO consider making this request-based and interface driven. For # example, the caller could say "I want a peer that does X." It's quite # possible that not all peers would do that. Since we know the service # capabilities, we could filter out services not meeting the # requirements. Possibly by consulting the interfaces defined by the # peer type. apipeerchoices = set(info.get(b'apis', {}).keys()) & set(API_PEERS.keys()) preferredchoices = sorted( apipeerchoices, key=lambda x: API_PEERS[x][b'priority'], reverse=True ) for service in preferredchoices: apipath = b'%s/%s' % (info[b'apibase'].rstrip(b'/'), service) return API_PEERS[service][b'init']( ui, respurl, apipath, opener, requestbuilder, info[b'apis'][service] ) # Failed to construct an API peer. Fall back to legacy. return httppeer( ui, path, respurl, opener, requestbuilder, info[b'v1capabilities'] ) def instance(ui, path, create, intents=None, createopts=None): if create: raise error.Abort(_(b'cannot create new http repository')) try: if path.startswith(b'https:') and not urlmod.has_https: raise error.Abort( _(b'Python support for SSL and HTTPS is not installed') ) inst = makepeer(ui, path) return inst except error.RepoError as httpexception: try: r = statichttprepo.instance(ui, b"static-" + path, create) ui.note(_(b'(falling back to static-http)\n')) return r except error.RepoError: raise httpexception # use the original http RepoError instead