kaxil commented on code in PR #77271:
URL: https://github.com/apache/airflow/pull/77271#discussion_r4710773781
##########
providers/databricks/src/airflow/providers/databricks/sensors/databricks.py:
##########
@@ -144,6 +150,8 @@ def execute(self, context: Context):
if self.deferrable:
self._handle_deferrable_execution(defer_method_name=DEFER_METHOD_NAME) # type:
ignore[misc]
+ return None
+ return super().execute(context)
Review Comment:
When `timeout` passes, `BaseSensorOperator.execute()` raises
`AirflowSensorTimeout` (or `AirflowSkipException` under `soft_fail`) without
cancelling the statement, so it keeps running on the warehouse after the task
gives up, and a retry submits a second copy. The deferrable trigger and the
operator's sync loop in `_handle_execution()` both call `cancel_sql_statement`
at the deadline. Should this path cancel on timeout too?
##########
providers/databricks/docs/operators/sql_statements.rst:
##########
@@ -82,6 +82,9 @@ response to that execution.
It supports configurable execution parameters such as warehouse selection,
catalog, schema, and parameterized queries.
The operator can either synchronously poll for query completion or run in a
deferrable mode for improved efficiency.
+When polling synchronously, the Sensor checks the statement every
``poke_interval`` seconds and fails if it does not
Review Comment:
The sensor also takes `polling_period_seconds` (default 30), but only the
deferrable path reads it; this path sleeps `poke_interval` (default 60). Can
this sentence say so, so someone who sets `polling_period_seconds` without
`deferrable` knows it has no effect here?
##########
providers/databricks/docs/operators/sql_statements.rst:
##########
@@ -82,6 +82,9 @@ response to that execution.
It supports configurable execution parameters such as warehouse selection,
catalog, schema, and parameterized queries.
The operator can either synchronously poll for query completion or run in a
deferrable mode for improved efficiency.
+When polling synchronously, the Sensor checks the statement every
``poke_interval`` seconds and fails if it does not
+finish within ``timeout``. ``mode="reschedule"`` is only supported with
``statement_id``: with ``statement``, each
Review Comment:
The guard only raises when `deferrable=False` and
`wait_for_termination=True`. With `deferrable=True` the poke loop never runs,
so `mode` doesn't matter, which is what the error message points people to.
Could the sentence name those conditions?
##########
providers/databricks/tests/unit/databricks/sensors/test_databricks.py:
##########
@@ -193,6 +191,64 @@ def test_exec_success(self, db_mock_class):
db_mock.post_sql_statement.assert_called_once_with(expected_json)
assert op.statement_id == STATEMENT_ID
+ @pytest.mark.parametrize("final_state", ["FAILED", "CANCELED", "CLOSED"])
Review Comment:
Databricks documents `CLOSED` as "execution successful, and statement
closed; result no longer available for fetch". Should the sensor fail on it?
`SQLStatementState.is_successful` only accepts `SUCCEEDED` and predates this PR
(changing it would also change the operator), but this test now pins `CLOSED`
as a failure, and a sensor watching someone else's `statement_id` is the case
most likely to see it.
##########
providers/databricks/tests/unit/databricks/sensors/test_databricks.py:
##########
@@ -404,6 +463,7 @@ def test_execute_includes_airflow_query_tags(self,
db_mock_class):
@mock.patch("airflow.providers.databricks.sensors.databricks.DatabricksHook")
def test_execute_skips_query_tags_when_statement_id_provided(self,
db_mock_class):
db_mock = db_mock_class.return_value
+ db_mock.get_sql_statement_state.return_value =
SQLStatementState("SUCCEEDED")
Review Comment:
With `wait_for_termination=False`, `execute()` returns before polling, so
this mock is never used and can go.
##########
providers/databricks/tests/unit/databricks/sensors/test_databricks.py:
##########
@@ -193,6 +191,64 @@ def test_exec_success(self, db_mock_class):
db_mock.post_sql_statement.assert_called_once_with(expected_json)
assert op.statement_id == STATEMENT_ID
+ @pytest.mark.parametrize("final_state", ["FAILED", "CANCELED", "CLOSED"])
+
@mock.patch("airflow.providers.databricks.sensors.databricks.DatabricksHook")
+ def test_exec_waits_for_statement_and_fails_with_it(self, db_mock_class,
final_state):
+ op = DatabricksSQLStatementsSensor(
+ task_id=TASK_ID, statement=STATEMENT, warehouse_id=WAREHOUSE_ID,
poke_interval=0
+ )
+ db_mock = db_mock_class.return_value
+ db_mock.post_sql_statement.return_value = STATEMENT_ID
+ db_mock.get_sql_statement_state.side_effect = [
+ SQLStatementState("PENDING"),
+ SQLStatementState("RUNNING"),
+ SQLStatementState(final_state, error_message="Table not found"),
+ ]
+
+ with pytest.raises(AirflowException, match=f"{STATEMENT_ID} failed
with error: Table not found"):
+ op.execute(None)
+
+ assert db_mock.get_sql_statement_state.call_args_list ==
[mock.call(STATEMENT_ID)] * 3
+
+
@mock.patch("airflow.providers.databricks.sensors.databricks.DatabricksHook")
+ def test_exec_waits_for_running_statement_to_succeed(self, db_mock_class):
+ op = DatabricksSQLStatementsSensor(
+ task_id=TASK_ID, statement=STATEMENT, warehouse_id=WAREHOUSE_ID,
poke_interval=0
+ )
+ db_mock = db_mock_class.return_value
+ db_mock.post_sql_statement.return_value = STATEMENT_ID
+ db_mock.get_sql_statement_state.side_effect = [
+ SQLStatementState("RUNNING"),
+ SQLStatementState("SUCCEEDED"),
+ ]
+
+ op.execute(None)
+
+ assert db_mock.get_sql_statement_state.call_count == 2
+
+
@mock.patch("airflow.providers.databricks.sensors.databricks.DatabricksHook")
+ def test_reschedule_mode_with_statement_is_rejected(self, db_mock_class):
Review Comment:
Removing `not self.deferrable` or `self.wait_for_termination` from the guard
still passes the suite. Can this test also try reschedule mode with
`deferrable=True` and with `wait_for_termination=False`? The deferrable one
needs a RUNNING state, otherwise `execute()` returns before it defers.
--
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]