Skip to content

Multiple hosts and target server type feature - #3607

Merged
vonzshik merged 65 commits into
npgsql:mainfrom
vonzshik:multiple-hosts-server-target-type
Mar 30, 2021
Merged

vonzshik merged 65 commits into
npgsql:mainfrom
vonzshik:multiple-hosts-server-target-type

Conversation

@vonzshik

@vonzshik vonzshik commented Mar 20, 2021 •

Copy link
Copy Markdown
Contributor

Closes #732
Closes #3612

Supersedes #3434

Was done under impression of #3434 as an attempt to make sense on how it's supposed to work.

The only missing feature is updating the server's state with KeepAlive. No longer required per discussion with @roji offline.

@mintsoft would be glad to hear your opinion.

@vonzshik
vonzshik requested a review from roji as a code owner March 20, 2021 15:34

@roji roji 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.

Looks awesome for our 1st iteration - this will be a very nice feature. See some comments below, and also vonzshik#12 (this is just a first round, I have more 🤣).

We'll need a lot of test coverage around this...

Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
Comment thread src/Npgsql/MultiHostConnectorPool.cs Resolved
Comment thread src/Npgsql/MultiHostConnectorPool.cs Resolved
Comment thread src/Npgsql/MultiHostConnectorPool.cs Resolved
Comment thread src/Npgsql/MultiHostConnectorPool.cs Resolved
Comment thread src/Npgsql/MultiHostConnectorPool.cs Resolved
Comment thread src/Npgsql/MultiHostConnectorPool.cs Resolved
Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/ClusterStateCache.cs Resolved
Comment thread src/Npgsql/ClusterStateCache.cs Resolved
@vonzshik
vonzshik requested a review from Brar as a code owner March 23, 2021 21:38
Comment thread src/Npgsql/NpgsqlConnector.cs Outdated

static readonly string[] CriticalFailureCodes =
{
PostgresErrorCodes.ConnectionException,

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.

Does this mean that if I kill a connection (i.e. it's holding a transaction for too long) using pg_terminate_backend() that it would result in the driver thinking that it needs to failover to the next server?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep, it does. The problem is, postgres throws the same exception if the database is stopped.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Or should we just consider this as something unlikely? @roji

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.

Hmm... I'd be conservative here - if a code could be generated in a situation which doesn't imply the cluster is offline, I wouldn't treat it as a critical failure code. Like @mintsoft says, there may be some legitimate uses for pg_terminate_backend which shouldn't cause the cluster to be treated as offline.

Note that there's a difference depending on where the error is caught... Any error caught when opening a connection can probably mean that the cluster is offline (though don't forget the difference between database and cluster). On the other hand, with errors caught in other situations (i.e. NpgsqlConnector.ReadMessage) we should be a bit more careful. So maybe we can treat ConnectionException as "critical failure" when it happens on opening, but not when it's in NpgsqlConnector.ReadMessage...

Thoughts?

@vonzshik vonzshik Mar 24, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Whenever there is an exception while opening a connection, I do not see any reason to not treat the host as 'offline'.
Also, here is the list of errors which pgjdbc treats as 'fatal'.

@roji roji 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.

A few more comments... I'll try to work on the tests today/tomorrow.

Comment thread src/Npgsql/NpgsqlConnector.cs Outdated

static readonly string[] CriticalFailureCodes =
{
PostgresErrorCodes.ConnectionException,

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.

Hmm... I'd be conservative here - if a code could be generated in a situation which doesn't imply the cluster is offline, I wouldn't treat it as a critical failure code. Like @mintsoft says, there may be some legitimate uses for pg_terminate_backend which shouldn't cause the cluster to be treated as offline.

Note that there's a difference depending on where the error is caught... Any error caught when opening a connection can probably mean that the cluster is offline (though don't forget the difference between database and cluster). On the other hand, with errors caught in other situations (i.e. NpgsqlConnector.ReadMessage) we should be a bit more careful. So maybe we can treat ConnectionException as "critical failure" when it happens on opening, but not when it's in NpgsqlConnector.ReadMessage...

Thoughts?

Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
Comment thread src/Npgsql/ClusterStateCache.cs Resolved
mintsoft added a commit to mintsoft/npgsqlTester that referenced this pull request Mar 27, 2021
get => _hostRecheckSeconds;
set
{
_hostRecheckSeconds = value;

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.

What happens if I set this to 0?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

With 0, it's only going to change if the host is going offline, or a new connector is open.

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.

Note that this should be meaningless (do nothing) on PostgreSQL 14, where state changes are supposed to be communicated to the client.

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.

Note offline decision to change 0 to mean "check every single time", since an infinite timeout means that an offline host never comes back.

@roji roji 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.

@vonzshik see some last comments.

Comment thread src/Npgsql/ConnectorSource.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/MultiHostConnectorPool.cs Resolved
Comment thread src/Npgsql/MultiHostConnectorPool.cs Resolved
get => _hostRecheckSeconds;
set
{
_hostRecheckSeconds = value;

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.

Note offline decision to change 0 to mean "check every single time", since an infinite timeout means that an offline host never comes back.

Comment thread src/Npgsql/UnpooledConnectorSource.cs Resolved
@vonzshik
vonzshik force-pushed the multiple-hosts-server-target-type branch from 4be9d61 to 67d8673 Compare March 30, 2021 20:29

@roji roji 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.

🎉

@vonzshik
vonzshik merged commit f3a28b7 into npgsql:main Mar 30, 2021
@vonzshik
vonzshik deleted the multiple-hosts-server-target-type branch March 31, 2021 06:34
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.

Connection's Host, Port and DataSource should return null, unless the connection is open Multiple hosts, failover and load balancing

3 participants