Gregory Szorc <gregory.szorc@gmail.com> [Fri, 13 Apr 2018 11:08:46 -0700] rev 37632
largefiles: use command executor for batch operation
This is the only other user of iterbatch() in core.
Tests changed because the new command executor is smart enough
to not send a "batch" command over the wire if only 1 command
was requested.
There is still coverage for the "batch" command in this test
though.
Differential Revision: https://phab.mercurial-scm.org/D3270
Gregory Szorc <gregory.szorc@gmail.com> [Fri, 13 Apr 2018 11:02:34 -0700] rev 37631
wireproto: implement batching on peer executor interface
This is a bit more complicated than non-batch requests because we
need to buffer sends until the last request arrives *and* we need
to support resolving futures as data arrives from the remote.
In a classical concurrent.futures executor model, the future
"starts" as soon as it is submitted. However, we have nothing to
start until the last command is submitted.
If we did nothing, calling result() would deadlock, since the future
hasn't "started." So in the case where we queue the command, we return
a special future type whose result() will trigger sendcommands().
This eliminates the deadlock potential. It also serves as a check
against callers who may be calling result() prematurely, as it will
prevent any subsequent callcommands() from working. This behavior
is slightly annoying and a bit restrictive. But it's the world
that half duplex connections forces on us.
In order to support streaming responses, we were previously using
a generator. But with a futures-based API, we're using futures
and not generators. So in order to get streaming, we need a
background thread to read data from the server.
The approach taken in this patch is to leverage the ThreadPoolExecutor
from concurrent.futures for managing a background thread. We create
an executor and future that resolves when all response data is
processed (or an error occurs). When exiting the context manager,
we wait on that background reading before returning.
I was hoping we could manually spin up a threading.Thread and this
would be simple. But I ran into a few deadlocks when implementing.
After looking at the source code to concurrent.futures, I figured
it would just be easier to use a ThreadPoolExecutor than implement
all the code needed to manually manage a thread.
To prove this works, a use of the batch API in discovery has been
updated.
Differential Revision: https://phab.mercurial-scm.org/D3269
Gregory Szorc <gregory.szorc@gmail.com> [Fri, 13 Apr 2018 10:51:23 -0700] rev 37630
wireproto: implement command executor interface for version 1 peers
Now that we've defined our new interface for issuing commands,
let's implement it.
We add the interface to the base peer interface. This means all
peer types must implement it.
The only peer types that we have are the local peer in localrepo
and a shared wire peer for version 1 of the wire protocol.
The local peer implementation is pretty straightforward. We
don't do anything fancy and just return a resolved future with
the result of a method call. This is similar to what
localiterbatcher does.
The wire protocol version 1 implementation is a bit more complicated
and is a more robust implementation.
The wire executor queues commands by default. And because the new
executor interface always allows multiple commands but not all version
1 commands are @batchable, it has to check that the requested commands
are batchable if multiple commands are being requested.
The wire executor currently only supports executing a single command.
This is for simplicity reasons. Support for multiple commands will
be added in a separate commit.
To prove the new interface works, a call to the "known" command
during discovery has been updated to use the new API.
It's worth noting that both implementations require a method having
the command name to exist on the peer. There is at least one caller
in core that don't have a method calls peer._call() directly. We
may need to shore up the requirements later...
Differential Revision: https://phab.mercurial-scm.org/D3268
Gregory Szorc <gregory.szorc@gmail.com> [Fri, 13 Apr 2018 10:23:05 -0700] rev 37629
repository: define new interface for running commands
Today, the peer interface exposes methods for each command that can
be executed. In addition, there is an iterbatch() API that allows
commands to be issued in batches and provides an iterator over the
results. This is a glorified wrapper around the "batch" wire command.
Wire protocol version 2 supports nicer things (such as batching
any command and out-of-order replies). It will require a more
flexible API for executing commands.
This commit introduces a new peer interface for making command
requests. In the new world, you can't simply call a method on the
peer to execute a command: you need to obtain an object to be used
for executing commands. That object can be used to issue a single
command or it can batch multiple requests. In the case of full duplex
peers, the command may even be sent out over the wire immediately.
There are no per-command methods. Instead, there is a generic
method to call a command. The implementation can then perform domain
specific processing for specific commands. This includes passing
data via a specially named argument.
Arguments are also passed as a dictionary instead of using **kwargs.
While **kwargs is nicer to use, we've historically gotten into
trouble using it because there will inevitably be a conflict between
the name of an argument to a wire protocol command and an argument
we want to pass into a function.
Instead of a command returning a value, it returns a future which
will resolve to a value. This opens the door for out-of-order
response handling and concurrent response handling in the version
2 protocol.
Differential Revision: https://phab.mercurial-scm.org/D3267
Gregory Szorc <gregory.szorc@gmail.com> [Mon, 09 Apr 2018 12:28:57 -0700] rev 37628
pycompat: export a handle on concurrent.futures
On Python 3, we use the built-in version in the standard library. Else
we use our vendored backport.
Differential Revision: https://phab.mercurial-scm.org/D3266
Gregory Szorc <gregory.szorc@gmail.com> [Mon, 09 Apr 2018 12:27:52 -0700] rev 37627
setup: add packages for concurrent.futures
We conceivably don't need to distribute this package on Python 3
since we will use the version in the standard library. However,
we want installs to be usable of multiple versions of Python. So
it is best to always have it.
Differential Revision: https://phab.mercurial-scm.org/D3265