You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2021/11/26 17:44:02 UTC

[GitHub] [airflow] siddharthvp opened a new pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

siddharthvp opened a new pull request #17891:
URL: https://github.com/apache/airflow/pull/17891


   While the exception (`AirflowDagDuplicatedIdException`) already
   existed, till now it was being raised only when both the DAGs with
   same ID were in the same file.
   
   Closes #17861.
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   ---
   **^ Add meaningful description above**
   
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#pull-request-guidelines)** for more information.
   In case of fundamental code change, Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvements+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in [UPDATING.md](https://github.com/apache/airflow/blob/main/UPDATING.md).
   


-- 
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] siddharthvp commented on pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
siddharthvp commented on pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#issuecomment-908299380


   Yes the logic is indeed hacky. 
   
   @uranusjr Even with the two solutions you suggest, the problem doesn't really go away (unless I'm much mistaken) - if a DAG is moved from one file to another in the middle of a parsing run and Airflow processed the source file before the rename occurred and the destination file after the rename, we still have a false-positive error. 
   
   So I think the only way to implement this feature would be via hacky heuristics. Maybe we should also change the exception text to clarify that it is a _possible_ duplication. When the next DAG parsing run occurs, the false-positive alert would go 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



[GitHub] [airflow] siddharthvp commented on a change in pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
siddharthvp commented on a change in pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#discussion_r700544206



##########
File path: airflow/models/dagbag.py
##########
@@ -466,6 +467,34 @@ def _bag_dag(self, *, dag, root_dag, recursive):
                         del self.dags[subdag.dag_id]
             raise
 
+    @provide_session
+    def _check_if_duplicate(self, dag, session=None):
+        """
+        Checks if a DAG with the same ID already exists.
+        If present, returns the fileloc of the existing DAG.
+        """
+        from airflow.models.serialized_dag import SerializedDagModel  # Avoid circular import
+
+        other_dag = session.query(SerializedDagModel).filter(
+            SerializedDagModel.dag_id == dag.dag_id,
+            SerializedDagModel.fileloc_hash != DagCode.dag_fileloc_hash(dag.fileloc)
+        ).first()

Review comment:
       From the code, I believe that an import error will be raised for each collision. (If there are 3 DAGs with same ID, one gets parsed and the other 2 raise errors.)




-- 
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] siddharthvp edited a comment on pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
siddharthvp edited a comment on pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#issuecomment-908299380


   Yes the logic is indeed hacky. 
   
   @uranusjr Even with the two solutions you suggest, the problem doesn't really go away (unless I'm much mistaken) - if a DAG is moved from one file to another in the middle of a parsing run and Airflow processed the source file before the rename occurred and the destination file after the rename, we still have a false-positive error. 
   
   So I think the only way to implement this feature would be via hacky heuristics. Maybe we should also change the exception text to clarify that it is a _possible_ duplication. When the next DAG parsing run occurs, the false-positive alert would disappear. 


-- 
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] ephraimbuddy commented on a change in pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
ephraimbuddy commented on a change in pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#discussion_r699050319



##########
File path: airflow/models/dagbag.py
##########
@@ -466,6 +467,34 @@ def _bag_dag(self, *, dag, root_dag, recursive):
                         del self.dags[subdag.dag_id]
             raise
 
+    @provide_session
+    def _check_if_dupe(self, dag, session=None):

Review comment:
       ```suggestion
       def _check_if_duplicate(self, dag, session=None):
   ```
   I would suggest it's named as above for clarity




-- 
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] SamWheating commented on a change in pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
SamWheating commented on a change in pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#discussion_r699562107



##########
File path: airflow/models/dagbag.py
##########
@@ -466,6 +467,34 @@ def _bag_dag(self, *, dag, root_dag, recursive):
                         del self.dags[subdag.dag_id]
             raise
 
+    @provide_session
+    def _check_if_duplicate(self, dag, session=None):
+        """
+        Checks if a DAG with the same ID already exists.
+        If present, returns the fileloc of the existing DAG.
+        """
+        from airflow.models.serialized_dag import SerializedDagModel  # Avoid circular import
+
+        other_dag = session.query(SerializedDagModel).filter(
+            SerializedDagModel.dag_id == dag.dag_id,
+            SerializedDagModel.fileloc_hash != DagCode.dag_fileloc_hash(dag.fileloc)
+        ).first()

Review comment:
       In the unlikely event that there are more than two instances of a single DAG name, this is only going to alert on one of the collisions, correct?
   
   Do you think its worthwhile to handle this case and alert for a larger collision, Or would it be too much added complexity for a rare failure?

##########
File path: airflow/models/dagbag.py
##########
@@ -466,6 +467,34 @@ def _bag_dag(self, *, dag, root_dag, recursive):
                         del self.dags[subdag.dag_id]
             raise
 
+    @provide_session
+    def _check_if_duplicate(self, dag, session=None):
+        """
+        Checks if a DAG with the same ID already exists.
+        If present, returns the fileloc of the existing DAG.
+        """
+        from airflow.models.serialized_dag import SerializedDagModel  # Avoid circular import
+
+        other_dag = session.query(SerializedDagModel).filter(
+            SerializedDagModel.dag_id == dag.dag_id,
+            SerializedDagModel.fileloc_hash != DagCode.dag_fileloc_hash(dag.fileloc)
+        ).first()
+        if other_dag:
+            # If a DAG was just moved from one file to another, this condition may sometimes check out.
+            # Heuristics below try to avoid a false-positive alert
+            if not os.path.exists(other_dag.fileloc):
+                # Other file is no more, nothing to worry about
+                return None
+            # Not feasible here to parse that other file to check if it still has the DAG,
+            # so resort to just checking the task ids
+            task_ids = [task.task_id for task in dag.tasks]
+            other_dag_task_ids = [task['label'] for task in other_dag.data['dag']['tasks']]
+            if task_ids != other_dag_task_ids:

Review comment:
       If these two lists are built from the same DAG, will they always be in the same order? If not then the comparison isn't going to be accurate. 
   
   If they aren't in a deterministic order, maybe we could sort or cast to a set before comparison? 




-- 
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] siddharthvp commented on a change in pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
siddharthvp commented on a change in pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#discussion_r698016903



##########
File path: airflow/models/dagbag.py
##########
@@ -466,6 +467,34 @@ def _bag_dag(self, *, dag, root_dag, recursive):
                         del self.dags[subdag.dag_id]
             raise
 
+    @provide_session
+    def _check_if_dupe(self, dag, session=None):
+        """
+        Checks if a DAG with the same ID already exists.
+        If present, returns the fileloc of the existing DAG.
+        """
+        from airflow.models.serialized_dag import SerializedDagModel  # Avoid circular import
+
+        other_dag = session.query(SerializedDagModel).filter(
+            SerializedDagModel.dag_id == dag.dag_id,
+            SerializedDagModel.fileloc_hash != DagCode.dag_fileloc_hash(dag.fileloc)

Review comment:
       Using fileloc_hash here as that's an indexed column so should be faster to query, though this does necessitate computing the hash of the current dag's fileloc.

##########
File path: airflow/models/dagbag.py
##########
@@ -466,6 +467,34 @@ def _bag_dag(self, *, dag, root_dag, recursive):
                         del self.dags[subdag.dag_id]
             raise
 
+    @provide_session
+    def _check_if_dupe(self, dag, session=None):
+        """
+        Checks if a DAG with the same ID already exists.
+        If present, returns the fileloc of the existing DAG.
+        """
+        from airflow.models.serialized_dag import SerializedDagModel  # Avoid circular import
+
+        other_dag = session.query(SerializedDagModel).filter(
+            SerializedDagModel.dag_id == dag.dag_id,
+            SerializedDagModel.fileloc_hash != DagCode.dag_fileloc_hash(dag.fileloc)
+        ).first()
+        if other_dag:
+            # If a DAG was just moved from one file to another, this condition may sometimes check out.
+            # Heuristics below try to avoid a false-positive alert
+            if not os.path.exists(other_dag.fileloc):
+                # Other file is no more, nothing to worry about
+                return None
+            # Not feasible here to parse that other file to check if it still has the DAG,
+            # so resort to just checking the task ids
+            task_ids = [task.task_id for task in dag.tasks]
+            other_dag_task_ids = [task['label'] for task in other_dag.data['dag']['tasks']]
+            if task_ids != other_dag_task_ids:
+                return other_dag.fileloc
+            else:
+                return None

Review comment:
       I'm assuming that we don't actually want to re-parse that other file to see if the same-id-DAG is still present. 
   
   If the two DAGs have the same number and IDs of tasks, the error is suppressed as it's possible it's the same DAG just moved from one file to another. This is awful - is there a better heuristic we can use?




-- 
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] siddharthvp commented on a change in pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
siddharthvp commented on a change in pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#discussion_r700561063



##########
File path: airflow/models/dagbag.py
##########
@@ -466,6 +467,34 @@ def _bag_dag(self, *, dag, root_dag, recursive):
                         del self.dags[subdag.dag_id]
             raise
 
+    @provide_session
+    def _check_if_duplicate(self, dag, session=None):
+        """
+        Checks if a DAG with the same ID already exists.
+        If present, returns the fileloc of the existing DAG.
+        """
+        from airflow.models.serialized_dag import SerializedDagModel  # Avoid circular import
+
+        other_dag = session.query(SerializedDagModel).filter(
+            SerializedDagModel.dag_id == dag.dag_id,
+            SerializedDagModel.fileloc_hash != DagCode.dag_fileloc_hash(dag.fileloc)
+        ).first()

Review comment:
       One case which I did miss was that of multiple DAGs with same ID in the same file. Have fixed that now.




-- 
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] SamWheating commented on a change in pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
SamWheating commented on a change in pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#discussion_r699564483



##########
File path: airflow/models/dagbag.py
##########
@@ -466,6 +467,34 @@ def _bag_dag(self, *, dag, root_dag, recursive):
                         del self.dags[subdag.dag_id]
             raise
 
+    @provide_session
+    def _check_if_duplicate(self, dag, session=None):
+        """
+        Checks if a DAG with the same ID already exists.
+        If present, returns the fileloc of the existing DAG.
+        """
+        from airflow.models.serialized_dag import SerializedDagModel  # Avoid circular import
+
+        other_dag = session.query(SerializedDagModel).filter(
+            SerializedDagModel.dag_id == dag.dag_id,
+            SerializedDagModel.fileloc_hash != DagCode.dag_fileloc_hash(dag.fileloc)
+        ).first()
+        if other_dag:
+            # If a DAG was just moved from one file to another, this condition may sometimes check out.
+            # Heuristics below try to avoid a false-positive alert
+            if not os.path.exists(other_dag.fileloc):
+                # Other file is no more, nothing to worry about
+                return None
+            # Not feasible here to parse that other file to check if it still has the DAG,
+            # so resort to just checking the task ids
+            task_ids = [task.task_id for task in dag.tasks]
+            other_dag_task_ids = [task['label'] for task in other_dag.data['dag']['tasks']]
+            if task_ids != other_dag_task_ids:

Review comment:
       If these two lists are built from the same DAG, will they always be in the same order? If not then the comparison isn't going to be accurate. 
   
   If they aren't in a deterministic order, maybe we could sort or cast to a set before comparison? 




-- 
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] ephraimbuddy commented on a change in pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
ephraimbuddy commented on a change in pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#discussion_r699050319



##########
File path: airflow/models/dagbag.py
##########
@@ -466,6 +467,34 @@ def _bag_dag(self, *, dag, root_dag, recursive):
                         del self.dags[subdag.dag_id]
             raise
 
+    @provide_session
+    def _check_if_dupe(self, dag, session=None):

Review comment:
       ```suggestion
       def _check_if_duplicate(self, dag, session=None):
   ```
   I would suggest it's named as above for clarity




-- 
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] siddharthvp edited a comment on pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
siddharthvp edited a comment on pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#issuecomment-908299380


   Yes the logic is indeed hacky. 
   
   @uranusjr Even with the two solutions you suggest, the problem doesn't really go away (unless I'm much mistaken) - if a DAG is moved from one file to another in the middle of a parsing run and Airflow processed the source file before the move occurred and the destination file after the move, we still have a false-positive error. 
   
   So I think the only way to implement this feature would be via hacky heuristics. Maybe we should also change the exception text to clarify that it is a _possible_ duplication. When the next DAG parsing run occurs, the false-positive alert would disappear. 


-- 
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] SamWheating commented on a change in pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
SamWheating commented on a change in pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#discussion_r699562107



##########
File path: airflow/models/dagbag.py
##########
@@ -466,6 +467,34 @@ def _bag_dag(self, *, dag, root_dag, recursive):
                         del self.dags[subdag.dag_id]
             raise
 
+    @provide_session
+    def _check_if_duplicate(self, dag, session=None):
+        """
+        Checks if a DAG with the same ID already exists.
+        If present, returns the fileloc of the existing DAG.
+        """
+        from airflow.models.serialized_dag import SerializedDagModel  # Avoid circular import
+
+        other_dag = session.query(SerializedDagModel).filter(
+            SerializedDagModel.dag_id == dag.dag_id,
+            SerializedDagModel.fileloc_hash != DagCode.dag_fileloc_hash(dag.fileloc)
+        ).first()

Review comment:
       In the unlikely event that there are more than two instances of a single DAG name, this is only going to alert on one of the collisions, correct?
   
   Do you think its worthwhile to handle this case and alert for a larger collision, Or would it be too much added complexity for a rare failure?




-- 
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] github-actions[bot] closed pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
github-actions[bot] closed pull request #17891:
URL: https://github.com/apache/airflow/pull/17891


   


-- 
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 pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
uranusjr commented on pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#issuecomment-908224414


   This logic feels hacky to me. Even if the numbers of tasks are not the same, we can’t be sure it’s a duplicated ID either; the user might have renamed the file *and* made some modification. I think this is theoratically impossible to fix in the current structure.
   
   To really resolve this, we need a place to actually aggregate all the known DAG IDs during a DAG-parsing run. One possibility is to implement something like a standalone process exposing a message queue for every DAG-parsing process to send parsed DAG IDs to, and raises an error if it seems duplication.
   
   Another possibility is, instead of storing parsed DAGs directly to `SerialiazedDagModel`, DAG-parsing processes should save things to a different tables (say `ParsingDagModel`) when they are running. This table would be empty when a DAG-parsing round starts, so any duplicated IDs are guaranteed to be real duplication (barring some filesystem race condition edge cases which we don’t currently cover anyway). After all the parsing processes successfully finish parsing this round (without reporting duplication, this `ParsingDagModel` is dumped into `SerializedDagModel` and truncated for the next parsing round.
   
   Both would require some pretty involved change though.


-- 
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] boring-cyborg[bot] commented on pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
boring-cyborg[bot] commented on pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#issuecomment-907782921


   Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst)
   Here are some useful points:
   - Pay attention to the quality of your code (flake8, mypy and type annotations). Our [pre-commits]( https://github.com/apache/airflow/blob/main/STATIC_CODE_CHECKS.rst#prerequisites-for-pre-commit-hooks) will help you with that.
   - In case of a new feature add useful documentation (in docstrings or in `docs/` directory). Adding a new operator? Check this short [guide](https://github.com/apache/airflow/blob/main/docs/apache-airflow/howto/custom-operator.rst) Consider adding an example DAG that shows how users should use it.
   - Consider using [Breeze environment](https://github.com/apache/airflow/blob/main/BREEZE.rst) for testing locally, it’s a heavy docker but it ships with a working Airflow and a lot of integrations.
   - Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
   - Please follow [ASF Code of Conduct](https://www.apache.org/foundation/policies/conduct) for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
   - Be sure to read the [Airflow Coding style]( https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#coding-style-and-best-practices).
   Apache Airflow is a community-driven project and together we are making it better 🚀.
   In case of doubts contact the developers at:
   Mailing List: dev@airflow.apache.org
   Slack: https://s.apache.org/airflow-slack
   


-- 
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] github-actions[bot] closed pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
github-actions[bot] closed pull request #17891:
URL: https://github.com/apache/airflow/pull/17891


   


-- 
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] MatrixManAtYrService commented on a change in pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
MatrixManAtYrService commented on a change in pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#discussion_r792072045



##########
File path: airflow/models/dagbag.py
##########
@@ -466,6 +467,34 @@ def _bag_dag(self, *, dag, root_dag, recursive):
                         del self.dags[subdag.dag_id]
             raise
 
+    @provide_session
+    def _check_if_dupe(self, dag, session=None):
+        """
+        Checks if a DAG with the same ID already exists.
+        If present, returns the fileloc of the existing DAG.
+        """
+        from airflow.models.serialized_dag import SerializedDagModel  # Avoid circular import
+
+        other_dag = session.query(SerializedDagModel).filter(
+            SerializedDagModel.dag_id == dag.dag_id,
+            SerializedDagModel.fileloc_hash != DagCode.dag_fileloc_hash(dag.fileloc)
+        ).first()
+        if other_dag:
+            # If a DAG was just moved from one file to another, this condition may sometimes check out.
+            # Heuristics below try to avoid a false-positive alert
+            if not os.path.exists(other_dag.fileloc):
+                # Other file is no more, nothing to worry about
+                return None
+            # Not feasible here to parse that other file to check if it still has the DAG,
+            # so resort to just checking the task ids
+            task_ids = [task.task_id for task in dag.tasks]
+            other_dag_task_ids = [task['label'] for task in other_dag.data['dag']['tasks']]
+            if task_ids != other_dag_task_ids:
+                return other_dag.fileloc
+            else:
+                return None

Review comment:
       If it was just a file rename, it would go away on the next scan, right?  Can we pend the error immediately but only show it to the user if it is still the case later on?




-- 
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] github-actions[bot] commented on pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#issuecomment-945024273


   This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.


-- 
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] siddharthvp commented on a change in pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
siddharthvp commented on a change in pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#discussion_r699443951



##########
File path: airflow/models/dagbag.py
##########
@@ -466,6 +467,34 @@ def _bag_dag(self, *, dag, root_dag, recursive):
                         del self.dags[subdag.dag_id]
             raise
 
+    @provide_session
+    def _check_if_dupe(self, dag, session=None):

Review comment:
       done




-- 
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] siddharthvp commented on a change in pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
siddharthvp commented on a change in pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#discussion_r700544206



##########
File path: airflow/models/dagbag.py
##########
@@ -466,6 +467,34 @@ def _bag_dag(self, *, dag, root_dag, recursive):
                         del self.dags[subdag.dag_id]
             raise
 
+    @provide_session
+    def _check_if_duplicate(self, dag, session=None):
+        """
+        Checks if a DAG with the same ID already exists.
+        If present, returns the fileloc of the existing DAG.
+        """
+        from airflow.models.serialized_dag import SerializedDagModel  # Avoid circular import
+
+        other_dag = session.query(SerializedDagModel).filter(
+            SerializedDagModel.dag_id == dag.dag_id,
+            SerializedDagModel.fileloc_hash != DagCode.dag_fileloc_hash(dag.fileloc)
+        ).first()

Review comment:
       From the code, I believe that an alert will be raised for each collision. (If there are 3 DAGs with same ID, one gets parsed and the other 2 raise errors.)




-- 
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] siddharthvp commented on a change in pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
siddharthvp commented on a change in pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#discussion_r699443951



##########
File path: airflow/models/dagbag.py
##########
@@ -466,6 +467,34 @@ def _bag_dag(self, *, dag, root_dag, recursive):
                         del self.dags[subdag.dag_id]
             raise
 
+    @provide_session
+    def _check_if_dupe(self, dag, session=None):

Review comment:
       done




-- 
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] siddharthvp commented on a change in pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
siddharthvp commented on a change in pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#discussion_r700560508



##########
File path: airflow/models/dagbag.py
##########
@@ -466,6 +467,34 @@ def _bag_dag(self, *, dag, root_dag, recursive):
                         del self.dags[subdag.dag_id]
             raise
 
+    @provide_session
+    def _check_if_duplicate(self, dag, session=None):
+        """
+        Checks if a DAG with the same ID already exists.
+        If present, returns the fileloc of the existing DAG.
+        """
+        from airflow.models.serialized_dag import SerializedDagModel  # Avoid circular import
+
+        other_dag = session.query(SerializedDagModel).filter(
+            SerializedDagModel.dag_id == dag.dag_id,
+            SerializedDagModel.fileloc_hash != DagCode.dag_fileloc_hash(dag.fileloc)
+        ).first()

Review comment:
       Yes, have tested and confirmed




-- 
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] github-actions[bot] commented on pull request #17891: Show error in UI when a DAG with same dag_id as another DAG is present

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #17891:
URL: https://github.com/apache/airflow/pull/17891#issuecomment-1009472838


   This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.


-- 
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