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/08/22 19:11:55 UTC

[GitHub] [airflow] dstandish commented on a diff in pull request #25419: Create a pluggable DatasetEventManager

dstandish commented on code in PR #25419:
URL: https://github.com/apache/airflow/pull/25419#discussion_r951808900


##########
airflow/models/taskinstance.py:
##########
@@ -1525,33 +1528,17 @@ def _run_raw_task(
             session.add(Log(self.state, self))
             session.merge(self)
             if self.state == TaskInstanceState.SUCCESS:
-                self._create_dataset_dag_run_queue_records(session=session)
+                self._register_dataset_changes(session=session)
             session.commit()
 
-    def _create_dataset_dag_run_queue_records(self, *, session: Session) -> None:
-        from airflow.datasets import Dataset
-        from airflow.models.dataset import DatasetModel
-
+    def _register_dataset_changes(self, *, session: Session) -> None:
         for obj in self.task.outlets or []:
             self.log.debug("outlet obj %s", obj)
-            if isinstance(obj, Dataset):
-                dataset = session.query(DatasetModel).filter(DatasetModel.uri == obj.uri).one_or_none()
-                if not dataset:
-                    self.log.warning("Dataset %s not found", obj)
-                    continue
-                consuming_dag_ids = [x.dag_id for x in dataset.consuming_dags]
-                self.log.debug("consuming dag ids %s", consuming_dag_ids)
-                session.add(
-                    DatasetEvent(
-                        dataset_id=dataset.id,
-                        source_task_id=self.task_id,
-                        source_dag_id=self.dag_id,
-                        source_run_id=self.run_id,
-                        source_map_index=self.map_index,
-                    )
-                )
-                for dag_id in consuming_dag_ids:
-                    session.merge(DatasetDagRunQueue(dataset_id=dataset.id, target_dag_id=dag_id))
+            self.dataset_event_manager.register_dataset_change(

Review Comment:
   probably makes sense to make sure you are dealing with a dataset _here_ no?
   
   i.e. to make sure `obj` is a Dataset and not something else?



##########
airflow/datasets/manager.py:
##########
@@ -0,0 +1,62 @@
+#
+# 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.
+from sqlalchemy.orm.session import Session
+
+from airflow.utils.log.logging_mixin import LoggingMixin
+
+
+class DatasetEventManager(LoggingMixin):
+    """
+    A pluggable class that manages operations for dataset events.
+
+    The intent is to have one place to handle all DatasetEvent-related operations, so different
+    Airflow deployments can use plugins that broadcast dataset events to each other.
+    """
+
+    def register_dataset_change(self, *, task_instance, dataset, extra=None, session: Session) -> None:
+        """
+        For local datasets, look them up, record the dataset event, queue dagruns, and broadcast
+        the dataset event
+        """
+        from airflow.datasets import Dataset
+        from airflow.models.dataset import DatasetEvent, DatasetModel
+
+        if isinstance(dataset, Dataset):

Review Comment:
   not sure we need to check the type here given that the method is `register_dataset_change`



##########
airflow/datasets/manager.py:
##########
@@ -0,0 +1,62 @@
+#
+# 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.
+from sqlalchemy.orm.session import Session
+
+from airflow.utils.log.logging_mixin import LoggingMixin
+
+
+class DatasetEventManager(LoggingMixin):
+    """
+    A pluggable class that manages operations for dataset events.
+
+    The intent is to have one place to handle all DatasetEvent-related operations, so different
+    Airflow deployments can use plugins that broadcast dataset events to each other.
+    """
+
+    def register_dataset_change(self, *, task_instance, dataset, extra=None, session: Session) -> None:
+        """
+        For local datasets, look them up, record the dataset event, queue dagruns, and broadcast
+        the dataset event
+        """
+        from airflow.datasets import Dataset
+        from airflow.models.dataset import DatasetEvent, DatasetModel
+
+        if isinstance(dataset, Dataset):

Review Comment:
   what else would it be?  do we want pluggability to extend to "anything you can put in outlets"?



##########
airflow/datasets/manager.py:
##########
@@ -0,0 +1,62 @@
+#
+# 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.
+from sqlalchemy.orm.session import Session
+
+from airflow.utils.log.logging_mixin import LoggingMixin
+
+
+class DatasetEventManager(LoggingMixin):
+    """
+    A pluggable class that manages operations for dataset events.
+
+    The intent is to have one place to handle all DatasetEvent-related operations, so different
+    Airflow deployments can use plugins that broadcast dataset events to each other.
+    """
+
+    def register_dataset_change(self, *, task_instance, dataset, extra=None, session: Session) -> None:

Review Comment:
   what do people think about accepting kwargs here?  perhaps that would help with future compat if we add new features / interaction capability? because if we make changes to the interface, and change the way we call the methods, maybe we'll have to inspect signature or look at interface version or something.....  @uranusjr thoughts?
   
   on one hand adding kwargs feels like it would allow a maybe a little more freedom... at the same time... depending on the changes that we make, it might behoove us to check signature (or version) anyway to know how it will operate / what it expects 🤷 



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