DatabricksTaskOperator

Use the DatabricksTaskOperator to launch and monitor task runs on Databricks as Airflow tasks. This can be used as a standalone operator in a Dag and as well as part of a Databricks Workflow by using it as an operator(task) within the DatabricksWorkflowTaskGroup.

Examples

Running a notebook in Databricks using DatabricksTaskOperator

tests/system/databricks/example_databricks.py[source]

    task_operator_nb_1 = DatabricksTaskOperator(
        task_id="nb_1",
        databricks_conn_id="databricks_conn",
        job_cluster_key="Shared_job_cluster",
        task_config={
            "notebook_task": {
                "notebook_path": "/Shared/Notebook_1",
                "source": "WORKSPACE",
            },
            "libraries": [
                {"pypi": {"package": "Faker"}},
                {"pypi": {"package": "simplejson"}},
            ],
        },
    )

Running a SQL query in Databricks using DatabricksTaskOperator

tests/system/databricks/example_databricks.py[source]

    task_operator_sql_query = DatabricksTaskOperator(
        task_id="sql_query",
        databricks_conn_id="databricks_conn",
        task_config={
            "sql_task": {
                "query": {
                    "query_id": QUERY_ID,
                },
                "warehouse_id": WAREHOUSE_ID,
            }
        },
    )

Configuring Databricks-native task retries

Use max_retries, min_retry_interval_millis and retry_on_timeout to configure Databricks-native task retries. Databricks reruns failed task attempts within the same job run, so Airflow sees only the final result. Set max_retries to -1 to retry indefinitely, or 0 to disable retries.

These settings are independent of the Airflow task-level retries parameter, which retries the whole Airflow task. You can set the same fields directly in task_config. When both are set, the operator parameter takes precedence. If a field is unset, Databricks uses its default.

Airflow retries behaves differently depending on where the operator runs. For a standalone operator, a retry reconnects to the run the previous try submitted only when that try was lost while the run was still going; a run that already reached a terminal failure on Databricks is replaced by a fresh submit (see Durable execution below). Inside a DatabricksWorkflowTaskGroup, the Airflow task monitors a sub-run that was already submitted by the workflow launch task, so a retry only re-polls the terminal sub-run. Use max_retries to retry Databricks work inside a workflow task group.

Inside a DatabricksWorkflowTaskGroup, a task that exhausts a finite max_retries is reported as failed as soon as its final failed attempt is observed, so downstream failure handling is not delayed by long-running sibling tasks. Only unlimited retries (max_retries=-1) keep the Airflow task waiting (or deferring) until the parent workflow run reaches a terminal state, because Databricks may still launch a retry attempt under the same task_key until then. Sibling tasks in the run continue independently.

Durable execution

Outside a DatabricksWorkflowTaskGroup, this operator submits a run and then waits for it, on the worker or in the Triggerer when deferrable=True. By default it runs in a durable mode: the Databricks run id is persisted to task state store before the wait begins, so if the worker crashes or is preempted and the task is retried, the operator reconnects to the run that is already executing on Databricks instead of submitting a duplicate.

On retry the operator checks the prior run’s state:

  • if it is still running, the operator reconnects and continues polling

  • if it already succeeded, the operator returns immediately without resubmitting

  • if it failed terminally, the operator submits a fresh run

Pass durable=False to always submit a fresh run on retry.

Unlike DatabricksSubmitRunOperator and DatabricksRunNowOperator, where durable has no effect in deferrable mode, durable execution here also covers deferrable=True: the run id is stored before the task defers, so a try lost while the Triggerer was waiting still reconnects on the next one.

Durable execution requires Airflow 3.3 or newer, since it relies on the task state store. On earlier Airflow versions the flag is a no-op and the operator always submits a fresh run on retry. If the task state store is unavailable at runtime, the operator logs that crash recovery is disabled and behaves the same way.

Inside a DatabricksWorkflowTaskGroup nothing changes: the run is submitted by the workflow launch task and this operator only monitors its own task within that shared run, so a retry already reconnects rather than resubmitting.

[state_store] default_retention_days (30 days by default) is stamped on the run id when it is stored, so a retry that comes later than that finds nothing and submits a fresh run instead of reconnecting, whatever schedule airflow state-store clean runs on. Size that setting against your longest retry_delay. See Task and Asset State Store Cleanup.

Clearing a task discards the stored run id by default, so the next attempt submits a fresh run. Set keep_task_state when clearing to keep it instead. Resumable Tasks lists which clear paths discard it and describes the [state_store] clear_on_success setting.

Where the id is kept, clearing behaves like a retry rather than a fresh start. If the stored run already succeeded, the next attempt reads it back, re-pushes its XComs and returns without submitting anything. If the run is still going and wait_for_termination=False, the attempt reconnects to it and returns immediately, also without submitting.

Was this entry helpful?