Skip to content

Async Schema APIs - #3017

Merged
roji merged 10 commits into
npgsql:mainfrom
manandre:AsyncSchema
Sep 25, 2020
Merged

roji merged 10 commits into
npgsql:mainfrom
manandre:AsyncSchema

Conversation

@manandre

Copy link
Copy Markdown
Collaborator

Closes #2976

@roji

roji commented Jun 17, 2020

Copy link
Copy Markdown
Member

@manandre thanks for doing this! I'm going to delay reviewing this for a bit - dotnet/runtime#38028 should get reviewed relatively soon. In any case it should have little effect on the work you've already done here.

@manandre

manandre commented Jun 30, 2020 •

Copy link
Copy Markdown
Collaborator Author

@roji API has been approved with some slight adjustments. I have updated this PR accordingly.

@manandre

Copy link
Copy Markdown
Collaborator Author

@roji I have prepared the implementation to be ready for new async Schema API in System.Data.Common expected in .net5.0-preview8, but I have a name conflict around the GetColumnSchemaAsync method.
We currently have

public Task<ReadOnlyCollection<NpgsqlDbColumn>> GetColumnSchemaAsync(CancellationToken cancellationToken = default)

and the new API introduces

public Task<ReadOnlyCollection<DbColumn>> GetColumnSchemaAsync(CancellationToken cancellationToken = default)

Should we rename the existing one?!? which would be a breaking change then... 😞

@YohDeadfall

Copy link
Copy Markdown
Contributor

No worries, the change will be for v5 in our driver. Therefore, it's absolutely fine to invest a breaking change (: Just replace existing one by the new.

@manandre

Copy link
Copy Markdown
Collaborator Author

Ok then for a breaking change. But we should also keep alignment with the sync variant:

public ReadOnlyCollection<NpgsqlDbColumn> GetColumnSchema()

Should we replace it also?

@Brar

Brar commented Jul 17, 2020

Copy link
Copy Markdown
Member

Wouldn't the common ADO.NET way be to hide the method with new and have the ADO.NET method point to our Implementation?
What I mean is the way we do in NpgsqlConnection.CreateCommand() and in NpgsqlConnection.BeginTransaction().

@manandre

Copy link
Copy Markdown
Collaborator Author

The default implementation of DbDataReader.GetColumnSchemaAsync() simply invokes the synchronous version and returns a completed task.
This is not the same for DbConnection.CreateCommand() and DbConnection.BeginTransaction() which call a protected abstract method (respectively DbConnection.CreateDbCommand() and DbConnection.BeginDbTransaction()) that we override in NpgsqlConnection.CreateDbCommand() and NpgsqlConnection.BeginDbTransaction()

@YohDeadfall

Copy link
Copy Markdown
Contributor

There is a explicitly implemented interface member to obtain schema information. We can make it visible and return a collection of DbColumn instead.

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

See below for some comments. More generally,

The default implementation of DbDataReader.GetColumnSchemaAsync() simply invokes the synchronous version and returns a completed task.

This is true, but I still don't see why we can't have hiding methods (with new) returning NpgsqlDbColumn, and corresponding overriding methods which simply call these methods... As @Brar said, this is the general ADO.NET practice, and it allows those referencing NpgsqlConnection directly to get the Npgsql-specific NpgsqlDbColumn instead of DbColumn. Am I missing something?

But I suggest we generally hold off on this until we get an SDK with the proper support...

Comment thread src/Npgsql/NpgsqlDataAdapter.cs Resolved
Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
@roji

roji commented Sep 17, 2020

Copy link
Copy Markdown
Member

@manandre we've just updated to 5.0.0-rc1 in #3148 and the new ADO.NET APIs are available, so this is the right time to do this. Do you have time to rebase this etc.?

@manandre

Copy link
Copy Markdown
Collaborator Author

@roji Good news! I will take care of that during the week-end.

@roji
roji changed the base branch from dev to main September 17, 2020 14:34
@manandre

Copy link
Copy Markdown
Collaborator Author

@roji Rebased!
BTW we still have an open discussion on FillAsync...

@roji

roji commented Sep 19, 2020

Copy link
Copy Markdown
Member

For FillAsync, there's dotnet/runtime#22109 for discussing at the System.Data level... I don't think we should do anything here until that's decided.

@roji

roji commented Sep 19, 2020

Copy link
Copy Markdown
Member

@manandre let me know when this is ready for reviewing.

@manandre

Copy link
Copy Markdown
Collaborator Author

Ready for reviewing!

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

Thanks @manandre, here's a first round of reviewing.

Comment thread src/Npgsql/NpgsqlDataAdapter.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlSchema.cs Resolved
Comment thread src/Npgsql/NpgsqlSchema.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
@manandre
manandre force-pushed the AsyncSchema branch 2 times, most recently from 70ad037 to 0782664 Compare September 20, 2020 12:34
Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
Comment thread src/Npgsql/NpgsqlDataAdapter.cs Resolved
Comment thread src/Npgsql/NpgsqlDataAdapter.cs Resolved
Comment thread src/Npgsql/NpgsqlDataAdapter.cs Resolved
Comment thread src/Npgsql/NpgsqlSchema.cs Resolved
Comment thread src/Npgsql/NpgsqlSchema.cs Resolved
Comment thread src/Npgsql/NpgsqlSchema.cs Resolved
Add GetSchemaTableAsync


Add FillAsync


Remove await using as not supported in dotnet 5.0 preview 2


Align on new API proposal


Merge sync and async SchemaTests


Align on new API proposal (bis)


Add async ReaderSchemaTests


Avoid business logic duplication between sync and async


Align new test after rebase


Align on approved API


Align API on nullability annotations of the sync equivalents

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

Almost there

Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
Comment thread src/Npgsql/NpgsqlConnection.cs Resolved
Comment thread src/Npgsql/NpgsqlConnection.cs Resolved

var values = new object[count];

while (async ? await dataReader.ReadAsync(cancellationToken) : dataReader.Read())

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.

Can directly call the Read overload which accepts an async flag (like you did with NpgslqCommand.ExecuteReader above)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

We can do that, but we would lose the TryFastRead optimization path... Should we create a new Read method which accepts an async flag and supports this optimization path ?

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.

That's a good point, thanks. This code is used only in completely low-perf scenarios - reading back the database schema - so I don't think we should care about the fast path. Refactoring to a new Read method with an async flag and the fast path could be a bit tricky, since we want to avoid applying the NoSynchronizationContextScope if the fast path is successful...

To summarize, I would spend any time on this. I'm OK both with keeping your code as is (with the conditional operator), or with giving up on the fast path and calling the existing Read with the async flag - up to you.

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.

OK, as everything else looks great I'll just go ahead and merge as-is.

Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlSchema.cs Resolved

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

Thanks @manandre! Great to have this!

@roji
roji merged commit d28ce7e into npgsql:main Sep 25, 2020
@manandre
manandre deleted the AsyncSchema branch September 25, 2020 11:20
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.

Introduce async schema APIs on connection and reader

4 participants