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: invalidate cache on bad connection info and failed IP lookup #389

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

rhatgadkar-goog
Copy link
Collaborator

@rhatgadkar-goog rhatgadkar-goog commented Nov 12, 2024

This change is analogous to CloudSQL's GoogleCloudPlatform/cloud-sql-python-connector#1118.

The Connector caches connection info for future connections and schedules refresh operations. For unrecoverable errors, the cache should be invalidated to stop future bad refreshes. The cache should also be invalidated on failed IP lookups. This PR does these things.

@rhatgadkar-goog rhatgadkar-goog linked an issue Nov 12, 2024 that may be closed by this pull request
@rhatgadkar-goog rhatgadkar-goog changed the title WIP: feat: invalidate cache on bad connections feat: invalidate cache on bad connections Nov 14, 2024
@rhatgadkar-goog rhatgadkar-goog marked this pull request as ready for review November 14, 2024 00:15
@rhatgadkar-goog rhatgadkar-goog requested a review from a team as a code owner November 14, 2024 00:15
Copy link
Collaborator

@jackwotherspoon jackwotherspoon left a comment

Choose a reason for hiding this comment

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

Couple small cleanups, otherwise looks great!

Also let's make sure to add a PR description as this work is non-trivial.

google/cloud/alloydb/connector/async_connector.py Outdated Show resolved Hide resolved
google/cloud/alloydb/connector/connector.py Outdated Show resolved Hide resolved
tests/unit/mocks.py Outdated Show resolved Hide resolved
tests/unit/mocks.py Outdated Show resolved Hide resolved
Comment on lines 313 to 316
connector._client = FakeAlloyDBClient(
instance=FakeInstance(name="bad-test-instance")
)
cache = RefreshAheadCache(instance_uri, connector._client, connector._keys)
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 not just use the fake_client fixture for these tests?

def fake_client(fake_instance: FakeInstance) -> FakeAlloyDBClient:
return FakeAlloyDBClient(fake_instance)

The connector should just use the cache's client, i don't think we need to override the connector._client

Suggested change
connector._client = FakeAlloyDBClient(
instance=FakeInstance(name="bad-test-instance")
)
cache = RefreshAheadCache(instance_uri, connector._client, connector._keys)
cache = RefreshAheadCache(instance_uri, fake_client, connector._keys)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

From what I'm observing: if I change a member variable of a fixture object, it affects other test cases that are run in the session.

For example, if test_Connector_remove_cached_bad_instance is run right before test_connect in a test session, and if I do something like fake_client.instance.name = "bad-test-instace" in test_Connector_remove_cached_bad_instance, this will also be propagated to test_connect.

So that's why I'm not using the fixture here. Because I was seeing similar behavior when running nox -s unit-3.11.

Copy link
Collaborator

@jackwotherspoon jackwotherspoon Nov 14, 2024

Choose a reason for hiding this comment

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

@rhatgadkar-goog you do not need to change any of the fake_client.instance attributes... since you do not need the bad-instance to be a FakeInstance it should give a 404 without needing it...

  instance_uri = "projects/test-project/locations/test-region/clusters/test-cluster/instances/bad-test-instance"
  async with AsyncConnector(credentials=credentials) as connector:
      cache = RefreshAheadCache(instance_uri, fake_client, connector._keys)
      connector._cache[instance_uri] = cache
      with pytest.raises(ClientResponseError):
          await connector.connect(instance_uri, "asyncpg")
      assert instance_uri not in connector._cache

Copy link
Collaborator Author

@rhatgadkar-goog rhatgadkar-goog Nov 15, 2024

Choose a reason for hiding this comment

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

Ok, I did this for test_Connector_remove_cached_bad_instance. I think I still need to initialize a FakeAlloyDBClient for test_Connector_remove_cached_no_ip_type to override the instance's IP address and to not send an actual API request.

"""
instance_uri = "projects/test-project/locations/test-region/clusters/test-cluster/instances/test-instance"
# set instance to only have Public IP
fake_client = FakeAlloyDBClient()
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here and below spots, can we not use the fake_client fixture?

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.

LGTM. I'll wait for you to address Jack's comments and am happy to approve.

@rhatgadkar-goog rhatgadkar-goog changed the title feat: invalidate cache on bad connections feat: invalidate cache on bad connection info and failed IP lookup Nov 15, 2024
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.

Failed connection calls should stop the background refresh
3 participants