1fanwang opened a new pull request, #72368:
URL: https://github.com/apache/airflow/pull/72368

   A task retry currently submits a second Azure Synapse Spark batch even when 
the first batch is still running. This can duplicate remote work.
   
   Synchronous `AzureSynapseRunSparkBatchOperator` tasks now store the 
submitted batch ID and reconnect to it on retry. A completed batch returns 
immediately; a failed batch is submitted again. `wait_for_termination=False` 
remains submit-only, and `durable=False` keeps the previous always-submit 
behavior. Airflow versions before 3.3 continue without task-state recovery.
   
   The shared resumable-job path also treats numeric external ID `0` as a valid 
ID.
   
   ## Testing
   
   - [x] Synapse hook and operator tests: 68 passed.
   - [x] Shared `ResumableJobMixin` tests: 41 passed.
   - [x] Local retry harness: the baseline submitted batches `0` and `1`; the 
changed operator submitted batch `0` once, reconnected to it, and restored its 
XCom.
   
   <details>
   <summary>Commands and raw output</summary>
   
   ```console
   $ set -o pipefail; AIRFLOW_HOME="$(mktemp -d)/airflow" .venv/bin/uv run 
--project providers/microsoft/azure pytest 
providers/microsoft/azure/tests/unit/microsoft/azure/hooks/test_synapse.py 
providers/microsoft/azure/tests/unit/microsoft/azure/operators/test_synapse.py 
-q 2>&1 | tail -1
   ======================== 68 passed, 1 warning in 27.39s 
========================
   
   $ set -o pipefail; AIRFLOW_HOME="$(mktemp -d)/airflow" .venv/bin/uv run 
--project task-sdk pytest 
task-sdk/tests/task_sdk/bases/test_resumablejobmixin.py -q 2>&1 | tail -1
   ======================== 41 passed, 1 warning in 1.44s 
=========================
   
   $ AIRFLOW_HOME="$(mktemp -d)/airflow" AIRFLOW__LOGGING__LOGGING_LEVEL=ERROR 
.venv/bin/uv run --project providers/microsoft/azure python - <<'PY'
   import json
   import subprocess
   from types import ModuleType, SimpleNamespace
   from unittest.mock import MagicMock
   
   from airflow.providers.microsoft.azure.hooks.synapse import 
AzureSynapseSparkBatchRunStatus
   from airflow.providers.microsoft.azure.operators.synapse import 
AzureSynapseRunSparkBatchOperator
   
   PATH = 
"providers/microsoft/azure/src/airflow/providers/microsoft/azure/operators/synapse.py"
   
   
   class Store(dict):
       def set(self, key, value):
           self[key] = value
   
   
   class TI:
       stats_tags = {}
   
       def __init__(self):
           self.xcom = {}
   
       def xcom_push(self, *, key, value):
           self.xcom[key] = value
   
   
   def load_baseline():
       source = subprocess.run(
           ["git", "show", f"upstream/main:{PATH}"], check=True, 
capture_output=True, text=True
       ).stdout
       module = ModuleType("baseline_synapse")
       module.__package__ = "airflow.providers.microsoft.azure.operators"
       exec(compile(source, PATH, "exec"), module.__dict__)
       return module.AzureSynapseRunSparkBatchOperator
   
   
   def run(operator_type):
       submissions, waited_for = [], []
       store = Store()
   
       def make_hook():
           hook = MagicMock()
   
           def submit(*, payload):
               job_id = len(submissions)
               submissions.append(job_id)
               return SimpleNamespace(id=job_id)
   
           hook.run_spark_job.side_effect = submit
           hook.get_job_run_status.return_value = 
AzureSynapseSparkBatchRunStatus.RUNNING
           hook.wait_for_job_run_status.side_effect = (
               lambda *, job_id, **_: waited_for.append(job_id) or True
           )
           return hook
   
       retry_ti = None
       retry = None
       for ti in (TI(), TI()):
           retry_ti = ti
           retry = operator_type(
               task_id=f"synapse_{len(submissions)}",
               azure_synapse_conn_id="unused",
               spark_pool="unused",
               payload={},
               check_interval=0,
           )
           retry.hook = make_hook()
           retry.execute(context={"task_state_store": store, "ti": ti})
       return {
           "submissions": submissions,
           "persisted_job_id": store.get("synapse_spark_batch_id"),
           "retry_job_id": retry.job_id,
           "retry_xcom_job_id": retry_ti.xcom["job_id"],
           "waited_for": waited_for,
       }
   
   
   result = {"before": run(load_baseline()), "after": 
run(AzureSynapseRunSparkBatchOperator)}
   assert result["before"]["submissions"] == [0, 1]
   assert result["after"]["submissions"] == [0]
   assert result["after"]["persisted_job_id"] == 
result["after"]["retry_xcom_job_id"] == 0
   print(json.dumps(result, indent=2))
   PY
   {
     "before": {
       "submissions": [0, 1],
       "persisted_job_id": null,
       "retry_job_id": 1,
       "retry_xcom_job_id": 1,
       "waited_for": [0, 1]
     },
     "after": {
       "submissions": [0],
       "persisted_job_id": 0,
       "retry_job_id": 0,
       "retry_xcom_job_id": 0,
       "waited_for": [0, 0]
     }
   }
   ```
   
   The retry harness used the real baseline operator, changed operator, and 
`ResumableJobMixin`. It replaced Azure with an in-process hook substitute; no 
Azure service was contacted.
   
   </details>
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes - GitHub Copilot CLI (GPT-5.6 Sol)
   
   Generated-by: GitHub Copilot CLI (GPT-5.6 Sol) following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   
   ---
   
   * Read the **[Pull Request 
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
 for more information. Note: commit author/co-author name and email in commits 
become permanently public when merged.
   * For fundamental code changes, an Airflow Improvement Proposal 
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
 is needed.
   * When adding dependency, check compliance with the [ASF 3rd Party License 
Policy](https://www.apache.org/legal/resolved.html#category-x).
   * For significant user-facing changes create newsfragment: 
`{pr_number}.significant.rst`, in 
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
 You can add this file in a follow-up commit after the PR is created so you 
know the PR number.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to