-
Notifications
You must be signed in to change notification settings - Fork 71
fix(cloud): Handle corrupted resources gracefully in list operations #898
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
Closed
Aaron ("AJ") Steers (aaronsteers)
wants to merge
2
commits into
main
from
devin/1765244433-investigate-flaky-ci-test
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
tests/unit_tests/test_cloud_workspace_error_handling.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| # Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
| """Unit tests for CloudWorkspace error handling in list operations.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from airbyte_api.errors import SDKError | ||
|
|
||
| from airbyte.cloud.workspaces import _is_corrupted_resource_error | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "error_body,expected", | ||
| [ | ||
| pytest.param( | ||
| '{"message":"Secret reference 0c646a6d-8cfa-4c97-9615-86d8ad3bbaf8 ' | ||
| 'does not exist but is referenced in the config"}', | ||
| True, | ||
| id="secret_reference_error", | ||
| ), | ||
| pytest.param( | ||
| '{"message":"Internal server error"}', | ||
| False, | ||
| id="unrelated_500_error", | ||
| ), | ||
| pytest.param( | ||
| '{"message":"Secret reference abc123"}', | ||
| False, | ||
| id="partial_match_secret_only", | ||
| ), | ||
| pytest.param( | ||
| '{"message":"does not exist but is referenced in the config"}', | ||
| False, | ||
| id="partial_match_config_only", | ||
| ), | ||
| pytest.param( | ||
| "", | ||
| False, | ||
| id="empty_body", | ||
| ), | ||
| pytest.param( | ||
| None, | ||
| False, | ||
| id="none_body", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_is_corrupted_resource_error(error_body: str | None, expected: bool) -> None: | ||
| """Test that _is_corrupted_resource_error correctly identifies corrupted resource errors.""" | ||
| error = MagicMock(spec=SDKError) | ||
| error.body = error_body | ||
| assert _is_corrupted_resource_error(error) is expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "list_method,api_mock_path", | ||
| [ | ||
| pytest.param( | ||
| "list_destinations", | ||
| "airbyte.cloud.workspaces.api_util.list_destinations", | ||
| id="list_destinations", | ||
| ), | ||
| pytest.param( | ||
| "list_sources", | ||
| "airbyte.cloud.workspaces.api_util.list_sources", | ||
| id="list_sources", | ||
| ), | ||
| pytest.param( | ||
| "list_connections", | ||
| "airbyte.cloud.workspaces.api_util.list_connections", | ||
| id="list_connections", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_list_operations_return_empty_on_corrupted_resource( | ||
| list_method: str, api_mock_path: str | ||
| ) -> None: | ||
| """List operations should return empty list when corrupted resource error occurs.""" | ||
| corrupted_error = SDKError( | ||
| message="API error occurred", | ||
| status_code=500, | ||
| body=( | ||
| '{"message":"Secret reference 0c646a6d-8cfa-4c97-9615-86d8ad3bbaf8 ' | ||
| 'does not exist but is referenced in the config"}' | ||
| ), | ||
| raw_response=MagicMock(), | ||
| ) | ||
|
|
||
| with patch(api_mock_path) as mock_list: | ||
| mock_list.side_effect = corrupted_error | ||
|
|
||
| from airbyte.cloud.workspaces import CloudWorkspace | ||
|
|
||
| with patch.object(CloudWorkspace, "__post_init__"): | ||
| workspace = CloudWorkspace( | ||
| workspace_id="test-workspace-id", | ||
| client_id="test-client-id", | ||
| client_secret="test-client-secret", | ||
| ) | ||
|
|
||
| method = getattr(workspace, list_method) | ||
| result = method() | ||
| assert result == [] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "list_method,api_mock_path", | ||
| [ | ||
| pytest.param( | ||
| "list_destinations", | ||
| "airbyte.cloud.workspaces.api_util.list_destinations", | ||
| id="list_destinations", | ||
| ), | ||
| pytest.param( | ||
| "list_sources", | ||
| "airbyte.cloud.workspaces.api_util.list_sources", | ||
| id="list_sources", | ||
| ), | ||
| pytest.param( | ||
| "list_connections", | ||
| "airbyte.cloud.workspaces.api_util.list_connections", | ||
| id="list_connections", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_list_operations_raise_on_other_errors( | ||
| list_method: str, api_mock_path: str | ||
| ) -> None: | ||
| """List operations should raise for non-corrupted-resource errors.""" | ||
| other_error = SDKError( | ||
| message="API error occurred", | ||
| status_code=500, | ||
| body='{"message":"Internal server error"}', | ||
| raw_response=MagicMock(), | ||
| ) | ||
|
|
||
| with patch(api_mock_path) as mock_list: | ||
| mock_list.side_effect = other_error | ||
|
|
||
| from airbyte.cloud.workspaces import CloudWorkspace | ||
|
|
||
| with patch.object(CloudWorkspace, "__post_init__"): | ||
| workspace = CloudWorkspace( | ||
| workspace_id="test-workspace-id", | ||
| client_id="test-client-id", | ||
| client_secret="test-client-secret", | ||
| ) | ||
|
|
||
| method = getattr(workspace, list_method) | ||
| with pytest.raises(SDKError): | ||
| method() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Wrong. Needs to be handled on the inner loop so all valid objects are still returned. It is not sufficient to explain why we are failing. We need to not fail. Try again from scratch.
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.
You're right - returning an empty list isn't acceptable. I investigated the API layer and found the constraint:
The
SDKErroris thrown byairbyte_api.destinations.list_destinationsbefore we receive any item data. The API fails the entire request when it tries to serialize a destination with a corrupted secret reference.Options I see:
Per-page handling with binary search fallback: Move error handling into
api_util.list_destinationsat the pagination level. When a page fails with the secret error, use binary search on the offset range to isolate the corrupted item(s) and return all valid items from that page. This gives true "inner loop" semantics.Alternative endpoint fallback: If there's a Config API endpoint that can list destination IDs without resolving secret-backed configs, we could use that as a fallback to enumerate IDs, then call
get_destinationper-item with inner-loop error handling.Backend fix: Address this in the backend so the list API doesn't fail entirely when one item is corrupted.
Which approach would you prefer? Option 1 is the most self-contained client-side fix but adds complexity. Happy to implement whichever you think is best.