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 lazy refresh strategy to the connector. Fixes #992. #1990

Merged
merged 1 commit into from
May 29, 2024

Conversation

hessjcg
Copy link
Collaborator

@hessjcg hessjcg commented May 20, 2024

The lazy refresh strategy only refreshes credentials and certificate information when the application attempts
to establish a new database connection. On Cloud Run and other serverless runtimes, this is more reliable
than the default background refresh strategy.

Fixes #992.

MIN_REFERSH_DELAY_MS);

ConnectionMetadata gotMetadata = connectionInfoCache.getConnectionMetadata(TEST_TIMEOUT_MS);
assertThat(gotMetadata.getKeyManagerFactory())
Copy link
Member

Choose a reason for hiding this comment

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

What is the significance of this assertion? Would the counter check be enough for an assertion?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed. I added comments to explain.

AuthType authType,
KeyPair keyPair) {
try {
Thread.sleep(100);
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to sleep 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.

The test needs to simulate a blocking call.

@hessjcg hessjcg force-pushed the gh-992-implement-lazy-strategy branch from 1abf29b to 84967d7 Compare May 21, 2024 20:04
@hessjcg hessjcg requested a review from enocom May 21, 2024 20:05
@hessjcg hessjcg force-pushed the gh-992-implement-lazy-strategy branch from 84967d7 to 0c74936 Compare May 21, 2024 20:06
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.

I've elaborated below, but the gist of this review is: we should be using a ConnectionInfoCache interface and then provide multiple implementations (refresh ahead, lazy, etc).

} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
return new DefaultConnectionInfoCache(
Copy link
Member

Choose a reason for hiding this comment

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

Looking more closely at this, I see we're missing a ConnectionInfoCache interface in this repo (which exists in Go and in AlloyDB Java). Instead of having multiple constructors for one cache type, we should be introducing separate caches that both implement a ConnectionInfoCache interface. This makes for better separation of concerns. The refresher strategy might still be useful, but the most important thing it to think of connection info cache as being the interface for which we have multiple implementations.

@enocom enocom self-requested a review May 23, 2024 15:33
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.

The ConnectionInfoCache interface is a blocking concern here.

@hessjcg hessjcg force-pushed the gh-992-implement-lazy-strategy branch from 0c74936 to a42f60c Compare May 23, 2024 20:23
@hessjcg hessjcg marked this pull request as draft May 23, 2024 20:25
@hessjcg hessjcg force-pushed the gh-992-implement-lazy-strategy branch from a42f60c to f8091ae Compare May 23, 2024 20:28
@hessjcg hessjcg changed the base branch from main to gh-992-refactor-refresh-ahead-cache May 23, 2024 20:29
@hessjcg
Copy link
Collaborator Author

hessjcg commented May 23, 2024

I made this a draft and created a new PR with the refactoring work to introduce a ConnectionInfoCache interface. #1997

Once #1997 is merged, we can pick up the review of this one again.

@hessjcg hessjcg force-pushed the gh-992-implement-lazy-strategy branch from f8091ae to 31e7eab Compare May 23, 2024 20:34
@hessjcg hessjcg force-pushed the gh-992-refactor-refresh-ahead-cache branch 4 times, most recently from 0ca91ff to 6143994 Compare May 28, 2024 17:26
@hessjcg hessjcg force-pushed the gh-992-implement-lazy-strategy branch from 31e7eab to 155809a Compare May 28, 2024 17:26
Copy link

conventional-commit-lint-gcf bot commented May 28, 2024

🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use automerge label. Good luck human!

-- conventional-commit-lint bot
https://conventionalcommits.org/

@hessjcg hessjcg force-pushed the gh-992-refactor-refresh-ahead-cache branch from 6143994 to 84ef897 Compare May 28, 2024 17:44
@hessjcg hessjcg force-pushed the gh-992-implement-lazy-strategy branch 2 times, most recently from 496b4f1 to ccb7678 Compare May 28, 2024 19:47
@hessjcg hessjcg force-pushed the gh-992-refactor-refresh-ahead-cache branch from 84ef897 to 86a443e Compare May 28, 2024 19:47
@hessjcg hessjcg force-pushed the gh-992-implement-lazy-strategy branch from ccb7678 to cd26691 Compare May 29, 2024 15:03
@hessjcg hessjcg force-pushed the gh-992-refactor-refresh-ahead-cache branch from 86a443e to f7de7c0 Compare May 29, 2024 15:03
Base automatically changed from gh-992-refactor-refresh-ahead-cache to main May 29, 2024 15:08
@hessjcg hessjcg force-pushed the gh-992-implement-lazy-strategy branch from cd26691 to 9518af3 Compare May 29, 2024 15:13
@hessjcg hessjcg marked this pull request as ready for review May 29, 2024 15:13
@hessjcg hessjcg force-pushed the gh-992-implement-lazy-strategy branch 2 times, most recently from 10b3cf1 to e5d7f08 Compare May 29, 2024 15:19
@hessjcg hessjcg requested a review from enocom May 29, 2024 15:20
@hessjcg
Copy link
Collaborator Author

hessjcg commented May 29, 2024

Refactoring is complete. Please give this Lazy Refresh implementation another look.

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.

One small test change and then LGTM.

ConnectionMetadata gotMetadata = connectionInfoCache.getConnectionMetadata(300);

// Assert that the underlying ConnectionInfo was retrieved exactly once.
assertThat(instanceDataSupplier.counter.get()).isEqualTo(1);
Copy link
Member

Choose a reason for hiding this comment

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

Let's call the method above twice to prove we're calling the underlying instance data supplier only once.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

@hessjcg hessjcg force-pushed the gh-992-implement-lazy-strategy branch from e5d7f08 to fa7848e Compare May 29, 2024 17:33
@hessjcg hessjcg requested a review from enocom May 29, 2024 17: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.

You'll need to fix the conventional commit error. Otherwise LGTM.

@hessjcg hessjcg merged commit d84d082 into main May 29, 2024
16 of 17 checks passed
@hessjcg hessjcg deleted the gh-992-implement-lazy-strategy branch May 29, 2024 17:59
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.

Add a "lazy strategy" option that prevents refreshes from happening in the background
3 participants