Repository navigation
Conversation
Deleting a Spanner backup waits for any optimizing restored databases in referencing_databases before calling DatabaseAdminClient.delete_backup. Add spanner_delete_backup.
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces a script and associated tests for deleting a Google Cloud Spanner database backup after waiting for referencing databases to finish optimizing. The feedback recommends adding a timeout mechanism and status logging to the polling loop in delete_backup to prevent indefinite hangs, and gracefully skipping the live integration test if the required environment variable is missing.
| def wait_until_unreferenced() -> None: | ||
| backup = client.get_backup(request=get_request) | ||
| while backup.referencing_databases: | ||
| time.sleep(10) | ||
| backup = client.get_backup(request=get_request) |
There was a problem hiding this comment.
The current implementation of wait_until_unreferenced loops indefinitely without providing any feedback to the user or having a timeout mechanism. Since database optimization after a restore can take a significant amount of time, a silent infinite loop can make the script appear frozen.
Adding a status message inside the loop and a timeout mechanism (e.g., 30 minutes) will improve usability and prevent the script from hanging indefinitely in CI/CD pipelines or local executions.
def wait_until_unreferenced(timeout_seconds: int = 1800) -> None:
start_time = time.time()
backup = client.get_backup(request=get_request)
while backup.referencing_databases:
if time.time() - start_time > timeout_seconds:
raise TimeoutError(
f"Timed out after {timeout_seconds} seconds waiting for backup to be unreferenced."
)
print(
f"Backup is still referenced by {len(backup.referencing_databases)} database(s). "
"Waiting 10 seconds...",
file=sys.stderr,
)
time.sleep(10)
backup = client.get_backup(request=get_request)There was a problem hiding this comment.
Good catch, done: the wait now prints a progress message, stops after 30 minutes, and reports the timeout on stderr; a unit test covers it (1336509).
|
|
||
| @conftest.live_only | ||
| def test_delete_backup_live(capsys: pytest.CaptureFixture[str]) -> None: | ||
| disposable_bkp = os.environ["SPANNER_DISPOSABLE_BACKUP_ID"] |
There was a problem hiding this comment.
Accessing os.environ["SPANNER_DISPOSABLE_BACKUP_ID"] directly will raise a KeyError if the environment variable is not set. It is safer to use os.environ.get and skip the test gracefully if the variable is missing, especially since this is a live integration test.
disposable_bkp = os.environ.get("SPANNER_DISPOSABLE_BACKUP_ID")
if not disposable_bkp:
pytest.skip("SPANNER_DISPOSABLE_BACKUP_ID environment variable not set.")There was a problem hiding this comment.
Good catch, done: the live test now checks for its required environment variables up front and skips with a clear message when one is missing, instead of failing with a KeyError (1336509).
Problem: review of this sample identified correctness and robustness improvements in the sample and its tests. Solution: apply the accepted review suggestions and extend the tests to cover the updated behavior. Details are in the review thread replies. Files updated: - spanner/backups/src/spanner_backups/delete_backup.py - spanner/backups/tests/delete_backup_test.py
Description
Adds
spanner_delete_backupcheckingreferencing_databasesand deleting a Spanner backup.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions