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): add support for external hosts #3474

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

sagnghos
Copy link

@sagnghos sagnghos commented Nov 14, 2024

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> ☕️

If you write sample code, please follow the samples format.

@sagnghos sagnghos requested a review from a team as a code owner November 14, 2024 15:50
Copy link

conventional-commit-lint-gcf bot commented Nov 14, 2024

🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use automerge label. Good luck human!

-- conventional-commit-lint bot
https://conventionalcommits.org/

@product-auto-label product-auto-label bot added size: s Pull request size is small. api: spanner Issues related to the googleapis/java-spanner API. labels Nov 14, 2024
@@ -49,10 +51,16 @@ private SessionId(DatabaseId db, String name) {
static SessionId of(String name) {
Preconditions.checkNotNull(name);
Map<String, String> parts = NAME_TEMPLATE.match(name);
if (parts == null) {
parts = EXTERNAL_HOST_NAME_TEMPLATE.match(name);

Choose a reason for hiding this comment

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

Can you add unit tests to cover this pattern and ensure the functionality works as intended?

Copy link
Author

Choose a reason for hiding this comment

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

Added unit test to cover existing pattern (for regression) as well as new pattern

@@ -94,6 +94,11 @@ public static DatabaseId of(String project, String instance, String database) {
return new DatabaseId(new InstanceId(project, instance), database);
}

/** Creates a {@code DatabaseId} given instance and database IDs. */
public static DatabaseId of(String instance, String database) {
return new DatabaseId(new InstanceId("default", instance), database);

Choose a reason for hiding this comment

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

I think we should document to users that this method assumes instance Id to be "default".

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe not instance Id, but rather project Id?

@@ -94,6 +94,11 @@ public static DatabaseId of(String project, String instance, String database) {
return new DatabaseId(new InstanceId(project, instance), database);
}

/** Creates a {@code DatabaseId} with "default" project, given instance and database IDs. */

Choose a reason for hiding this comment

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

nit nit pick: "default" as project id.

try {
SessionId.of(host);
SessionId.of(externalHost);
// If no exceptions are thrown, the test will pass

Choose a reason for hiding this comment

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

Is it possible to add assertEquas here to validate all the attributes instanceid, projectid and databaseid as set in both the scenarios?

Copy link
Author

Choose a reason for hiding this comment

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

added assert for session name.

@rahul2393 rahul2393 added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 15, 2024
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 15, 2024
Copy link
Collaborator

@olavloite olavloite left a comment

Choose a reason for hiding this comment

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

LGTM, with a couple of tiny nits.

@@ -94,6 +94,15 @@ public static DatabaseId of(String project, String instance, String database) {
return new DatabaseId(new InstanceId(project, instance), database);
}

/** Creates a {@code DatabaseId} with "default" as project id, given instance and database IDs. */
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/** Creates a {@code DatabaseId} with "default" as project id, given instance and database IDs. */
/** Creates a {@code DatabaseId} with the default project ID of this environment and the given instance and database IDs. The project ID is set to "default" if no project ID could be found for this environment. */

/** Creates a {@code DatabaseId} with "default" as project id, given instance and database IDs. */
public static DatabaseId of(String instance, String database) {
String projectId = SpannerOptions.getDefaultProjectId();
if (projectId == null) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: maybe use Strings.isNullOrEmpty(String) instead. That prevents an empty string from being used as the project ID.

SessionId externalHostSession = SessionId.of(externalHost);
assertEquals("abcd1234", externalHostSession.getName());
// If no exceptions are thrown, the test will pass
} catch (IllegalArgumentException e) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: you can remove the entire try-catch block. If a (runtime) exception occurs during a test, then this test will be marked as failed with the error message. I.e. you will get the exact same behavior as you have now with the try-catch block, but with a bit less code and indentation.

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 googleapis/java-spanner API. size: s Pull request size is small.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants