SLA misses and notifications in Apache Airflow - Time & Space Complexity
We want to understand how the time it takes to check SLA misses and send notifications grows as the number of tasks increases.
How does the system handle more tasks and their SLA checks over time?
Analyze the time complexity of the following code snippet.
def sla_miss_callback(dag, task_list, blocking_task_list, slas, blocking_tis):
for sla in slas:
send_notification(sla.task_id, sla.execution_date)
# Airflow calls this function when SLA misses happen
# slas is a list of SLA miss objects
# send_notification sends an alert for each SLA miss
This code sends notifications for each SLA miss detected in a DAG run.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over the list of SLA misses.
- How many times: Once for each SLA miss in the list.
Each SLA miss causes one notification to be sent, so the work grows directly with the number of SLA misses.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 notifications sent |
| 100 | 100 notifications sent |
| 1000 | 1000 notifications sent |
Pattern observation: The number of notifications grows linearly as SLA misses increase.
Time Complexity: O(n)
This means the time to send notifications grows directly in proportion to the number of SLA misses.
[X] Wrong: "Sending notifications happens instantly no matter how many SLA misses there are."
[OK] Correct: Each notification takes some time, so more SLA misses mean more notifications and more total time.
Understanding how notification time grows helps you design systems that handle alerts efficiently as workloads grow.
"What if notifications were sent in batches instead of one by one? How would the time complexity change?"
Practice
sla parameter on an Airflow task do?Solution
Step 1: Understand the purpose of
Theslain Airflow tasksslaparameter sets a time limit for task execution.Step 2: Identify what happens when the limit is exceeded
If the task runs longer than theslatime, Airflow marks it as an SLA miss.Final Answer:
It defines a time limit; if the task runs longer, it triggers an SLA miss. -> Option AQuick Check:
slasets time limit = C [OK]
- Confusing SLA with retry count
- Thinking SLA schedules task start time
- Assuming SLA disables tasks
Solution
Step 1: Recall the correct data type for
Theslaslaparameter expects adatetime.timedeltaobject.Step 2: Identify the correct syntax for 30 minutes timedelta
timedelta(minutes=30)correctly creates a 30-minute duration.Final Answer:
sla=timedelta(minutes=30) -> Option CQuick Check:
slauses timedelta object = B [OK]
- Using string instead of timedelta
- Passing integer without timedelta
- Using incorrect string format
task1 runs longer than 10 minutes?from datetime import timedelta
from airflow import DAG
from airflow.operators.dummy import DummyOperator
from airflow.utils.dates import days_ago
def sla_miss_callback(dag, task_list, blocking_task_list, slas, session=None):
print('SLA missed!')
dag = DAG('sla_example', start_date=days_ago(1))
task1 = DummyOperator(
task_id='task1',
dag=dag,
sla=timedelta(minutes=10)
)
dag.sla_miss_callback = sla_miss_callbackSolution
Step 1: Understand SLA miss callback usage
The DAG-levelsla_miss_callbackis called when any task misses its SLA.Step 2: Identify what the callback does
The callback prints 'SLA missed!' when triggered by a task exceeding its SLA.Final Answer:
Thesla_miss_callbackfunction will print 'SLA missed!' when the SLA is missed. -> Option AQuick Check:
SLA miss triggers callback print = A [OK]
- Thinking SLA causes retries automatically
- Assuming DummyOperator ignores SLA
- Believing DAG fails immediately on SLA miss
def notify_sla_miss(dag, task_list, blocking_task_list, slas, session=None):
send_email('admin@example.com', 'SLA Missed')
dag = DAG('sla_notify', start_date=days_ago(1), sla_miss_callback=notify_sla_miss)
task = DummyOperator(task_id='task', dag=dag, sla=timedelta(minutes=5))What is the likely reason notifications are not sent?
Solution
Step 1: Check the callback function implementation
The callback callssend_emailbut no import or definition is shown.Step 2: Understand Airflow's email sending
Airflow requires proper email function or operator; undefinedsend_emailcauses failure silently.Final Answer:
Thesend_emailfunction is not defined or imported. -> Option DQuick Check:
Undefined email function blocks notification = D [OK]
- Setting callback on DAG instead of task (both valid but DAG preferred)
- Wrong SLA time units
- Assuming DummyOperator ignores SLA
Solution
Step 1: Understand how SLA miss notifications work
SLA misses trigger thesla_miss_callbackfunction assigned to the DAG or task.Step 2: Identify correct way to send Slack message on SLA miss
Implement a callback function that calls Slack API and assign it to DAG'ssla_miss_callback.Final Answer:
Define asla_miss_callbackfunction that uses Slack API and assign it to the DAG'ssla_miss_callbackparameter. -> Option BQuick Check:
SLA miss callback with Slack API = A [OK]
- Using on_failure_callback instead of sla_miss_callback
- Trying to notify via downstream tasks
- Misconfiguring sla parameter as Slack channel
Ready to prove it?
Pass the test and this pattern moves unknown
