wireproto: separate commands tables for version 1 and 2 commands
authorGregory Szorc <gregory.szorc@gmail.com>
Wed, 28 Mar 2018 10:40:41 -0700
changeset 37295 45b39c69fae0
parent 37294 27527d8cff5c
child 37296 78103e4138b1
wireproto: separate commands tables for version 1 and 2 commands We can't easily reuse existing command handlers for version 2 commands because the response types will be different. e.g. many commands return nodes encoded as hex. Our new wire protocol is binary safe, so we'll wish to encode nodes as binary. We /could/ teach each command handler to look at the protocol handler and change behavior based on the version in use. However, this would make logic a bit unwieldy over time and would make it harder to design a unified protocol handler interface. I think it's better to create a clean break between version 1 and version 2 of commands on the server. What I imagine happening is we will have separate @wireprotocommand functions for each protocol generation. Those functions will parse the request, dispatch to a common function to process it, then generate the response in its own, transport-specific manner. This commit establishes a separate table for tracking version 1 commands from version 2 commands. The HTTP server pieces have been updated to use this new table. Most commands are marked as both version 1 and version 2, so there is little practical impact to this change. A side-effect of this change is we now rely on transport registration in wireprototypes.TRANSPORTS and certain properties of the protocol interface. So a test had to be updated to conform. Differential Revision: https://phab.mercurial-scm.org/D2982
hgext/largefiles/uisetup.py
mercurial/wireproto.py
mercurial/wireprotoserver.py
tests/test-wireproto.py
--- a/hgext/largefiles/uisetup.py	Wed Mar 28 10:12:02 2018 -0700
+++ b/hgext/largefiles/uisetup.py	Wed Mar 28 10:40:41 2018 -0700
@@ -175,6 +175,7 @@
 
     # ... and wrap some existing ones
     wireproto.commands['heads'].func = proto.heads
+    # TODO also wrap wireproto.commandsv2 once heads is implemented there.
 
     extensions.wrapfunction(webcommands, 'decodepath', overrides.decodepath)
 
--- a/mercurial/wireproto.py	Wed Mar 28 10:12:02 2018 -0700
+++ b/mercurial/wireproto.py	Wed Mar 28 10:40:41 2018 -0700
@@ -502,7 +502,11 @@
 
 def dispatch(repo, proto, command):
     repo = getdispatchrepo(repo, proto, command)
-    func, spec = commands[command]
+
+    transportversion = wireprototypes.TRANSPORTS[proto.name]['version']
+    commandtable = commandsv2 if transportversion == 2 else commands
+    func, spec = commandtable[command]
+
     args = proto.getargs(spec)
     return func(repo, proto, *args)
 
@@ -679,8 +683,12 @@
 POLICY_V1_ONLY = 'v1-only'
 POLICY_V2_ONLY = 'v2-only'
 
+# For version 1 transports.
 commands = commanddict()
 
+# For version 2 transports.
+commandsv2 = commanddict()
+
 def wireprotocommand(name, args='', transportpolicy=POLICY_ALL,
                      permission='push'):
     """Decorator to declare a wire protocol command.
@@ -702,12 +710,15 @@
     """
     if transportpolicy == POLICY_ALL:
         transports = set(wireprototypes.TRANSPORTS)
+        transportversions = {1, 2}
     elif transportpolicy == POLICY_V1_ONLY:
         transports = {k for k, v in wireprototypes.TRANSPORTS.items()
                       if v['version'] == 1}
+        transportversions = {1}
     elif transportpolicy == POLICY_V2_ONLY:
         transports = {k for k, v in wireprototypes.TRANSPORTS.items()
                       if v['version'] == 2}
+        transportversions = {2}
     else:
         raise error.ProgrammingError('invalid transport policy value: %s' %
                                      transportpolicy)
@@ -724,8 +735,21 @@
                                      permission)
 
     def register(func):
-        commands[name] = commandentry(func, args=args, transports=transports,
-                                      permission=permission)
+        if 1 in transportversions:
+            if name in commands:
+                raise error.ProgrammingError('%s command already registered '
+                                             'for version 1' % name)
+            commands[name] = commandentry(func, args=args,
+                                          transports=transports,
+                                          permission=permission)
+        if 2 in transportversions:
+            if name in commandsv2:
+                raise error.ProgrammingError('%s command already registered '
+                                             'for version 2' % name)
+            commandsv2[name] = commandentry(func, args=args,
+                                            transports=transports,
+                                            permission=permission)
+
         return func
     return register
 
--- a/mercurial/wireprotoserver.py	Wed Mar 28 10:12:02 2018 -0700
+++ b/mercurial/wireprotoserver.py	Wed Mar 28 10:40:41 2018 -0700
@@ -335,7 +335,7 @@
     # extension.
     extracommands = {'multirequest'}
 
-    if command not in wireproto.commands and command not in extracommands:
+    if command not in wireproto.commandsv2 and command not in extracommands:
         res.status = b'404 Not Found'
         res.headers[b'Content-Type'] = b'text/plain'
         res.setbodybytes(_('unknown wire protocol command: %s\n') % command)
@@ -346,7 +346,7 @@
 
     proto = httpv2protocolhandler(req, ui)
 
-    if (not wireproto.commands.commandavailable(command, proto)
+    if (not wireproto.commandsv2.commandavailable(command, proto)
         and command not in extracommands):
         res.status = b'404 Not Found'
         res.headers[b'Content-Type'] = b'text/plain'
@@ -502,7 +502,7 @@
     proto = httpv2protocolhandler(req, ui, args=command['args'])
 
     if reqcommand == b'multirequest':
-        if not wireproto.commands.commandavailable(command['command'], proto):
+        if not wireproto.commandsv2.commandavailable(command['command'], proto):
             # TODO proper error mechanism
             res.status = b'200 OK'
             res.headers[b'Content-Type'] = b'text/plain'
@@ -512,7 +512,7 @@
 
         # TODO don't use assert here, since it may be elided by -O.
         assert authedperm in (b'ro', b'rw')
-        wirecommand = wireproto.commands[command['command']]
+        wirecommand = wireproto.commandsv2[command['command']]
         assert wirecommand.permission in ('push', 'pull')
 
         if authedperm == b'ro' and wirecommand.permission != 'pull':
--- a/tests/test-wireproto.py	Wed Mar 28 10:12:02 2018 -0700
+++ b/tests/test-wireproto.py	Wed Mar 28 10:40:41 2018 -0700
@@ -13,6 +13,8 @@
 class proto(object):
     def __init__(self, args):
         self.args = args
+        self.name = 'dummyproto'
+
     def getargs(self, spec):
         args = self.args
         args.setdefault(b'*', {})
@@ -22,6 +24,11 @@
     def checkperm(self, perm):
         pass
 
+wireprototypes.TRANSPORTS['dummyproto'] = {
+    'transport': 'dummy',
+    'version': 1,
+}
+
 class clientpeer(wireproto.wirepeer):
     def __init__(self, serverrepo, ui):
         self.serverrepo = serverrepo