Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions airflow/providers/databricks/operators/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,24 @@ def _handle_databricks_operator_execution(operator, hook, log, context) -> None:
if operator.do_xcom_push:
context['ti'].xcom_push(key=XCOM_RUN_PAGE_URL_KEY, value=run_page_url)

log.info('View run status, Spark UI, and logs at %s', run_page_url)
while True:
run_state = hook.get_run_state(operator.run_id)
if run_state.is_terminal:
if run_state.is_successful:
log.info('%s completed successfully.', operator.task_id)
log.info('View run status, Spark UI, and logs at %s', run_page_url)
return
if operator.wait_for_termination:
while True:
run_state = hook.get_run_state(operator.run_id)
if run_state.is_terminal:
if run_state.is_successful:
log.info('%s completed successfully.', operator.task_id)
log.info('View run status, Spark UI, and logs at %s', run_page_url)
return
else:
error_message = f'{operator.task_id} failed with terminal state: {run_state}'
raise AirflowException(error_message)
else:
error_message = f'{operator.task_id} failed with terminal state: {run_state}'
raise AirflowException(error_message)
else:
log.info('%s in run state: %s', operator.task_id, run_state)
log.info('View run status, Spark UI, and logs at %s', run_page_url)
log.info('Sleeping for %s seconds.', operator.polling_period_seconds)
time.sleep(operator.polling_period_seconds)
log.info('%s in run state: %s', operator.task_id, run_state)
log.info('View run status, Spark UI, and logs at %s', run_page_url)
log.info('Sleeping for %s seconds.', operator.polling_period_seconds)
time.sleep(operator.polling_period_seconds)
else:
log.info('View run status, Spark UI, and logs at %s', run_page_url)


class DatabricksSubmitRunOperator(BaseOperator):
Expand Down Expand Up @@ -282,6 +284,7 @@ def __init__(
do_xcom_push: bool = False,
idempotency_token: Optional[str] = None,
access_control_list: Optional[List[Dict[str, str]]] = None,
wait_for_termination: bool = True,
**kwargs,
) -> None:
"""Creates a new ``DatabricksSubmitRunOperator``."""
Expand All @@ -291,6 +294,7 @@ def __init__(
self.polling_period_seconds = polling_period_seconds
self.databricks_retry_limit = databricks_retry_limit
self.databricks_retry_delay = databricks_retry_delay
self.wait_for_termination = wait_for_termination
if tasks is not None:
self.json['tasks'] = tasks
if spark_jar_task is not None:
Expand Down Expand Up @@ -511,6 +515,7 @@ def __init__(
databricks_retry_limit: int = 3,
databricks_retry_delay: int = 1,
do_xcom_push: bool = False,
wait_for_termination: bool = True,
**kwargs,
) -> None:
"""Creates a new ``DatabricksRunNowOperator``."""
Expand All @@ -520,6 +525,7 @@ def __init__(
self.polling_period_seconds = polling_period_seconds
self.databricks_retry_limit = databricks_retry_limit
self.databricks_retry_delay = databricks_retry_delay
self.wait_for_termination = wait_for_termination

if job_id is not None:
self.json['job_id'] = job_id
Expand Down
106 changes: 106 additions & 0 deletions tests/providers/databricks/operators/test_databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,57 @@ def test_on_kill(self, db_mock_class):

db_mock.cancel_run.assert_called_once_with(RUN_ID)

@mock.patch('airflow.providers.databricks.operators.databricks.DatabricksHook')
def test_wait_for_termination(self, db_mock_class):
run = {
'new_cluster': NEW_CLUSTER,
'notebook_task': NOTEBOOK_TASK,
}
op = DatabricksSubmitRunOperator(task_id=TASK_ID, json=run)
db_mock = db_mock_class.return_value
db_mock.submit_run.return_value = 1
db_mock.get_run_state.return_value = RunState('TERMINATED', 'SUCCESS', '')

assert op.wait_for_termination

op.execute(None)

expected = databricks_operator._deep_string_coerce(
{'new_cluster': NEW_CLUSTER, 'notebook_task': NOTEBOOK_TASK, 'run_name': TASK_ID}
)
db_mock_class.assert_called_once_with(
DEFAULT_CONN_ID, retry_limit=op.databricks_retry_limit, retry_delay=op.databricks_retry_delay
)

db_mock.submit_run.assert_called_once_with(expected)
db_mock.get_run_page_url.assert_called_once_with(RUN_ID)
db_mock.get_run_state.assert_called_once_with(RUN_ID)

@mock.patch('airflow.providers.databricks.operators.databricks.DatabricksHook')
def test_no_wait_for_termination(self, db_mock_class):
run = {
'new_cluster': NEW_CLUSTER,
'notebook_task': NOTEBOOK_TASK,
}
op = DatabricksSubmitRunOperator(task_id=TASK_ID, wait_for_termination=False, json=run)
db_mock = db_mock_class.return_value
db_mock.submit_run.return_value = 1

assert not op.wait_for_termination

op.execute(None)

expected = databricks_operator._deep_string_coerce(
{'new_cluster': NEW_CLUSTER, 'notebook_task': NOTEBOOK_TASK, 'run_name': TASK_ID}
)
db_mock_class.assert_called_once_with(
DEFAULT_CONN_ID, retry_limit=op.databricks_retry_limit, retry_delay=op.databricks_retry_delay
)

db_mock.submit_run.assert_called_once_with(expected)
db_mock.get_run_page_url.assert_called_once_with(RUN_ID)
db_mock.get_run_state.assert_not_called()


class TestDatabricksRunNowOperator(unittest.TestCase):
def test_init_with_named_parameters(self):
Expand Down Expand Up @@ -439,3 +490,58 @@ def test_on_kill(self, db_mock_class):

op.on_kill()
db_mock.cancel_run.assert_called_once_with(RUN_ID)

@mock.patch('airflow.providers.databricks.operators.databricks.DatabricksHook')
def test_wait_for_termination(self, db_mock_class):
run = {'notebook_params': NOTEBOOK_PARAMS, 'notebook_task': NOTEBOOK_TASK, 'jar_params': JAR_PARAMS}
op = DatabricksRunNowOperator(task_id=TASK_ID, job_id=JOB_ID, json=run)
db_mock = db_mock_class.return_value
db_mock.run_now.return_value = 1
db_mock.get_run_state.return_value = RunState('TERMINATED', 'SUCCESS', '')

assert op.wait_for_termination

op.execute(None)

expected = databricks_operator._deep_string_coerce(
{
'notebook_params': NOTEBOOK_PARAMS,
'notebook_task': NOTEBOOK_TASK,
'jar_params': JAR_PARAMS,
'job_id': JOB_ID,
}
)
db_mock_class.assert_called_once_with(
DEFAULT_CONN_ID, retry_limit=op.databricks_retry_limit, retry_delay=op.databricks_retry_delay
)

db_mock.run_now.assert_called_once_with(expected)
db_mock.get_run_page_url.assert_called_once_with(RUN_ID)
db_mock.get_run_state.assert_called_once_with(RUN_ID)

@mock.patch('airflow.providers.databricks.operators.databricks.DatabricksHook')
def test_no_wait_for_termination(self, db_mock_class):
run = {'notebook_params': NOTEBOOK_PARAMS, 'notebook_task': NOTEBOOK_TASK, 'jar_params': JAR_PARAMS}
op = DatabricksRunNowOperator(task_id=TASK_ID, job_id=JOB_ID, wait_for_termination=False, json=run)
db_mock = db_mock_class.return_value
db_mock.run_now.return_value = 1

assert not op.wait_for_termination

op.execute(None)

expected = databricks_operator._deep_string_coerce(
{
'notebook_params': NOTEBOOK_PARAMS,
'notebook_task': NOTEBOOK_TASK,
'jar_params': JAR_PARAMS,
'job_id': JOB_ID,
}
)
db_mock_class.assert_called_once_with(
DEFAULT_CONN_ID, retry_limit=op.databricks_retry_limit, retry_delay=op.databricks_retry_delay
)

db_mock.run_now.assert_called_once_with(expected)
db_mock.get_run_page_url.assert_called_once_with(RUN_ID)
db_mock.get_run_state.assert_not_called()