Skip to content

Changed database info cache key#3183

Merged
YohDeadfall merged 2 commits intonpgsql:mainfrom
YohDeadfall:database-info-caching
Nov 13, 2020
Merged

Changed database info cache key#3183
YohDeadfall merged 2 commits intonpgsql:mainfrom
YohDeadfall:database-info-caching

Conversation

@YohDeadfall
Copy link
Contributor

Fixes #3069

@YohDeadfall YohDeadfall requested a review from roji as a code owner September 24, 2020 11:30
@YohDeadfall
Copy link
Contributor Author

Looks like it's a breaking change, @roji.

Copy link
Member

@roji roji left a comment

Choose a reason for hiding this comment

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

Look good, thanks @YohDeadfall.

Apart from the nullability issue below, we should have a couple tests for this. One test could connect to two connection strings with the same host/port/database, but different application paths - and then assert that the connectors' DatabaseInfo property points to the same reference. Another test would do the opposite, asserting that the DatabaseInfo properties are different if the database is different (the test would have to create and drop a temporary database).

Check out TestUtil.CreateTempPool for when you need to play around with connection strings.

Copy link
Member

@roji roji Sep 24, 2020

Choose a reason for hiding this comment

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

The strings here should be non-nullable, no? It makes no sense to have a dictionary entry without a host/database, and at the point where we're loading the types, we should have already verified that we have a host and a database..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, NpgsqlConnectionStringBuilder says that these fields are nullable. Do you want some bangs here? (:

Copy link
Member

Choose a reason for hiding this comment

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

I hate bangs as much as you do :)

The thing to do here, is refactor things a bit. We already validate that Host is never null, and we even have a bang'ed non-nullable property because of this which you can use. It would be cleaner to remove the bang from that property by make it a non-nullable auto-property, and just assign the host after we've validated it.

For Database, we know it can't be null because we null-coalesce Database to the user. We could use the same kind of technique - have a bang'ed property, or more cleanly by having a non-nullable auto-property. Another option is to pass the coalesced, non-nullable database through the stack (this avoids adding another property), but it makes the code a bit nastier.

IMHO this is a good example of how the nullable references features forces us to clean up code to be better :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will fix it, but there is a bigger problem as said above. It's more important problem than bangs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's because of test run parallelization.

@roji
Copy link
Member

roji commented Oct 3, 2020

@YohDeadfall this is waiting for you, right? Where do you see a breaking change here (unless you mean that the tests don't pass)?

@roji
Copy link
Member

roji commented Oct 9, 2020

@YohDeadfall just a reminder on this, when you get some time.

@roji
Copy link
Member

roji commented Oct 25, 2020

@YohDeadfall should we move this out of 5.0, or do you want me to take a look? I still don't know what breaking change you were referring to above...

@YohDeadfall YohDeadfall merged commit cffa5f5 into npgsql:main Nov 13, 2020
@YohDeadfall YohDeadfall deleted the database-info-caching branch November 13, 2020 20:54
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework Condition="'$(DeveloperBuild)' == 'True'">net5.0</TargetFramework>
<TargetFrameworks>net5.0</TargetFrameworks>
<TargetFrameworks Condition="'$(DeveloperBuild)' != 'True'">net461;netstandard2.0</TargetFrameworks>
Copy link
Member

Choose a reason for hiding this comment

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

Why this change, why are we targeting net461 again?


internal static readonly ConcurrentDictionary<string, NpgsqlDatabaseInfo> Cache
= new ConcurrentDictionary<string, NpgsqlDatabaseInfo>();
internal static readonly ConcurrentDictionary<(string Host, int Port, string Database, bool NoTypeLoading), NpgsqlDatabaseInfo> Cache
Copy link
Member

Choose a reason for hiding this comment

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

Maybe a readonly struct (DatabaseInfoCacheKey)? Big tuples are hard to parse...

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
Copy link
Member

Choose a reason for hiding this comment

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

Committed by mistake?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

VS is behaving badly on rebases. Will cleanup.

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.

Cache DatabaseInfo on host/port/database instead of on the entire connection string

2 participants