From dc59969a63d4332c45953c5d9a87a3698b31c56b Mon Sep 17 00:00:00 2001 From: rajatrb Date: Thu, 20 Apr 2023 18:48:56 +0000 Subject: [PATCH 01/10] sample: add support for Directed Read options --- .../example/spanner/DirectedReadSample.java | 109 ++++++++++++++++++ .../example/spanner/DirectedReadSampleIT.java | 103 +++++++++++++++++ 2 files changed, 212 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java create mode 100644 samples/snippets/src/test/java/com/example/spanner/DirectedReadSampleIT.java diff --git a/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java b/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java new file mode 100644 index 00000000000..dc6621a67fa --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java @@ -0,0 +1,109 @@ +/* + * Copyright 2023 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.spanner; + +// [START spanner_directed_read] +import com.google.cloud.spanner.DatabaseClient; +import com.google.cloud.spanner.DatabaseId; +import com.google.cloud.spanner.Options; +import com.google.cloud.spanner.ResultSet; +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; +import com.google.cloud.spanner.Statement; +import com.google.spanner.v1.DirectedReadOptions; +import com.google.spanner.v1.DirectedReadOptions.ExcludeReplicas; +import com.google.spanner.v1.DirectedReadOptions.IncludeReplicas; +import com.google.spanner.v1.DirectedReadOptions.ReplicaSelection; + +public class DirectedReadSample { + static void directedRead() { + // TODO(developer): Replace these variables before running the sample. + final String projectId = "my-project"; + final String instanceId = "my-instance"; + final String databaseId = "my-database"; + directedRead(projectId, instanceId, databaseId); + } + + static void directedRead(String projectId, String instanceId, String databaseId) { + // Only one of excludeReplicas or includeReplicas can be set + // Each accepts a list of replicaSelections which contains location and type + // * `location` - The location must be one of the regions within the + // multi-region configuration of your database. + // * `type` - The type of the replica + // Some examples of using replicaSelectors are: + // * `location:us-east1` --> The "us-east1" replica(s) of any available type + // will be used to process the request. + // * `type:READ_ONLY` --> The "READ_ONLY" type replica(s) in nearest + // . available location will be used to process the + // request. + // * `location:us-east1 type:READ_ONLY` --> The "READ_ONLY" type replica(s) + // in location "us-east1" will be used to process + // the request. + // includeReplicas also contains an option for autoFailover which when set + // Spanner will not route requests to a replica outside the + // includeReplicas list when all the specified replicas are unavailable + // or unhealthy. The default value is `false`. + final DirectedReadOptions directedReadOptionsForClient = + DirectedReadOptions.newBuilder() + .setExcludeReplicas( + ExcludeReplicas.newBuilder() + .addReplicaSelections( + ReplicaSelection.newBuilder().setLocation("us-east4").build()) + .build()) + .build(); + + try (Spanner spanner = + SpannerOptions.newBuilder() + .setProjectId(projectId) + .setDirectedReadOptions(directedReadOptionsForClient) + .build() + .getService()) { + final DatabaseClient dbClient = + spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); + + // DirectedReadOptions at Request level will override the options set at + // Client level (through SpannerOptions). + final DirectedReadOptions directedReadOptionsForRequest = + DirectedReadOptions.newBuilder() + .setIncludeReplicas( + IncludeReplicas.newBuilder() + .addReplicaSelections( + ReplicaSelection.newBuilder() + .setType(ReplicaSelection.Type.READ_WRITE) + .build()) + .setAutoFailover(true) + .build()) + .build(); + + // Read rows while passing DirectedReadOptions directly to the query + try (ResultSet rs = + dbClient + .singleUse() + .executeQuery( + Statement.of("SELECT SingerId, AlbumId, AlbumTitle FROM Albums"), + Options.directedRead(directedReadOptionsForRequest))) { + while (rs.next()) { + System.out.printf( + "SingerId: %d, AlbumId: %d, AlbumTitle: %s\n", + rs.getLong(0), rs.getLong(1), rs.getString(2)); + } + System.out.println("Successfully executed read-only transaction with directedReadOptions"); + } + } + } +} +// [END spanner_directed_read] diff --git a/samples/snippets/src/test/java/com/example/spanner/DirectedReadSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/DirectedReadSampleIT.java new file mode 100644 index 00000000000..9c04ae19b9d --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/DirectedReadSampleIT.java @@ -0,0 +1,103 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.spanner; + +import static com.example.spanner.SampleRunner.runSample; +import static org.junit.Assert.assertTrue; + +import com.google.cloud.spanner.DatabaseAdminClient; +import com.google.cloud.spanner.DatabaseClient; +import com.google.cloud.spanner.DatabaseId; +import com.google.cloud.spanner.KeySet; +import com.google.cloud.spanner.Mutation; +import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerOptions; +import com.google.common.collect.ImmutableList; +import java.util.Arrays; +import java.util.Collections; +import java.util.concurrent.TimeUnit; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Integration tests for {@link DirectedReadSample} */ +@RunWith(JUnit4.class) +public class DirectedReadSampleIT extends SampleTestBase { + + private static DatabaseId databaseId; + private static Spanner spanner; + + @BeforeClass + public static void createTestDatabase() throws Exception { + spanner = SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); + DatabaseAdminClient databaseAdminClient = spanner.getDatabaseAdminClient(); + final String database = idGenerator.generateDatabaseId(); + databaseAdminClient + .createDatabase( + instanceId, + database, + ImmutableList.of( + "CREATE TABLE Albums (" + + " SingerId INT64 NOT NULL," + + " AlbumId INT64," + + " AlbumTitle STRING(1024)" + + ") PRIMARY KEY (SingerId, AlbumId)")) + .get(10, TimeUnit.MINUTES); + databaseId = DatabaseId.of(projectId, instanceId, database); + } + + @Before + public void insertTestData() { + final DatabaseClient client = spanner.getDatabaseClient(databaseId); + client.write( + Arrays.asList( + Mutation.newInsertOrUpdateBuilder("Albums") + .set("SingerId") + .to(1L) + .set("AlbumId") + .to(1L) + .set("AlbumTitle") + .to("title 1") + .build(), + Mutation.newInsertOrUpdateBuilder("Albums") + .set("SingerId") + .to(2L) + .set("AlbumId") + .to(2L) + .set("AlbumTitle") + .to("title 2") + .build())); + } + + @After + public void removeTestData() { + final DatabaseClient client = spanner.getDatabaseClient(databaseId); + client.write(Collections.singletonList(Mutation.delete("Albums", KeySet.all()))); + } + + @Test + public void testDirectedRead() throws Exception { + final String out = + runSample( + () -> DirectedReadSample.directedRead(projectId, instanceId, databaseId.getDatabase())); + assertTrue(out.contains("SingerId: 1, AlbumId: 1, AlbumTitle: title 1")); + assertTrue(out.contains("SingerId: 2, AlbumId: 2, AlbumTitle: title 2")); + } +} From 071777b2dd98cffc20faadce362c74000c0583cb Mon Sep 17 00:00:00 2001 From: rajatrb Date: Fri, 21 Apr 2023 10:20:12 +0000 Subject: [PATCH 02/10] add additional assertion on output from sample --- .../src/test/java/com/example/spanner/DirectedReadSampleIT.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/samples/snippets/src/test/java/com/example/spanner/DirectedReadSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/DirectedReadSampleIT.java index 9c04ae19b9d..771e157a3df 100644 --- a/samples/snippets/src/test/java/com/example/spanner/DirectedReadSampleIT.java +++ b/samples/snippets/src/test/java/com/example/spanner/DirectedReadSampleIT.java @@ -99,5 +99,7 @@ public void testDirectedRead() throws Exception { () -> DirectedReadSample.directedRead(projectId, instanceId, databaseId.getDatabase())); assertTrue(out.contains("SingerId: 1, AlbumId: 1, AlbumTitle: title 1")); assertTrue(out.contains("SingerId: 2, AlbumId: 2, AlbumTitle: title 2")); + assertTrue( + out.contains("Successfully executed read-only transaction with directedReadOptions")); } } From 8b3fadd4721f97438ac0f0ea1960e3e96deb3924 Mon Sep 17 00:00:00 2001 From: Rajat Bhatta <93644539+rajatbhatta@users.noreply.github.com> Date: Sat, 26 Aug 2023 02:34:49 +0530 Subject: [PATCH 03/10] fix nit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Knut Olav Løite --- .../src/main/java/com/example/spanner/DirectedReadSample.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java b/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java index dc6621a67fa..b30255675bb 100644 --- a/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java @@ -75,7 +75,7 @@ static void directedRead(String projectId, String instanceId, String databaseId) final DatabaseClient dbClient = spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); - // DirectedReadOptions at Request level will override the options set at + // DirectedReadOptions at request level will override the options set at // Client level (through SpannerOptions). final DirectedReadOptions directedReadOptionsForRequest = DirectedReadOptions.newBuilder() From 7e256ed2b1a708b36b8e787727d1aa728c218026 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Thu, 18 Jan 2024 16:58:11 +0000 Subject: [PATCH 04/10] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20?= =?UTF-8?q?post-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 69efbdfaad2..ec71221a6e9 100644 --- a/README.md +++ b/README.md @@ -50,20 +50,20 @@ If you are using Maven without the BOM, add this to your dependencies: If you are using Gradle 5.x or later, add this to your dependencies: ```Groovy -implementation platform('com.google.cloud:libraries-bom:26.12.0') +implementation platform('com.google.cloud:libraries-bom:26.30.0') implementation 'com.google.cloud:google-cloud-spanner' ``` If you are using Gradle without BOM, add this to your dependencies: ```Groovy -implementation 'com.google.cloud:google-cloud-spanner:6.40.1' +implementation 'com.google.cloud:google-cloud-spanner:6.56.0' ``` If you are using SBT, add this to your dependencies: ```Scala -libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.40.1" +libraryDependencies += "com.google.cloud" % "google-cloud-spanner" % "6.56.0" ``` @@ -270,6 +270,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-spanner/tree/ | Custom Timeout And Retry Settings Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CustomTimeoutAndRetrySettingsExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CustomTimeoutAndRetrySettingsExample.java) | | Delete Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/DeleteInstanceConfigSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/DeleteInstanceConfigSample.java) | | Delete Using Dml Returning Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/DeleteUsingDmlReturningSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/DeleteUsingDmlReturningSample.java) | +| Directed Read Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java) | | Enable Fine Grained Access | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/EnableFineGrainedAccess.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/EnableFineGrainedAccess.java) | | Get Commit Stats Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/GetCommitStatsSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/GetCommitStatsSample.java) | | Get Database Ddl Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/GetDatabaseDdlSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/GetDatabaseDdlSample.java) | @@ -411,7 +412,7 @@ Java is a registered trademark of Oracle and/or its affiliates. [kokoro-badge-link-5]: http://storage.googleapis.com/cloud-devrel-public/java/badges/java-spanner/java11.html [stability-image]: https://img.shields.io/badge/stability-stable-green [maven-version-image]: https://img.shields.io/maven-central/v/com.google.cloud/google-cloud-spanner.svg -[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-spanner/6.40.1 +[maven-version-link]: https://central.sonatype.com/artifact/com.google.cloud/google-cloud-spanner/6.56.0 [authentication]: https://github.com/googleapis/google-cloud-java#authentication [auth-scopes]: https://developers.google.com/identity/protocols/oauth2/scopes [predefined-iam-roles]: https://cloud.google.com/iam/docs/understanding-roles#predefined_roles From 3c5396c82a9eb4ed351077ad4762d73a173f07b4 Mon Sep 17 00:00:00 2001 From: Sri Harsha CH Date: Thu, 18 Jan 2024 17:01:59 +0000 Subject: [PATCH 05/10] feat: update setter --- .../src/main/java/com/example/spanner/DirectedReadSample.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java b/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java index b30255675bb..9c5d92f0445 100644 --- a/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java @@ -85,7 +85,7 @@ static void directedRead(String projectId, String instanceId, String databaseId) ReplicaSelection.newBuilder() .setType(ReplicaSelection.Type.READ_WRITE) .build()) - .setAutoFailover(true) + .setAutoFailoverDisabled(true) .build()) .build(); From 3d1d070a074fe60f4a9fe5f55573e26f94e122d3 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Fri, 19 Jan 2024 04:23:02 +0000 Subject: [PATCH 06/10] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20?= =?UTF-8?q?post-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index bb85fae99ef..5c4bc7289e5 100644 --- a/README.md +++ b/README.md @@ -285,6 +285,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-spanner/tree/ | Custom Timeout And Retry Settings Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/CustomTimeoutAndRetrySettingsExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/CustomTimeoutAndRetrySettingsExample.java) | | Delete Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/DeleteInstanceConfigSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/DeleteInstanceConfigSample.java) | | Delete Using Dml Returning Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/DeleteUsingDmlReturningSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/DeleteUsingDmlReturningSample.java) | +| Directed Read Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java) | | Drop Foreign Key Constraint Delete Cascade Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/DropForeignKeyConstraintDeleteCascadeSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/DropForeignKeyConstraintDeleteCascadeSample.java) | | Drop Sequence Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/DropSequenceSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/DropSequenceSample.java) | | Enable Fine Grained Access | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/EnableFineGrainedAccess.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/EnableFineGrainedAccess.java) | From bf989b361ee0816e1d8007be6f2b6b1ae6e89dce Mon Sep 17 00:00:00 2001 From: Sri Harsha CH Date: Fri, 19 Jan 2024 04:47:43 +0000 Subject: [PATCH 07/10] feat: update comment --- .../main/java/com/example/spanner/DirectedReadSample.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java b/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java index 9c5d92f0445..fdd14cd8f30 100644 --- a/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java @@ -53,10 +53,10 @@ static void directedRead(String projectId, String instanceId, String databaseId) // * `location:us-east1 type:READ_ONLY` --> The "READ_ONLY" type replica(s) // in location "us-east1" will be used to process // the request. - // includeReplicas also contains an option for autoFailover which when set + // includeReplicas also contains an option called autoFailoverDisabled, which when set to true // Spanner will not route requests to a replica outside the // includeReplicas list when all the specified replicas are unavailable - // or unhealthy. The default value is `false`. + // or unhealthy. Default value is `false`. final DirectedReadOptions directedReadOptionsForClient = DirectedReadOptions.newBuilder() .setExcludeReplicas( @@ -85,7 +85,7 @@ static void directedRead(String projectId, String instanceId, String databaseId) ReplicaSelection.newBuilder() .setType(ReplicaSelection.Type.READ_WRITE) .build()) - .setAutoFailoverDisabled(true) + .setAutoFailoverDisabled(false) .build()) .build(); From 069e0cd093431b25a07f1f8a813763fa1456e2a1 Mon Sep 17 00:00:00 2001 From: Sri Harsha CH Date: Fri, 19 Jan 2024 04:50:38 +0000 Subject: [PATCH 08/10] feat: update comment --- .../src/main/java/com/example/spanner/DirectedReadSample.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java b/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java index fdd14cd8f30..2ab357d24a7 100644 --- a/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java @@ -85,7 +85,7 @@ static void directedRead(String projectId, String instanceId, String databaseId) ReplicaSelection.newBuilder() .setType(ReplicaSelection.Type.READ_WRITE) .build()) - .setAutoFailoverDisabled(false) + .setAutoFailoverDisabled(true) .build()) .build(); From 4eff6902ec411bff478053cefcfd3a678d6d60af Mon Sep 17 00:00:00 2001 From: Sri Harsha CH Date: Fri, 19 Jan 2024 09:17:27 +0000 Subject: [PATCH 09/10] feat: add additional comments --- .../java/com/example/spanner/DirectedReadSample.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java b/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java index 2ab357d24a7..c6ced8d0b8a 100644 --- a/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java @@ -54,7 +54,7 @@ static void directedRead(String projectId, String instanceId, String databaseId) // in location "us-east1" will be used to process // the request. // includeReplicas also contains an option called autoFailoverDisabled, which when set to true - // Spanner will not route requests to a replica outside the + // will instruct Spanner to not route requests to a replica outside the // includeReplicas list when all the specified replicas are unavailable // or unhealthy. Default value is `false`. final DirectedReadOptions directedReadOptionsForClient = @@ -66,6 +66,11 @@ static void directedRead(String projectId, String instanceId, String databaseId) .build()) .build(); + // You can set default `DirectedReadOptions` for a Spanner client. These options will be applied to + // all read-only transactions that are executed by this client, unless specific `DirectedReadOptions` + // are set for a query. + // Directed read can only be used for read-only transactions. The default options will be ignored for + // any read/write transaction that the client executes. try (Spanner spanner = SpannerOptions.newBuilder() .setProjectId(projectId) @@ -76,7 +81,7 @@ static void directedRead(String projectId, String instanceId, String databaseId) spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); // DirectedReadOptions at request level will override the options set at - // Client level (through SpannerOptions). + // client level (through SpannerOptions). final DirectedReadOptions directedReadOptionsForRequest = DirectedReadOptions.newBuilder() .setIncludeReplicas( @@ -89,7 +94,7 @@ static void directedRead(String projectId, String instanceId, String databaseId) .build()) .build(); - // Read rows while passing DirectedReadOptions directly to the query + // Read rows while passing DirectedReadOptions directly to the query. try (ResultSet rs = dbClient .singleUse() From 22df1e7b36d9d2320acf08462bfe4af37f64720d Mon Sep 17 00:00:00 2001 From: Sri Harsha CH Date: Fri, 19 Jan 2024 09:21:56 +0000 Subject: [PATCH 10/10] feat: lint fix --- .../java/com/example/spanner/DirectedReadSample.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java b/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java index c6ced8d0b8a..141d9e28244 100644 --- a/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/DirectedReadSample.java @@ -66,11 +66,11 @@ static void directedRead(String projectId, String instanceId, String databaseId) .build()) .build(); - // You can set default `DirectedReadOptions` for a Spanner client. These options will be applied to - // all read-only transactions that are executed by this client, unless specific `DirectedReadOptions` - // are set for a query. - // Directed read can only be used for read-only transactions. The default options will be ignored for - // any read/write transaction that the client executes. + // You can set default `DirectedReadOptions` for a Spanner client. These options will be applied + // to all read-only transactions that are executed by this client, unless specific + // DirectedReadOptions are set for a query. + // Directed read can only be used for read-only transactions. The default options will be + // ignored for any read/write transaction that the client executes. try (Spanner spanner = SpannerOptions.newBuilder() .setProjectId(projectId)