1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
|
# fileserverclient.py - client for communicating with the cache process
#
# 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.
import io
import os
import threading
import time
import zlib
from mercurial.i18n import _
from mercurial.node import bin, hex
from mercurial import (
error,
pycompat,
revlog,
sshpeer,
util,
wireprotov1peer,
)
from mercurial.utils import (
hashutil,
procutil,
)
from . import (
constants,
contentstore,
metadatastore,
)
_sshv1peer = sshpeer.sshv1peer
# Statistics for debugging
fetchcost = 0
fetches = 0
fetched = 0
fetchmisses = 0
_lfsmod = None
def getcachekey(reponame, file, id):
pathhash = hex(hashutil.sha1(file).digest())
return os.path.join(reponame, pathhash[:2], pathhash[2:], id)
def getlocalkey(file, id):
pathhash = hex(hashutil.sha1(file).digest())
return os.path.join(pathhash, id)
def peersetup(ui, peer):
class remotefilepeer(peer.__class__):
@wireprotov1peer.batchable
def x_rfl_getfile(self, file, node):
if not self.capable(b'x_rfl_getfile'):
raise error.Abort(
b'configured remotefile server does not support getfile'
)
def decode(d):
code, data = d.split(b'\0', 1)
if int(code):
raise error.LookupError(file, node, data)
return data
return {b'file': file, b'node': node}, decode
@wireprotov1peer.batchable
def x_rfl_getflogheads(self, path):
if not self.capable(b'x_rfl_getflogheads'):
raise error.Abort(
b'configured remotefile server does not '
b'support getflogheads'
)
def decode(d):
return d.split(b'\n') if d else []
return {b'path': path}, decode
def _updatecallstreamopts(self, command, opts):
if command != b'getbundle':
return
if (
constants.NETWORK_CAP_LEGACY_SSH_GETFILES
not in self.capabilities()
):
return
if not util.safehasattr(self, '_localrepo'):
return
if (
constants.SHALLOWREPO_REQUIREMENT
not in self._localrepo.requirements
):
return
bundlecaps = opts.get(b'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(constants.BUNDLE2_CAPABLITY)
if self._localrepo.includepattern:
patterns = b'\0'.join(self._localrepo.includepattern)
includecap = b"includepattern=" + patterns
bundlecaps.append(includecap)
if self._localrepo.excludepattern:
patterns = b'\0'.join(self._localrepo.excludepattern)
excludecap = b"excludepattern=" + patterns
bundlecaps.append(excludecap)
opts[b'bundlecaps'] = b','.join(bundlecaps)
def _sendrequest(self, command, args, **opts):
self._updatecallstreamopts(command, args)
return super(remotefilepeer, self)._sendrequest(
command, args, **opts
)
def _callstream(self, command, **opts):
supertype = super(remotefilepeer, self)
if not util.safehasattr(supertype, '_sendrequest'):
self._updatecallstreamopts(command, pycompat.byteskwargs(opts))
return super(remotefilepeer, self)._callstream(command, **opts)
peer.__class__ = remotefilepeer
class cacheconnection:
"""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:
raise error.Abort(_(b"cache connection already open"))
self.pipei, self.pipeo, self.pipee, self.subprocess = procutil.popen4(
cachecommand
)
self.connected = True
def close(self):
def tryclose(pipe):
try:
pipe.close()
except Exception:
pass
if self.connected:
try:
self.pipei.write(b"exit\n")
except Exception:
pass
tryclose(self.pipei)
self.pipei = None
tryclose(self.pipeo)
self.pipeo = None
tryclose(self.pipee)
self.pipee = None
try:
# 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()
except Exception:
pass
self.subprocess = None
self.connected = False
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
def _getfilesbatch(
remote, receivemissing, progresstick, missed, idmap, batchsize
):
# 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.
with remote.commandexecutor() as e:
futures = []
for m in missed:
futures.append(
e.callcommand(
b'x_rfl_getfile', {b'file': idmap[m], b'node': m[-40:]}
)
)
for i, m in enumerate(missed):
r = futures[i].result()
futures[i] = None # release memory
file_ = idmap[m]
node = m[-40:]
receivemissing(io.BytesIO(b'%d\n%s' % (len(r), r)), file_, node)
progresstick()
def _getfiles_optimistic(
remote, receivemissing, progresstick, missed, idmap, step
):
remote._callstream(b"x_rfl_getfiles")
i = 0
pipeo = remote._pipeo
pipei = remote._pipei
while i < len(missed):
# issue a batch of requests
start = i
end = min(len(missed), start + step)
i = end
for missingid in missed[start:end]:
# issue new request
versionid = missingid[-40:]
file = idmap[missingid]
sshrequest = b"%s%s\n" % (versionid, file)
pipeo.write(sshrequest)
pipeo.flush()
# receive batch results
for missingid in missed[start:end]:
versionid = missingid[-40:]
file = idmap[missingid]
receivemissing(pipei, file, versionid)
progresstick()
# End the command
pipeo.write(b'\n')
pipeo.flush()
def _getfiles_threaded(
remote, receivemissing, progresstick, missed, idmap, step
):
remote._callstream(b"x_rfl_getfiles")
pipeo = remote._pipeo
pipei = remote._pipei
def writer():
for missingid in missed:
versionid = missingid[-40:]
file = idmap[missingid]
sshrequest = b"%s%s\n" % (versionid, file)
pipeo.write(sshrequest)
pipeo.flush()
writerthread = threading.Thread(target=writer)
writerthread.daemon = True
writerthread.start()
for missingid in missed:
versionid = missingid[-40:]
file = idmap[missingid]
receivemissing(pipei, file, versionid)
progresstick()
writerthread.join()
# End the command
pipeo.write(b'\n')
pipeo.flush()
class fileserverclient:
"""A client for requesting files from the remote file server."""
def __init__(self, repo):
ui = repo.ui
self.repo = repo
self.ui = ui
self.cacheprocess = ui.config(b"remotefilelog", b"cacheprocess")
if self.cacheprocess:
self.cacheprocess = util.expandpath(self.cacheprocess)
# This option causes remotefilelog to pass the full file path to the
# cacheprocess instead of a hashed key.
self.cacheprocesspasspath = ui.configbool(
b"remotefilelog", b"cacheprocess.includepath"
)
self.debugoutput = ui.configbool(b"remotefilelog", b"debug")
self.remotecache = cacheconnection()
def setstore(self, datastore, historystore, writedata, writehistory):
self.datastore = datastore
self.historystore = historystore
self.writedata = writedata
self.writehistory = writehistory
def _connect(self):
return self.repo.connectionpool.get(self.repo.fallbackpath)
def request(self, fileids):
"""Takes a list of filename/node pairs and fetches them from the
server. Files are stored in the local cache.
A list of nodes that the server couldn't find is returned.
If the connection fails, an exception is raised.
"""
if not self.remotecache.connected:
self.connect()
cache = self.remotecache
writedata = self.writedata
repo = self.repo
total = len(fileids)
request = b"get\n%d\n" % total
idmap = {}
reponame = repo.name
for file, id in fileids:
fullid = getcachekey(reponame, file, id)
if self.cacheprocesspasspath:
request += file + b'\0'
request += fullid + b"\n"
idmap[fullid] = file
cache.request(request)
progress = self.ui.makeprogress(_(b'downloading'), total=total)
progress.update(0)
missed = []
while True:
missingid = cache.receiveline()
if not missingid:
missedset = set(missed)
for missingid in idmap:
if not missingid in missedset:
missed.append(missingid)
self.ui.warn(
_(
b"warning: cache connection closed early - "
+ b"falling back to server\n"
)
)
break
if missingid == b"0":
break
if missingid.startswith(b"_hits_"):
# receive progress reports
parts = missingid.split(b"_")
progress.increment(int(parts[2]))
continue
missed.append(missingid)
global fetchmisses
fetchmisses += len(missed)
fromcache = total - len(missed)
progress.update(fromcache, total=total)
self.ui.log(
b"remotefilelog",
b"remote cache hit rate is %r of %r\n",
fromcache,
total,
hit=fromcache,
total=total,
)
oldumask = os.umask(0o002)
try:
# receive cache misses from master
if missed:
# When verbose is true, sshpeer prints 'running ssh...'
# to stdout, which can interfere with some command
# outputs
verbose = self.ui.verbose
self.ui.verbose = False
try:
with self._connect() as conn:
remote = conn.peer
if remote.capable(
constants.NETWORK_CAP_LEGACY_SSH_GETFILES
):
if not isinstance(remote, _sshv1peer):
raise error.Abort(
b'remotefilelog requires ssh servers'
)
step = self.ui.configint(
b'remotefilelog', b'getfilesstep'
)
getfilestype = self.ui.config(
b'remotefilelog', b'getfilestype'
)
if getfilestype == b'threaded':
_getfiles = _getfiles_threaded
else:
_getfiles = _getfiles_optimistic
_getfiles(
remote,
self.receivemissing,
progress.increment,
missed,
idmap,
step,
)
elif remote.capable(b"x_rfl_getfile"):
if remote.capable(b'batch'):
batchdefault = 100
else:
batchdefault = 10
batchsize = self.ui.configint(
b'remotefilelog', b'batchsize', batchdefault
)
self.ui.debug(
b'requesting %d files from '
b'remotefilelog server...\n' % len(missed)
)
_getfilesbatch(
remote,
self.receivemissing,
progress.increment,
missed,
idmap,
batchsize,
)
else:
raise error.Abort(
b"configured remotefilelog server"
b" does not support remotefilelog"
)
self.ui.log(
b"remotefilefetchlog",
b"Success\n",
fetched_files=progress.pos - fromcache,
total_to_fetch=total - fromcache,
)
except Exception:
self.ui.log(
b"remotefilefetchlog",
b"Fail\n",
fetched_files=progress.pos - fromcache,
total_to_fetch=total - fromcache,
)
raise
finally:
self.ui.verbose = verbose
# send to memcache
request = b"set\n%d\n%s\n" % (len(missed), b"\n".join(missed))
cache.request(request)
progress.complete()
# mark ourselves as a user of this cache
writedata.markrepo(self.repo.path)
finally:
os.umask(oldumask)
def receivemissing(self, pipe, filename, node):
line = pipe.readline()[:-1]
if not line:
raise error.ResponseError(
_(b"error downloading file contents:"),
_(b"connection closed early"),
)
size = int(line)
data = pipe.read(size)
if len(data) != size:
raise error.ResponseError(
_(b"error downloading file contents:"),
_(b"only received %s of %s bytes") % (len(data), size),
)
self.writedata.addremotefilelognode(
filename, bin(node), zlib.decompress(data)
)
def connect(self):
if self.cacheprocess:
cmd = b"%s %s" % (self.cacheprocess, self.writedata._path)
self.remotecache.connect(cmd)
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.
class simplecache:
def __init__(self):
self.missingids = []
self.connected = True
def close(self):
pass
def request(self, value, flush=True):
lines = value.split(b"\n")
if lines[0] != b"get":
return
self.missingids = lines[2:-1]
self.missingids.append(b'0')
def receiveline(self):
if len(self.missingids) > 0:
return self.missingids.pop(0)
return None
self.remotecache = simplecache()
def close(self):
if fetches:
msg = (
b"%d files fetched over %d fetches - "
+ b"(%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(
b"remotefilelog.prefetch",
msg.replace(b"%", b"%%"),
remotefilelogfetched=fetched,
remotefilelogfetches=fetches,
remotefilelogfetchmisses=fetchmisses,
remotefilelogfetchtime=fetchcost * 1000,
)
if self.remotecache.connected:
self.remotecache.close()
def prefetch(
self, fileids, force=False, fetchdata=True, fetchhistory=False
):
"""downloads the given file versions to the cache"""
repo = self.repo
idstocheck = []
for file, id in fileids:
# hack
# - we don't use .hgtags
# - workingctx produces ids with length 42,
# which we skip since they aren't in any cache
if (
file == b'.hgtags'
or len(id) == 42
or not repo.shallowmatch(file)
):
continue
idstocheck.append((file, bin(id)))
datastore = self.datastore
historystore = self.historystore
if force:
datastore = contentstore.unioncontentstore(*repo.shareddatastores)
historystore = metadatastore.unionmetadatastore(
*repo.sharedhistorystores
)
missingids = set()
if fetchdata:
missingids.update(datastore.getmissing(idstocheck))
if fetchhistory:
missingids.update(historystore.getmissing(idstocheck))
# 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 == self.repo.nullid]
)
if nullids:
missingids = [
(f, id) for f, id in missingids if id != self.repo.nullid
]
repo.ui.develwarn(
(
b'remotefilelog not fetching %d null revs'
b' - this is likely hiding bugs' % nullids
),
config=b'remotefilelog-ext',
)
if missingids:
global fetches, fetched, fetchcost
fetches += 1
# 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(
b'remotefilelog', b'fetchwarning'
)
if fetchwarning:
self.ui.warn(fetchwarning + b'\n')
self.logstacktrace()
missingids = [(file, hex(id)) for file, id in sorted(missingids)]
fetched += len(missingids)
start = time.time()
missingids = self.request(missingids)
if missingids:
raise error.Abort(
_(b"unable to download %d files") % len(missingids)
)
fetchcost += time.time() - start
self._lfsprefetch(fileids)
def _lfsprefetch(self, fileids):
if not _lfsmod or not util.safehasattr(
self.repo.svfs, b'lfslocalblobstore'
):
return
if not _lfsmod.wrapper.candownload(self.repo):
return
pointers = []
store = self.repo.svfs.lfslocalblobstore
for file, id in fileids:
node = bin(id)
rlog = self.repo.file(file)
if rlog.flags(node) & revlog.REVIDX_EXTSTORED:
text = rlog.rawdata(node)
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)
def logstacktrace(self):
import traceback
self.ui.log(
b'remotefilelog',
b'excess remotefilelog fetching:\n%s\n',
b''.join(pycompat.sysbytes(s) for s in traceback.format_stack()),
)
|