-
Notifications
You must be signed in to change notification settings - Fork 16.8k
Add transient-error retry to SalesforceBulkOperator #64575
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
Merged
potiuk
merged 21 commits into
apache:main
from
nagasrisai:feat/salesforce-bulk-transient-retry
Apr 6, 2026
+300
−24
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a600fa4
Add tests for SalesforceBulkOperator transient-error retry
nagasrisai cd74153
Add transient-error retry to SalesforceBulkOperator
nagasrisai 516eaa9
Merge branch 'main' into feat/salesforce-bulk-transient-retry
nagasrisai 0d3d665
Merge branch 'main' into feat/salesforce-bulk-transient-retry
nagasrisai 3250d1a
Fix lint: remove unused pytest import and dead variable assignments
nagasrisai cd7b6b0
Add input validation for max_retries, retry_delay, and transient_erro…
nagasrisai a117bcf
Merge branch 'main' into feat/salesforce-bulk-transient-retry
nagasrisai 9ba6b41
Fix IndentationError in _validate_inputs: use consistent 8-space indent
nagasrisai 419c916
Rename retry_delay → bulk_retry_delay to avoid collision with BaseOpe…
nagasrisai 774c3e0
Update tests: retry_delay → bulk_retry_delay
nagasrisai 38c9dcc
Fix: correct mock chain for hook conn.bulk; ruff format long dicts
nagasrisai 2ba32a3
Fix: remove list() from _run_operation, add to retry call; fix ruff f…
nagasrisai 6f3d72d
Apply ruff format: split long lines, wrap method signature and retry …
nagasrisai 351fea5
Apply ruff format: split long dicts in test helpers
nagasrisai a448191
Fix ruff: reformat with line-length=110 (Airflow project standard)
nagasrisai 49c75ce
Fix mypy: cast(list,...) in _run_operation; fix ruff: use line-length…
nagasrisai 4896b4f
Fix ruff: use cast("list",...) string-quoted form (TC rule)
nagasrisai b97c8ac
Merge branch 'main' into feat/salesforce-bulk-transient-retry
nagasrisai 400168f
Merge branch 'main' into feat/salesforce-bulk-transient-retry
nagasrisai bf41e32
Merge branch 'main' into feat/salesforce-bulk-transient-retry
nagasrisai 6e067ea
Merge branch 'main' into feat/salesforce-bulk-transient-retry
nagasrisai 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
199 changes: 199 additions & 0 deletions
199
providers/salesforce/tests/unit/salesforce/operators/test_bulk_retry.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,199 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| from __future__ import annotations | ||
|
|
||
| from unittest import mock | ||
|
|
||
| from airflow.providers.salesforce.operators.bulk import SalesforceBulkOperator | ||
|
|
||
|
|
||
| def _make_op(**kwargs): | ||
| defaults = dict( | ||
| task_id="test_task", | ||
| operation="insert", | ||
| object_name="Contact", | ||
| payload=[{"FirstName": "Ada"}, {"FirstName": "Grace"}], | ||
| ) | ||
| defaults.update(kwargs) | ||
| return SalesforceBulkOperator(**defaults) | ||
|
|
||
|
|
||
| def _transient_failure(status_code="UNABLE_TO_LOCK_ROW"): | ||
| return { | ||
| "success": False, | ||
| "errors": [{"statusCode": status_code, "message": "locked", "fields": []}], | ||
| } | ||
|
|
||
|
|
||
| def _permanent_failure(): | ||
| return { | ||
| "success": False, | ||
| "errors": [ | ||
| { | ||
| "statusCode": "REQUIRED_FIELD_MISSING", | ||
| "message": "missing", | ||
| "fields": ["Name"], | ||
| } | ||
| ], | ||
| } | ||
|
|
||
|
|
||
| def _success(): | ||
| return {"success": True, "errors": []} | ||
|
|
||
|
|
||
| class TestSalesforceBulkOperatorRetry: | ||
| def test_no_retry_when_max_retries_zero(self): | ||
| op = _make_op(max_retries=0) | ||
| assert op.max_retries == 0 | ||
|
|
||
| bulk_mock = mock.MagicMock() | ||
| bulk_mock.__getattr__("Contact").insert.return_value = [_success(), _success()] | ||
|
|
||
| with mock.patch("airflow.providers.salesforce.operators.bulk.SalesforceHook") as hook_cls: | ||
| hook_cls.return_value.get_conn.return_value.bulk = bulk_mock | ||
| result = op.execute(context={}) | ||
|
|
||
nagasrisai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert result == [_success(), _success()] | ||
| assert bulk_mock.__getattr__("Contact").insert.call_count == 1 | ||
|
|
||
| def test_transient_failure_is_retried(self): | ||
| op = _make_op(max_retries=2, bulk_retry_delay=0) | ||
|
|
||
| first_result = [_transient_failure(), _success()] | ||
| second_result = [_success()] | ||
|
|
||
| run_mock = mock.MagicMock(side_effect=[first_result, second_result]) | ||
|
|
||
| with mock.patch.object(op, "_run_operation", run_mock): | ||
| with mock.patch("airflow.providers.salesforce.operators.bulk.time.sleep"): | ||
| final = op._retry_transient_failures( | ||
| bulk=mock.MagicMock(), | ||
| payload=[{"FirstName": "Ada"}, {"FirstName": "Grace"}], | ||
| result=first_result, | ||
| ) | ||
|
|
||
| assert final[0] == _success() | ||
| assert final[1] == _success() | ||
| assert run_mock.call_count == 2 | ||
| retry_call = run_mock.call_args_list[1] | ||
| assert retry_call == mock.call(mock.ANY, [{"FirstName": "Ada"}]) | ||
|
|
||
| def test_permanent_failure_is_not_retried(self): | ||
| op = _make_op(max_retries=3, bulk_retry_delay=0) | ||
| result = [_permanent_failure(), _success()] | ||
|
|
||
| run_mock = mock.MagicMock() | ||
|
|
||
| with mock.patch.object(op, "_run_operation", run_mock): | ||
| final = op._retry_transient_failures( | ||
| bulk=mock.MagicMock(), | ||
| payload=[{"FirstName": "Ada"}, {"FirstName": "Grace"}], | ||
| result=result, | ||
| ) | ||
|
|
||
| run_mock.assert_not_called() | ||
| assert final[0] == _permanent_failure() | ||
|
|
||
| def test_retries_stop_after_max_retries(self): | ||
| op = _make_op(max_retries=2, bulk_retry_delay=0) | ||
|
|
||
| always_transient = [_transient_failure()] | ||
| run_mock = mock.MagicMock(return_value=always_transient) | ||
|
|
||
| with mock.patch.object(op, "_run_operation", run_mock): | ||
| with mock.patch("airflow.providers.salesforce.operators.bulk.time.sleep"): | ||
| final = op._retry_transient_failures( | ||
| bulk=mock.MagicMock(), | ||
| payload=[{"FirstName": "Ada"}], | ||
| result=always_transient, | ||
| ) | ||
|
|
||
| assert run_mock.call_count == 2 | ||
| assert final[0]["success"] is False | ||
|
|
||
| def test_retry_delay_is_respected(self): | ||
| op = _make_op(max_retries=1, bulk_retry_delay=30.0) | ||
|
|
||
| run_mock = mock.MagicMock(return_value=[_success()]) | ||
|
|
||
| with mock.patch.object(op, "_run_operation", run_mock): | ||
| with mock.patch("airflow.providers.salesforce.operators.bulk.time.sleep") as sleep_mock: | ||
| op._retry_transient_failures( | ||
| bulk=mock.MagicMock(), | ||
| payload=[{"FirstName": "Ada"}], | ||
| result=[_transient_failure()], | ||
| ) | ||
|
|
||
| sleep_mock.assert_called_once_with(30.0) | ||
|
|
||
| def test_custom_transient_error_codes(self): | ||
| op = _make_op(max_retries=1, bulk_retry_delay=0, transient_error_codes=["MY_CUSTOM_ERROR"]) | ||
| assert op.transient_error_codes == frozenset({"MY_CUSTOM_ERROR"}) | ||
|
|
||
| custom_failure = { | ||
| "success": False, | ||
| "errors": [{"statusCode": "MY_CUSTOM_ERROR", "message": "custom"}], | ||
| } | ||
| run_mock = mock.MagicMock(return_value=[_success()]) | ||
|
|
||
| with mock.patch.object(op, "_run_operation", run_mock): | ||
| with mock.patch("airflow.providers.salesforce.operators.bulk.time.sleep"): | ||
| final = op._retry_transient_failures( | ||
| bulk=mock.MagicMock(), | ||
| payload=[{"FirstName": "Ada"}], | ||
| result=[custom_failure], | ||
| ) | ||
|
|
||
| run_mock.assert_called_once() | ||
| assert final[0] == _success() | ||
|
|
||
| def test_api_temporarily_unavailable_is_retried(self): | ||
| op = _make_op(max_retries=1, bulk_retry_delay=0) | ||
| failure = _transient_failure("API_TEMPORARILY_UNAVAILABLE") | ||
| run_mock = mock.MagicMock(return_value=[_success()]) | ||
|
|
||
| with mock.patch.object(op, "_run_operation", run_mock): | ||
| with mock.patch("airflow.providers.salesforce.operators.bulk.time.sleep"): | ||
| final = op._retry_transient_failures( | ||
| bulk=mock.MagicMock(), | ||
| payload=[{"FirstName": "Ada"}], | ||
| result=[failure], | ||
| ) | ||
|
|
||
| run_mock.assert_called_once() | ||
| assert final[0] == _success() | ||
|
|
||
| def test_mixed_failures_only_retries_transient(self): | ||
| op = _make_op(max_retries=1, bulk_retry_delay=0) | ||
| payload = [{"FirstName": "A"}, {"FirstName": "B"}, {"FirstName": "C"}] | ||
| initial = [_transient_failure(), _permanent_failure(), _success()] | ||
|
|
||
| run_mock = mock.MagicMock(return_value=[_success()]) | ||
|
|
||
| with mock.patch.object(op, "_run_operation", run_mock): | ||
| with mock.patch("airflow.providers.salesforce.operators.bulk.time.sleep"): | ||
| final = op._retry_transient_failures( | ||
| bulk=mock.MagicMock(), | ||
| payload=payload, | ||
| result=initial, | ||
| ) | ||
|
|
||
| run_mock.assert_called_once_with(mock.ANY, [{"FirstName": "A"}]) | ||
| assert final[0] == _success() | ||
| assert final[1] == _permanent_failure() | ||
| assert final[2] == _success() | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.