The exact day when I published the blog post on the Table update trigger I discovered a different way for triggering Lakeflow Jobs having upstream dependencies not managed in the same Declarative Automation Bundle (DAB). Although this trigger is still in beta, it's a great addition to the scheduling capabilities.
What would it take for you to trust your Databricks pipelines in production?
A 3-day bug hunt on a 3-person team costs up to €7,200 in lost engineering time. This workshop teaches you to prevent that — unit tests, data tests, and integration tests for PySpark and Databricks Lakeflow, including Spark Declarative Pipelines.
Konieczny
Why
If your Lakeflow job depends on a different job, you have different scheduling choices:
- You can use a data dependency like a table update or a file upload that will be used as the trigger for your job
- You can create an approximate CRON schedule that would detect if the upstream pipeline already ran and if not, raise an error to force you repair it once the upstream dependency completes fully
- You can modify the upstream pipeline to trigger your job
The schema below illustrates them:
Many choices but none of them come for free. Both data dependency and upstream pipeline modification require changing something that is not probably in your scope. You know it like me, asking for changes - even the tiniest ones - is not always prioritized compared to business use cases the upstream team is working on.
Fine, you might then opt for the second option where you define an approximate trigger time that would start your pipeline roughly after the upstream pipeline completes. You add some additional safety time before your pipeline triggers, and also create the task that is going to assert on the upstream pipeline completeness. It might work but not without a lot of implementation effort on your side. Besides, it can also bring some maintenance work as the upstream pipeline may complete longer than you have been expecting, and therefore, trigger alerts with your first completeness checking task.
None of them is perfect and for that exact reason Databricks Lakeflow runner proposes now a trigger called job completion.
Job completion trigger
Nothing better to start than the snapshot from the API documentation:
From that screenshot we can identify a few of the important features of this new trigger:
- Cross-workspace. You can refer to a job existing in another workspace. The single requirement here for both workspaces is to use the same metastore.
- Job id. You need to know the job id before creating the trigger. Besides, the identity running your job must have at least the CAN VIEW privilege on the upstream job. Consequently, you may still need to interact with the upstream teams when it comes to fulfilling this requirement but this interaction is rather a single configuration addition at the infrastructure layer rather than the application changes.
- Many upstream jobs. As you can notice from the screenshot, the jobs parameter is an array that can store at most 10 job references. Consequently, you need a fine grained control over these many jobs. You can solve it by using the if_status flag and control them a little bit like with Apache Airflow's trigger rules
- If you need all upstream jobs to be successfully executed, use the SUCCESS condition.
- If you need all upstream jobs to fail, use the FAILED condition.
- Finally, if you don't care about the outcome but only about the completeness status, use the DONE condition.
Once you define the trigger, you can get the jobs that triggered it from the {{job.trigger.job_completion.completed_jobs}} variable. It's an array where each entry contains: the job id, the run id, the workspace id, the termination status, and the completion time.
Consistency
There is a consistency trap, though. Let's consider your job depends on two upstream jobs that are managed by two different teams who have two different SLAs. One team has a strict SLA and in practice their pipelines are fixed within a few hours from any outage. Another team has much weaker SLA and their pipelines are usually not fixed for a few days.
If your job uses the ALL_SUCCESS condition and the weaker SLA job has not been running for several days, you will face one of these situations:
- You will be running your job manually and maybe having some temporary inconsistency as the weaker's job SLA data won't be refreshed
- You will not refresh your data for several days and once your pipeline will trigger, it will process several days of data at once. Even for an auto-scaling compute it can be difficult to handle and lead to resources problems.
For that reason it's quite important to properly monitor your part and be alerted whenever data freshness of the table(s) managed by your job is broken. That way you can adapt and either prefer a temporary inconsistency, or prepare the cluster for handling a few days of data at once.
Demo
Ready to see some code? Let's check the trigger in action. The code is written against the Databricks Free Edition, so there are no cross-workspace access constraints.
Upstream jobs trigger is another great extension to the Lakeflow Jobs capabilities that favors the micro-DAB approach where teams can work independently on their bundles and despite the physical-repo isolation, be able to interact together.
Data Engineering Design Patterns
Looking for a book that defines and solves most common data engineering problems? I wrote
one on that topic! You can read it online
on the O'Reilly platform,
or get a print copy on Amazon.
I also help solve your data engineering problems contact@waitingforcode.com đź“©
Read also about Upstream jobs trigger on Databricks here:
Related blog posts:
- SQL Condition triggers on Databricks
- Python Declarative Automation Bundle aka PyDAB
- Table update trigger for Lakeflow Jobs
