Repository navigation
Multiple hosts and target server type feature #3607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vonzshik
merged 65 commits into
npgsql:main
from
vonzshik:multiple-hosts-server-target-type
Mar 30, 2021
Merged
Changes from all commits
Commits
Show all changes
65 commits
Select commit
Hold shift + click to select a range
2de2bd2
Base commit for the multiple hosts feature
vonzshik a8a861c
FIxed mock not being await of a failover check query
vonzshik 5c061ca
Attempt to fix replication tests
vonzshik 888bff5
Yet another attempt to fix tests
vonzshik a35337a
Final fix for the replication tests
vonzshik be27ea9
Fixed rent for non-prefered connection
vonzshik 7d63ea2
First round of change proposals
roji 9f2a513
Small nit on documentation
vonzshik 858be14
Changed exception messages + moved clusterValidator check
vonzshik f7afc2b
Got rid of flags
vonzshik e7cd892
Removed unnecessary todo
vonzshik ed54317
Fallback now also returns preferred
vonzshik adf2319
Small nit
vonzshik 0fd50d6
Tiny tweaks
roji 4ebb8d9
Renamed TargetServerType to TargetSessionAttributes (like libpq)
vonzshik c7f1e9f
Fixed compilation
vonzshik fa23b9f
Connection's Host, Port and DataSource return null (or empty string) …
vonzshik 2ac88fe
Fixed some tests
vonzshik 94b26a1
Fixed DataSource for multiplexing
vonzshik 4a143ec
Added an exception if no suitable host has been found
vonzshik ccdb4e2
Added a basic test, fixed getting the pool by canonical string
vonzshik d6c8252
Added a check for the pool count
vonzshik a8ec69b
Added assert for multi host wrapper is not added as a new pool
vonzshik 3b91fe3
MultiHostConnectorPoolWrapper now works correctly for enlisted connec…
vonzshik a4fd761
Fixed compilation
vonzshik 040790b
Fixed a very theoretical problem of concurrent update with different …
vonzshik 31a4f1c
Changed the ordering of timestamp
vonzshik 4bf31d5
Added a connection string param to control the cluster state expiration
vonzshik 4bf4f43
Added a list of critical failure codes
vonzshik 3f2a870
Fixed test
vonzshik 0faa240
Also consider the clusted offline if we've timed out
vonzshik 0bcfbb8
Designate server as offline on IOException
vonzshik 81568c2
Made TargetSessionAttributes start from 0
vonzshik 71018b2
Removed admin shutdown from critial errors
vonzshik 569d031
ClusterRecheckSeconds -> HostRecheckSeconds
vonzshik b6aaa48
NpgsqlConnection.Port is again non-nullable
vonzshik b35f33e
Only update the cluster state for the MultiHostPool
vonzshik 84351b4
Fixed tests
vonzshik 932259a
Yet another test fix
vonzshik 637c9f6
Only throw aggregate exception if there are any exceptions
vonzshik 43b39e6
Small rewrite of no suitable host exception
vonzshik 402927a
Added a support for the host:pool do the default pool (and unpool con…
vonzshik 90e4def
Throw exception if there is anything but TargetSessionAttributes.Any …
vonzshik f02e21d
Fixed UnixDomainSockets for windows
vonzshik cdf001d
Moved error handling to PostgresErrorCodes + changed to StartsWith
vonzshik f0707e1
Fix for state update while reading notifications
vonzshik 2b64d10
Added a support for the ReadWrite and ReadOnly connections
vonzshik 28e8e60
Missed
vonzshik 2a86836
Some very first testing
roji ea1a90f
Fixed changing the connection's state whenever the physical connectio…
vonzshik b110252
Do not update the query state, if pool.Get opens a new connection
vonzshik ec1b2be
UpdateClusterState returns the state after update
vonzshik bb7d0fd
MultiHostPool now only re-checks the cluster's state if it was uknown…
vonzshik 2997707
Wrapper references MultiHostConnectorPool directly
vonzshik 8a426e7
Added a few missing sealed
vonzshik ca52b78
Yet another few missing sealed
vonzshik 1c64a6e
Missed sealed, missed PrimaryReadOnly for fallback, simplified get logic
vonzshik 8ead171
MultipleHostPool now checks for the cluster's state while enlisting i…
vonzshik bc1c2cb
Added a test for the cluster state cache
vonzshik 258e97a
Small nit
vonzshik 2989610
Spelling
vonzshik 363df79
Removed unnecessary sealed keyword
vonzshik 94937b9
Made TargetSessionAttributes a string
vonzshik 0af2ab3
Made MultiHostConnectorPool.Get pretty
vonzshik 67d8673
After rebase fix
vonzshik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| using Npgsql.Util; | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
|
|
||
| namespace Npgsql | ||
| { | ||
| static class ClusterStateCache | ||
| { | ||
| static readonly ConcurrentDictionary<ClusterIdentifier, ClusterInfo> Clusters = new(); | ||
|
|
||
| internal static ClusterState GetClusterState(string host, int port, bool ignoreExpiration) | ||
| => Clusters.TryGetValue(new(host, port), out var cs) && (ignoreExpiration || !cs.Timeout.HasExpired) | ||
| ? cs.State | ||
| : ClusterState.Unknown; | ||
|
|
||
| #if NETSTANDARD2_0 | ||
| internal static ClusterState UpdateClusterState(string host, int port, ClusterState state, DateTime timeStamp, TimeSpan stateExpiration) | ||
| => Clusters.AddOrUpdate( | ||
| new ClusterIdentifier(host, port), | ||
| new ClusterInfo(state, new NpgsqlTimeout(stateExpiration), timeStamp), | ||
| (_, oldInfo) => oldInfo.TimeStamp >= timeStamp ? oldInfo : new ClusterInfo(state, new NpgsqlTimeout(stateExpiration), timeStamp)).State; | ||
| #else | ||
| internal static ClusterState UpdateClusterState(string host, int port, ClusterState state, DateTime timeStamp, TimeSpan stateExpiration) | ||
| => Clusters.AddOrUpdate( | ||
| new ClusterIdentifier(host, port), | ||
| (_, newInfo) => newInfo, | ||
| (_, oldInfo, newInfo) => oldInfo.TimeStamp >= newInfo.TimeStamp ? oldInfo : newInfo, | ||
| new ClusterInfo(state, new NpgsqlTimeout(stateExpiration), timeStamp)).State; | ||
| #endif | ||
|
|
||
| readonly struct ClusterIdentifier : IEquatable<ClusterIdentifier> | ||
| { | ||
| readonly string _host; | ||
| readonly int _port; | ||
|
|
||
| public ClusterIdentifier(string host, int port) => (_host, _port) = (host, port); | ||
| public override bool Equals(object? obj) => obj is ClusterIdentifier other && Equals(other); | ||
| public bool Equals(ClusterIdentifier other) => _port == other._port && _host == other._host; | ||
| public override int GetHashCode() => HashCode.Combine(_host, _port); | ||
| } | ||
|
|
||
| readonly struct ClusterInfo | ||
| { | ||
| internal readonly ClusterState State; | ||
| internal readonly NpgsqlTimeout Timeout; | ||
| // While the TimeStamp is not strictly required, it does lower the risk of overwriting the current state with an old value | ||
| internal readonly DateTime TimeStamp; | ||
|
|
||
| public ClusterInfo(ClusterState state, NpgsqlTimeout timeout, DateTime timeStamp) | ||
| => (State, Timeout, TimeStamp) = (state, timeout, timeStamp); | ||
| } | ||
| } | ||
|
|
||
| enum ClusterState : byte | ||
| { | ||
| Unknown = 0, | ||
| Offline = 1, | ||
| PrimaryReadWrite = 2, | ||
| PrimaryReadOnly = 3, | ||
| Standby = 4 | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| using Npgsql.Util; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using System.Transactions; | ||
|
|
||
| namespace Npgsql | ||
| { | ||
| abstract class ConnectorSource | ||
| { | ||
| internal NpgsqlConnectionStringBuilder Settings { get; } | ||
|
|
||
| /// <summary> | ||
| /// Contains the connection string returned to the user from <see cref="NpgsqlConnection.ConnectionString"/> | ||
| /// after the connection has been opened. Does not contain the password unless Persist Security Info=true. | ||
| /// </summary> | ||
| internal string UserFacingConnectionString { get; } | ||
|
|
||
| // Note that while the dictionary is protected by locking, we assume that the lists it contains don't need to be | ||
| // (i.e. access to connectors of a specific transaction won't be concurrent) | ||
| protected readonly Dictionary<Transaction, List<NpgsqlConnector>> _pendingEnlistedConnectors | ||
| = new(); | ||
|
|
||
| internal abstract (int Total, int Idle, int Busy) Statistics { get; } | ||
|
|
||
| internal ConnectorSource(NpgsqlConnectionStringBuilder settings, string connString) | ||
| { | ||
| Settings = settings; | ||
|
|
||
| UserFacingConnectionString = settings.PersistSecurityInfo | ||
| ? connString | ||
| : settings.ToStringWithoutPassword(); | ||
| } | ||
|
|
||
| internal abstract ValueTask<NpgsqlConnector> Get( | ||
| NpgsqlConnection conn, NpgsqlTimeout timeout, bool async, CancellationToken cancellationToken); | ||
|
|
||
| internal abstract void Return(NpgsqlConnector connector); | ||
|
|
||
| internal abstract void Clear(); | ||
|
|
||
| #region Pending Enlisted Connections | ||
|
|
||
| internal virtual void AddPendingEnlistedConnector(NpgsqlConnector connector, Transaction transaction) | ||
| { | ||
| lock (_pendingEnlistedConnectors) | ||
| { | ||
| if (!_pendingEnlistedConnectors.TryGetValue(transaction, out var list)) | ||
| list = _pendingEnlistedConnectors[transaction] = new List<NpgsqlConnector>(); | ||
| list.Add(connector); | ||
| } | ||
| } | ||
|
|
||
| internal virtual void TryRemovePendingEnlistedConnector(NpgsqlConnector connector, Transaction transaction) | ||
| { | ||
| lock (_pendingEnlistedConnectors) | ||
| { | ||
| if (!_pendingEnlistedConnectors.TryGetValue(transaction, out var list)) | ||
| return; | ||
| list.Remove(connector); | ||
| if (list.Count == 0) | ||
| _pendingEnlistedConnectors.Remove(transaction); | ||
| } | ||
| } | ||
|
|
||
| internal virtual bool TryRentEnlistedPending(Transaction transaction, NpgsqlConnection connection, | ||
| [NotNullWhen(true)] out NpgsqlConnector? connector) | ||
| { | ||
| lock (_pendingEnlistedConnectors) | ||
| { | ||
| if (!_pendingEnlistedConnectors.TryGetValue(transaction, out var list)) | ||
| { | ||
| connector = null; | ||
| return false; | ||
| } | ||
| connector = list[list.Count - 1]; | ||
| list.RemoveAt(list.Count - 1); | ||
| if (list.Count == 0) | ||
| _pendingEnlistedConnectors.Remove(transaction); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| #endregion | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.