Repository navigation
Multiplexing - #2993
Multiplexing#2993roji wants to merge 22 commits into
Conversation
|
@ajcvickers good one 🤣 |
NinoFloris
left a comment
There was a problem hiding this comment.
This was quite the chunky unit to review but I'm super happy multiplexing is here so thank you!
I've done an initial pass over the implementation (not so much the tests). Nice to see these few channels that coordinate most of the mux and demux work, creates a nice clean data flow. I'll follow up with more in depth reviewing when I run this branch against the Crowded test suite.
Some thoughts I had while reading that I think are worth mentioning:
- There is a concept of a
DbOperationscreaming to get out. As it stands this code is spread out over all involved pieces but I think it'd be very worthwhile to create a central threadpool-like concept in npgsql. This would help to reduce the increasingly entangled code - and cyclic dependencies - between Reader/Connector/Connection and to some extent Command which time and time again give me the feeling they force so much accidental complexity onto the codebase. This is also something I'm personally interested in improving. - The pool refactor - though good does come of it by moving to channels - seems to come with a lot of changes for change sake. As multiplexing really seems quite orthogonal it would be my ask if we could break out these changes to another PR, barring benign changes like
Open -> Rent,Release -> Returnand things like the CommandInFlight asserts.
Moving it out would help the reviewing process and make this a more additive change. Considering multiplexing is off by default it would be a safer change to push. We/you ;) could then take as long as needed to fix any issues arising in the multiplexing code paths. If not hotfixes would need to be released stat, as I also expect you want EF.Core PG 5.0 to depend on multiplexing npgsql?
Ultimately up to you how you want to approach this. In any case, to be continued in the coming days!
| } | ||
| } | ||
|
|
||
| async Task MultiplexingWriteLoop() |
There was a problem hiding this comment.
I understand this is one thread doing all the writes, correct?
There was a problem hiding this comment.
Very correct, and it's somewhat central to the design - there are various assumptions that this code will never run concurrently, so no locking/sync needed. At some scale this could become a problem, especially if encoding the wire data is somehow very expensive (e.g. huge UTF8 strings?).
Long-term, I definitely want to move encoding out of here to before the command channel, leaving this code to just get buffers out of the channel and write them - nothing more. However, this would depend on:
- Usage of gather I/O to collect buffers from the different commands from the channel, and write them into a single packet in one go (hence Performance of multi-buffer socket write APIs (scatter-gather) dotnet/runtime#36951). This doesn't worry me too much (it's not amazing but is OK). However:
- We'd need a different model for auto-prepared statements, since before we enqueue a command to the channel we have no connector, so no idea if the SQL is prepared or not...
Stepping back, multiplexing won't immediately be the best thing for every scenario - there are probably some cases where it's going to be worse. Or it may reduce throughput a bit, but at the same time considerably reduce the number of physical connections that are needed (thus reducing strain on PG). At the beginning I had hoped this would be a "just turn it on and rock" kind of feature, but it's (unfortunately) more complicated than that.
Opportunities to improve in the future :)
|
Thanks for the great review @NinoFloris, have commented on everything and have some work items for myself now... Would be happy to continue the above conversations whenever you have time. |
| } | ||
|
|
||
| default: | ||
| throw new Exception("When writing command to connector, task is in invalid state " + task.Status); |
There was a problem hiding this comment.
Probably InvalidOperationExceptions would be better here.
There was a problem hiding this comment.
These are basically assertions - they're not ever supposed to happen. So I don't think the exception type is very important...
| } | ||
|
|
||
| default: | ||
| throw new Exception("When flushing, task is in invalid state " + task.Status); |
Where the BCL isn't annotated yet.
TODO: Decide what to do with these
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.
Turns out we don't need netcoreapp5.0 at the moment.
|
Squashed manually and merged to dev via 4bd4115, leaving this PR un-squashed for future reference. |
OK, this is finally in a state where I think it's OK to review and hopefully merge soon.
This PR is not 100% complete - some tests are skipped, some stuff is not yet implemented (e.g. keepalive). Also, I'd rather we didn't discuss nits or refactors at this point - I think it's better to merge this and continue work in separate, self-contained PRs (this work is just too big to get done in a single PR). So be sparing with your comments if you can :)
I'm hoping we can merge this relatively quickly (is one week too aggressive? maybe two?), and release an alpha package on nuget.org to get some user testing. We're nowhere near releasing, so it's OK for this not to be completely stable yet.
Supercedes #2852