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 2020/03/09 20:59:36 UTC

[GitHub] [airflow] kaxil commented on a change in pull request #7633: [AIRFLOW-6989][depends on AIRFLOW-5944] Store UnRenderedTemplateFields in SerializedDag table

kaxil commented on a change in pull request #7633: [AIRFLOW-6989][depends on AIRFLOW-5944] Store UnRenderedTemplateFields in SerializedDag table
URL: https://github.com/apache/airflow/pull/7633#discussion_r389958399
 
 

 ##########
 File path: airflow/models/renderedtifields.py
 ##########
 @@ -0,0 +1,153 @@
+#
+# 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.
+"""Save Rendered Template Fields """
+import json
+from typing import Optional
+
+from sqlalchemy import JSON, Column, String, and_, exists, tuple_
+from sqlalchemy.orm import Session
+
+from airflow.configuration import conf
+from airflow.models.base import ID_LEN, Base
+from airflow.models.taskinstance import TaskInstance
+from airflow.serialization.serialization_helpers import serialize_template_field
+from airflow.utils.session import provide_session
+from airflow.utils.sqlalchemy import UtcDateTime
+
+
+class RenderedTaskInstanceFields(Base):
+    """
+    Save Rendered Template Fields
+    """
+
+    __tablename__ = "rendered_task_instance_fields"
+
+    dag_id = Column(String(ID_LEN), primary_key=True)
+    task_id = Column(String(ID_LEN), primary_key=True)
+    execution_date = Column(UtcDateTime, primary_key=True)
+    rendered_fields = Column(JSON, nullable=False)
+
+    def __init__(self, ti: TaskInstance, render_templates=True):
+        self.dag_id = ti.dag_id
+        self.task_id = ti.task_id
+        self.task = ti.task
+        self.execution_date = ti.execution_date
+        self.ti = ti
+        if render_templates:
+            ti.render_templates()
+        self.rendered_fields = {
+            field: serialize_template_field(
+                getattr(self.task, field)
+            ) for field in self.task.template_fields
+        }
+
+    def __repr__(self):
+        return f"<{self.__class__.__name__}: {self.dag_id}.{self.task_id} {self.execution_date}"
+
+    @classmethod
+    @provide_session
+    def get_templated_fields(cls, ti: TaskInstance, session: Session = None) -> Optional[dict]:
+        """
+        Get templated field for a TaskInstance from the RenderedTaskInstanceFields
+        table.
+
+        :param ti: Task Instance
+        :param session: SqlAlchemy Session
+        :return: Rendered Templated TI field
+        """
+        result = session.query(cls.rendered_fields).filter(
+            cls.dag_id == ti.dag_id,
+            cls.task_id == ti.task_id,
+            cls.execution_date == ti.execution_date
+        ).first()
+
+        if result:
+            rendered_fields = result.rendered_fields
+            if isinstance(rendered_fields, str):
+                rendered_fields = json.loads(rendered_fields)
+            return rendered_fields
+        else:
+            return None
+
+    @classmethod
+    @provide_session
+    def has_templated_fields(cls, ti: TaskInstance, session: Session = None) -> bool:
+        """Checks templated field exist for this ti.
+
+        :param ti: Task Instance
+        :param session: SqlAlchemy Session
+        """
+        # We cant use q.exists() because some databases such as SQL Server don’t allow
+        # an EXISTS expression to be present in the columns clause of a SELECT.
+        # Details: https://docs.sqlalchemy.org/en/13/orm/query.html#sqlalchemy.orm.query.Query.exists
+        return session.query(exists().where(
+            and_(cls.dag_id == ti.dag_id,
+                 cls.task_id == ti.task_id,
+                 cls.execution_date == ti.execution_date)
+        )).scalar()
+
+    @provide_session
+    def write(self, session: Session = None):
+        """Write instance to database
+
+        :param session: SqlAlchemy Session
+        """
+        session.merge(self)
+
+    @classmethod
+    @provide_session
+    def delete_old_records(
+        cls, task_id: str, dag_id: str,
+        num_to_keep=conf.getint("core", "max_num_rendered_ti_fields_per_task", fallback=0),
+        session: Session = None
+    ):
+        """
+        Keep only Last X (num_to_keep) number of records for a task by deleting others
+
+        :param task_id: Task ID
+        :param dag_id: Dag ID
+        :param num_to_keep: Number of Records to keep
+        :param session: SqlAlchemy Session
+        """
+        if num_to_keep > 0:
+            # Fetch Top X records given dag_id & task_id ordered by Execution Date
+            subq1 = (
+                session
+                .query(cls.dag_id, cls.task_id, cls.execution_date)
+                .filter(cls.dag_id == dag_id, cls.task_id == task_id)
+                .order_by(cls.execution_date.desc())
+                .limit(num_to_keep)
+                .subquery('subq1')
+            )
+
+            # Second Subquery
+            # Workaround for MySQL Limitation (https://stackoverflow.com/a/19344141/5691525)
+            # Limitation: This version of MySQL does not yet support
+            # LIMIT & IN/ALL/ANY/SOME subquery
+            subq2 = (
+                session
+                .query(subq1.c.dag_id, subq1.c.task_id, subq1.c.execution_date)
+                .subquery('subq2')
+            )
+
+        session.query(cls) \
+            .filter(and_(
+                cls.dag_id == dag_id,
+                cls.task_id == task_id,
+                tuple_(cls.dag_id, cls.task_id, cls.execution_date).notin_(subq2))) \
 
 Review comment:
   For `TaskInstance.filter_for_tis` we already knew the TIs we wanted to delete. 
   
   In this case, we don't fetch the TIs first and then delete them. We do entire operation on the 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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services