Skip to content

Support Multiple hosts in connection string - #1764

Closed
higuchi-daisuke wants to merge 10 commits into
npgsql:devfrom
higuchi-daisuke:multiplehosts
Closed

higuchi-daisuke wants to merge 10 commits into
npgsql:devfrom
higuchi-daisuke:multiplehosts

Conversation

@higuchi-daisuke

Copy link
Copy Markdown
Contributor

Hi,
I'm interested in multiple hosts feature (#732 and #1710). I tested PostgreSQL 10's this feature before.
So, I modified @ayaiwt 's codes. ayaiwt allowed me to change the codes.
External specification is not changed except for parameter name.
I created the test code. However, please tell me if you have any good idea to implement the test for this feature. Because this feature needs multiple hosts, so I could not use current interface.

Implementation summary

  • The code about analyzing connection string is not changed from ayaiwt'scodes.
  • The parameter to identify which server should be connected is "TargetServerType". This parameter name is the same as pgjdbc.
  • The connected server is master or slave is identified by "SHOW transaction_read_only" command. This is the same as PostgreSQL10 and pgjdbc.
  • When TargetServerType is set to "any", "SHOW transaction_read_only" is not issued. If no server is not connected, exception is thrown away.
  • When TargetServerType is set to "master" or "slave", "SHOW transaction_read_only" is issued to server in order until connecting to target server. If there is the target server, the connection to the server is kept. If there is no target server, exception is thrown away.
  • When TargetServerType is set to "preferSlave", "SHOW transaction_read_only" is issued until connecting to slave server. If Npgsql connected to master server before connecting to slave server, the connection to master server is kept temporally. Finally Npgsql connected to slave server, that temporal connection is not used. However, there is no slave server, that temporal connection is used.
  • The default value of TargetServerType is "any".
  • If Npgsql could not connect to target server (no target server or connection timeout), new exception is thrown away (the exception message is "Could not find a suitable target server.").
  • Other error is occurred, the exception to date is thrown away because of compatibility. It means that regression tests are succeeded even if DefaultConnectionString has multiple hosts.

Especially, I want to discuss with exception handling.

Regards,
Daisuke, Higuchi

@roji
roji force-pushed the dev branch 3 times, most recently from 5151389 to 8d35b8a Compare February 24, 2018 06:40
@higuchi-daisuke

Copy link
Copy Markdown
Contributor Author

I did some refactoring. Especially, multiple hosts tests are not run as default.
If these tests should be run, ConnectionString must have multiple hosts.
It is easy to change DefaultConnectionString in TestBase.cs like following:

    const string DefaultConnectionString = "Server=localhost:5432,localhost:5433;User ID=npgsql_tests;Password=npgsql_tests;Database=npgsql_tests;Timeout=0;Command Timeout=0";

@laskoviymishka

Copy link
Copy Markdown

Any news? Me also very interesting in these feature.

@roji

roji commented Jul 31, 2018

Copy link
Copy Markdown
Member

I'm sorry I don't have more time at the moment to look into this feature. I think we can (and should) definitely have multiple hosts support in the next release, but I'm involved in an intensive project that won't leave much time before end of October. Just to set everyone's expectations...

Having said that, I'll try to find a weekend soon to at least review this, comment on the general direction etc.

@Dzoge

Dzoge commented Sep 27, 2019

Copy link
Copy Markdown

What is the status of this feature, besides having a few conflicts? Has this pull request been tested? I would love to see this feature and would like to contribute.

@roji

roji commented Sep 27, 2019

Copy link
Copy Markdown
Member

No, unfortunately we had no time to properly review this PR at any point. We've just recently released Npgsql 4.1 and are stabilizing it, I do plan for this feature to make it in for the next minor/major version.

@ayaiwt

ayaiwt commented Nov 12, 2019

Copy link
Copy Markdown

Do you know the release date (or season) for the next major version of Npgsql?
This Multiple connection feature was developed for v3.0, so I think code update is needed.(Ex. resolving code conflicts)
I will re-develop this feature and aim to commit in the next major version.
If I know release day, I can work with that day in mind.

@roji

roji commented Nov 12, 2019

Copy link
Copy Markdown
Member

Sorry that this has been continuously pushed back @ayaiwt. I'm very interested in seeing this feature merged, but just haven't found the time.

I'm not sure when we'll next release, but it's definitely at least a few months away. I'm planning some pretty major architecture work in the near future which may make a mess of things (for multiplexing/pipelining), although that should hopefully not conflict too much with what this PR touches.

It's really up to you, based on when you have spare time. If you can find time to rebase this, I can promise to review and push it in to the best of my ability.

@ayaiwt

ayaiwt commented Nov 13, 2019

Copy link
Copy Markdown

@roji Thank you for your kindness.
Will the release after a few months be version 5.0?
Multiple connection feature found in v5.0 Milestore items. If I know the release schedule of v5.0, I change the code accordingly and re-submit, so I asked that question 😃
Sure. When I spare time, I will update the code considering the big architecture.

@roji

roji commented Nov 15, 2019

Copy link
Copy Markdown
Member

@ayaiwt it so happens that after next week I may have some more time to devote to this. So this could be a good time to do the rebase and for us to do some back-and-forths on this work to finally get it in.

@mintsoft

Copy link
Copy Markdown
Contributor

@higuchi-daisuke I was about to start testing this in our dev environment to get some useful feedback, however I'm fighting a little with the merge. Is there any chance you could rebase? :)

@mintsoft

mintsoft commented Dec 18, 2019 •

Copy link
Copy Markdown
Contributor

@higuchi-daisuke I've just been testing this against the https://github.com/higuchi-daisuke/npgsql/tree/multiplehosts branch as it stands without any rebasing and it generally seems to work :) However I've noticed that the connection-pool gets "stuck" on same host.

So in the following scenario:Host=PG1,PG2 and PG1 dies, all queries after the point-in-time that PG1 dies result in initially this being thrown (expected):

Npgsql.NpgsqlException (0x80004005): Exception while reading from stream ---> System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at Npgsql.NpgsqlReadBuffer.<EnsureLong>d__32.MoveNext() in C:\Users\Rob\source\repos\npgsql\src\Npgsql\NpgsqlReadBuffer.cs:line 139
   at Npgsql.NpgsqlReadBuffer.<EnsureLong>d__32.MoveNext() in C:\Users\Rob\source\repos\npgsql\src\Npgsql\NpgsqlReadBuffer.cs:line 161
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
...

But then also every query gets this:

System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it
   at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout) in C:\Users\Rob\source\repos\npgsql\src\Npgsql\NpgsqlConnector.cs:line 694
   at Npgsql.NpgsqlConnector.<RawOpen>d__147.MoveNext() in C:\Users\Rob\source\repos\npgsql\src\Npgsql\NpgsqlConnector.cs:line 609
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Npgsql.NpgsqlConnector.<Open>d__144.MoveNext() in C:\Users\Rob\source\repos\npgsql\src\Npgsql\NpgsqlConnector.cs:line 435

Which isn't the behaviour I was expecting; it seems like this is then broken until the application is recycled. I'll do some more digging to confirm the exact behaviour, possibly this is being influenced by TargetServerType

I've got my test-application here for now: https://github.com/mintsoft/npgsqlTester if you'd like to see it

@mintsoft

mintsoft commented Dec 22, 2019 •

Copy link
Copy Markdown
Contributor

I've been trying to get this rebased so I can have an attack at the fail-conditions of one of the servers in the connection pool and I've come basically come unstuck on the PoolManager.cs at this commit: https://github.com/mintsoft/npgsql/commit/2056b3c5fbb52fd530f409c132a6bf3bd66e9304#diff-33bb36ea0d45f66ae316561892d5b5a9

The new lockless mechanism is completely differently architected compared to the old one and I can't see an easy seam to slice into. I'm thinking that the easiest option might be to make every multi-host connectionstring expand out into n connectionStrings before the PoolManager "knows"; treat them as seperate connectionstrings for that purpose and then have the NpgsqlConnection handle "which" connectionstring it needs to pull from the pool by changing the logic around https://github.com/npgsql/npgsql/blob/dev/src/Npgsql/NpgsqlConnection.cs#L137

Thoughts @roji / @higuchi-daisuke ?

@roji

roji commented Dec 25, 2019

Copy link
Copy Markdown
Member

@mintsoft I haven't yet looked at this PR at any point.

However, in my mind:

  1. A connection string containing multiple hosts should always be mapped to a single pool, just like any other connection string; I wouldn't expect to have a pool per individual host. Is there any reason to go down that path?
  2. I don't really see any reason for the multiple hosts implementation to interact with the pool in any way - it probably make sense for it to be wholly self-contained in NpgsqlConnection.Open (i.e. the establishing of a single physical connection). For one thing, it should be possible to open an unpooled connection providing multiple hosts, so the two things (pooling and multiple hosts) seem completely orthogonal to me. If that's indeed the case, then the pool should be totally unaware of this new feature and no conflicts should arise.

Note that I haven't really thought anything through (or looked at this PR!) so I may be missing lots of context etc. - please feel free to challenge me :)

@mintsoft

Copy link
Copy Markdown
Contributor

@roji Interesting, that's not quite how it's implemented as it stands, basically there's a reasonable amount of logic in PoolManager to make it work. I think rather than trying to rebase that in, I'll take a stab at working that logic out and seperating the PoolManager vs the multi-host/failover parts a little and see what it looks like.

@roji

roji commented Dec 27, 2019

Copy link
Copy Markdown
Member

I'm sorry I don't have time to look into the PR in its current state. If it looks like there's any advantage in implementing things in the pool, please don't hesitate to let me know and we'll discuss the design.

@roji

roji commented Jun 27, 2020

Copy link
Copy Markdown
Member

@higuchi-daisuke @ayaiwt and others, I'm really sorry this hasn't received the proper attention... This is a pretty big feature that's complex to review and bring to a mergeable state. @mintsoft is working on this via #2778, presumably based on the ideas already explored here.

In any case, I'm going to go ahead and close this for age, especially as the pool implementation has been rewritten from the ground up. On the upside, now that this rewrite has happened, it may be a good moment to attack this again, as a lot of lock-free complexity has been removed.

@roji roji closed this Jun 27, 2020
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.

6 participants