comparison mercurial/wireprotov2server.py @ 39637:c7a7c7e844e5

wireprotov2: define and implement "manifestdata" command The added command can be used for obtaining manifest data. Given a manifest path and set of manifest nodes, data about manifests can be retrieved. Unlike changeset data, we wish to emit deltas to describe manifest revisions. So the command uses the relatively new API for building delta requests and emitting them. The code calls into deltaparent(), which I'm not very keen of. There's still work to be done in delta generation land so implementation details of storage (e.g. exactly one delta is stored/available) don't creep into higher levels. But we can worry about this later (there is already a TODO on imanifestorage tracking this). On the subject of parent deltas, the server assumes parent revisions exist on the receiving end. This is obviously wrong for shallow clone. I've added TODOs to add a mechanism to the command to allow clients to specify desired behavior. This shouldn't be too difficult to implement. Another big change is that the client must explicitly request manifest nodes to retrieve. This is a major departure from "getbundle," where the server derives relevant manifests as it iterates changesets and sends them automatically. As implemented, the client must transmit each requested node to the server. At 20 bytes per node, we're looking at 2 MB per 100,000 nodes. Plus wire encoding overhead. This isn't ideal for clients with limited upload bandwidth. I plan to address this in the future by allowing alternate mechanisms for defining the revisions to retrieve. One idea is to define a range of changeset revisions whose manifest revisions to retrieve (similar to how "changesetdata" works). We almost certainly want an API to look up an individual manifest by node. And that's where I've chosen to start with the implementation. Again, a theme of this early exchangev2 work is I want to start by building primitives for accessing raw repository data first and see how far we can get with those before we need more complexity. Differential Revision: https://phab.mercurial-scm.org/D4488
author Gregory Szorc <gregory.szorc@gmail.com>
date Wed, 05 Sep 2018 09:09:52 -0700
parents 399ddd3227a4
children 0e03e6a44dee
comparison
equal deleted inserted replaced
39636:399ddd3227a4 39637:c7a7c7e844e5
9 import contextlib 9 import contextlib
10 10
11 from .i18n import _ 11 from .i18n import _
12 from .node import ( 12 from .node import (
13 nullid, 13 nullid,
14 nullrev,
14 ) 15 )
15 from . import ( 16 from . import (
17 changegroup,
18 dagop,
16 discovery, 19 discovery,
17 encoding, 20 encoding,
18 error, 21 error,
19 pycompat, 22 pycompat,
20 streamclone, 23 streamclone,
409 caps['rawrepoformats'] = sorted(repo.requirements & 412 caps['rawrepoformats'] = sorted(repo.requirements &
410 repo.supportedformats) 413 repo.supportedformats)
411 414
412 return proto.addcapabilities(repo, caps) 415 return proto.addcapabilities(repo, caps)
413 416
417 def builddeltarequests(store, nodes):
418 """Build a series of revision delta requests against a backend store.
419
420 Returns a list of revision numbers in the order they should be sent
421 and a list of ``irevisiondeltarequest`` instances to be made against
422 the backend store.
423 """
424 # We sort and send nodes in DAG order because this is optimal for
425 # storage emission.
426 # TODO we may want a better storage API here - one where we can throw
427 # a list of nodes and delta preconditions over a figurative wall and
428 # have the storage backend figure it out for us.
429 revs = dagop.linearize({store.rev(n) for n in nodes}, store.parentrevs)
430
431 requests = []
432
433 for rev in revs:
434 node = store.node(rev)
435 parents = store.parents(node)
436 deltaparent = store.node(store.deltaparent(rev))
437
438 # There is a delta in storage. That means we can send the delta
439 # efficiently.
440 #
441 # But, the delta may be against a revision the receiver doesn't
442 # have (e.g. shallow clone or when the delta isn't against a parent
443 # revision). For now, we ignore the problem of shallow clone. As
444 # long as a delta exists against a parent, we send it.
445 # TODO allow arguments to control this behavior, as the receiver
446 # may not have the base revision in some scenarios.
447 if deltaparent != nullid and deltaparent in parents:
448 basenode = deltaparent
449
450 # Else there is no delta parent in storage or the delta that is
451 # # there isn't suitable. Let's use a delta against a parent
452 # revision, if possible.
453 #
454 # There is room to check if the delta parent is in the ancestry of
455 # this node. But there isn't an API on the manifest storage object
456 # for that. So ignore this case for now.
457
458 elif parents[0] != nullid:
459 basenode = parents[0]
460 elif parents[1] != nullid:
461 basenode = parents[1]
462
463 # No potential bases to delta against. Send a full revision.
464 else:
465 basenode = nullid
466
467 requests.append(changegroup.revisiondeltarequest(
468 node=node,
469 p1node=parents[0],
470 p2node=parents[1],
471 # Receiver deals with linknode resolution.
472 linknode=nullid,
473 basenode=basenode,
474 ))
475
476 return revs, requests
477
414 def wireprotocommand(name, args=None, permission='push'): 478 def wireprotocommand(name, args=None, permission='push'):
415 """Decorator to declare a wire protocol command. 479 """Decorator to declare a wire protocol command.
416 480
417 ``name`` is the name of the wire protocol command being provided. 481 ``name`` is the name of the wire protocol command being provided.
418 482
627 691
628 # TODO handle exception. 692 # TODO handle exception.
629 node = repo.lookup(key) 693 node = repo.lookup(key)
630 694
631 yield node 695 yield node
696
697 @wireprotocommand('manifestdata',
698 args={
699 'nodes': [b'0123456...'],
700 'fields': [b'parents', b'revision'],
701 'tree': b'',
702 },
703 permission='pull')
704 def manifestdata(repo, proto, nodes=None, fields=None, tree=None):
705 fields = fields or set()
706
707 if nodes is None:
708 raise error.WireprotoCommandError(
709 'nodes argument must be defined')
710
711 if tree is None:
712 raise error.WireprotoCommandError(
713 'tree argument must be defined')
714
715 store = repo.manifestlog.getstorage(tree)
716
717 # Validate the node is known and abort on unknown revisions.
718 for node in nodes:
719 try:
720 store.rev(node)
721 except error.LookupError:
722 raise error.WireprotoCommandError(
723 'unknown node: %s', (node,))
724
725 revs, requests = builddeltarequests(store, nodes)
726
727 yield {
728 b'totalitems': len(revs),
729 }
730
731 if b'revision' in fields:
732 deltas = store.emitrevisiondeltas(requests)
733 else:
734 deltas = None
735
736 for rev in revs:
737 node = store.node(rev)
738
739 if deltas is not None:
740 delta = next(deltas)
741 else:
742 delta = None
743
744 d = {
745 b'node': node,
746 }
747
748 if b'parents' in fields:
749 d[b'parents'] = store.parents(node)
750
751 if b'revision' in fields:
752 assert delta is not None
753 assert delta.flags == 0
754 assert d[b'node'] == delta.node
755
756 if delta.revision is not None:
757 revisiondata = delta.revision
758 d[b'revisionsize'] = len(revisiondata)
759 else:
760 d[b'deltabasenode'] = delta.basenode
761 revisiondata = delta.delta
762 d[b'deltasize'] = len(revisiondata)
763 else:
764 revisiondata = None
765
766 yield d
767
768 if revisiondata is not None:
769 yield revisiondata
770
771 if deltas is not None:
772 try:
773 next(deltas)
774 raise error.ProgrammingError('should not have more deltas')
775 except GeneratorExit:
776 pass
632 777
633 @wireprotocommand('pushkey', 778 @wireprotocommand('pushkey',
634 args={ 779 args={
635 'namespace': b'ns', 780 'namespace': b'ns',
636 'key': b'key', 781 'key': b'key',