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 2022/11/22 15:49:26 UTC

[GitHub] [airflow] jedcunningham commented on a diff in pull request #27828: Soft delete datasets that are no longer referenced in DAG schedules or task outlets

jedcunningham commented on code in PR #27828:
URL: https://github.com/apache/airflow/pull/27828#discussion_r1029509222


##########
airflow/models/dag.py:
##########
@@ -2850,8 +2850,18 @@ def bulk_write_to_db(
             )
             dag_refs_to_add = {x for x in dag_refs_needed if x not in dag_refs_stored}
             session.bulk_save_objects(dag_refs_to_add)
-            for obj in dag_refs_stored - dag_refs_needed:
-                session.delete(obj)
+
+            dag_refs_to_remove = {x for x in dag_refs_stored if x not in dag_refs_needed}

Review Comment:
   Why the refactor here? I don't exactly follow how it's related.



##########
airflow/models/dag.py:
##########
@@ -2867,8 +2877,45 @@ def bulk_write_to_db(
             task_refs_stored = existing_task_outlet_refs_dict[(dag_id, task_id)]
             task_refs_to_add = {x for x in task_refs_needed if x not in task_refs_stored}
             session.bulk_save_objects(task_refs_to_add)
-            for obj in task_refs_stored - task_refs_needed:
-                session.delete(obj)
+
+            task_refs_to_remove = {x for x in task_refs_stored if x not in task_refs_needed}
+            for todr in task_refs_to_remove:
+                log.debug(
+                    "Removing TaskOutletDatasetReference (dataset_id=%s, dag_id=%s, task_id=%s)",
+                    todr.dataset_id,
+                    todr.dag_id,
+                    todr.task_id,
+                )
+                session.query(TaskOutletDatasetReference).filter(
+                    TaskOutletDatasetReference.dataset_id == todr.dataset_id,
+                    TaskOutletDatasetReference.dag_id == todr.dag_id,
+                    TaskOutletDatasetReference.task_id == todr.task_id,
+                ).delete()
+
+        # Remove any orphaned datasets
+        orphaned_dataset_query = (

Review Comment:
   This looks pretty good, but we might want it do it like we do for stale DAGs: https://github.com/apache/airflow/blob/093345cb61cea623bffe6af5a41d6ba417dfcf6f/airflow/dag_processing/manager.py#L492



##########
airflow/www/views.py:
##########
@@ -3574,6 +3580,54 @@ def dataset_dependencies(self):
             {"Content-Type": "application/json; charset=utf-8"},
         )
 
+    @expose("/object/dataset/delete", methods=["POST"])
+    @action_logging
+    def delete_dataset(self):

Review Comment:
   This should be an actual rest api endpoint.



##########
airflow/migrations/versions/0122_2_5_0_add_is_orphaned_to_datasetmodel.py:
##########
@@ -0,0 +1,49 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""Add is_orphaned to DatasetModel
+
+Revision ID: 290244fb8b83
+Revises: 65a852f26899
+Create Date: 2022-11-22 00:12:53.432961
+
+"""
+
+from __future__ import annotations
+
+import sqlalchemy as sa
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision = "290244fb8b83"
+down_revision = "65a852f26899"
+branch_labels = None
+depends_on = None
+airflow_version = "2.5.0"
+
+
+def upgrade():
+    """Add is_orphaned to DatasetModel"""
+    with op.batch_alter_table("dataset") as batch_op:
+        batch_op.add_column(sa.Column("is_orphaned", sa.Boolean, nullable=False))

Review Comment:
   Does this actually work when you have rows? Isn't it missing setting false for the existing rows?



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