Skip to content

Multiplexing draft - #2852

Closed
roji wants to merge 7 commits into
npgsql:devfrom
roji:Multiplexing
Closed

roji wants to merge 7 commits into
npgsql:devfrom
roji:Multiplexing

Conversation

@roji

@roji roji commented Feb 14, 2020

Copy link
Copy Markdown
Member

Here's a first rough draft of Npgsql with multiplexing. Lots of tests and scenarios are broken, and it's too early for nits/style feedback - am looking more for general architecture feedback/ideas. The first 3 commits are uninteresting, then come two commits which don't do multiplexing at all - just rewrite the current pool as-is with Channels (performing very similarly). The last commit is the big one :)

Architecture/notes:

  • The idle connector list has been replaced with a Channel
    • Rent (previously Allocate) reads from it, Return (previously Release) writes to it.
    • (Almost) no more lock-free code in our pool (yay!).
    • Channels already guarantee FIFO behavior when calling ReadAsync, for when the Channel is empty and we want to block on someone returning. This has always been a pain point, and it's handled internally not by us - hooray.
  • There's a new connector "binding scope" mechanism; when we start a transaction on a multiplexed connection, it must (exclusively) bind to a connector much like it would in traditional non-multiplexed mode (since we can never multiplex when a transaction is in progress). The same happens with various other scenarios (COPY, COPY within transaction).
  • When a command is executed on a bound connection, we're in the traditional flow: we write directly and wait for the results.
  • When a command is executed on an unbound connection, we're in multiplexing mode, and enqueue the command to a pool-wide channel for multiplexed commands.
  • On the other side is an async loop waiting on the Channel. When a command arrives, it gets a connector (from the same idle connector Channel above), and starts encoding commands on its before
  • As long as the buffer hasn't been filled to a threshold, and a timeout hasn't expired, it continues encoding more and more commands into the buffer. When one of these conditions are triggered, it flushes.
  • The timeout is optional and will probably be disabled by default - it represents a latency/throughput tradeoff; the more we wait for further commands, the more potential latency we add, but may increase the average number of commands per packet.
  • The timeout is adaptive: if the timeout fires, we reduce it a little (10us); if we reach the bytes threshold, we increase it (to allow for more commands to come in next time, up to a user-provided maximum).
  • In addition to flushing the packet, we enqueue the command to the connector's "in flight" Channel. Each connector has its own loop which reads from that channel, then reads from the network, and completes the command execution, handing off the reader to the user.
  • If we're at MaxPoolSize and there are no idle connectors, normal Rent blocks by calling WaitAsync on the idle connector Channel (same behavior as before). But the multiplexing loop continues to send packets on connectors which are already busy, fully utilizing the physical connections as long as they're not saturating (i.e. TCP zero window). This scenario does not occur in TE Fortunes. This mechanism still has a nasty race condition I need to resolve.
  • I'm seeing around 25%->30% RPS improvement for the TechEmpower Fortunes benchmark when using multiplexing, with timeout activated (but never firing) and a bytes threshold of ~900 bytes per packet - that's 25 commands coalesced into each packet. I've done quite a bit of experimentation and can share some more details.
  • Possible future optimization: the multiplexing write loop is a single "thread" doing all the encoding of commands to the buffer. I have some ideas about how to have multiple command "producers" pre-calculate how many bytes they need for their command, reserve a segment in the current connector's buffer, and encode - all in parallel. But that's really non-trivial, and I have some reason to believe it's not going to get us much faster.

I'll continue working on my side and may add commits (but will try not to amend for easier reviewing).

/cc @YohDeadfall @NinoFloris @damageboy @Brar @ajcvickers

roji added 7 commits January 4, 2020 20:19
To make editing Directory.Build.{props,targets} easier
* We only turn on C# 8.0 nullable reference types for newer TFMs where
  the BCL is annotated, otherwise there are issues
  (e.g. Debug.Assert(x != null) doesn't work).
* Ignore warnings of NRT use for older TFMs
* Target netstandard2.1 in plugins to have at least one TFM where NRTs
  are enabled.
Tried to keep things as similar as possible to the previous implementation,
for comparison purposes.

We notably still have fast (sync) and slow (async) paths into the pool,
complicating factoring somewhat.
Remove fast (sync) and slow (async) paths into the pool, simplifying
the code.
@roji

roji commented May 31, 2020

Copy link
Copy Markdown
Member Author

Superceded by #2993

@roji roji closed this May 31, 2020
@roji roji mentioned this pull request May 31, 2020
@roji
roji deleted the Multiplexing branch January 21, 2021 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant