-
Notifications
You must be signed in to change notification settings - Fork 136
feat(spanner): add support for external hosts #3474
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
Changes from all commits
f1b5719
0a74b9c
786e764
7fc9367
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. */ | ||
| public static DatabaseId of(String instance, String database) { | ||
| String projectId = SpannerOptions.getDefaultProjectId(); | ||
| if (projectId == null) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: maybe use |
||
| projectId = "default"; | ||
| } | ||
| return new DatabaseId(new InstanceId(projectId, instance), database); | ||
| } | ||
|
|
||
| /** Creates a {@code DatabaseId} given the instance identity and database id. */ | ||
| public static DatabaseId of(InstanceId instanceId, String database) { | ||
| return new DatabaseId(instanceId, database); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,8 @@ static class SessionId { | |
| private static final PathTemplate NAME_TEMPLATE = | ||
| PathTemplate.create( | ||
| "projects/{project}/instances/{instance}/databases/{database}/sessions/{session}"); | ||
| private static final PathTemplate EXTERNAL_HOST_NAME_TEMPLATE = | ||
| PathTemplate.create("instances/{instance}/databases/{database}/sessions/{session}"); | ||
| private final DatabaseId db; | ||
| private final String name; | ||
|
|
||
|
|
@@ -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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| } | ||
| Preconditions.checkArgument( | ||
| parts != null, "Name should conform to pattern %s: %s", NAME_TEMPLATE, name); | ||
| return of( | ||
| parts.get("project"), parts.get("instance"), parts.get("database"), parts.get("session")); | ||
| parts.containsKey("project") ? parts.get("project") : "default", | ||
| parts.get("instance"), | ||
| parts.get("database"), | ||
| parts.get("session")); | ||
| } | ||
|
|
||
| /** Creates a {@code SessionId} given project, instance, database and session IDs. */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.Assert.fail; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.doNothing; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
@@ -30,6 +31,7 @@ | |
| import com.google.cloud.grpc.GrpcTransportOptions; | ||
| import com.google.cloud.grpc.GrpcTransportOptions.ExecutorFactory; | ||
| import com.google.cloud.spanner.SessionClient.SessionConsumer; | ||
| import com.google.cloud.spanner.SessionClient.SessionId; | ||
| import com.google.cloud.spanner.spi.v1.SpannerRpc; | ||
| import com.google.cloud.spanner.spi.v1.SpannerRpc.Option; | ||
| import com.google.common.collect.ImmutableMap; | ||
|
|
@@ -503,4 +505,23 @@ public void onSessionCreateFailure(Throwable t, int createFailureForSessionCount | |
| } | ||
| assertThat(returnedSessionCount.get()).isEqualTo(numSessions); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Test | ||
| public void testSessionNamePatterns() { | ||
| // Valid pattern for host session name | ||
| String host = | ||
| "projects/spanner-project/instances/spanner-instance/databases/test-db/sessions/abcd1234"; | ||
| // Valid pattern for external host session name | ||
| String externalHost = "instances/default/databases/test-db/sessions/abcd1234"; | ||
| try { | ||
| SessionId hostSession = SessionId.of(host); | ||
| assertEquals("abcd1234", hostSession.getName()); | ||
| SessionId externalHostSession = SessionId.of(externalHost); | ||
| assertEquals("abcd1234", externalHostSession.getName()); | ||
| // If no exceptions are thrown, the test will pass | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added assert for session name. |
||
| } catch (IllegalArgumentException e) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| fail("Expected no exception to be thrown, but got: " + e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.