kaxil commented on code in PR #77015:
URL: https://github.com/apache/airflow/pull/77015#discussion_r4646814112
##########
shared/observability/src/airflow_shared/observability/metrics/metrics_template.yaml:
##########
@@ -74,6 +74,14 @@ metrics:
legacy_name: "-"
name_variables: []
+ - name: "resumable_job.resume_failed"
Review Comment:
`resume_failed` reads like the resume failed, but it counts successful ones.
Could it be `resumable_job.resumed` to match the action, while nothing has
shipped with the old name? Also neither this counter nor the span value is in
the `TestMetrics` / `TestTracing` cases.
##########
providers/databricks/src/airflow/providers/databricks/operators/databricks.py:
##########
@@ -1582,6 +1596,25 @@ def is_job_active(self, status: str) -> bool:
def is_job_succeeded(self, status: str) -> bool:
return is_run_succeeded(status)
+ def resume_failed_job(self, external_id: JsonValue, status: str, context:
Context) -> JsonValue | None:
+ if status == RUN_NOT_FOUND_STATUS:
+ return None
+ run_id = cast("int", external_id)
+ self._merged_json = self._build_run_now_payload(context)
+ if self.databricks_repair_reason_new_settings:
+ run_state = self._hook.get_run_state(run_id)
+ if not is_repair_reason_match_exist(self, run_state):
+ return None
+ update_job_for_repair(self, self._hook,
self._merged_json["job_id"], run_state)
+ repair_json: dict[str, Any] = {"run_id": run_id,
"rerun_all_failed_tasks": True}
+ latest_repair_id = self._hook.get_latest_repair_id(run_id)
+ if latest_repair_id is not None:
+ repair_json["latest_repair_id"] = latest_repair_id
+ if "job_parameters" in self._merged_json:
+ repair_json["job_parameters"] = self._merged_json["job_parameters"]
+ self._hook.repair_run(repair_json)
Review Comment:
With `cancel_previous_runs=True`, this path never calls `cancel_all_runs`.
If Dag run B's submit cancelled A's run, A's retry now repairs A while B keeps
running, where before it cancelled B. Should this cancel first, or return
`None` when `cancel_previous_runs` is set?
##########
providers/databricks/src/airflow/providers/databricks/operators/databricks.py:
##########
@@ -1340,6 +1341,9 @@ class DatabricksRunNowOperator(ResumableJobMixin,
BaseOperator):
before polling begins so that a worker crash and retry reconnects to
the existing run
instead of triggering a duplicate run of the same job. Set to
``False`` to always trigger a
fresh run on retry. Requires Airflow 3.3+; on earlier versions it is
silently ignored.
+ :param resume_on_retry: When ``True`` (the default), a retry that finds
the stored run failed repairs
Review Comment:
With `deferrable=True` (or `[operators] default_deferrable`), `execute`
calls `run_now` directly and skips the mixin, so this does nothing there. Can
the docstring and the changelog note say it's non-deferrable only?
##########
providers/databricks/tests/unit/databricks/operators/test_databricks.py:
##########
@@ -3648,6 +3648,69 @@ def
test_resubmits_when_stored_run_in_terminal_failure(self, db_mock_class):
task_store.set.assert_called_once_with("databricks_run_now_id", RUN_ID)
assert op.run_id == RUN_ID
+ @pytest.mark.parametrize(
+ ("resume_on_retry", "airflow_3_5_plus", "warns"),
+ [
+ pytest.param(True, False, True, id="true_below_3_5"),
+ pytest.param(False, False, False, id="false_below_3_5"),
+ pytest.param(None, False, False, id="unset_below_3_5"),
+ pytest.param(True, True, False, id="true_on_3_5"),
+ ],
+ )
+ def test_resume_on_retry_warns_only_when_requested_below_3_5(
+ self, recwarn, resume_on_retry, airflow_3_5_plus, warns
+ ):
+ with mock.patch(
+
"airflow.providers.databricks.operators.databricks.AIRFLOW_V_3_5_PLUS",
airflow_3_5_plus
+ ):
+ DatabricksRunNowOperator(task_id=TASK_ID, job_id=JOB_ID,
resume_on_retry=resume_on_retry)
+
+ messages = [str(w.message) for w in recwarn if issubclass(w.category,
UserWarning)]
+ assert ("`resume_on_retry` has no effect on Airflow versions below
3.5." in messages) is warns
+
+ @pytest.mark.skipif(not AIRFLOW_V_3_5_PLUS, reason="resume_failed_job
requires Airflow 3.5+")
+
@mock.patch("airflow.providers.databricks.operators.databricks.DatabricksHook")
+ def test_repairs_stored_failed_run_instead_of_resubmitting(self,
db_mock_class):
+ op = DatabricksRunNowOperator(task_id=TASK_ID, job_id=JOB_ID)
+ db_mock = db_mock_class.return_value
+ db_mock.get_run.side_effect = [
+ make_run_state("TERMINATED", "FAILED"),
+ make_run_state("TERMINATED", "SUCCESS"),
+ ]
+ db_mock.get_latest_repair_id.return_value = None
+ task_store = MagicMock(spec_set=["get", "set"])
+ task_store.get.return_value = RUN_ID
+
+ op.execute(self._context(task_store))
+
+ db_mock.repair_run.assert_called_once_with({"run_id": RUN_ID,
"rerun_all_failed_tasks": True})
Review Comment:
Can we get a case where `get_latest_repair_id` returns an id and
`job_parameters` is set? With these mocks, dropping `latest_repair_id` from the
payload still passes, and in prod Databricks would reject the repair and we'd
fall back to a fresh run without anyone noticing. Same for
`update_job_for_repair`, which no test reaches since the only reason test
doesn't match.
##########
task-sdk/src/airflow/sdk/bases/resumablejobmixin.py:
##########
@@ -225,6 +249,32 @@ def persist_job_id(self, context: Context, external_id:
JsonValue) -> None:
external_id=external_id,
)
+ def resume_failed_job(self, external_id: JsonValue, status: str, context:
Context) -> JsonValue | None:
+ """
+ Ask the external system to resume a failed job from where it stopped,
instead of starting over.
+
+ Called on a retry when the stored job is in a terminal failed state.
Return the ID to poll,
+ which may be the same ID (for example a Databricks run repaired in
place) or a new one. Return
+ ``None`` to submit a fresh job, which is the default.
+
+ Only called when ``resume_on_retry`` is set. A cleared task reaches it
only when the clear kept
+ task state, because clearing otherwise discards the stored ID.
+ """
+ return None
+
+ def _resume_failed_job_safely(self, external_id: JsonValue, status: str,
context: Context) -> JsonValue:
+ try:
+ return self.resume_failed_job(external_id, status, context)
+ except Exception:
+ # A failed resume must never leave a retry worse off than
resubmitting.
Review Comment:
What if Databricks accepted the repair but the response timed out?
`_do_api_call` raises, we submit fresh, and now two runs of the job are going.
Since `resume_on_retry` is on by default, can the status re-check from What's
next go in this PR instead of a follow-up?
##########
task-sdk/src/airflow/sdk/bases/resumablejobmixin.py:
##########
@@ -145,7 +151,9 @@ def get_resume_decision(self, context: Context) ->
ResumeDecision:
"""
Decide whether to reconnect to the stored job, take its result, or
submit a new one.
- Never submits or polls, so a deferrable operator can call it before
``defer()``.
+ Never submits or polls, so a deferrable operator can call it before
``defer()``. The one
Review Comment:
Deferrable operators call this before `defer()` because it had no side
effects, and now it can trigger a repair. The deferrable example in
`resumable-job-mixin.rst` wasn't updated either: it sends anything that isn't
`reconnect` to `submit_job`, so an operator copying it would repair the old job
and also submit a new one. Since #77161 hasn't shipped, could this return a
`"resume"` action and let the caller run `resume_failed_job`?
##########
providers/databricks/src/airflow/providers/databricks/operators/databricks.py:
##########
@@ -1582,6 +1596,25 @@ def is_job_active(self, status: str) -> bool:
def is_job_succeeded(self, status: str) -> bool:
return is_run_succeeded(status)
+ def resume_failed_job(self, external_id: JsonValue, status: str, context:
Context) -> JsonValue | None:
+ if status == RUN_NOT_FOUND_STATUS:
+ return None
+ run_id = cast("int", external_id)
+ self._merged_json = self._build_run_now_payload(context)
+ if self.databricks_repair_reason_new_settings:
+ run_state = self._hook.get_run_state(run_id)
+ if not is_repair_reason_match_exist(self, run_state):
+ return None
+ update_job_for_repair(self, self._hook,
self._merged_json["job_id"], run_state)
+ repair_json: dict[str, Any] = {"run_id": run_id,
"rerun_all_failed_tasks": True}
Review Comment:
This is the third copy of the repair request (with
`_handle_databricks_operator_execution` and `execute_complete`), and they
already build `job_parameters` differently. A shared helper would help, fine as
a follow-up.
--
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]