Repository navigation
fix(spanner): add stale-instance cleanup and scrubbing to async tests #19260
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,14 +12,77 @@ | |
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import time | ||
|
|
||
| import pytest | ||
| from google.api_core import exceptions | ||
|
|
||
| from google.cloud import spanner_v1 | ||
| from google.cloud.spanner_admin_database_v1 import DatabaseDialect | ||
| from google.cloud.spanner_v1._async import instance as async_instance_mod | ||
|
|
||
| from .. import _helpers | ||
|
|
||
|
|
||
| async def _scrub_instance_ignore_not_found(to_scrub): | ||
| """Delete backups, disable DB drop protection, and delete an async instance.""" | ||
| try: | ||
| async for database_pb in await to_scrub.list_databases(): | ||
| db = await to_scrub.database(database_pb.name.split("/")[-1]) | ||
| try: | ||
| await db.reload() | ||
| if db.enable_drop_protection: | ||
| db.enable_drop_protection = False | ||
| operation = await db.update(["enable_drop_protection"]) | ||
| await operation.result( | ||
| _helpers.DATABASE_OPERATION_TIMEOUT_IN_SECONDS | ||
| ) | ||
| except exceptions.NotFound: | ||
| pass | ||
| except exceptions.NotFound: | ||
| return | ||
|
|
||
| try: | ||
| async for backup_pb in await to_scrub.list_backups(): | ||
| for db_name in backup_pb.referencing_databases: | ||
| db = await to_scrub.database(db_name.split("/")[-1]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above: to_scrub is an async Instance (google.cloud.spanner_v1._async.instance.Instance), where database() is async def because it awaits db._pool.bind(db). Keeping await here to match the rest of tests/system/_async/. |
||
| try: | ||
| await db.drop() | ||
| except exceptions.NotFound: | ||
| pass | ||
| try: | ||
| await to_scrub._client.database_admin_api.delete_backup( | ||
| name=backup_pb.name | ||
| ) | ||
| except exceptions.NotFound: | ||
| pass | ||
| except (exceptions.MethodNotImplemented, exceptions.NotFound): | ||
| pass | ||
|
|
||
| try: | ||
| await to_scrub.delete() | ||
| except exceptions.NotFound: | ||
| pass | ||
|
|
||
|
|
||
| async def _cleanup_old_instances(spanner_client): | ||
| """Sweep stale labeled system-test instances older than 3 hours.""" | ||
| cutoff = int(time.time()) - 3 * 60 * 60 | ||
| instance_filter = "labels.python-spanner-systests:true" | ||
|
|
||
| async for instance_pb in await spanner_client.list_instances( | ||
| filter_=instance_filter | ||
| ): | ||
| instance = async_instance_mod.Instance.from_pb(instance_pb, spanner_client) | ||
| if "created" in instance.labels: | ||
| try: | ||
| create_time = int(instance.labels["created"]) | ||
| except ValueError: | ||
| continue | ||
| if create_time <= cutoff: | ||
| await _scrub_instance_ignore_not_found(instance) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def spanner_client(): | ||
| if _helpers.USE_EMULATOR: | ||
|
|
@@ -140,10 +203,9 @@ async def shared_instance( | |
| instance_config, | ||
| ): | ||
| spanner_client._instance_admin_api = None | ||
| await _cleanup_old_instances(spanner_client) | ||
|
|
||
| if _helpers.CREATE_INSTANCE: | ||
| import time | ||
|
|
||
| from google.cloud.spanner_admin_instance_v1.types import spanner_instance_admin | ||
|
|
||
| create_time = str(int(time.time())) | ||
|
|
@@ -160,22 +222,21 @@ async def shared_instance( | |
| edition=spanner_instance_admin.Instance.Edition.ENTERPRISE, | ||
| ), | ||
| ) | ||
| created_op = await spanner_client.instance_admin_api.create_instance( | ||
| request=request | ||
| ) | ||
| await created_op.result(instance_operation_timeout) | ||
|
|
||
| instance = spanner_client.instance( | ||
| shared_instance_id, instance_config.name, labels=labels | ||
| ) | ||
| created_op = await spanner_client.instance_admin_api.create_instance( | ||
| request=request | ||
| ) | ||
| try: | ||
| await created_op.result(instance_operation_timeout) | ||
| yield instance | ||
| finally: | ||
| await _scrub_instance_ignore_not_found(instance) | ||
| else: | ||
| instance = spanner_client.instance(shared_instance_id, instance_config.name) | ||
| await instance.reload() | ||
|
|
||
| yield instance | ||
|
|
||
| if _helpers.CREATE_INSTANCE: | ||
| await instance.delete() | ||
| yield instance | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Instance.databasemethod is a synchronous factory method that returns aDatabaseobject. Awaiting it will raise aTypeErrorat runtime because the returned object is not awaitable. Remove theawaitkeyword.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to_scrub is an async Instance (google.cloud.spanner_v1._async.instance.Instance), where database() is an async def that awaits db._pool.bind(db). Removing await here would return an unawaited coroutine and break db.reload().