[GitHub] [airflow] pingzh commented on a diff in pull request #22917: remove stale serialized dags

2022-05-11 Thread GitBox


pingzh commented on code in PR #22917:
URL: https://github.com/apache/airflow/pull/22917#discussion_r870560339


##
airflow/dag_processing/manager.py:
##
@@ -514,6 +514,10 @@ def _deactivate_stale_dags(self, session=None):
 if deactivated:
 self.log.info("Deactivated %i DAGs which are no longer 
present in file.", deactivated)
 
+for dag_id in to_deactivate:

Review Comment:
   good call



-- 
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-unsubscr...@airflow.apache.org

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



[GitHub] [airflow] pingzh commented on a diff in pull request #22917: remove stale serialized dags

2022-05-11 Thread GitBox


pingzh commented on code in PR #22917:
URL: https://github.com/apache/airflow/pull/22917#discussion_r870551878


##
airflow/dag_processing/manager.py:
##
@@ -514,6 +514,10 @@ def _deactivate_stale_dags(self, session=None):
 if deactivated:
 self.log.info("Deactivated %i DAGs which are no longer 
present in file.", deactivated)
 
+for dag_id in to_deactivate:
+SerializedDagModel.remove_dag(dag_id)
+self.log.info("Deleted DAG %s in serialized_dag table", 
dag_id)
+

Review Comment:
   @kaxil `remove_deleted_dags` only removes dags whose dag_files are deleted.
   
   this pr takes care of the case like a dag_id is renamed in a dag_file, or 
some dag_ids are removed in a dag_file



-- 
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-unsubscr...@airflow.apache.org

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



[GitHub] [airflow] pingzh commented on a diff in pull request #22917: remove stale serialized dags

2022-05-03 Thread GitBox


pingzh commented on code in PR #22917:
URL: https://github.com/apache/airflow/pull/22917#discussion_r864437030


##
airflow/models/serialized_dag.py:
##
@@ -220,6 +220,35 @@ def remove_dag(cls, dag_id: str, session: Session = None):
 """
 session.execute(cls.__table__.delete().where(cls.dag_id == dag_id))
 
+@classmethod
+@provide_session
+def remove_unknown_dags_in_dag_file(
+cls, active_dag_ids: List[str], dag_file_path: str, session=None
+) -> List[str]:
+"""
+Deletes serialized_dags whose dag_ids are not in the active_dag_ids 
for a given dag_file_path
+
+:return: a list of inactive dag ids
+:rtype: list[str]
+"""
+file_path_hash = DagCode.dag_fileloc_hash(dag_file_path)
+# there is a db index on fileloc_hash
+rows = (
+session.query(cls.dag_id)
+.filter(cls.fileloc_hash == file_path_hash, cls.fileloc == 
dag_file_path)
+.all()
+)
+dag_ids_from_db = [i[0] for i in rows]

Review Comment:
   this is way better. thanks



-- 
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-unsubscr...@airflow.apache.org

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



[GitHub] [airflow] pingzh commented on a diff in pull request #22917: remove stale serialized dags

2022-05-03 Thread GitBox


pingzh commented on code in PR #22917:
URL: https://github.com/apache/airflow/pull/22917#discussion_r864436993


##
tests/dag_processing/test_processor.py:
##
@@ -635,6 +636,30 @@ def test_import_error_tracebacks(self, tmpdir):
 )
 session.rollback()
 
+def test_process_file_should_remove_unknown_serialized_dags(self):
+dag_file = os.path.join(
+os.path.dirname(os.path.realpath(__file__)), 
'../dags/test_only_dummy_tasks.py'
+)

Review Comment:
   nice, thanks



-- 
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-unsubscr...@airflow.apache.org

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



[GitHub] [airflow] pingzh commented on a diff in pull request #22917: remove stale serialized dags

2022-04-12 Thread GitBox


pingzh commented on code in PR #22917:
URL: https://github.com/apache/airflow/pull/22917#discussion_r848675598


##
airflow/models/serialized_dag.py:
##
@@ -220,6 +220,36 @@ def remove_dag(cls, dag_id: str, session: Session = None):
 """
 session.execute(cls.__table__.delete().where(cls.dag_id == dag_id))
 
+@classmethod
+@provide_session
+def remove_unknown_dags_in_dag_file(
+cls, active_dag_ids: List[str], dag_file_path: str, session=None
+) -> List[str]:
+"""
+Deletes serialized_dags whose dag_ids are not in the active_dag_ids 
for a given dag_file_path
+
+:return: a list of inactive dag ids
+:rtype: list[str]
+"""
+file_path_hash = DagCode.dag_fileloc_hash(dag_file_path)
+# there is a db index on fileloc_hash
+rows = session.query(cls.dag_id).filter(cls.fileloc_hash == 
file_path_hash).all()
+dag_ids_from_db = [i[0] for i in rows]
+
+dag_ids_to_remove = [dag_id for dag_id in dag_ids_from_db if dag_id 
not in active_dag_ids]

Review Comment:
   after reading the implementation of `dag_fileloc_hash`, it makes sense to do 
the check. updated.



-- 
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-unsubscr...@airflow.apache.org

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



[GitHub] [airflow] pingzh commented on a diff in pull request #22917: remove stale serialized dags

2022-04-11 Thread GitBox


pingzh commented on code in PR #22917:
URL: https://github.com/apache/airflow/pull/22917#discussion_r847844780


##
airflow/models/serialized_dag.py:
##
@@ -220,6 +220,36 @@ def remove_dag(cls, dag_id: str, session: Session = None):
 """
 session.execute(cls.__table__.delete().where(cls.dag_id == dag_id))
 
+@classmethod
+@provide_session
+def remove_unknown_dags_in_dag_file(
+cls, active_dag_ids: List[str], dag_file_path: str, session=None
+) -> List[str]:
+"""
+Deletes serialized_dags whose dag_ids are not in the active_dag_ids 
for a given dag_file_path
+
+:return: a list of inactive dag ids
+:rtype: list[str]
+"""
+file_path_hash = DagCode.dag_fileloc_hash(dag_file_path)
+# there is a db index on fileloc_hash
+rows = session.query(cls.dag_id).filter(cls.fileloc_hash == 
file_path_hash).all()
+dag_ids_from_db = [i[0] for i in rows]
+
+dag_ids_to_remove = [dag_id for dag_id in dag_ids_from_db if dag_id 
not in active_dag_ids]
+
+if dag_ids_to_remove:
+# delete one by one to avoid locking the table

Review Comment:
   good question. I am not sure about the other dbs, as we only use mysql. but 
i think deleting with small batch could have smaller load in db.



-- 
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-unsubscr...@airflow.apache.org

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



[GitHub] [airflow] pingzh commented on a diff in pull request #22917: remove stale serialized dags

2022-04-11 Thread GitBox


pingzh commented on code in PR #22917:
URL: https://github.com/apache/airflow/pull/22917#discussion_r847844344


##
airflow/models/serialized_dag.py:
##
@@ -220,6 +220,36 @@ def remove_dag(cls, dag_id: str, session: Session = None):
 """
 session.execute(cls.__table__.delete().where(cls.dag_id == dag_id))
 
+@classmethod
+@provide_session
+def remove_unknown_dags_in_dag_file(
+cls, active_dag_ids: List[str], dag_file_path: str, session=None
+) -> List[str]:
+"""
+Deletes serialized_dags whose dag_ids are not in the active_dag_ids 
for a given dag_file_path
+
+:return: a list of inactive dag ids
+:rtype: list[str]
+"""
+file_path_hash = DagCode.dag_fileloc_hash(dag_file_path)
+# there is a db index on fileloc_hash
+rows = session.query(cls.dag_id).filter(cls.fileloc_hash == 
file_path_hash).all()
+dag_ids_from_db = [i[0] for i in rows]
+
+dag_ids_to_remove = [dag_id for dag_id in dag_ids_from_db if dag_id 
not in active_dag_ids]

Review Comment:
   good call. but i don't think it is necessary to check here. if we think 
collision is an issue, we might want to change the way it is generated. let me 
know your thoughts



-- 
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-unsubscr...@airflow.apache.org

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