Skip to content

Commit e7e4e34

Browse files
committed
Make AzureBatchOperator batch_max_retries take effect
Signed-off-by: PoAn Yang <payang@apache.org>
1 parent b072197 commit e7e4e34

4 files changed

Lines changed: 51 additions & 4 deletions

File tree

‎providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/batch.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class AzureBatchHook(BaseHook):
4444
4545
:param azure_batch_conn_id: :ref:`Azure Batch connection id<howto/connection:azure_batch>`
4646
of a service principal which will be used to start the container instance.
47+
:param batch_max_retries: The number of times a request to the Batch service is retried
48+
before it is considered failed. Default is 3.
4749
"""
4850

4951
conn_name_attr = "azure_batch_conn_id"
@@ -74,9 +76,10 @@ def get_ui_field_behaviour(cls) -> dict[str, Any]:
7476
},
7577
}
7678

77-
def __init__(self, azure_batch_conn_id: str = default_conn_name) -> None:
79+
def __init__(self, azure_batch_conn_id: str = default_conn_name, batch_max_retries: int = 3) -> None:
7880
super().__init__()
7981
self.conn_id = azure_batch_conn_id
82+
self.batch_max_retries = batch_max_retries
8083

8184
def _get_field(self, extras, name):
8285
return get_field(
@@ -117,6 +120,7 @@ def get_conn(self) -> BatchClient:
117120
batch_client = BatchClient(
118121
endpoint=batch_account_url,
119122
credential=credential,
123+
retry_total=self.batch_max_retries,
120124
)
121125
return batch_client
122126

‎providers/microsoft/azure/src/airflow/providers/microsoft/azure/operators/batch.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ class AzureBatchOperator(BaseOperator):
5757
:param batch_start_task: A Task specified to run on each Compute Node as it joins the Pool.
5858
The Task runs when the Compute Node is added to the Pool or
5959
when the Compute Node is restarted.
60-
:param batch_max_retries: The number of times to retry this batch operation before it's
61-
considered a failed operation. Default is 3
60+
:param batch_max_retries: The number of times a request to the Batch service is retried
61+
before it is considered failed. Default is 3
6262
:param batch_task_resource_files: A list of files that the Batch service will
6363
download to the Compute Node before running the command line.
6464
:param batch_task_output_files: A list of files that the Batch service will upload
@@ -184,7 +184,7 @@ def __init__(
184184
@cached_property
185185
def hook(self) -> AzureBatchHook:
186186
"""Create and return an AzureBatchHook (cached)."""
187-
return AzureBatchHook(self.azure_batch_conn_id)
187+
return AzureBatchHook(self.azure_batch_conn_id, batch_max_retries=self.batch_max_retries)
188188

189189
def _check_inputs(self) -> Any:
190190
if not self.vm_publisher:

‎providers/microsoft/azure/tests/unit/microsoft/azure/hooks/test_batch.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ def test_connection_and_client(self):
6767
assert isinstance(conn, BatchClient)
6868
assert hook.connection is conn, "`connection` property should be cached"
6969

70+
@pytest.mark.parametrize(
71+
("hook_kwargs", "expected_retry_total"),
72+
[
73+
pytest.param({}, 3, id="default"),
74+
pytest.param({"batch_max_retries": 7}, 7, id="explicit"),
75+
],
76+
)
77+
def test_batch_max_retries_is_applied_to_the_client(self, hook_kwargs, expected_retry_total):
78+
# Asserted on a real client: ``retry_total`` reaches azure-core through ``**kwargs``.
79+
hook = AzureBatchHook(azure_batch_conn_id=self.test_vm_conn_id, **hook_kwargs)
80+
client = hook.get_conn()
81+
assert client._config.retry_policy.total_retries == expected_retry_total
82+
assert client._config.retry_policy in client._client._pipeline._impl_policies
83+
7084
@mock.patch(f"{MODULE}.get_sync_default_azure_credential")
7185
def test_fallback_to_default_azure_credential_when_name_and_key_is_not_provided(
7286
self, mock_get_default_credential, create_mock_connections

‎providers/microsoft/azure/tests/unit/microsoft/azure/operators/test_batch.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,35 @@ def setup_test_cases(self, mocked_batch_client, create_mock_connections):
146146
timeout=2,
147147
)
148148

149+
@pytest.mark.parametrize(
150+
("operator_kwargs", "expected_retry_total"),
151+
[
152+
pytest.param({}, 3, id="default"),
153+
pytest.param({"batch_max_retries": 7}, 7, id="explicit"),
154+
],
155+
)
156+
def test_batch_max_retries_reaches_the_batch_client(
157+
self, mocked_batch_client, operator_kwargs, expected_retry_total
158+
):
159+
operator = AzureBatchOperator(
160+
task_id=TASK_ID,
161+
batch_pool_id=BATCH_POOL_ID,
162+
batch_pool_vm_size=BATCH_VM_SIZE,
163+
batch_job_id=BATCH_JOB_ID,
164+
batch_task_id=BATCH_TASK_ID,
165+
vm_publisher=self.test_vm_publisher,
166+
vm_offer=self.test_vm_offer,
167+
vm_sku=self.test_vm_sku,
168+
vm_node_agent_sku_id=self.test_node_agent_sku,
169+
sku_starts_with=self.test_vm_sku,
170+
batch_task_command_line="echo hello",
171+
azure_batch_conn_id=self.test_vm_conn_id,
172+
target_dedicated_nodes=1,
173+
**operator_kwargs,
174+
)
175+
operator.hook.get_conn()
176+
assert mocked_batch_client.call_args.kwargs["retry_total"] == expected_retry_total
177+
149178
@mock.patch.object(AzureBatchHook, "wait_for_all_node_state")
150179
def test_execute_without_failures(self, wait_mock):
151180
wait_mock.return_value = True

0 commit comments

Comments
 (0)