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: Refactor RefreshAheadConnectionInfoCache. Part of #992. #1997

Merged
merged 3 commits into from
May 29, 2024

Conversation

hessjcg
Copy link
Collaborator

@hessjcg hessjcg commented May 23, 2024

This makes a number of refactoring changes to align the Java connector with other implementations.

  • Introduce a ConnectionInfoCache interface
  • Rename DefaultConnectionInfoCache to RefreshAheadConnectionInfoCache
  • Update and simplify instantiation logic of RefreshAheadConnectionInfoCache

* @param executor executor used to schedule asynchronous tasks
* @param keyPair public/private key pair used to authenticate connections
*/
public static RefreshAheadConnectionInfoCache newInstance(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have to use a static factory method instead of a constructor because it has to instantiate an AccessTokenSupplier and RefreshAheadStrategy before it calls the constructor.,

@hessjcg hessjcg force-pushed the gh-992-refactor-refresh-ahead-cache branch from e0fa4bb to e9708f6 Compare May 23, 2024 20:34
Copy link
Member

@enocom enocom left a comment

Choose a reason for hiding this comment

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

What if the ConnectinInfoCache just returned the connection info instead of creating SSL sockets? Recently, in Go and Python we've been pulling apart the thing that caches connection info (e.g., the "cache") from the thing that creates SSL sockets (e.g., the "connector"). That's what we're doing in AlloyDB and it's working well: https://github.com/GoogleCloudPlatform/alloydb-java-connector/blob/main/alloydb-jdbc-connector/src/main/java/com/google/cloud/alloydb/ConnectionInfoCache.java

@jackwotherspoon jackwotherspoon changed the title chore: Refactor RefreshAheadConnectionInfoCache. Part of #992. refactor: Refactor RefreshAheadConnectionInfoCache. Part of #992. May 23, 2024
@hessjcg
Copy link
Collaborator Author

hessjcg commented May 24, 2024

I'll remove the createSslSocket() method from the ConnectionInfoCache. It really is only a convenience method anyway.

@hessjcg hessjcg force-pushed the gh-992-refactor-refresh-ahead-cache branch from e9708f6 to 3716afd Compare May 24, 2024 20:52
@hessjcg hessjcg requested a review from enocom May 24, 2024 20:52
this.instanceName = new CloudSqlInstanceName(config.getCloudSqlInstance());
this.config = config;

abstract class BaseConnectionInfoCache implements ConnectionInfoCache {
Copy link
Member

Choose a reason for hiding this comment

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

The base class is delegating all calls to the refresh strategy. What if we avoid introducing more indirection and just had the implementations of ConnectionInfoCache call through to their refresher?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I removed the base class.


RefreshStrategy getRefresher();

CloudSqlInstanceName getInstanceName();
Copy link
Member

Choose a reason for hiding this comment

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

This is used only in test. Let's remove the method here and either drop the usages in test or find a more direct way at getting at the information the test wants.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I removed this method too.


void refreshIfExpired();

RefreshStrategy getRefresher();
Copy link
Member

Choose a reason for hiding this comment

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

This is a test-only method. How can we avoid adding it here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've removed it from the interface.

long minRefreshDelayMs) {

AccessTokenSupplier accessTokenSupplier =
BaseConnectionInfoCache.newAccessTokenSupplier(config, tokenSourceFactory);
Copy link
Member

Choose a reason for hiding this comment

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

This static method is making our lives harder. Why don't we pass in the access token supplier from where we instantiate the cache?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Refactored, removing the base class, so that this can be a normal constructor.

@hessjcg hessjcg force-pushed the gh-992-refactor-refresh-ahead-cache branch 3 times, most recently from 6143994 to 84ef897 Compare May 28, 2024 17:44
@@ -51,4 +52,29 @@ Map<IpType, String> getIpAddrs() {
SslData getSslData() {
return sslData;
}

ConnectionMetadata toConnectionMetadata(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved here because BaseConnectionInfoCache was deleted.

(Additional context: ConnectionMetadata is part of the legacy public Java API contract.)

Copy link
Member

Choose a reason for hiding this comment

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

This seems like the perfect place for such concerns -- basically the connection info data class.

@@ -43,6 +44,14 @@ class DefaultAccessTokenSupplier implements AccessTokenSupplier {
private final int retryCount;
private final Duration retryDuration;

static AccessTokenSupplier newInstance(AuthType authType, CredentialFactory tokenSourceFactory) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved here because BaseConnectionInfoCache was deleted.

Copy link
Member

Choose a reason for hiding this comment

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

Perfect spot.

@hessjcg hessjcg requested a review from enocom May 28, 2024 17:44
Copy link
Member

@enocom enocom left a comment

Choose a reason for hiding this comment

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

There's one use of public to probably remove. Otherwise LGTM.

@@ -51,4 +52,29 @@ Map<IpType, String> getIpAddrs() {
SslData getSslData() {
return sslData;
}

ConnectionMetadata toConnectionMetadata(
Copy link
Member

Choose a reason for hiding this comment

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

This seems like the perfect place for such concerns -- basically the connection info data class.

package com.google.cloud.sql.core;

/** ConnectionInfoCache is the contract for a caching strategy for ConnectionInfo. */
public interface ConnectionInfoCache {
Copy link
Member

Choose a reason for hiding this comment

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

Do we want this to be public?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I removed public.

@@ -43,6 +44,14 @@ class DefaultAccessTokenSupplier implements AccessTokenSupplier {
private final int retryCount;
private final Duration retryDuration;

static AccessTokenSupplier newInstance(AuthType authType, CredentialFactory tokenSourceFactory) {
Copy link
Member

Choose a reason for hiding this comment

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

Perfect spot.

@hessjcg hessjcg force-pushed the gh-992-refactor-refresh-ahead-cache branch from 84ef897 to 86a443e Compare May 28, 2024 19:47
@hessjcg hessjcg enabled auto-merge (squash) May 28, 2024 19:48
@hessjcg hessjcg disabled auto-merge May 28, 2024 19:48
@hessjcg hessjcg force-pushed the gh-992-refactor-refresh-ahead-cache branch from 86a443e to f7de7c0 Compare May 29, 2024 15:03
@hessjcg hessjcg enabled auto-merge (squash) May 29, 2024 15:04
@hessjcg hessjcg merged commit d97a93b into main May 29, 2024
17 checks passed
@hessjcg hessjcg deleted the gh-992-refactor-refresh-ahead-cache branch May 29, 2024 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants