Skip to content

Multiplexing - #2993

Closed
roji wants to merge 22 commits into
npgsql:devfrom
roji:Multiplexing2
Closed

roji wants to merge 22 commits into
npgsql:devfrom
roji:Multiplexing2

Conversation

@roji

@roji roji commented May 31, 2020 •

Copy link
Copy Markdown
Member

OK, this is finally in a state where I think it's OK to review and hopefully merge soon.

  • Most of the comments in Multiplexing draft #2852 are still valid, so it's probably a good idea to look at them first.
  • While Multiplexing draft #2852 was unsafe in various ways, I've done a lot of work around safety, and hopefully haven't missed anything important.
  • I really recommend filtering by commit when reviewing. There are some commits before multiplexing which simply rewrite the pool using Channels, without any lock-free code. The last commit is where multiplexing occurs.
  • For multiplexing, the "entry point" is in NpgsqlCommand.ExecuteReaderAsync. If multiplexing is on, we simply enqueue to the pool's command channel and wait. The main bulk of the actual multiplexing write logic is in ConnectorPool.Multiplexing.cs. The read logic is in NpgsqlConnector.ReadLoop. Between these three you should get a pretty good idea of what's happening.
  • Note that we have a safe "over-capacity" mode; if all connections are in use (Max Pool Size), we still continue to push commands down the pipe. This is the only place which requires some basic lock-free techniques, but nothing as complex as what we used to have. Hopefully it's safe - I'd appreciate a good look at this.
  • Lots of tests are still lacking. For now some of the main test suites simply run twice - once in multiplexing, once without. And of course a lot of manual stress testing was done with the TE Fortunes scenario.

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

@roji
roji requested review from Brar, NinoFloris and YohDeadfall May 31, 2020 20:01
@roji roji mentioned this pull request May 31, 2020

@ajcvickers ajcvickers left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐑 🇮🇹 ;-)

@roji

roji commented May 31, 2020

Copy link
Copy Markdown
Member Author

@ajcvickers good one 🤣

@NinoFloris NinoFloris left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 DbOperation screaming 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 -> Return and 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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand this is one thread doing all the writes, correct?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
Comment thread test/Npgsql.Tests/PoolTests.cs Resolved
Comment thread src/Npgsql/ConnectorPool.cs Resolved
Comment thread src/Npgsql/ConnectorPool.cs Resolved
Comment thread src/Npgsql/ConnectorPool.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
@roji

roji commented Jun 2, 2020

Copy link
Copy Markdown
Member Author

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.

@YohDeadfall YohDeadfall added this to the 5.0 milestone Jun 13, 2020
}

default:
throw new Exception("When writing command to connector, task is in invalid state " + task.Status);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably InvalidOperationExceptions would be better here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

Comment thread src/Npgsql/ConnectorPool.cs Resolved
Comment thread Directory.Build.targets Resolved
Comment thread src/Npgsql/Npgsql.csproj Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
roji added 12 commits June 16, 2020 14:27
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.
@roji

roji commented Jun 16, 2020

Copy link
Copy Markdown
Member Author

Squashed manually and merged to dev via 4bd4115, leaving this PR un-squashed for future reference.

@roji roji closed this Jun 16, 2020
@roji roji removed this from the 5.0 milestone Jun 16, 2020
@roji roji removed the performance label Jun 16, 2020
@roji
roji deleted the Multiplexing2 branch January 21, 2021 15:37
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.

4 participants