Sub topic 4
Building a multi-task workflow with dependencies
This chapter assumes you can write a Databricks notebook, know what a cluster is, and have read the earlier chapters on jobs, tasks, runs, task types and job compute. Here you connect tasks into a workflow.
Tasks form a directed acyclic graph
A job with several tasks is a directed acyclic graph (DAG). Each task is a node. Each depends_on entry is an edge that points from an upstream task to a downstream task. "Acyclic" means no task can depend on itself, directly or through a chain. The Jobs API rejects a definition that contains a cycle.
You declare an edge on the downstream task. Name the upstream task by its task_key, which must be unique within the job.
{
"task_key": "transform",
"depends_on": [{"task_key": "ingest"}],
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/transform"}
}A task with no depends_on starts as soon as the run begins. A task with several entries in depends_on waits for all of them.
A three-task example
The following definition chains ingest, transform and publish. All three tasks share one job cluster, which is created when the run starts and terminated when it ends. The schedule, retries and concurrency settings are covered in later chapters. Note that node_type_id is AWS-specific and differs on other clouds.
{
"name": "daily-sales-pipeline",
"max_concurrent_runs": 1,
"schedule": {
"quartz_cron_expression": "0 0 6 * * ?",
"timezone_id": "UTC",
"pause_status": "UNPAUSED"
},
"job_clusters": [
{
"job_cluster_key": "etl_cluster",
"new_cluster": {
"spark_version": "15.4.x-scala2.12",
"node_type_id": "i3.xlarge",
"num_workers": 2
}
}
],
"tasks": [
{
"task_key": "ingest",
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/ingest"},
"max_retries": 2,
"min_retry_interval_millis": 60000
},
{
"task_key": "transform",
"depends_on": [{"task_key": "ingest"}],
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/transform"}
},
{
"task_key": "publish",
"depends_on": [{"task_key": "transform"}],
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/publish"}
}
]
}Fan-out and fan-in
Tasks that depend on the same upstream task run in parallel. This is a fan-out. A task that lists several upstream tasks waits for all of them. This is a fan-in. For example, publish could depend on both transform_orders and transform_customers, and it would start only after both finish.
Parallel tasks on one shared job cluster compete for the same workers. If they are heavy, give them separate clusters or add workers.
What happens when a task fails
By default, a downstream task runs only if all its upstream tasks succeeded. If ingest fails after its retries, transform and publish are marked as upstream failed and never start. Tasks on other branches that do not depend on the failed task still run. The run_if setting, covered in a later chapter, changes this default.
Common errors
- Typo in
task_key. Adepends_onentry that names a nonexistent task makes the request fail validation. - Circular dependency.
adepends onbandbdepends ona. Remove one edge. - Missing cluster. Each task needs a
job_cluster_key, an existing cluster, or serverless compute. A task without any of these has nowhere to run. - Assuming file order. Task order comes only from
depends_on. The order of entries in thetasksarray has no effect.
Exercise
A job has tasks extract, clean_a, clean_b and load. Both clean tasks depend on extract. load depends on both clean tasks. Which tasks can run in parallel, and what happens to load if clean_b fails?
Show answer
clean_a and clean_b run in parallel once extract succeeds. If clean_b fails, load does not run, because it requires all upstream tasks to succeed. It is marked as upstream failed.