comparison mercurial/wireprotoserver.py @ 37057:2ec1fb9de638

wireproto: add request IDs to frames One of my primary goals with the new wire protocol is to make operations faster and enable both client and server-side operations to scale to multiple CPU cores. One of the ways we make server interactions faster is by reducing the number of round trips to that server. With the existing wire protocol, the "batch" command facilitates executing multiple commands from a single request payload. The way it works is the requests for multiple commands are serialized. The server executes those commands sequentially then serializes all their results. As an optimization for reducing round trips, this is very effective. The technical implementation, however, is pretty bad and suffers from a number of deficiencies. For example, it creates a new place where authorization to run a command must be checked. (The lack of this checking in older Mercurial releases was CVE-2018-1000132.) The principles behind the "batch" command are sound. However, the execution is not. Therefore, I want to ditch "batch" in the new wire protocol and have protocol level support for issuing multiple requests in a single round trip. This commit introduces support in the frame-based wire protocol to facilitate this. We do this by adding a "request ID" to each frame. If a server sees frames associated with different "request IDs," it handles them as separate requests. All of this happening possibly as part of the same message from client to server (the same request body in the case of HTTP). We /could/ model the exchange the way pipelined HTTP requests do, where the server processes requests in order they are issued and received. But this artifically constrains scalability. A better model is to allow multi-requests to be executed concurrently and for responses to be sent and handled concurrently. So the specification explicitly allows this. There is some work to be done around specifying dependencies between multi-requests. We take the easy road for now and punt on this problem, declaring that if order is important, clients must not issue the request until responses to dependent requests have been received. This commit focuses on the boilerplate of implementing the request ID. The server reactor still can't manage multiple, in-flight request IDs. This will be addressed in a subsequent commit. Because the wire semantics have changed, we bump the version of the media type. Differential Revision: https://phab.mercurial-scm.org/D2869
author Gregory Szorc <gregory.szorc@gmail.com>
date Wed, 14 Mar 2018 16:51:34 -0700
parents 861e9d37e56e
children bbea991635d0
comparison
equal deleted inserted replaced
37056:861e9d37e56e 37057:2ec1fb9de638
31 HTTP_OK = 200 31 HTTP_OK = 200
32 32
33 HGTYPE = 'application/mercurial-0.1' 33 HGTYPE = 'application/mercurial-0.1'
34 HGTYPE2 = 'application/mercurial-0.2' 34 HGTYPE2 = 'application/mercurial-0.2'
35 HGERRTYPE = 'application/hg-error' 35 HGERRTYPE = 'application/hg-error'
36 FRAMINGTYPE = b'application/mercurial-exp-framing-0001' 36 FRAMINGTYPE = b'application/mercurial-exp-framing-0002'
37 37
38 HTTPV2 = wireprototypes.HTTPV2 38 HTTPV2 = wireprototypes.HTTPV2
39 SSHV1 = wireprototypes.SSHV1 39 SSHV1 = wireprototypes.SSHV1
40 SSHV2 = wireprototypes.SSHV2 40 SSHV2 = wireprototypes.SSHV2
41 41
392 392
393 if not frame: 393 if not frame:
394 states.append(b'received: <no frame>') 394 states.append(b'received: <no frame>')
395 break 395 break
396 396
397 frametype, frameflags, payload = frame 397 requestid, frametype, frameflags, payload = frame
398 states.append(b'received: %d %d %s' % (frametype, frameflags, payload)) 398 states.append(b'received: %d %d %d %s' % (frametype, frameflags,
399 399 requestid, payload))
400 action, meta = reactor.onframerecv(frametype, frameflags, payload) 400
401 action, meta = reactor.onframerecv(requestid, frametype, frameflags,
402 payload)
401 states.append(json.dumps((action, meta), sort_keys=True, 403 states.append(json.dumps((action, meta), sort_keys=True,
402 separators=(', ', ': '))) 404 separators=(', ', ': ')))
403 405
404 action, meta = reactor.oninputeof() 406 action, meta = reactor.oninputeof()
405 meta['action'] = action 407 meta['action'] = action
515 517
516 res.status = b'200 OK' 518 res.status = b'200 OK'
517 res.headers[b'Content-Type'] = FRAMINGTYPE 519 res.headers[b'Content-Type'] = FRAMINGTYPE
518 520
519 if isinstance(rsp, wireprototypes.bytesresponse): 521 if isinstance(rsp, wireprototypes.bytesresponse):
520 action, meta = reactor.onbytesresponseready(rsp.data) 522 action, meta = reactor.onbytesresponseready(command['requestid'],
523 rsp.data)
521 else: 524 else:
522 action, meta = reactor.onapplicationerror( 525 action, meta = reactor.onapplicationerror(
523 _('unhandled response type from wire proto command')) 526 _('unhandled response type from wire proto command'))
524 527
525 if action == 'sendframes': 528 if action == 'sendframes':