amoghrajesh commented on code in PR #76295:
URL: https://github.com/apache/airflow/pull/76295#discussion_r4509238968


##########
providers/databricks/src/airflow/providers/databricks/operators/databricks.py:
##########
@@ -82,6 +82,45 @@
 XCOM_RUN_PAGE_URL_KEY = "run_page_url"
 XCOM_STATEMENT_ID_KEY = "statement_id"
 
+# Databricks splits run state across life_cycle_state and result_state, while 
the ResumableJobMixin
+# status interface is a single string. The durable operators encode both as 
"LIFE_CYCLE:RESULT" and
+# decode them with the predicates below, so all three share one view of which 
runs can be rejoined.
+RUN_NOT_FOUND_STATUS = "NOT_FOUND:"
+ACTIVE_RUN_LIFE_CYCLE_STATES = (
+    "PENDING",
+    "QUEUED",
+    "RUNNING",
+    "TERMINATING",
+    "BLOCKED",
+    "WAITING_FOR_RETRY",
+)
+
+
+def fetch_run_status(hook: DatabricksHook, run_id: JsonValue) -> str:
+    """Return the encoded status of ``run_id``, or a sentinel if its run 
history is gone."""
+    try:
+        # The stored external id is always a Databricks run id; the mixin 
types it as JsonValue.
+        run_info = hook.get_run(cast("int", run_id))
+    except DatabricksApiError as e:
+        # A stored run whose history expired (or was deleted) returns 404. The 
sentinel is neither
+        # active nor succeeded, so the mixin resubmits fresh instead of 
failing the task.
+        if e.http_status_code == 404:
+            return RUN_NOT_FOUND_STATUS
+        raise
+    state = RunState(**run_info["state"])
+    return f"{state.life_cycle_state}:{state.result_state}"
+
+
+def is_run_active(status: str) -> bool:
+    """Whether an encoded status means the run is still going and can be 
reconnected to."""
+    return status.split(":", 1)[0] in ACTIVE_RUN_LIFE_CYCLE_STATES

Review Comment:
   Fixed. The problem was that a run being cancelled and a run just finishing 
both look the same (TERMINATING), so we'd latch onto a cancelled run and fail 
with it. Now we wait a moment and look at what the run actually ended up as. 
Cancelled means start fresh, finished means we're done.
   
   I kept TERMINATING in the "still going" list on purpose, as the fallback if 
the wait times
   out. If a worker dies while a run is wrapping up normally, we want to rejoin 
it, not start a
   second one. Rejoining a slow run is the safer mistake of the two.
   
   Tests cover both endings and the timeout. Taking the wait back out turns 
four of them red.



-- 
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