You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "AutomationDev85 (via GitHub)" <gi...@apache.org> on 2023/04/27 14:02:33 UTC

[GitHub] [airflow] AutomationDev85 opened a new pull request, #30911: Added runtime save if dag is not scheduleable

AutomationDev85 opened a new pull request, #30911:
URL: https://github.com/apache/airflow/pull/30911

   Hi airflow community,
   this is my fifth PR and be happy to work DAG parsing runtime. We faced an issue with slow dag parsing if DAG has huge amount of dagruns and DB query is slow down the run time during DAG parsing. Then the DAG parsing was skipped due to long running query and this is not required if DAG is not scheduled. I think DAGs  which are not scheduled have biggest change to have huge number dagruns and this save runtime during this use case.
   
   This PR will check is the DAG is not scheduled and then skips the DB query which are not required to be executed.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] potiuk commented on a diff in pull request #30911: Save DAG parsing time if dag is not scheduleable

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on code in PR #30911:
URL: https://github.com/apache/airflow/pull/30911#discussion_r1186704387


##########
airflow/models/dag.py:
##########
@@ -2755,25 +2755,28 @@ def bulk_write_to_db(
             session.add(orm_dag)
             orm_dags.append(orm_dag)
 
-        # Get the latest dag run for each existing dag as a single query (avoid n+1 query)
-        most_recent_subq = (
-            session.query(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-            .filter(
-                DagRun.dag_id.in_(existing_dags),
-                or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+        most_recent_runs = {}
+        num_active_runs = {}
+        # Save runtime if dag is not scheduleable
+        if any([dag for dag in dags if not isinstance(dag.timetable, NullTimetable)]):

Review Comment:
   Actually I think it is not what I asked for, but likely this is even more readable:
   
   ```suggestion
           if not all([dag.isinstance(dag.timetable, NullTimetable) for dag in dags]):
   ```
   
   BTW. Can you also please add a unit test actually testing it? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] jens-scheffler-bosch commented on a diff in pull request #30911: Save DAG parsing time if dag is not scheduleable

Posted by "jens-scheffler-bosch (via GitHub)" <gi...@apache.org>.
jens-scheffler-bosch commented on code in PR #30911:
URL: https://github.com/apache/airflow/pull/30911#discussion_r1253593027


##########
airflow/models/dag.py:
##########
@@ -2884,25 +2884,28 @@ def bulk_write_to_db(
             session.add(orm_dag)
             orm_dags.append(orm_dag)
 
-        # Get the latest dag run for each existing dag as a single query (avoid n+1 query)
-        most_recent_subq = (
-            session.query(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-            .filter(
-                DagRun.dag_id.in_(existing_dags),
-                or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+        most_recent_runs = {}
+        num_active_runs = {}

Review Comment:
   Adjusted code. Thanks for the hint!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] AutomationDev85 commented on a diff in pull request #30911: Save DAG parsing time if dag is not scheduleable

Posted by "AutomationDev85 (via GitHub)" <gi...@apache.org>.
AutomationDev85 commented on code in PR #30911:
URL: https://github.com/apache/airflow/pull/30911#discussion_r1227865247


##########
airflow/models/dag.py:
##########
@@ -2755,25 +2755,28 @@ def bulk_write_to_db(
             session.add(orm_dag)
             orm_dags.append(orm_dag)
 
-        # Get the latest dag run for each existing dag as a single query (avoid n+1 query)
-        most_recent_subq = (
-            session.query(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-            .filter(
-                DagRun.dag_id.in_(existing_dags),
-                or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+        most_recent_runs = {}
+        num_active_runs = {}
+        # Save runtime if dag is not scheduleable
+        if any([dag for dag in dags if not isinstance(dag.timetable, NullTimetable)]):

Review Comment:
   As PR https://github.com/apache/airflow/pull/30706 is merged I changed the logic accordingly and added a unit test for it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] uranusjr commented on a diff in pull request #30911: Save DAG parsing time if dag is not scheduleable

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #30911:
URL: https://github.com/apache/airflow/pull/30911#discussion_r1253955878


##########
airflow/models/dag.py:
##########
@@ -2925,27 +2925,30 @@ def bulk_write_to_db(
             session.add(orm_dag)
             orm_dags.append(orm_dag)
 
-        # Get the latest dag run for each existing dag as a single query (avoid n+1 query)
-        most_recent_subq = (
-            select(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-            .where(
-                DagRun.dag_id.in_(existing_dags),
-                or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+        most_recent_runs: dict[str, DagRun] = {}
+        num_active_runs: dict[str, int] = {}
+        # Save runtime if dag is not scheduleable

Review Comment:
   ```suggestion
           # Skip these queries entirely if no DAGs can be scheduled to save time.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] AutomationDev85 commented on a diff in pull request #30911: Save DAG parsing time if dag is not scheduleable

Posted by "AutomationDev85 (via GitHub)" <gi...@apache.org>.
AutomationDev85 commented on code in PR #30911:
URL: https://github.com/apache/airflow/pull/30911#discussion_r1182092773


##########
airflow/models/dag.py:
##########
@@ -2755,25 +2755,28 @@ def bulk_write_to_db(
             session.add(orm_dag)
             orm_dags.append(orm_dag)
 
-        # Get the latest dag run for each existing dag as a single query (avoid n+1 query)
-        most_recent_subq = (
-            session.query(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-            .filter(
-                DagRun.dag_id.in_(existing_dags),
-                or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+        most_recent_runs = {}
+        num_active_runs = {}
+        # Save runtime if dag is not scheduleable
+        if [dag for dag in dags if not isinstance(dag.timetable, NullTimetable)]:

Review Comment:
   Yep you are right! I changed the code.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] potiuk merged pull request #30911: Save DAG parsing time if dag is not scheduleable

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk merged PR #30911:
URL: https://github.com/apache/airflow/pull/30911


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] uranusjr commented on a diff in pull request #30911: Save DAG parsing time if dag is not scheduleable

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #30911:
URL: https://github.com/apache/airflow/pull/30911#discussion_r1227874336


##########
airflow/models/dag.py:
##########
@@ -2884,25 +2884,28 @@ def bulk_write_to_db(
             session.add(orm_dag)
             orm_dags.append(orm_dag)
 
-        # Get the latest dag run for each existing dag as a single query (avoid n+1 query)
-        most_recent_subq = (
-            session.query(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-            .filter(
-                DagRun.dag_id.in_(existing_dags),
-                or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+        most_recent_runs = {}
+        num_active_runs = {}
+        # Save runtime if dag is not scheduleable
+        if any([dag.timetable.can_be_scheduled for dag in dags]):

Review Comment:
   ```suggestion
           if any(dag.timetable.can_be_scheduled for dag in dags):
   ```



##########
airflow/models/dag.py:
##########
@@ -2884,25 +2884,28 @@ def bulk_write_to_db(
             session.add(orm_dag)
             orm_dags.append(orm_dag)
 
-        # Get the latest dag run for each existing dag as a single query (avoid n+1 query)
-        most_recent_subq = (
-            session.query(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-            .filter(
-                DagRun.dag_id.in_(existing_dags),
-                or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+        most_recent_runs = {}
+        num_active_runs = {}

Review Comment:
   From past experience, Mypy will probably ask you to provide better type hints for these. Instead of having an empty dict here, it’s generally easier to add an else block.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] jens-scheffler-bosch commented on a diff in pull request #30911: Save DAG parsing time if dag is not scheduleable

Posted by "jens-scheffler-bosch (via GitHub)" <gi...@apache.org>.
jens-scheffler-bosch commented on code in PR #30911:
URL: https://github.com/apache/airflow/pull/30911#discussion_r1193212433


##########
airflow/models/dag.py:
##########
@@ -2755,25 +2755,28 @@ def bulk_write_to_db(
             session.add(orm_dag)
             orm_dags.append(orm_dag)
 
-        # Get the latest dag run for each existing dag as a single query (avoid n+1 query)
-        most_recent_subq = (
-            session.query(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-            .filter(
-                DagRun.dag_id.in_(existing_dags),
-                or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+        most_recent_runs = {}
+        num_active_runs = {}
+        # Save runtime if dag is not scheduleable
+        if any([dag for dag in dags if not isinstance(dag.timetable, NullTimetable)]):

Review Comment:
   As PR #30706 changes the structure of ǸullTimeTable` and review feedback was pointing to not using `isinstance()` I propose to wait until #30706 is merged and then re-base this change accordingly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] potiuk commented on a diff in pull request #30911: Save DAG parsing time if dag is not scheduleable

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on code in PR #30911:
URL: https://github.com/apache/airflow/pull/30911#discussion_r1181091669


##########
airflow/models/dag.py:
##########
@@ -2755,25 +2755,28 @@ def bulk_write_to_db(
             session.add(orm_dag)
             orm_dags.append(orm_dag)
 
-        # Get the latest dag run for each existing dag as a single query (avoid n+1 query)
-        most_recent_subq = (
-            session.query(DagRun.dag_id, func.max(DagRun.execution_date).label("max_execution_date"))
-            .filter(
-                DagRun.dag_id.in_(existing_dags),
-                or_(DagRun.run_type == DagRunType.BACKFILL_JOB, DagRun.run_type == DagRunType.SCHEDULED),
+        most_recent_runs = {}
+        num_active_runs = {}
+        # Save runtime if dag is not scheduleable
+        if [dag for dag in dags if not isinstance(dag.timetable, NullTimetable)]:

Review Comment:
   I think `any([not dag.isinstance(dag.timetable, NullTimeTable) for dag in dags])` syntax is slightly more readable, can you please change it this way? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org