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: add support for Directed Read options #2766

Merged
merged 27 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
edc5bbf
fix: prevent illegal negative timeout values into thread sleep() meth…
arpan14 Feb 6, 2023
49a85df
Merge pull request #1 from arpan14/retryerror
arpan14 Feb 8, 2023
4cd497b
Fixing lint issues.
arpan14 Feb 8, 2023
4a6aa8e
Merge branch 'googleapis:main' into main
arpan14 Mar 13, 2023
b2aa09d
Merge branch 'googleapis:main' into main
arpan14 Mar 15, 2023
8d6d71e
Merge branch 'googleapis:main' into main
arpan14 May 9, 2023
77e6e7d
Merge branch 'googleapis:main' into main
arpan14 Jul 17, 2023
e8b7fad
Merge branch 'googleapis:main' into main
arpan14 Jul 25, 2023
8aa84e1
Merge branch 'googleapis:main' into main
arpan14 Oct 10, 2023
57fd405
Merge branch 'googleapis:main' into main
arpan14 Oct 27, 2023
1253563
Merge branch 'googleapis:main' into main
arpan14 Nov 20, 2023
d4f6a60
Merge branch 'googleapis:main' into main
arpan14 Dec 15, 2023
3efaf7c
Merge branch 'googleapis:main' into main
arpan14 Dec 26, 2023
3269c6b
feat: add support for Directed Read options.
arpan14 Dec 27, 2023
8725334
chore: fix lint issues.
arpan14 Dec 27, 2023
2311384
test: add unit tests for options class.
arpan14 Dec 27, 2023
bf5e6e0
test: add tests using mock spanner.
arpan14 Dec 27, 2023
5a3427e
test: add unit test for partitioned read.
arpan14 Dec 27, 2023
5c8028e
test: add unit test for partitioned read.
arpan14 Dec 28, 2023
12e93a7
chore: adding option in spanner options.
arpan14 Dec 29, 2023
9122ece
chore: fix NPE.
arpan14 Dec 29, 2023
c8df52c
chore: disabling test on emulator.
arpan14 Dec 30, 2023
ead5ab6
chore: adding test for query in RW transaction.
arpan14 Jan 2, 2024
641cb0b
chore: adding IT for transaction manager interface.
arpan14 Jan 2, 2024
54b8ee0
chore: disable IT for emulator.
arpan14 Jan 3, 2024
87aa78d
chore: PR comments.
arpan14 Jan 3, 2024
db8b52c
chore: address PR comments.
arpan14 Jan 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
chore: address PR comments.
  • Loading branch information
arpan14 committed Jan 3, 2024
commit db8b52ccd6690e14d051a0f8e65a1b3badffe600
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,19 @@ public Builder setAsyncExecutorProvider(CloseableExecutorProvider provider) {
*
* <p>DirectedReadOptions set at the request level will take precedence over the options set
* using this method.
*
* <p>An example below of how {@link DirectedReadOptions} can be constructed by including a
* replica.
*
* <pre><code>
* DirectedReadOptions.newBuilder()
* .setIncludeReplicas(
* IncludeReplicas.newBuilder()
* .addReplicaSelections(
* ReplicaSelection.newBuilder().setLocation("us-east1").build()))
* .build();
* }
* </code></pre>
*/
public Builder setDirectedReadOptions(DirectedReadOptions directedReadOptions) {
this.directedReadOptions =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2976,6 +2976,7 @@ public void testBackendPartitionQueryOptions() {
.setChannelProvider(channelProvider)
.setCredentials(NoCredentials.getInstance())
.setSessionPoolOption(SessionPoolOptions.newBuilder().setMinSessions(0).build())
.setDirectedReadOptions(DIRECTED_READ_OPTIONS2)
.build()
.getService()) {
BatchClient client =
Expand Down Expand Up @@ -3015,6 +3016,56 @@ public void testBackendPartitionQueryOptions() {
}
}

@Test
public void
testBackendPartitionQueryOptions_whenDirectedReadOptionsViaSpannerOptions_assertOptions() {
// Use a Spanner instance with MinSession=0 to prevent background requests
// from the session pool interfering with the test case.
try (Spanner spanner =
SpannerOptions.newBuilder()
.setProjectId("[PROJECT]")
.setChannelProvider(channelProvider)
.setCredentials(NoCredentials.getInstance())
.setSessionPoolOption(SessionPoolOptions.newBuilder().setMinSessions(0).build())
.setDirectedReadOptions(DIRECTED_READ_OPTIONS2)
.build()
.getService()) {
BatchClient client =
spanner.getBatchClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE"));
BatchReadOnlyTransaction transaction =
client.batchReadOnlyTransaction(TimestampBound.strong());
List<Partition> partitions =
transaction.partitionQuery(
PartitionOptions.newBuilder().setMaxPartitions(10L).build(),
Statement.newBuilder(SELECT1.getSql())
.withQueryOptions(
QueryOptions.newBuilder()
.setOptimizerVersion("1")
.setOptimizerStatisticsPackage("custom-package")
.build())
.build());
try (ResultSet rs = transaction.execute(partitions.get(0))) {
// Just iterate over the results to execute the query.
while (rs.next()) {}
} finally {
transaction.cleanup();
}
// Check if the last query executed is a DeleteSessionRequest and the second last query
// executed is a ExecuteSqlRequest and was executed using a custom optimizer version,
// statistics package and directed read options.
List<AbstractMessage> requests = mockSpanner.getRequests();
assert requests.size() >= 2 : "required to have at least 2 requests";
assertThat(requests.get(requests.size() - 1)).isInstanceOf(DeleteSessionRequest.class);
assertThat(requests.get(requests.size() - 2)).isInstanceOf(ExecuteSqlRequest.class);
ExecuteSqlRequest executeSqlRequest = (ExecuteSqlRequest) requests.get(requests.size() - 2);
assertThat(executeSqlRequest.getQueryOptions()).isNotNull();
assertThat(executeSqlRequest.getQueryOptions().getOptimizerVersion()).isEqualTo("1");
assertThat(executeSqlRequest.getQueryOptions().getOptimizerStatisticsPackage())
.isEqualTo("custom-package");
assertThat(executeSqlRequest.getDirectedReadOptions()).isEqualTo(DIRECTED_READ_OPTIONS2);
}
}

@Test
public void testBackendPartitionReadOptions() {
// Use a Spanner instance with MinSession=0 to prevent background requests
Expand All @@ -3025,6 +3076,7 @@ public void testBackendPartitionReadOptions() {
.setChannelProvider(channelProvider)
.setCredentials(NoCredentials.getInstance())
.setSessionPoolOption(SessionPoolOptions.newBuilder().setMinSessions(0).build())
.setDirectedReadOptions(DIRECTED_READ_OPTIONS2)
.build()
.getService()) {
BatchClient client =
Expand Down Expand Up @@ -3056,6 +3108,48 @@ public void testBackendPartitionReadOptions() {
}
arpan14 marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
public void
testBackendPartitionReadOptions_whenDirectedReadOptionsViaSpannerOptions_assertOptions() {
// Use a Spanner instance with MinSession=0 to prevent background requests
// from the session pool interfering with the test case.
try (Spanner spanner =
SpannerOptions.newBuilder()
.setProjectId("[PROJECT]")
.setChannelProvider(channelProvider)
.setCredentials(NoCredentials.getInstance())
.setSessionPoolOption(SessionPoolOptions.newBuilder().setMinSessions(0).build())
.setDirectedReadOptions(DIRECTED_READ_OPTIONS2)
.build()
.getService()) {
BatchClient client =
spanner.getBatchClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE"));
BatchReadOnlyTransaction transaction =
client.batchReadOnlyTransaction(TimestampBound.strong());
List<Partition> partitions =
transaction.partitionRead(
PartitionOptions.newBuilder().setMaxPartitions(10L).build(),
"FOO",
KeySet.all(),
Lists.newArrayList("1"));
try (ResultSet rs = transaction.execute(partitions.get(0))) {
// Just iterate over the results to execute the query.
while (rs.next()) {}
} finally {
transaction.cleanup();
}
// Check if the last query executed is a DeleteSessionRequest and the second last query
// executed is a ExecuteSqlRequest and was executed using a custom optimizer version,
// statistics package and directed read options.
List<AbstractMessage> requests = mockSpanner.getRequests();
assert requests.size() >= 2 : "required to have at least 2 requests";
assertThat(requests.get(requests.size() - 1)).isInstanceOf(DeleteSessionRequest.class);
assertThat(requests.get(requests.size() - 2)).isInstanceOf(ReadRequest.class);
ReadRequest readRequest = (ReadRequest) requests.get(requests.size() - 2);
assertThat(readRequest.getDirectedReadOptions()).isEqualTo(DIRECTED_READ_OPTIONS2);
}
}

@Test
public void testAsyncQuery() throws Exception {
final int EXPECTED_ROW_COUNT = 10;
Expand Down
Loading