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

refactor: Add EndpointContext #2275

Merged
merged 13 commits into from
Dec 8, 2023
Merged

refactor: Add EndpointContext #2275

merged 13 commits into from
Dec 8, 2023

Conversation

lqiu96
Copy link
Contributor

@lqiu96 lqiu96 commented Nov 29, 2023

Thank you for opening a Pull Request! For general contributing guidelines, please refer to contributing guide

Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Description

EndpointContext is a new class created to resolve the correct endpoint. Client library users are able customize the endpoint in various ways and this class is the source of truth to resolve all the customizations.

Currently supports:

  • ClientSettings/ StubSettings endpoint value
  • mTLS endpoint value
  • switchToMtls flag

The logic to resolve the endpoint was moved from the ClientContext into the EndpointContext. In this PR, no additional logic was added.

@product-auto-label product-auto-label bot added the size: l Pull request size is large. label Nov 29, 2023
@lqiu96 lqiu96 added the owlbot:run Add this label to trigger the Owlbot post processor. label Nov 29, 2023
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Nov 29, 2023
@blakeli0
Copy link
Collaborator

Can we integrate EndpointContext into the existing code and make sure all functionalities are not affected? It may not be a good practice to have a chore PR introduces new classes without any reference to it. Because it does not provide additional functionality, and it's not easy for reviewers to understand the purpose of the new class. It would be better if we can make this PR a proper refactor.

@lqiu96 lqiu96 changed the title chore: Add EndpointContext feat: Add EndpointContext Nov 30, 2023
@lqiu96 lqiu96 added the owlbot:run Add this label to trigger the Owlbot post processor. label Dec 1, 2023
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Dec 1, 2023
@lqiu96 lqiu96 marked this pull request as ready for review December 1, 2023 02:07
@lqiu96 lqiu96 requested a review from a team as a code owner December 1, 2023 02:07
@lqiu96
Copy link
Contributor Author

lqiu96 commented Dec 1, 2023

Can we integrate EndpointContext into the existing code and make sure all functionalities are not affected? It may not be a good practice to have a chore PR introduces new classes without any reference to it. Because it does not provide additional functionality, and it's not easy for reviewers to understand the purpose of the new class. It would be better if we can make this PR a proper refactor.

Added usage of the EndpointContext in the ClientContext to determine the endpoint.

@lqiu96 lqiu96 requested a review from blakeli0 December 1, 2023 17:54
public abstract String clientSettingsEndpoint();

@Nullable
public abstract String transportChannelEndpoint();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will we ever need to pass in transportChannelEndpoint?
I see that there is no getters for the endpoint set to transport channel, maybe we want to expose one? So that we can set all the info to EndpointContext?
It's also OK to leave transportChannelEndpoint out of EndpointContext for now, if that's the case, then it's better to remove this field at this moment.

Copy link
Contributor Author

@lqiu96 lqiu96 Dec 5, 2023

Choose a reason for hiding this comment

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

Will we ever need to pass in transportChannelEndpoint?

I believe so to keep with the existing behavior where transportChannelEndpoint overrides the clientSettingsEndpoint if set. Right now, the ClientContext doesn't set it inside the EndpointContext but the code to use it exists here:

if (transportChannelProvider.needsEndpoint()) {
transportChannelProvider = transportChannelProvider.withEndpoint(endpoint);
}

The reason I haven't added it in yet is because I most likely will need to expose an additional getEndpoint() method to the TransportChannelProvider: https://github.com/googleapis/sdk-platform-java/blob/da6607b17130ab045640618d505fda915ddb8e49/gax-java/gax/src/main/java/com/google/api/gax/rpc/TransportChannelProvider.java (Note: grpc and httpjson's transportchannel already have a getEndpoint() method). I was planning on doing this on the next PR as this one only introduces the EndpointContext.

I see your point about including it then, so I'll drop the transportChannelEndpoint values from this PR and add it in the next PR.

return endpoint;
}

public String resolveEndpoint() throws IOException {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we try to resolve the endpoint in the build() method and just expose a getter for the resolved endpoint?

Copy link
Contributor Author

@lqiu96 lqiu96 Dec 5, 2023

Choose a reason for hiding this comment

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

I'm using Autovalue's Builder which I believe generates the build() implementation for us. I could create my own Builder class if that's preferable.


Edit: Actually, I think I see a section that I can configure. I'll try that.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, it's a common practice to validate or normalize a field value in the build() method, see internal docs here. We have examples in our repo as well.

@AutoValue
public abstract class EndpointContext {
@Nullable
public abstract String clientSettingsEndpoint();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this can be null as it is coming from StubSettings.endpoint?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe for most non-modified GAPICs, this is the case. It should be set via the {Service}StubSettings like: https://github.com/googleapis/google-cloud-java/blob/6227504db7c511b379cad2d67960d63f36eed39b/java-grafeas/src/main/java/io/grafeas/v1/stub/GrafeasStubSettings.java#L635 with a non-null value (as the generator will fail if it can't find it).

I'm accounting for the Grafeas edge case: https://github.com/googleapis/google-cloud-java/blob/6227504db7c511b379cad2d67960d63f36eed39b/java-grafeas/src/main/java/io/grafeas/v1/stub/GrafeasStubSettings.java#L403-L405 which has post-processed changes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see, this is good for now then. Let's keep this in mind and come back for it later if we have time. I'm not sure why we removed the default endpoint for grafeas, and how does grafeas create channels without a default endpoint. But ideally this should not be nullable, otherwise gRPC channel creation could fail.

@lqiu96 lqiu96 force-pushed the add-endpoint-context branch from 1f56ada to a6eeda8 Compare December 6, 2023 18:00
@lqiu96
Copy link
Contributor Author

lqiu96 commented Dec 6, 2023

Removed the methods and seeing CLIRR check failures:

I've removed the public functions and seeing the CLIRR failure:
Error:  7002: com.google.api.gax.rpc.EndpointContext: Method 'public java.lang.String resolveEndpoint()' has been removed
Error:  7002: com.google.api.gax.rpc.EndpointContext: Method 'public java.lang.String transportChannelEndpoint()' has been removed
Error:  7002: com.google.api.gax.rpc.EndpointContext$Builder: Method 'public com.google.api.gax.rpc.EndpointContext$Builder setTransportChannelEndpoint(java.lang.String)' has been removed

This was due to the old gax version being cached in gh actions. Cleared the cache and re-ran.

Copy link

sonarqubecloud bot commented Dec 7, 2023

[gapic-generator-java-root] SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

Copy link

sonarqubecloud bot commented Dec 7, 2023

[java_showcase_integration_tests] SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

74.4% 74.4% Coverage
0.0% 0.0% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

@lqiu96 lqiu96 requested a review from blakeli0 December 7, 2023 22:37
Copy link
Collaborator

@blakeli0 blakeli0 left a comment

Choose a reason for hiding this comment

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

LGTM. Before merging, can you please add more info to the description to explain the purpose of EndpointContext?
I would reword the title as well, I think this is more like a refactor instead of feat. Because we added a new class to encapsulate existing features but didn't introduce any new features.

@lqiu96 lqiu96 changed the title feat: Add EndpointContext refactor: Add EndpointContext Dec 8, 2023
@lqiu96
Copy link
Contributor Author

lqiu96 commented Dec 8, 2023

LGTM. Before merging, can you please add more info to the description to explain the purpose of EndpointContext? I would reword the title as well, I think this is more like a refactor instead of feat. Because we added a new class to encapsulate existing features but didn't introduce any new features.

Added.

@lqiu96 lqiu96 merged commit 2cb0f9e into main Dec 8, 2023
32 of 34 checks passed
@lqiu96 lqiu96 deleted the add-endpoint-context branch December 8, 2023 02:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: l Pull request size is large.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants