Skip to main content
๐Ÿ“˜ Complete PDF

Apache Airflow Certification Practice Test 03

Real Company Scenario Questions

Question 1 of 20
๐Ÿ“‹ Multiple Choice
Question 41
A newly joined Data Engineer at a global retail company is asked to support a nightly ingestion DAG. The previous developer attempted to handle unpredictable vendor file arrival times by adding `time.sleep(7200)` inside a PythonOperator, causing the Airflow worker to hang and block other scheduled jobs for two hours. What is the Airflow-native architectural pattern to wait for file arrivals without blocking worker execution slots?
A. Increase worker count from 2 to 50 nodes
B. Convert the entire DAG into a single monolithic Bash script
โœ“
C. Replace time.sleep with a File or S3 Sensor configured in reschedule mode
D. Execute the Python script directly on the Airflow scheduler VM
Answer
โœ… C. Replace time.sleep with a File or S3 Sensor configured in reschedule mode
Explanation

1. Concept Overview Using `time.sleep()` inside Airflow tasks is a severe anti-pattern. While Python sleeps, the task continues to consume an active Airflow worker execution slot, preventing other pending tasks across the cluster from being scheduled. The native solution is to use an **Airflow Sensor** configured with **`mode='reschedule'`**.

2. Why the Correct Option is Right Replacing `time.sleep` with a **Sensor in `reschedule` mode** (e.g., `S3KeySensor(mode='reschedule', poke_interval=300)`) checks for file existence. If the file is not yet present, the sensor immediately releases its worker execution slot back to the worker pool and transitions to the `UP_FOR_RESCHEDULE` state. The worker is free to process other DAG tasks until the sensor's next check.

3. Why Other Options Are Incorrect - **Increase worker count to 50 nodes:** Inefficient and expensive; provisioning dozens of cloud VMs just to run sleeping threads burns budget without fixing the architectural flaw. - **Convert DAG into a monolithic Bash script:** Bypasses Airflow's task isolation, retries, and monitoring, losing all orchestration benefits. - **Execute script directly on the scheduler VM:** Violates separation of concerns, risks exhausting scheduler CPU/memory, and can crash the entire Airflow deployment.

4. Production Scenario / Code Example

from datetime import datetime
from airflow import DAG
from airflow.providers.amazon.aws.sensors.s3 import S3KeySensor
from airflow.operators.python import PythonOperator

with DAG(
    dag_id="safe_vendor_file_wait",
    start_date=datetime(2024, 1, 1),
    schedule="0 2 * * *",
    catchup=False
) as dag:

    # Native Sensor in reschedule mode: ZERO worker slot blocking while waiting!
    wait_for_delivery = S3KeySensor(
        task_id="wait_for_vendor_s3_file",
        bucket_name="retail-inbound-lake",
        bucket_key="vendor_deliveries/{{ ds }}/sales.csv",
        poke_interval=300,      # Check every 5 minutes
        timeout=10800,          # 3-hour maximum wait
        mode="reschedule"       # Crucial: releases worker slot!
    )

    process_data = PythonOperator(
        task_id="process_sales",
        python_callable=lambda: print("File found, processing...")
    )

    wait_for_delivery >> process_data

5. Real-World Architecture Context In modern Airflow 2.2+, data platform engineers take this further using **Deferrable Operators** (Triggers). Deferrable sensors offload waiting logic to an asynchronous event loop daemon (the Airflow Triggerer), allowing tens of thousands of sensors to wait concurrently with near-zero resource consumption.

6. Common Pitfalls & Anti-Patterns - **Using `mode='poke'` on sensors waiting over 15 minutes:** `mode='poke'` holds the worker slot active during sleep intervals. For long waits, always use `mode='reschedule'`. - **Calling `time.sleep()` in top-level code or task callables:** Always avoid `time.sleep()` in Airflow pipelines.

7. Performance & Cost Considerations Using `mode='reschedule'` allows a small 4-node Airflow worker cluster to handle hundreds of concurrent waiting sensors without task starvation or queuing backlogs.

8. Exam / Interview Tip When an exam scenario mentions *"tasks waiting for external events"*, *"workers blocked"*, or *"replacing sleep"*, the answer is **Sensors with mode='reschedule'** (or Deferrable Operators).

9. Key Takeaways - Never use `time.sleep()` inside Airflow tasks. - Sensors evaluate external conditions and support `mode='reschedule'`. - `mode='reschedule'` frees up worker slots between checks.

10. Related Tutorials & Resources - [Sensors in Apache Airflow โ€” Poking, Rescheduling & Triggers](/tutorials/airflow/sensors-airflow) - [Performance Tuning โ€” Pools, Priority Weights, Parallelism](/tutorials/airflow/performance-tuning-pools-priority-weights-parallelism)`

Answered: 0 / 20
Question Navigator (Test 03)0% Answered