-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dagster-airlift] airflow operator switcher
- Loading branch information
Showing
8 changed files
with
199 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...lift/dagster_airlift_tests/integration_tests/airflow_op_switcheroo/dags/switcheroo_dag.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import logging | ||
import os | ||
from datetime import datetime | ||
|
||
from airflow import DAG | ||
from airflow.operators.python import PythonOperator | ||
from dagster_airlift.in_airflow import mark_as_dagster_migrating | ||
from dagster_airlift.migration_state import ( | ||
AirflowMigrationState, | ||
DagMigrationState, | ||
TaskMigrationState, | ||
) | ||
|
||
logging.basicConfig() | ||
logging.getLogger().setLevel(logging.INFO) | ||
requests_log = logging.getLogger("requests.packages.urllib3") | ||
requests_log.setLevel(logging.INFO) | ||
requests_log.propagate = True | ||
|
||
|
||
def write_to_file_in_airflow_home() -> None: | ||
airflow_home = os.environ["AIRFLOW_HOME"] | ||
with open(os.path.join(airflow_home, "airflow_home_file.txt"), "w") as f: | ||
f.write("Hello") | ||
|
||
|
||
def write_to_other_file_in_airflow_home() -> None: | ||
airflow_home = os.environ["AIRFLOW_HOME"] | ||
with open(os.path.join(airflow_home, "other_airflow_home_file.txt"), "w") as f: | ||
f.write("Hello") | ||
|
||
|
||
default_args = { | ||
"owner": "airflow", | ||
"depends_on_past": False, | ||
"start_date": datetime(2023, 1, 1), | ||
"retries": 1, | ||
} | ||
|
||
dag = DAG( | ||
"the_dag", default_args=default_args, schedule_interval=None, is_paused_upon_creation=False | ||
) | ||
op_to_migrate = PythonOperator( | ||
task_id="some_task", python_callable=write_to_file_in_airflow_home, dag=dag | ||
) | ||
op_doesnt_migrate = PythonOperator( | ||
task_id="other_task", python_callable=write_to_other_file_in_airflow_home, dag=dag | ||
) | ||
# Add a dependency between the two tasks | ||
op_doesnt_migrate.set_upstream(op_to_migrate) | ||
|
||
# # set up the debugger | ||
# print("Waiting for debugger to attach...") | ||
# debugpy.listen(("localhost", 7778)) | ||
# debugpy.wait_for_client() | ||
mark_as_dagster_migrating( | ||
migration_state=AirflowMigrationState( | ||
dags={ | ||
"the_dag": DagMigrationState( | ||
tasks={ | ||
"some_task": TaskMigrationState(migrated=True), | ||
"other_task": TaskMigrationState(migrated=True), | ||
} | ||
) | ||
} | ||
) | ||
) |
9 changes: 9 additions & 0 deletions
9
...ter-airlift/dagster_airlift_tests/integration_tests/airflow_op_switcheroo/dagster_defs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from dagster import Definitions, asset | ||
|
||
|
||
@asset | ||
def the_dag__some_task(): | ||
return "asset_value" | ||
|
||
|
||
defs = Definitions(assets=[the_dag__some_task]) |
57 changes: 57 additions & 0 deletions
57
...ental/dagster-airlift/dagster_airlift_tests/integration_tests/test_operator_switcheroo.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import time | ||
from pathlib import Path | ||
|
||
import pytest | ||
import requests | ||
from dagster import AssetKey, DagsterInstance, DagsterRunStatus | ||
from dagster._core.test_utils import environ | ||
from dagster._time import get_current_timestamp | ||
|
||
|
||
@pytest.fixture(name="dags_dir") | ||
def setup_dags_dir() -> Path: | ||
return Path(__file__).parent / "airflow_op_switcheroo" / "dags" | ||
|
||
|
||
@pytest.fixture(name="dagster_defs_path") | ||
def setup_dagster_defs_path() -> str: | ||
return str(Path(__file__).parent / "airflow_op_switcheroo" / "dagster_defs.py") | ||
|
||
|
||
def test_migrated_operator( | ||
airflow_instance: None, dagster_dev: None, dagster_home: str, airflow_home: str | ||
) -> None: | ||
"""Tests that dagster migrated operator can correctly map airflow tasks to dagster tasks, and kick off executions.""" | ||
response = requests.post( | ||
"http://localhost:8080/api/v1/dags/the_dag/dagRuns", auth=("admin", "admin"), json={} | ||
) | ||
assert response.status_code == 200, response.json() | ||
# Wait until the run enters a terminal state | ||
terminal_status = None | ||
start_time = get_current_timestamp() | ||
while get_current_timestamp() - start_time < 30: | ||
response = requests.get( | ||
"http://localhost:8080/api/v1/dags/the_dag/dagRuns", auth=("admin", "admin") | ||
) | ||
assert response.status_code == 200, response.json() | ||
dag_runs = response.json()["dag_runs"] | ||
if dag_runs[0]["state"] in ["success", "failed"]: | ||
terminal_status = dag_runs[0]["state"] | ||
break | ||
time.sleep(1) | ||
assert terminal_status == "success", ( | ||
"Never reached terminal status" | ||
if terminal_status is None | ||
else f"terminal status was {terminal_status}" | ||
) | ||
with environ({"DAGSTER_HOME": dagster_home}): | ||
instance = DagsterInstance.get() | ||
runs = instance.get_runs() | ||
# The graphql endpoint kicks off a run for each of the tasks in the dag | ||
assert len(runs) == 1 | ||
some_task_run = [ # noqa | ||
run | ||
for run in runs | ||
if set(list(run.asset_selection)) == {AssetKey(["the_dag__some_task"])} # type: ignore | ||
][0] | ||
assert some_task_run.status == DagsterRunStatus.SUCCESS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters