Skip to main content
๐Ÿ“˜ Complete PDF

Apache Airflow Certification Practice Test 02

Core Workflow Concepts and Integration Scenarios

Question 1 of 20
๐Ÿ“‹ Multiple Choice
Question 21
A newly joined Data Engineer at a retail company discovers that daily batch processing DAGs frequently fail because upstream vendor files in cloud storage arrive at unpredictable times between 01:00 AM and 04:00 AM. Currently, the DAG starts at 01:00 AM and crashes immediately if the file is absent. Which native Airflow component should be added at the start of the DAG to wait and poll for the file's arrival before executing downstream tasks?
A. EmptyOperator
B. BranchPythonOperator
C. TriggerDagRunOperator
โœ“
D. Sensor (e.g., S3KeySensor or FileSensor)
Answer
โœ… D. Sensor (e.g., S3KeySensor or FileSensor)
Explanation

1. Concept Overview In Apache Airflow, a **Sensor** is a specialized type of operator derived from `BaseSensorOperator` designed to wait for a specific external event, condition, or file arrival to occur before allowing downstream tasks to execute.

2. Why the Correct Option is Right A **Sensor** (such as `S3KeySensor`, `GCSObjectExistenceSensor`, or `FileSensor`) periodically evaluates (pokes) the external storage location at a defined interval (`poke_interval`). Once the vendor file is detected, the sensor completes with `SUCCESS`, and downstream transformation tasks proceed reliably without premature failures.

3. Why Other Options Are Incorrect - **EmptyOperator (formerly DummyOperator):** A no-op operator used solely for visual grouping or joining DAG branches; it does not check external systems or wait for events. - **BranchPythonOperator:** Evaluates Python code to conditionally choose which downstream path to follow; it does not pause or wait for external file arrivals. - **TriggerDagRunOperator:** Programmatically triggers another DAG run; it does not poll cloud storage for incoming files.

4. Production Scenario / Code Example

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

with DAG(
    dag_id="vendor_ingestion_pipeline",
    start_date=datetime(2024, 1, 1),
    schedule="0 1 * * *",
    catchup=False,
    default_args={"retries": 2}
) as dag:

    # Wait for the file using mode='reschedule' to release worker slots between pokes
    wait_for_file = S3KeySensor(
        task_id="wait_for_vendor_file",
        bucket_name="retail-vendor-landing",
        bucket_key="daily_sales_{{ ds_nodash }}.csv",
        poke_interval=300,      # Check every 5 minutes
        timeout=14400,          # 4-hour max wait timeout
        mode="reschedule"       # Frees up worker slot while waiting!
    )

    process_file = PythonOperator(
        task_id="process_vendor_file",
        python_callable=lambda: print("Processing file...")
    )

    wait_for_file >> process_file

5. Real-World Architecture Context In real-world data lakes, third-party partners rarely deliver files at an exact minute. Using Sensors eliminates fragile cron guesswork (e.g., waiting until 5:00 AM hoping the file arrived) and enables event-driven batch scheduling that runs as early as possible.

6. Common Pitfalls & Anti-Patterns - **Using `mode='poke'` for long-running sensors:** In `poke` mode, the sensor holds an active worker execution slot continuously while sleeping, starving other tasks of worker capacity. Always use `mode='reschedule'` for sensors that wait longer than a few minutes. - **Not setting a `timeout`:** Sensors without a timeout will run indefinitely if a file never arrives, blocking DAG completion.

7. Performance & Cost Considerations Using `mode='reschedule'` or modern Airflow **Deferrable Operators (Triggers)** allows thousands of sensors to wait concurrently on a lightweight single async triggerer daemon, consuming virtually zero worker CPU.

8. Exam / Interview Tip Remember the two sensor modes: - `poke` (default): Keeps worker slot occupied during sleep. - `reschedule`: Releases worker slot and sleeps in the database until the next poke interval.

9. Key Takeaways - Sensors wait for external events, files, or states. - Always configure `timeout` and `mode='reschedule'` for production sensors. - Airflow Deferrable Operators provide even greater resource efficiency via the Triggerer.

10. Related Tutorials & Resources - [Sensors in Apache Airflow โ€” Poking, Rescheduling & Triggers](/tutorials/airflow/sensors-airflow) - [Operators Basics in Apache Airflow](/tutorials/airflow/operators-basics)`

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