2013-06-21 21:14:29 +04:00
|
|
|
# fileserverclient.py - client for communicating with the cache process
|
2013-05-07 03:44:04 +04:00
|
|
|
#
|
|
|
|
# Copyright 2013 Facebook, Inc.
|
|
|
|
#
|
|
|
|
# This software may be used and distributed according to the terms of the
|
|
|
|
# GNU General Public License version 2 or any later version.
|
|
|
|
|
2017-08-22 01:10:41 +03:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2017-03-18 00:02:26 +03:00
|
|
|
import hashlib, os, time, io, struct
|
2016-03-22 20:06:24 +03:00
|
|
|
import itertools
|
2013-05-07 03:59:05 +04:00
|
|
|
|
2017-08-22 01:10:41 +03:00
|
|
|
from mercurial.i18n import _
|
|
|
|
from mercurial.node import hex, bin, nullid
|
|
|
|
from mercurial import (
|
|
|
|
error,
|
|
|
|
httppeer,
|
2017-09-21 00:02:58 +03:00
|
|
|
revlog,
|
2017-08-22 01:10:41 +03:00
|
|
|
sshpeer,
|
|
|
|
util,
|
|
|
|
wireproto,
|
|
|
|
)
|
|
|
|
|
2017-06-20 21:08:15 +03:00
|
|
|
from . import (
|
|
|
|
connectionpool,
|
|
|
|
constants,
|
|
|
|
shallowutil,
|
|
|
|
wirepack,
|
|
|
|
)
|
2017-08-07 22:33:30 +03:00
|
|
|
from .contentstore import unioncontentstore
|
|
|
|
from .metadatastore import unionmetadatastore
|
2017-08-22 01:10:41 +03:00
|
|
|
from .lz4wrapper import lz4decompress
|
2016-05-16 20:59:09 +03:00
|
|
|
|
2013-05-07 03:59:05 +04:00
|
|
|
# Statistics for debugging
|
|
|
|
fetchcost = 0
|
|
|
|
fetches = 0
|
|
|
|
fetched = 0
|
2013-07-02 04:37:55 +04:00
|
|
|
fetchmisses = 0
|
2013-05-07 03:44:04 +04:00
|
|
|
|
2017-09-21 00:02:58 +03:00
|
|
|
_lfsmod = None
|
2013-05-07 03:44:04 +04:00
|
|
|
_downloading = _('downloading')
|
|
|
|
|
2013-08-15 22:00:51 +04:00
|
|
|
def getcachekey(reponame, file, id):
|
2016-06-16 01:48:16 +03:00
|
|
|
pathhash = hashlib.sha1(file).hexdigest()
|
2013-08-15 22:00:51 +04:00
|
|
|
return os.path.join(reponame, pathhash[:2], pathhash[2:], id)
|
2013-09-07 00:28:15 +04:00
|
|
|
|
|
|
|
def getlocalkey(file, id):
|
2016-06-16 01:48:16 +03:00
|
|
|
pathhash = hashlib.sha1(file).hexdigest()
|
2013-05-18 05:08:53 +04:00
|
|
|
return os.path.join(pathhash, id)
|
|
|
|
|
2015-07-01 00:32:31 +03:00
|
|
|
def peersetup(ui, peer):
|
|
|
|
class remotefilepeer(peer.__class__):
|
2015-07-01 00:34:01 +03:00
|
|
|
@wireproto.batchable
|
2015-07-01 00:32:31 +03:00
|
|
|
def getfile(self, file, node):
|
2015-07-01 00:34:01 +03:00
|
|
|
if not self.capable('getfile'):
|
2016-04-26 23:00:31 +03:00
|
|
|
raise error.Abort(
|
2015-07-01 00:34:01 +03:00
|
|
|
'configured remotefile server does not support getfile')
|
|
|
|
f = wireproto.future()
|
|
|
|
yield {'file': file, 'node': node}, f
|
2015-08-04 21:59:53 +03:00
|
|
|
code, data = f.value.split('\0', 1)
|
|
|
|
if int(code):
|
2015-08-18 22:35:21 +03:00
|
|
|
raise error.LookupError(file, node, data)
|
2015-08-04 21:59:53 +03:00
|
|
|
yield data
|
2016-07-22 23:47:02 +03:00
|
|
|
|
2016-08-02 20:40:42 +03:00
|
|
|
@wireproto.batchable
|
|
|
|
def getflogheads(self, path):
|
|
|
|
if not self.capable('getflogheads'):
|
|
|
|
raise error.Abort('configured remotefile server does not '
|
|
|
|
'support getflogheads')
|
|
|
|
f = wireproto.future()
|
|
|
|
yield {'path': path}, f
|
2016-08-02 20:41:41 +03:00
|
|
|
heads = f.value.split('\n') if f.value else []
|
2016-08-02 20:40:42 +03:00
|
|
|
yield heads
|
|
|
|
|
fix crash in "hg pull" when remotefilelog not enabled
Summary:
764cd9916c94 recently introduced code that was unconditionally checking the
repo.includepattern and repo.excludepattern attributes on a local repository
without first checking if this is a shallow repository. These attributes only
exist on shallow repositories, causing "hg pull" to crash on non-shallow
repositories. This crash wouldn't happen in simple circumstances, since the
remotefilelog extension only gets fully set up once a shallow repository object
has been created, however when using chg you can end up with scenarios where a
non-shallow repository is used in the same hg process after a shallow one.
This refactors the code to now store the local repository object on the remote
peer rather than trying to store the individual shallow, includepattern, and
excludepattern attributes.
Overall this code does still feel a bit janky to me -- the rest of the peer API
is independent of the local repository, but the _callstream() wrapper cares
about the local repository being referenced. It seems like we should ideally
redesign the APIs so that _callstream() receives the local repository data as
an argument (or we should make the peer <--> local repository assocation more
formal and explicit if think it's better to force an association here).
Test Plan: Added a new test which triggered the crash, but passes with these changes.
Reviewers: ttung, mitrandir, durham
Reviewed By: durham
Subscribers: net-systems-diffs@, yogeshwer
Differential Revision: https://phabricator.intern.facebook.com/D3756493
Tasks: 12823586
Signature: t1:3756493:1471971600:9666e9c31bf59070c3ace0821d47d322671eb5b1
2016-08-24 00:14:42 +03:00
|
|
|
def _updatecallstreamopts(self, command, opts):
|
|
|
|
if command != 'getbundle':
|
|
|
|
return
|
2017-08-22 01:10:41 +03:00
|
|
|
if 'remotefilelog' not in shallowutil.peercapabilities(self):
|
fix crash in "hg pull" when remotefilelog not enabled
Summary:
764cd9916c94 recently introduced code that was unconditionally checking the
repo.includepattern and repo.excludepattern attributes on a local repository
without first checking if this is a shallow repository. These attributes only
exist on shallow repositories, causing "hg pull" to crash on non-shallow
repositories. This crash wouldn't happen in simple circumstances, since the
remotefilelog extension only gets fully set up once a shallow repository object
has been created, however when using chg you can end up with scenarios where a
non-shallow repository is used in the same hg process after a shallow one.
This refactors the code to now store the local repository object on the remote
peer rather than trying to store the individual shallow, includepattern, and
excludepattern attributes.
Overall this code does still feel a bit janky to me -- the rest of the peer API
is independent of the local repository, but the _callstream() wrapper cares
about the local repository being referenced. It seems like we should ideally
redesign the APIs so that _callstream() receives the local repository data as
an argument (or we should make the peer <--> local repository assocation more
formal and explicit if think it's better to force an association here).
Test Plan: Added a new test which triggered the crash, but passes with these changes.
Reviewers: ttung, mitrandir, durham
Reviewed By: durham
Subscribers: net-systems-diffs@, yogeshwer
Differential Revision: https://phabricator.intern.facebook.com/D3756493
Tasks: 12823586
Signature: t1:3756493:1471971600:9666e9c31bf59070c3ace0821d47d322671eb5b1
2016-08-24 00:14:42 +03:00
|
|
|
return
|
|
|
|
if not util.safehasattr(self, '_localrepo'):
|
|
|
|
return
|
2016-10-21 21:02:09 +03:00
|
|
|
if constants.REQUIREMENT not in self._localrepo.requirements:
|
fix crash in "hg pull" when remotefilelog not enabled
Summary:
764cd9916c94 recently introduced code that was unconditionally checking the
repo.includepattern and repo.excludepattern attributes on a local repository
without first checking if this is a shallow repository. These attributes only
exist on shallow repositories, causing "hg pull" to crash on non-shallow
repositories. This crash wouldn't happen in simple circumstances, since the
remotefilelog extension only gets fully set up once a shallow repository object
has been created, however when using chg you can end up with scenarios where a
non-shallow repository is used in the same hg process after a shallow one.
This refactors the code to now store the local repository object on the remote
peer rather than trying to store the individual shallow, includepattern, and
excludepattern attributes.
Overall this code does still feel a bit janky to me -- the rest of the peer API
is independent of the local repository, but the _callstream() wrapper cares
about the local repository being referenced. It seems like we should ideally
redesign the APIs so that _callstream() receives the local repository data as
an argument (or we should make the peer <--> local repository assocation more
formal and explicit if think it's better to force an association here).
Test Plan: Added a new test which triggered the crash, but passes with these changes.
Reviewers: ttung, mitrandir, durham
Reviewed By: durham
Subscribers: net-systems-diffs@, yogeshwer
Differential Revision: https://phabricator.intern.facebook.com/D3756493
Tasks: 12823586
Signature: t1:3756493:1471971600:9666e9c31bf59070c3ace0821d47d322671eb5b1
2016-08-24 00:14:42 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
bundlecaps = opts.get('bundlecaps')
|
|
|
|
if bundlecaps:
|
|
|
|
bundlecaps = [bundlecaps]
|
|
|
|
else:
|
|
|
|
bundlecaps = []
|
|
|
|
|
|
|
|
# shallow, includepattern, and excludepattern are a hacky way of
|
|
|
|
# carrying over data from the local repo to this getbundle
|
|
|
|
# command. We need to do it this way because bundle1 getbundle
|
|
|
|
# doesn't provide any other place we can hook in to manipulate
|
|
|
|
# getbundle args before it goes across the wire. Once we get rid
|
|
|
|
# of bundle1, we can use bundle2's _pullbundle2extraprepare to
|
|
|
|
# do this more cleanly.
|
|
|
|
bundlecaps.append('remotefilelog')
|
|
|
|
if self._localrepo.includepattern:
|
|
|
|
patterns = '\0'.join(self._localrepo.includepattern)
|
|
|
|
includecap = "includepattern=" + patterns
|
|
|
|
bundlecaps.append(includecap)
|
|
|
|
if self._localrepo.excludepattern:
|
|
|
|
patterns = '\0'.join(self._localrepo.excludepattern)
|
|
|
|
excludecap = "excludepattern=" + patterns
|
|
|
|
bundlecaps.append(excludecap)
|
|
|
|
opts['bundlecaps'] = ','.join(bundlecaps)
|
|
|
|
|
2016-07-22 23:47:02 +03:00
|
|
|
def _callstream(self, command, **opts):
|
fix crash in "hg pull" when remotefilelog not enabled
Summary:
764cd9916c94 recently introduced code that was unconditionally checking the
repo.includepattern and repo.excludepattern attributes on a local repository
without first checking if this is a shallow repository. These attributes only
exist on shallow repositories, causing "hg pull" to crash on non-shallow
repositories. This crash wouldn't happen in simple circumstances, since the
remotefilelog extension only gets fully set up once a shallow repository object
has been created, however when using chg you can end up with scenarios where a
non-shallow repository is used in the same hg process after a shallow one.
This refactors the code to now store the local repository object on the remote
peer rather than trying to store the individual shallow, includepattern, and
excludepattern attributes.
Overall this code does still feel a bit janky to me -- the rest of the peer API
is independent of the local repository, but the _callstream() wrapper cares
about the local repository being referenced. It seems like we should ideally
redesign the APIs so that _callstream() receives the local repository data as
an argument (or we should make the peer <--> local repository assocation more
formal and explicit if think it's better to force an association here).
Test Plan: Added a new test which triggered the crash, but passes with these changes.
Reviewers: ttung, mitrandir, durham
Reviewed By: durham
Subscribers: net-systems-diffs@, yogeshwer
Differential Revision: https://phabricator.intern.facebook.com/D3756493
Tasks: 12823586
Signature: t1:3756493:1471971600:9666e9c31bf59070c3ace0821d47d322671eb5b1
2016-08-24 00:14:42 +03:00
|
|
|
self._updatecallstreamopts(command, opts)
|
2016-07-22 23:47:02 +03:00
|
|
|
return super(remotefilepeer, self)._callstream(command, **opts)
|
|
|
|
|
2015-07-01 00:32:31 +03:00
|
|
|
peer.__class__ = remotefilepeer
|
|
|
|
|
2014-01-09 23:41:12 +04:00
|
|
|
class cacheconnection(object):
|
|
|
|
"""The connection for communicating with the remote cache. Performs
|
|
|
|
gets and sets by communicating with an external process that has the
|
|
|
|
cache-specific implementation.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self.pipeo = self.pipei = self.pipee = None
|
|
|
|
self.subprocess = None
|
|
|
|
self.connected = False
|
|
|
|
|
|
|
|
def connect(self, cachecommand):
|
|
|
|
if self.pipeo:
|
2016-04-26 23:00:31 +03:00
|
|
|
raise error.Abort(_("cache connection already open"))
|
2014-01-09 23:41:12 +04:00
|
|
|
self.pipei, self.pipeo, self.pipee, self.subprocess = \
|
|
|
|
util.popen4(cachecommand)
|
|
|
|
self.connected = True
|
|
|
|
|
|
|
|
def close(self):
|
2015-10-15 04:49:55 +03:00
|
|
|
def tryclose(pipe):
|
2015-10-14 18:12:48 +03:00
|
|
|
try:
|
2015-10-15 04:49:55 +03:00
|
|
|
pipe.close()
|
2016-04-26 23:00:31 +03:00
|
|
|
except Exception:
|
2015-10-14 18:12:48 +03:00
|
|
|
pass
|
2015-10-15 04:49:55 +03:00
|
|
|
if self.connected:
|
2015-10-14 18:12:48 +03:00
|
|
|
try:
|
2015-10-15 04:49:55 +03:00
|
|
|
self.pipei.write("exit\n")
|
2016-04-26 23:00:31 +03:00
|
|
|
except Exception:
|
2015-10-15 04:49:55 +03:00
|
|
|
pass
|
|
|
|
tryclose(self.pipei)
|
|
|
|
self.pipei = None
|
|
|
|
tryclose(self.pipeo)
|
|
|
|
self.pipeo = None
|
|
|
|
tryclose(self.pipee)
|
|
|
|
self.pipee = None
|
2015-10-14 18:12:48 +03:00
|
|
|
try:
|
2015-10-15 04:49:55 +03:00
|
|
|
# Wait for process to terminate, making sure to avoid deadlock.
|
|
|
|
# See https://docs.python.org/2/library/subprocess.html for
|
|
|
|
# warnings about wait() and deadlocking.
|
|
|
|
self.subprocess.communicate()
|
2016-04-26 23:00:31 +03:00
|
|
|
except Exception:
|
2015-10-15 04:49:55 +03:00
|
|
|
pass
|
2014-01-09 23:41:12 +04:00
|
|
|
self.subprocess = None
|
2015-10-15 04:49:55 +03:00
|
|
|
self.connected = False
|
2014-01-09 23:41:12 +04:00
|
|
|
|
|
|
|
def request(self, request, flush=True):
|
|
|
|
if self.connected:
|
|
|
|
try:
|
|
|
|
self.pipei.write(request)
|
|
|
|
if flush:
|
|
|
|
self.pipei.flush()
|
|
|
|
except IOError:
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def receiveline(self):
|
|
|
|
if not self.connected:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
result = self.pipeo.readline()[:-1]
|
|
|
|
if not result:
|
|
|
|
self.close()
|
|
|
|
except IOError:
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2015-08-18 22:14:01 +03:00
|
|
|
def _getfilesbatch(
|
|
|
|
remote, receivemissing, progresstick, missed, idmap, batchsize):
|
2016-03-22 20:06:24 +03:00
|
|
|
# Over http(s), iterbatch is a streamy method and we can start
|
|
|
|
# looking at results early. This means we send one (potentially
|
|
|
|
# large) request, but then we show nice progress as we process
|
|
|
|
# file results, rather than showing chunks of $batchsize in
|
|
|
|
# progress.
|
|
|
|
#
|
|
|
|
# Over ssh, iterbatch isn't streamy because batch() wasn't
|
|
|
|
# explicitly designed as a streaming method. In the future we
|
|
|
|
# should probably introduce a streambatch() method upstream and
|
|
|
|
# use that for this.
|
|
|
|
if (getattr(remote, 'iterbatch', False) and remote.capable('httppostargs')
|
|
|
|
and isinstance(remote, httppeer.httppeer)):
|
|
|
|
b = remote.iterbatch()
|
|
|
|
for m in missed:
|
|
|
|
file_ = idmap[m]
|
|
|
|
node = m[-40:]
|
|
|
|
b.getfile(file_, node)
|
|
|
|
b.submit()
|
|
|
|
for m, r in itertools.izip(missed, b.results()):
|
2016-05-19 08:39:20 +03:00
|
|
|
file_ = idmap[m]
|
|
|
|
node = m[-40:]
|
|
|
|
receivemissing(io.BytesIO('%d\n%s' % (len(r), r)), file_, node)
|
2016-03-22 20:06:24 +03:00
|
|
|
progresstick()
|
|
|
|
return
|
2015-08-18 22:14:01 +03:00
|
|
|
while missed:
|
|
|
|
chunk, missed = missed[:batchsize], missed[batchsize:]
|
2017-08-22 01:10:41 +03:00
|
|
|
b = remote.iterbatch()
|
2015-08-18 22:14:01 +03:00
|
|
|
for m in chunk:
|
|
|
|
file_ = idmap[m]
|
|
|
|
node = m[-40:]
|
2017-08-22 01:10:41 +03:00
|
|
|
b.getfile(file_, node)
|
2015-08-18 22:14:01 +03:00
|
|
|
b.submit()
|
2017-08-22 01:10:41 +03:00
|
|
|
for m, v in zip(chunk, b.results()):
|
2016-04-05 02:26:12 +03:00
|
|
|
file_ = idmap[m]
|
|
|
|
node = m[-40:]
|
|
|
|
receivemissing(io.BytesIO('%d\n%s' % (len(v), v)), file_, node)
|
2015-08-18 22:14:01 +03:00
|
|
|
progresstick()
|
2015-07-01 00:32:31 +03:00
|
|
|
|
2015-06-30 23:43:18 +03:00
|
|
|
def _getfiles(
|
2017-02-28 01:45:51 +03:00
|
|
|
remote, receivemissing, progresstick, missed, idmap, step):
|
2017-06-20 21:08:15 +03:00
|
|
|
remote._callstream("getfiles")
|
2015-06-30 23:43:18 +03:00
|
|
|
i = 0
|
2017-08-22 01:10:41 +03:00
|
|
|
pipeo = shallowutil.trygetattr(remote, ('_pipeo', 'pipeo'))
|
|
|
|
pipei = shallowutil.trygetattr(remote, ('_pipei', 'pipei'))
|
2015-06-30 23:43:18 +03:00
|
|
|
while i < len(missed):
|
|
|
|
# issue a batch of requests
|
|
|
|
start = i
|
2017-02-28 01:45:51 +03:00
|
|
|
end = min(len(missed), start + step)
|
2015-06-30 23:43:18 +03:00
|
|
|
i = end
|
2017-02-01 18:00:31 +03:00
|
|
|
for missingid in missed[start:end]:
|
|
|
|
# issue new request
|
|
|
|
versionid = missingid[-40:]
|
|
|
|
file = idmap[missingid]
|
|
|
|
sshrequest = "%s%s\n" % (versionid, file)
|
2017-08-22 01:10:41 +03:00
|
|
|
pipeo.write(sshrequest)
|
|
|
|
pipeo.flush()
|
2017-02-01 18:00:31 +03:00
|
|
|
|
|
|
|
# receive batch results
|
|
|
|
for missingid in missed[start:end]:
|
|
|
|
versionid = missingid[-40:]
|
|
|
|
file = idmap[missingid]
|
2017-08-22 01:10:41 +03:00
|
|
|
receivemissing(pipei, file, versionid)
|
2017-02-01 18:00:31 +03:00
|
|
|
progresstick()
|
2015-06-30 23:43:18 +03:00
|
|
|
|
2017-06-20 21:08:15 +03:00
|
|
|
# End the command
|
2017-08-22 01:10:41 +03:00
|
|
|
pipeo.write('\n')
|
|
|
|
pipeo.flush()
|
2017-06-20 21:08:15 +03:00
|
|
|
|
2013-05-07 03:44:04 +04:00
|
|
|
class fileserverclient(object):
|
|
|
|
"""A client for requesting files from the remote file server.
|
|
|
|
"""
|
2016-04-05 02:26:12 +03:00
|
|
|
def __init__(self, repo):
|
2014-02-12 02:41:56 +04:00
|
|
|
ui = repo.ui
|
|
|
|
self.repo = repo
|
2013-05-07 03:51:48 +04:00
|
|
|
self.ui = ui
|
2013-06-21 21:14:29 +04:00
|
|
|
self.cacheprocess = ui.config("remotefilelog", "cacheprocess")
|
2014-05-21 23:28:03 +04:00
|
|
|
if self.cacheprocess:
|
|
|
|
self.cacheprocess = util.expandpath(self.cacheprocess)
|
2016-01-28 00:22:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
# This option causes remotefilelog to pass the full file path to the
|
|
|
|
# cacheprocess instead of a hashed key.
|
|
|
|
self.cacheprocesspasspath = ui.configbool(
|
|
|
|
"remotefilelog", "cacheprocess.includepath")
|
|
|
|
|
2013-06-21 21:14:29 +04:00
|
|
|
self.debugoutput = ui.configbool("remotefilelog", "debug")
|
2013-05-07 03:44:04 +04:00
|
|
|
|
2014-01-09 23:41:12 +04:00
|
|
|
self.remotecache = cacheconnection()
|
2017-06-20 21:08:15 +03:00
|
|
|
self.connpool = connectionpool.connectionpool(repo)
|
2015-12-11 22:18:51 +03:00
|
|
|
|
2016-05-16 20:59:09 +03:00
|
|
|
def setstore(self, datastore, historystore, writedata, writehistory):
|
|
|
|
self.datastore = datastore
|
|
|
|
self.historystore = historystore
|
|
|
|
self.writedata = writedata
|
|
|
|
self.writehistory = writehistory
|
2016-04-05 02:26:12 +03:00
|
|
|
|
2015-12-11 22:18:51 +03:00
|
|
|
def _connect(self):
|
2017-06-20 21:08:15 +03:00
|
|
|
return self.connpool.get(self.repo.fallbackpath)
|
2013-06-08 02:13:58 +04:00
|
|
|
|
2014-02-12 02:41:56 +04:00
|
|
|
def request(self, fileids):
|
2013-05-07 03:44:04 +04:00
|
|
|
"""Takes a list of filename/node pairs and fetches them from the
|
2014-02-12 04:25:55 +04:00
|
|
|
server. Files are stored in the local cache.
|
2013-05-07 03:44:04 +04:00
|
|
|
A list of nodes that the server couldn't find is returned.
|
|
|
|
If the connection fails, an exception is raised.
|
|
|
|
"""
|
2014-01-09 23:41:12 +04:00
|
|
|
if not self.remotecache.connected:
|
2013-05-07 03:44:04 +04:00
|
|
|
self.connect()
|
2014-01-09 23:41:12 +04:00
|
|
|
cache = self.remotecache
|
2016-05-16 20:59:09 +03:00
|
|
|
writedata = self.writedata
|
2013-05-07 03:44:04 +04:00
|
|
|
|
2016-05-16 20:59:09 +03:00
|
|
|
if self.ui.configbool('remotefilelog', 'fetchpacks'):
|
|
|
|
self.requestpack(fileids)
|
|
|
|
return
|
|
|
|
|
2014-02-12 02:41:56 +04:00
|
|
|
repo = self.repo
|
2013-05-07 03:44:04 +04:00
|
|
|
count = len(fileids)
|
2013-06-08 02:13:58 +04:00
|
|
|
request = "get\n%d\n" % count
|
|
|
|
idmap = {}
|
2013-08-15 22:00:51 +04:00
|
|
|
reponame = repo.name
|
2013-05-07 03:44:04 +04:00
|
|
|
for file, id in fileids:
|
2013-08-15 22:00:51 +04:00
|
|
|
fullid = getcachekey(reponame, file, id)
|
2016-01-28 00:22:22 +03:00
|
|
|
if self.cacheprocesspasspath:
|
|
|
|
request += file + '\0'
|
2013-06-08 02:13:58 +04:00
|
|
|
request += fullid + "\n"
|
|
|
|
idmap[fullid] = file
|
2013-05-07 03:44:04 +04:00
|
|
|
|
2014-01-09 23:41:12 +04:00
|
|
|
cache.request(request)
|
2013-05-07 03:44:04 +04:00
|
|
|
|
2013-05-07 03:51:48 +04:00
|
|
|
total = count
|
|
|
|
self.ui.progress(_downloading, 0, total=count)
|
|
|
|
|
2013-06-08 02:13:58 +04:00
|
|
|
missed = []
|
|
|
|
count = 0
|
|
|
|
while True:
|
2014-01-09 23:41:12 +04:00
|
|
|
missingid = cache.receiveline()
|
2013-06-08 02:13:58 +04:00
|
|
|
if not missingid:
|
2014-01-09 23:41:12 +04:00
|
|
|
missedset = set(missed)
|
|
|
|
for missingid in idmap.iterkeys():
|
|
|
|
if not missingid in missedset:
|
|
|
|
missed.append(missingid)
|
|
|
|
self.ui.warn(_("warning: cache connection closed early - " +
|
|
|
|
"falling back to server\n"))
|
|
|
|
break
|
2013-06-25 22:38:48 +04:00
|
|
|
if missingid == "0":
|
2013-06-08 02:13:58 +04:00
|
|
|
break
|
|
|
|
if missingid.startswith("_hits_"):
|
2013-06-25 22:38:48 +04:00
|
|
|
# receive progress reports
|
2013-06-08 02:13:58 +04:00
|
|
|
parts = missingid.split("_")
|
|
|
|
count += int(parts[2])
|
|
|
|
self.ui.progress(_downloading, count, total=total)
|
|
|
|
continue
|
2013-05-07 03:44:04 +04:00
|
|
|
|
2013-06-08 02:13:58 +04:00
|
|
|
missed.append(missingid)
|
2013-05-07 03:44:04 +04:00
|
|
|
|
2013-12-12 01:39:53 +04:00
|
|
|
global fetchmisses
|
|
|
|
fetchmisses += len(missed)
|
|
|
|
|
2015-06-30 23:43:18 +03:00
|
|
|
count = [total - len(missed)]
|
2017-07-06 16:53:11 +03:00
|
|
|
fromcache = count[0]
|
2015-07-22 04:31:01 +03:00
|
|
|
self.ui.progress(_downloading, count[0], total=total)
|
2016-04-01 13:16:00 +03:00
|
|
|
self.ui.log("remotefilelog", "remote cache hit rate is %r of %r ",
|
|
|
|
count[0], total, hit=count[0], total=total)
|
2013-12-12 01:39:53 +04:00
|
|
|
|
|
|
|
oldumask = os.umask(0o002)
|
|
|
|
try:
|
|
|
|
# receive cache misses from master
|
|
|
|
if missed:
|
2015-06-30 23:43:18 +03:00
|
|
|
def progresstick():
|
|
|
|
count[0] += 1
|
|
|
|
self.ui.progress(_downloading, count[0], total=total)
|
|
|
|
# When verbose is true, sshpeer prints 'running ssh...'
|
|
|
|
# to stdout, which can interfere with some command
|
|
|
|
# outputs
|
2013-07-25 00:20:13 +04:00
|
|
|
verbose = self.ui.verbose
|
2015-06-30 23:43:18 +03:00
|
|
|
self.ui.verbose = False
|
2013-07-25 00:20:13 +04:00
|
|
|
try:
|
2017-06-20 21:08:15 +03:00
|
|
|
with self._connect() as conn:
|
|
|
|
remote = conn.peer
|
|
|
|
# TODO: deduplicate this with the constant in
|
|
|
|
# shallowrepo
|
|
|
|
if remote.capable("remotefilelog"):
|
|
|
|
if not isinstance(remote, sshpeer.sshpeer):
|
|
|
|
raise error.Abort('remotefilelog requires ssh '
|
|
|
|
'servers')
|
|
|
|
step = self.ui.configint('remotefilelog',
|
|
|
|
'getfilesstep', 10000)
|
|
|
|
_getfiles(remote, self.receivemissing, progresstick,
|
|
|
|
missed, idmap, step)
|
|
|
|
elif remote.capable("getfile"):
|
|
|
|
if remote.capable('batch'):
|
|
|
|
batchdefault = 100
|
|
|
|
else:
|
|
|
|
batchdefault = 10
|
|
|
|
batchsize = self.ui.configint(
|
|
|
|
'remotefilelog', 'batchsize', batchdefault)
|
|
|
|
_getfilesbatch(
|
|
|
|
remote, self.receivemissing, progresstick,
|
|
|
|
missed, idmap, batchsize)
|
|
|
|
else:
|
|
|
|
raise error.Abort("configured remotefilelog server"
|
|
|
|
" does not support remotefilelog")
|
2017-07-06 16:53:11 +03:00
|
|
|
|
|
|
|
self.ui.log("remotefilefetchlog",
|
|
|
|
"Success",
|
|
|
|
fetched_files = count[0] - fromcache,
|
|
|
|
total_to_fetch = total - fromcache)
|
2017-07-08 07:08:26 +03:00
|
|
|
except Exception:
|
2017-07-06 16:53:11 +03:00
|
|
|
self.ui.log("remotefilefetchlog",
|
|
|
|
"Fail",
|
|
|
|
fetched_files = count[0] - fromcache,
|
|
|
|
total_to_fetch = total - fromcache)
|
|
|
|
raise
|
2013-07-25 00:20:13 +04:00
|
|
|
finally:
|
|
|
|
self.ui.verbose = verbose
|
2013-06-29 04:12:20 +04:00
|
|
|
# send to memcache
|
2015-06-30 23:43:18 +03:00
|
|
|
count[0] = len(missed)
|
|
|
|
request = "set\n%d\n%s\n" % (count[0], "\n".join(missed))
|
2014-01-09 23:41:12 +04:00
|
|
|
cache.request(request)
|
2013-06-29 04:12:20 +04:00
|
|
|
|
|
|
|
self.ui.progress(_downloading, None)
|
|
|
|
|
|
|
|
# mark ourselves as a user of this cache
|
2016-05-16 20:59:09 +03:00
|
|
|
writedata.markrepo(self.repo.path)
|
2013-06-29 04:12:20 +04:00
|
|
|
finally:
|
|
|
|
os.umask(oldumask)
|
2013-06-29 02:57:15 +04:00
|
|
|
|
2016-04-05 02:26:12 +03:00
|
|
|
def receivemissing(self, pipe, filename, node):
|
2013-12-12 01:39:53 +04:00
|
|
|
line = pipe.readline()[:-1]
|
|
|
|
if not line:
|
2016-12-06 02:02:22 +03:00
|
|
|
raise error.ResponseError(_("error downloading file contents:"),
|
|
|
|
_("connection closed early"))
|
2013-12-12 01:39:53 +04:00
|
|
|
size = int(line)
|
|
|
|
data = pipe.read(size)
|
2016-04-05 19:50:12 +03:00
|
|
|
if len(data) != size:
|
2016-12-06 02:02:22 +03:00
|
|
|
raise error.ResponseError(_("error downloading file contents:"),
|
|
|
|
_("only received %s of %s bytes")
|
|
|
|
% (len(data), size))
|
2013-12-12 01:39:53 +04:00
|
|
|
|
2016-05-16 20:59:09 +03:00
|
|
|
self.writedata.addremotefilelognode(filename, bin(node),
|
2017-03-18 00:02:26 +03:00
|
|
|
lz4decompress(data))
|
2013-12-12 01:39:53 +04:00
|
|
|
|
2016-05-16 20:59:09 +03:00
|
|
|
def requestpack(self, fileids):
|
|
|
|
"""Requests the given file revisions from the server in a pack format.
|
|
|
|
|
|
|
|
See `remotefilelogserver.getpack` for the file format.
|
|
|
|
"""
|
2017-07-06 16:53:11 +03:00
|
|
|
try:
|
|
|
|
with self._connect() as conn:
|
|
|
|
total = len(fileids)
|
|
|
|
rcvd = 0
|
|
|
|
|
|
|
|
remote = conn.peer
|
|
|
|
remote._callstream("getpackv1")
|
|
|
|
|
|
|
|
self._sendpackrequest(remote, fileids)
|
|
|
|
|
2017-07-08 07:08:26 +03:00
|
|
|
packpath = shallowutil.getcachepackpath(
|
|
|
|
self.repo, constants.FILEPACK_CATEGORY)
|
2017-08-22 01:10:41 +03:00
|
|
|
pipei = shallowutil.trygetattr(remote, ('_pipei', 'pipei'))
|
2017-07-08 07:08:26 +03:00
|
|
|
receiveddata, receivedhistory = wirepack.receivepack(
|
2017-08-22 01:10:41 +03:00
|
|
|
self.repo.ui, pipei, packpath)
|
2017-07-06 16:53:11 +03:00
|
|
|
rcvd = len(receiveddata)
|
|
|
|
|
|
|
|
self.ui.log("remotefilefetchlog",
|
|
|
|
"Success(pack)" if (rcvd==total) else "Fail(pack)",
|
|
|
|
fetched_files = rcvd,
|
|
|
|
total_to_fetch = total)
|
2017-07-08 07:08:26 +03:00
|
|
|
except Exception:
|
2017-07-06 16:53:11 +03:00
|
|
|
self.ui.log("remotefilefetchlog",
|
|
|
|
"Fail(pack)",
|
|
|
|
fetched_files = rcvd,
|
|
|
|
total_to_fetch = total)
|
|
|
|
raise
|
2016-05-16 20:59:09 +03:00
|
|
|
|
2016-05-16 20:59:09 +03:00
|
|
|
def _sendpackrequest(self, remote, fileids):
|
|
|
|
"""Formats and writes the given fileids to the remote as part of a
|
|
|
|
getpackv1 call.
|
|
|
|
"""
|
|
|
|
# Sort the requests by name, so we receive requests in batches by name
|
|
|
|
grouped = {}
|
|
|
|
for filename, node in fileids:
|
|
|
|
grouped.setdefault(filename, set()).add(node)
|
|
|
|
|
|
|
|
# Issue request
|
2017-08-22 01:10:41 +03:00
|
|
|
pipeo = shallowutil.trygetattr(remote, ('_pipeo', 'pipeo'))
|
2016-05-16 20:59:09 +03:00
|
|
|
for filename, nodes in grouped.iteritems():
|
|
|
|
filenamelen = struct.pack(constants.FILENAMESTRUCT, len(filename))
|
|
|
|
countlen = struct.pack(constants.PACKREQUESTCOUNTSTRUCT, len(nodes))
|
|
|
|
rawnodes = ''.join(bin(n) for n in nodes)
|
|
|
|
|
2017-08-22 01:10:41 +03:00
|
|
|
pipeo.write('%s%s%s%s' % (filenamelen, filename, countlen,
|
|
|
|
rawnodes))
|
|
|
|
pipeo.flush()
|
|
|
|
pipeo.write(struct.pack(constants.FILENAMESTRUCT, 0))
|
|
|
|
pipeo.flush()
|
2016-05-16 20:59:09 +03:00
|
|
|
|
2013-05-07 03:44:04 +04:00
|
|
|
def connect(self):
|
2013-09-04 07:03:24 +04:00
|
|
|
if self.cacheprocess:
|
2016-05-16 20:59:09 +03:00
|
|
|
cmd = "%s %s" % (self.cacheprocess, self.writedata._path)
|
2014-01-09 23:41:12 +04:00
|
|
|
self.remotecache.connect(cmd)
|
2013-09-04 07:03:24 +04:00
|
|
|
else:
|
|
|
|
# If no cache process is specified, we fake one that always
|
|
|
|
# returns cache misses. This enables tests to run easily
|
|
|
|
# and may eventually allow us to be a drop in replacement
|
|
|
|
# for the largefiles extension.
|
2014-01-09 23:41:12 +04:00
|
|
|
class simplecache(object):
|
2013-09-04 07:03:24 +04:00
|
|
|
def __init__(self):
|
|
|
|
self.missingids = []
|
2014-01-09 23:41:12 +04:00
|
|
|
self.connected = True
|
|
|
|
|
2013-09-04 07:03:24 +04:00
|
|
|
def close(self):
|
|
|
|
pass
|
2014-01-09 23:41:12 +04:00
|
|
|
|
|
|
|
def request(self, value, flush=True):
|
|
|
|
lines = value.split("\n")
|
|
|
|
if lines[0] != "get":
|
|
|
|
return
|
|
|
|
self.missingids = lines[2:-1]
|
|
|
|
self.missingids.append('0')
|
|
|
|
|
|
|
|
def receiveline(self):
|
|
|
|
if len(self.missingids) > 0:
|
|
|
|
return self.missingids.pop(0)
|
2013-09-04 07:03:24 +04:00
|
|
|
return None
|
2014-01-09 23:41:12 +04:00
|
|
|
|
|
|
|
self.remotecache = simplecache()
|
2013-05-07 03:44:04 +04:00
|
|
|
|
|
|
|
def close(self):
|
2017-03-16 06:57:32 +03:00
|
|
|
if fetches:
|
|
|
|
msg = ("%s files fetched over %d fetches - " +
|
|
|
|
"(%d misses, %0.2f%% hit ratio) over %0.2fs\n") % (
|
|
|
|
fetched,
|
|
|
|
fetches,
|
|
|
|
fetchmisses,
|
|
|
|
float(fetched - fetchmisses) / float(fetched) * 100.0,
|
|
|
|
fetchcost)
|
|
|
|
if self.debugoutput:
|
|
|
|
self.ui.warn(msg)
|
|
|
|
self.ui.log("remotefilelog.prefetch", msg.replace("%", "%%"),
|
|
|
|
remotefilelogfetched=fetched,
|
|
|
|
remotefilelogfetches=fetches,
|
|
|
|
remotefilelogfetchmisses=fetchmisses,
|
|
|
|
remotefilelogfetchtime=fetchcost * 1000)
|
2013-05-07 03:59:05 +04:00
|
|
|
|
2014-01-09 23:41:12 +04:00
|
|
|
if self.remotecache.connected:
|
|
|
|
self.remotecache.close()
|
2013-05-07 03:49:55 +04:00
|
|
|
|
2017-06-20 21:08:15 +03:00
|
|
|
self.connpool.close()
|
2015-12-11 22:18:51 +03:00
|
|
|
|
2016-05-16 20:59:09 +03:00
|
|
|
def prefetch(self, fileids, force=False, fetchdata=True,
|
|
|
|
fetchhistory=False):
|
2013-05-07 03:49:55 +04:00
|
|
|
"""downloads the given file versions to the cache
|
|
|
|
"""
|
2014-02-12 02:41:56 +04:00
|
|
|
repo = self.repo
|
2016-04-05 02:26:12 +03:00
|
|
|
idstocheck = []
|
2013-05-07 03:49:55 +04:00
|
|
|
for file, id in fileids:
|
|
|
|
# hack
|
2013-07-25 09:16:50 +04:00
|
|
|
# - we don't use .hgtags
|
|
|
|
# - workingctx produces ids with length 42,
|
|
|
|
# which we skip since they aren't in any cache
|
2016-04-26 23:00:31 +03:00
|
|
|
if (file == '.hgtags' or len(id) == 42
|
|
|
|
or not repo.shallowmatch(file)):
|
2013-05-07 03:49:55 +04:00
|
|
|
continue
|
|
|
|
|
2016-04-05 02:26:12 +03:00
|
|
|
idstocheck.append((file, bin(id)))
|
2013-05-07 03:49:55 +04:00
|
|
|
|
2016-05-16 20:59:09 +03:00
|
|
|
datastore = self.datastore
|
|
|
|
historystore = self.historystore
|
2016-04-05 02:26:12 +03:00
|
|
|
if force:
|
2017-08-07 22:33:30 +03:00
|
|
|
datastore = unioncontentstore(*repo.shareddatastores)
|
|
|
|
historystore = unionmetadatastore(*repo.sharedhistorystores)
|
2016-05-16 20:59:09 +03:00
|
|
|
|
|
|
|
missingids = set()
|
|
|
|
if fetchdata:
|
|
|
|
missingids.update(datastore.getmissing(idstocheck))
|
|
|
|
if fetchhistory:
|
|
|
|
missingids.update(historystore.getmissing(idstocheck))
|
2013-05-07 03:49:55 +04:00
|
|
|
|
2016-10-25 22:29:44 +03:00
|
|
|
# partition missing nodes into nullid and not-nullid so we can
|
|
|
|
# warn about this filtering potentially shadowing bugs.
|
|
|
|
nullids = len([None for unused, id in missingids if id == nullid])
|
|
|
|
if nullids:
|
|
|
|
missingids = [(f, id) for f, id in missingids if id != nullid]
|
|
|
|
repo.ui.develwarn(
|
|
|
|
('remotefilelog not fetching %d null revs'
|
|
|
|
' - this is likely hiding bugs' % nullids),
|
|
|
|
config='remotefilelog-ext')
|
2013-05-07 03:49:55 +04:00
|
|
|
if missingids:
|
2013-05-07 03:59:05 +04:00
|
|
|
global fetches, fetched, fetchcost
|
|
|
|
fetches += 1
|
2015-09-29 08:16:12 +03:00
|
|
|
|
|
|
|
# We want to be able to detect excess individual file downloads, so
|
|
|
|
# let's log that information for debugging.
|
|
|
|
if fetches >= 15 and fetches < 18:
|
|
|
|
if fetches == 15:
|
|
|
|
fetchwarning = self.ui.config('remotefilelog',
|
|
|
|
'fetchwarning')
|
|
|
|
if fetchwarning:
|
|
|
|
self.ui.warn(fetchwarning + '\n')
|
|
|
|
self.logstacktrace()
|
2016-04-05 02:26:12 +03:00
|
|
|
missingids = [(file, hex(id)) for file, id in missingids]
|
2013-05-07 03:59:05 +04:00
|
|
|
fetched += len(missingids)
|
|
|
|
start = time.time()
|
2014-02-12 02:41:56 +04:00
|
|
|
missingids = self.request(missingids)
|
2013-05-07 03:49:55 +04:00
|
|
|
if missingids:
|
2016-04-26 23:00:31 +03:00
|
|
|
raise error.Abort(_("unable to download %d files") %
|
|
|
|
len(missingids))
|
2013-05-07 03:59:05 +04:00
|
|
|
fetchcost += time.time() - start
|
2017-09-21 00:02:58 +03:00
|
|
|
self._lfsprefetch(fileids)
|
|
|
|
|
|
|
|
def _lfsprefetch(self, fileids):
|
2017-09-21 18:07:09 +03:00
|
|
|
if not _lfsmod or not util.safehasattr(
|
|
|
|
self.repo.svfs, 'lfslocalblobstore'):
|
2017-09-21 00:02:58 +03:00
|
|
|
return
|
|
|
|
if not _lfsmod.wrapper.candownload(self.repo):
|
|
|
|
return
|
|
|
|
pointers = []
|
|
|
|
store = self.repo.svfs.lfslocalblobstore
|
|
|
|
for file, id in fileids:
|
|
|
|
nodehash = bin(id)
|
|
|
|
rlog = self.repo.file(file)
|
|
|
|
if rlog.flags(nodehash) & revlog.REVIDX_EXTSTORED:
|
|
|
|
text = rlog.revision(nodehash, raw=True)
|
|
|
|
p = _lfsmod.pointer.deserialize(text)
|
|
|
|
oid = p.oid()
|
|
|
|
if not store.has(oid):
|
|
|
|
pointers.append(p)
|
|
|
|
if len(pointers) > 0:
|
|
|
|
self.repo.svfs.lfsremoteblobstore.readbatch(pointers, store)
|
|
|
|
assert all(store.has(p.oid()) for p in pointers)
|
2014-02-12 04:25:55 +04:00
|
|
|
|
2015-09-29 08:16:12 +03:00
|
|
|
def logstacktrace(self):
|
|
|
|
import traceback
|
|
|
|
self.ui.log('remotefilelog', 'excess remotefilelog fetching:\n%s',
|
|
|
|
''.join(traceback.format_stack()))
|