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: Leader Aware Routing in Connection API #2308

Merged
merged 9 commits into from
Aug 4, 2023

Conversation

rajatbhatta
Copy link
Contributor

@rajatbhatta rajatbhatta commented Feb 27, 2023

By default, RW/PDML requests are preferred to route to leader after LAR is enabled from backend.

This PR adds routeToLeader Boolean connection option for implementing opt-out feature for LAR.
Example URL: cloudspanner:/projects/test-project-123/instances/test-instance-123/databases/test-database-123?routeToLeader=false

Note: You can also pass in routeToLeader=true, but that is the default behaviour (even if you do not pass in this option).

BEGIN_COMMIT_OVERRIDE
feat: Enable leader aware routing by default in Connection API. This enables its use in the JDBC driver and PGAdapter. The update contains performance optimisations that will reduce the latency of read/write transactions that originate from a region other than the default leader region.
END_COMMIT_OVERRIDE

@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: spanner Issues related to the googleapis/java-spanner API. labels Feb 27, 2023
@rajatbhatta rajatbhatta marked this pull request as ready for review February 27, 2023 07:16
@rajatbhatta rajatbhatta requested a review from a team as a code owner February 27, 2023 07:16
@rajatbhatta rajatbhatta requested a review from olavloite March 1, 2023 06:20
@rajatbhatta rajatbhatta requested a review from arpan14 March 1, 2023 06:48
@@ -65,6 +65,8 @@ public class SpannerPoolTest {

Copy link
Contributor

Choose a reason for hiding this comment

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

Query : Do we have a test which fails when the new property is not added to the equals() method?

Copy link
Contributor Author

@rajatbhatta rajatbhatta Mar 1, 2023

Choose a reason for hiding this comment

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

As discussed offline, we can take it as a separate exercise, as we'll need this across classes. Keeping the thread open for the time being.

builder.setUri(
"cloudspanner:/projects/test-project-123/instances/test-instance-123/databases/test-database-123?routeToLeader=false");
ConnectionOptions options = builder.build();
assertEquals(options.getHost(), DEFAULT_HOST);
Copy link
Contributor

Choose a reason for hiding this comment

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

While the asserts here are consistent with what is used in the test class, we should try having a single assert statement. For ex - assertEquals(expectedConnectionOptions, actualConnectionOptions)

This provides benefits where you asserting the entire object and not picking few of the members and can help to pick code side-effects where any member got modified by mistake.

Copy link
Contributor Author

@rajatbhatta rajatbhatta Mar 6, 2023

Choose a reason for hiding this comment

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

I understand your concern here. I'll want to take this up as a separate effort, as it's changing the way we have been writing unit tests so far. Let's discuss more in details offline, and take it forward from there. Keeping this thread open until then.

@@ -151,21 +154,19 @@ public void testBuildWithAutoConfigEmulator() {

@Test
public void testBuildWithRouteToLeader() {
final String BASE_URI =
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Can be defined as private static.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are several places in this file where this can be done and replace the hard-codings with constants. We can take it as a follow-up task in a separate PR.

ConnectionOptions.Builder builder = ConnectionOptions.newBuilder();
builder.setUri(
"cloudspanner:/projects/test-project-123/instances/test-instance-123/databases/test-database-123?routeToLeader=false");
builder.setUri(BASE_URI + "?routeToLeader=false");
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Prefer StringBuilder() . + is a more expensive way of concatenating strings.

Copy link
Contributor Author

@rajatbhatta rajatbhatta Jul 21, 2023

Choose a reason for hiding this comment

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

I think for single statement, the compiler would use StringBuilder automatically: https://stackoverflow.com/a/4645155.

@rajatbhatta rajatbhatta added the owlbot:run Add this label to trigger the Owlbot post processor. label Jul 26, 2023
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Jul 26, 2023
@rajatbhatta rajatbhatta added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Jul 26, 2023
@rajatbhatta
Copy link
Contributor Author

Pls do not merge this PR until LAR is enabled by default in the Java client library.

@rajatbhatta rajatbhatta merged commit 2a85446 into googleapis:main Aug 4, 2023
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. do not merge Indicates a pull request not ready for merge, due to either quality or timing. size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants