From f1b5719e1a162b23319cf87d843b800362024276 Mon Sep 17 00:00:00 2001 From: sagnghos Date: Thu, 14 Nov 2024 15:38:10 +0000 Subject: [PATCH 1/4] feat(spanner): add support for external hosts --- .../main/java/com/google/cloud/spanner/DatabaseId.java | 5 +++++ .../java/com/google/cloud/spanner/SessionClient.java | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseId.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseId.java index 3602d0ee080..ca70d76708f 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseId.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseId.java @@ -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); + } + /** Creates a {@code DatabaseId} given the instance identity and database id. */ public static DatabaseId of(InstanceId instanceId, String database) { return new DatabaseId(instanceId, database); diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionClient.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionClient.java index 0eed13b018c..6878809e4f6 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionClient.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionClient.java @@ -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 parts = NAME_TEMPLATE.match(name); + if (parts == null) { + parts = EXTERNAL_HOST_NAME_TEMPLATE.match(name); + } 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. */ From 0a74b9c183981e767b6a0890f1a5f201b145f437 Mon Sep 17 00:00:00 2001 From: sagnghos Date: Fri, 15 Nov 2024 07:37:17 +0000 Subject: [PATCH 2/4] feat(spanner): added unit test --- .../com/google/cloud/spanner/DatabaseId.java | 2 +- .../cloud/spanner/SessionClientTests.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseId.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseId.java index ca70d76708f..02fe643ba38 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseId.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseId.java @@ -94,7 +94,7 @@ 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. */ + /** Creates a {@code DatabaseId} with "default" project, given instance and database IDs. */ public static DatabaseId of(String instance, String database) { return new DatabaseId(new InstanceId("default", instance), database); } diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionClientTests.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionClientTests.java index bcba430c521..dce1a44894d 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionClientTests.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionClientTests.java @@ -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,21 @@ 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.of(host); + SessionId.of(externalHost); + // If no exceptions are thrown, the test will pass + } catch (IllegalArgumentException e) { + fail("Expected no exception to be thrown, but got: " + e.getMessage()); + } + } } From 786e76453f612c8d75e0cfd7f02d59ae5a042a48 Mon Sep 17 00:00:00 2001 From: sagnghos Date: Fri, 15 Nov 2024 09:18:00 +0000 Subject: [PATCH 3/4] feat(spanner): added asserts to test --- .../src/main/java/com/google/cloud/spanner/DatabaseId.java | 2 +- .../java/com/google/cloud/spanner/SessionClientTests.java | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseId.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseId.java index 02fe643ba38..f984da4008e 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseId.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseId.java @@ -94,7 +94,7 @@ 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. */ + /** Creates a {@code DatabaseId} with "default" as project id, given instance and database IDs. */ public static DatabaseId of(String instance, String database) { return new DatabaseId(new InstanceId("default", instance), database); } diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionClientTests.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionClientTests.java index dce1a44894d..27b33cb2fe0 100644 --- a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionClientTests.java +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionClientTests.java @@ -515,8 +515,10 @@ public void testSessionNamePatterns() { // Valid pattern for external host session name String externalHost = "instances/default/databases/test-db/sessions/abcd1234"; try { - SessionId.of(host); - SessionId.of(externalHost); + 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 } catch (IllegalArgumentException e) { fail("Expected no exception to be thrown, but got: " + e.getMessage()); From 7fc93678fe5877ec15360734a8fa9a04b31d8a69 Mon Sep 17 00:00:00 2001 From: sagnghos Date: Fri, 15 Nov 2024 14:03:20 +0000 Subject: [PATCH 4/4] feat(spanner): added code to fetch default project id before setting default --- .../src/main/java/com/google/cloud/spanner/DatabaseId.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseId.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseId.java index f984da4008e..5b645c9ac77 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseId.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/DatabaseId.java @@ -96,7 +96,11 @@ public static DatabaseId of(String project, String instance, String database) { /** Creates a {@code DatabaseId} with "default" as project id, given instance and database IDs. */ public static DatabaseId of(String instance, String database) { - return new DatabaseId(new InstanceId("default", instance), database); + String projectId = SpannerOptions.getDefaultProjectId(); + if (projectId == null) { + projectId = "default"; + } + return new DatabaseId(new InstanceId(projectId, instance), database); } /** Creates a {@code DatabaseId} given the instance identity and database id. */