Skip to content
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

feat:Spanner pg samples #1661

Merged

Conversation

Rishabh-V
Copy link
Contributor

First draft of Spanner PostgreSQL dialect samples for feedback. The samples depend on v3.14.0 of C# Spanner client library which were created locally for sample creation and testing.

@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Mar 18, 2022
@snippet-bot
Copy link

snippet-bot bot commented Mar 18, 2022

Here is the summary of changes.

You are about to add 16 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@Rishabh-V Rishabh-V force-pushed the spanner-pg-samples branch 2 times, most recently from 9103370 to cf74af7 Compare March 19, 2022 17:36
Copy link
Member

@amanda-tarafa amanda-tarafa left a comment

Choose a reason for hiding this comment

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

Structure and overall looks good. Just a few comments.

@@ -1,11 +1,11 @@
<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.

Not sure what changed in this line, revert it.

Copy link
Member

Choose a reason for hiding this comment

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

This is still here. Revert the changes on this whole file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I reverted the entire file. However, still seeing it. I will seek your help to revert this tomorrow.

spanner/api/Spanner.Samples/Spanner.Samples.csproj Outdated Show resolved Hide resolved
spanner/api/Spanner.Samples.Tests/SpannerFixture.cs Outdated Show resolved Hide resolved
spanner/api/Spanner.Samples.Tests/SpannerFixture.cs Outdated Show resolved Hide resolved
spanner/api/Spanner.Samples.Tests/SpannerFixture.cs Outdated Show resolved Hide resolved
spanner/api/Spanner.Samples/CastDataTypesAsyncPostgre.cs Outdated Show resolved Hide resolved
spanner/api/Spanner.Samples.Tests/SpannerFixture.cs Outdated Show resolved Hide resolved
Copy link
Contributor Author

@Rishabh-V Rishabh-V left a comment

Choose a reason for hiding this comment

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

Fixed review comments

spanner/api/Spanner.Samples.Tests/SpannerFixture.cs Outdated Show resolved Hide resolved
spanner/api/Spanner.Samples/CreateDatabaseAsyncPostgre.cs Outdated Show resolved Hide resolved
@@ -388,6 +433,13 @@ private async Task InitializeBackupAsync()
}
}

private async Task InitializePostgreSqlDatabaseAsync()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Initialize Database creates a PG database as well as couple of tables namely Singers and Albums. There is another sample which depicts the ordering of NULL (Nulls First, Nulls Last etc.) that creates a different version of Singers table and inserted data is then ordered in different ways. To keep the sample consistent and inline with other languages and to avoid the chances of duplicate data being inserted, I also created a CreateEmptyPostgreSqlDatabase method. I will also share this sample in next batch.

@amanda-tarafa amanda-tarafa added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 24, 2022
@kokoro-team kokoro-team removed kokoro:force-run Add this label to force Kokoro to re-run the tests. labels Mar 24, 2022
Copy link
Member

@amanda-tarafa amanda-tarafa left a comment

Choose a reason for hiding this comment

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

A few nits but otherwise looks good. Feel free to apply the changes here and create the PR containing the next batch (with relevant changes there as well acording to this PR's comments.)

@@ -1,11 +1,11 @@
<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.

This is still here. Revert the changes on this whole file.

spanner/api/Spanner.Samples/CreateDatabaseAsyncPostgre.cs Outdated Show resolved Hide resolved
var createOperation = await databaseAdminClient.CreateDatabaseAsync(createDatabaseRequest);

// Wait until the operation has finished.
Console.WriteLine("Waiting for the operation to finish.");
Copy link
Member

Choose a reason for hiding this comment

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

This is just a nit, but when you run this sample this text will be printed twice, once for creating the db and once for adding the tables. Consider distinguising the two as in "Waiting for the DB to be created" and "Waiting for the tables to be created."

Here and everywhere where there's something like this happening.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines 45 to 46
Assert.Contains(databases, d => d.DatabaseName.DatabaseId == databaseId);
Assert.Equal(DatabaseDialect.Postgresql, databases.FirstOrDefault(j=> j.DatabaseName.DatabaseId == databaseId).DatabaseDialect);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Assert.Contains(databases, d => d.DatabaseName.DatabaseId == databaseId);
Assert.Equal(DatabaseDialect.Postgresql, databases.FirstOrDefault(j=> j.DatabaseName.DatabaseId == databaseId).DatabaseDialect);
Assert.Contains(databases, d => d.DatabaseName.DatabaseId == databaseId && d.DatabaseDialect == ....);
databaseId).DatabaseDialect);

Comment on lines 31 to 38
var result = await command.ExecuteScalarAsync();
if (result is string statement)
{
Console.WriteLine(statement);
return statement;
}

return string.Empty;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
var result = await command.ExecuteScalarAsync();
if (result is string statement)
{
Console.WriteLine(statement);
return statement;
}
return string.Empty;
var result = await command.ExecuteScalarAsync<string>();
Console.WriteLine(result);
return result;

spanner/api/Spanner.Samples/CastDataTypesAsyncPostgre.cs Outdated Show resolved Hide resolved
spanner/api/Spanner.Samples.Tests/SpannerFixture.cs Outdated Show resolved Hide resolved
@@ -388,6 +433,13 @@ private async Task InitializeBackupAsync()
}
}

private async Task InitializePostgreSqlDatabaseAsync()
Copy link
Member

Choose a reason for hiding this comment

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

OK, so then CreateEmpty... should be on the PR that uses it if posssible.

@Rishabh-V Rishabh-V force-pushed the spanner-pg-samples branch 4 times, most recently from ddef70b to 1a36f4a Compare April 5, 2022 14:48
Copy link
Member

@amanda-tarafa amanda-tarafa left a comment

Choose a reason for hiding this comment

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

Apart from the specific change requests I have several PR wide comments.

  1. There's a lot of information in comments inside the samples that should really be part of documentation sorrounding the sample instead. In that way, it doesn't have to be repeated across languages and it is maintained by TWs. Comments in samples should explain something particular of the language, i.e. something that is just needed for the .NET library for instance; and maybe small notes making reference to documentation, but not the whole explanation.
  2. A lot of samples do many (related) things and we tend to prefer simpler samples. The current samples are harder to test and possibly harder for users to figure out (for instance the ORDER BY, the name Identifier samples, etc.)
  3. Some samples create database and tables, some samples create tables and some samples asume everything they need is already created. We need consistency here and also sample simplification. If someone wants to see how to execute a command they probably don't need the whole create database => create table => execute command. If some samples are creating DBs to avoid flakyness, then those should be created in the corresponding tests but not on the samples themselves.
  4. It's not clear to me that all samples and samples tests that are creating DBs are doing so because of flakyness. We are already blowing Spanner quota ocasionally, if we continut to create DBs without needing them these tests will never pass. Take a look at Spanner: Refactor the test fixture. #1425. In addition consider creating a separate Fixture for PG.
  5. See if you can reuse samples for the Spanner dialect, all of those that are the same, simply add the PG region tag on the existing one.

I know some of these issues are not because decisions you have made, but we should discus them nonetheless. Let's chat tomorrow.

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<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.

Yep, this is weird, maybe a line ending or something. Let's not worry too much about this, but we can take a look later.

// The returned count is the lower bound of the number of records that was deleted.
long rowCount = await cmd.ExecutePartitionedUpdateAsync();

Console.WriteLine($"{rowCount} row(s) deleted...");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Console.WriteLine($"{rowCount} row(s) deleted...");
Console.WriteLine($"At least {rowCount} row(s) deleted...");

public async Task AddStoringIndexAsyncPostgre(string projectId, string instanceId, string databaseId)
{
string connectionString = $"Data Source=projects/{projectId}/instances/{instanceId}/databases/{databaseId}";
string createIndexStatement = "CREATE INDEX SingersBySingerName ON Singers(FirstName) INCLUDE(LastName, SingerInfo)";
Copy link
Member

Choose a reason for hiding this comment

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

Is this one different to what you would do for normal Spanner. Do we have a sample that does this for normal Spanner already?

If the answer to both questions above is yes, then you can just add the posgre region tag to the existing sample without the need of duplicating the sample and tests code.
And this applies to all other samples where there's no distinction between both dialects.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed Amanda, only samples that are different from Standard SQL are chosen for PostgreSQL dialect.

@@ -30,11 +30,12 @@

[CollectionDefinition(nameof(SpannerFixture))]
public class SpannerFixture : IAsyncLifetime, ICollectionFixture<SpannerFixture>
{
{
Copy link
Member

Choose a reason for hiding this comment

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

revert

public string ProjectId { get; } = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
// Allow environment variables to override the default instance and database names.
public string InstanceId { get; } = Environment.GetEnvironmentVariable("TEST_SPANNER_INSTANCE") ?? "my-instance";
public string DatabaseId { get; } = Environment.GetEnvironmentVariable("TEST_SPANNER_DATABASE") ?? $"my-db-{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}";
public string PostgreSqlDatabaseId { get; } = Environment.GetEnvironmentVariable("TEST_SPANNER_POSTGRESQL_DATABASE") ?? $"my-db-{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}";
Copy link
Member

Choose a reason for hiding this comment

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

I think it'd make more sense to add a new fixture for PG. We can do that later though.

spanner/api/Spanner.Samples.Tests/SpannerFixture.cs Outdated Show resolved Hide resolved
@Rishabh-V Rishabh-V force-pushed the spanner-pg-samples branch 2 times, most recently from 697674d to 66e63e4 Compare April 14, 2022 03:31
Copy link
Contributor Author

@Rishabh-V Rishabh-V left a comment

Choose a reason for hiding this comment

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

Fixed review comments. The following changes are now done:

  1. Removed comments that should be in documentation. Have kept only short summary now.
  2. Refactored tests and samples, so that new database is not created. To avoid data insertion conflicts, distinct data specific to the test scenario is inserted in tests as needed.

public async Task AddStoringIndexAsyncPostgre(string projectId, string instanceId, string databaseId)
{
string connectionString = $"Data Source=projects/{projectId}/instances/{instanceId}/databases/{databaseId}";
string createIndexStatement = "CREATE INDEX SingersBySingerName ON Singers(FirstName) INCLUDE(LastName, SingerInfo)";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed Amanda, only samples that are different from Standard SQL are chosen for PostgreSQL dialect.

// limitations under the License.

// [START spanner_postgresql_identifier_case_sensitivity]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

{
string connectionString = $"Data Source=projects/{projectId}/instances/{instanceId}/databases/{databaseId}";

DatabaseAdminClient databaseAdminClient = await DatabaseAdminClient.CreateAsync();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. Apart from one sample, none of the samples or tests create database now.


// This returns the singers in the order NULL, Bruce, Alice.
selectCommand = connection.CreateSelectCommand("SELECT name FROM singers ORDER BY name DESC");
using var dataReader = await selectCommand.ExecuteReaderAsync();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -388,6 +433,13 @@ private async Task InitializeBackupAsync()
}
}

private async Task InitializePostgreSqlDatabaseAsync()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

CreateEmptyPostgreSqlDatabaseAsync and ListTableNamesAsync are now removed from fixture.

Copy link
Member

@amanda-tarafa amanda-tarafa left a comment

Choose a reason for hiding this comment

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

Thanks Rishabh! These look good. LGTM

I'm marking this PR as do not merge just because we need production changes released first.

@amanda-tarafa amanda-tarafa added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Apr 14, 2022
@amanda-tarafa
Copy link
Member

@Rishabh-V I think we might need to increase the overall timeout for Spanner samples. You can do that in the runTests.ps1 file. Do include that change as part of this PR.

@amanda-tarafa
Copy link
Member

I'm marking this PR as do not merge just because we need production changes released first.

Is my assumption right that we still need to release some library changes or has everything been released already.

@Rishabh-V
Copy link
Contributor Author

Thanks Rishabh! These look good. LGTM

I'm marking this PR as do not merge just because we need production changes released first.

Thanks Amanda!

@Rishabh-V I think we might need to increase the overall timeout for Spanner samples. You can do that in the runTests.ps1 file. Do include that change as part of this PR.

Sure Amanda. Timeout is currently set to 7200 seconds. Should I increase it to 9000 seconds?

I'm marking this PR as do not merge just because we need production changes released first.

Is my assumption right that we still need to release some library changes or has everything been released already.

Right Amanda. I believe PgNumeric is not released yet.

@amanda-tarafa
Copy link
Member

Let's wait for the timeout until we can actually build the PR, as right now it is failing because it's missing PgNumeric as expected.

So, nothing to do for now. Thanks!

@amanda-tarafa amanda-tarafa added the api: spanner Issues related to the Spanner API. label Apr 21, 2022
@Rishabh-V Rishabh-V added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label May 4, 2022
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label May 4, 2022
@Rishabh-V Rishabh-V force-pushed the spanner-pg-samples branch 3 times, most recently from 25b5654 to bdbf49b Compare May 5, 2022 14:35
@Rishabh-V Rishabh-V marked this pull request as ready for review May 5, 2022 14:35
@Rishabh-V Rishabh-V requested a review from skuruppu as a code owner May 5, 2022 14:35
@Rishabh-V Rishabh-V added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label May 5, 2022
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label May 5, 2022
@amanda-tarafa amanda-tarafa removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label May 5, 2022
@amanda-tarafa amanda-tarafa merged commit 23879d3 into GoogleCloudPlatform:main May 5, 2022
@Rishabh-V Rishabh-V deleted the spanner-pg-samples branch May 6, 2022 04:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants