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 2019/09/02 19:46:29 UTC

[GitHub] [airflow] kaxil commented on a change in pull request #5743: [AIRFLOW-5088][AIP-24] Persisting serialized DAG in DB for webserver scalability

kaxil commented on a change in pull request #5743: [AIRFLOW-5088][AIP-24] Persisting serialized DAG in DB for webserver scalability
URL: https://github.com/apache/airflow/pull/5743#discussion_r320038635
 
 

 ##########
 File path: airflow/models/serialized_dag.py
 ##########
 @@ -0,0 +1,155 @@
+# -*- coding: utf-8 -*-
+#
+# 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.
+
+"""Serialzed DAG table in database."""
+
+import hashlib
+from typing import Any, Dict, List, Optional, TYPE_CHECKING
+from sqlalchemy import Column, Index, Integer, String, Text, and_
+from sqlalchemy.sql import exists
+
+from airflow.models.base import Base, ID_LEN
+from airflow.utils import db, timezone
+from airflow.utils.sqlalchemy import UtcDateTime
+
+
+if TYPE_CHECKING:
+    from airflow.dag.serialization.serialized_dag import SerializedDAG  # noqa: F401, E501; # pylint: disable=cyclic-import
+    from airflow.models import DAG  # noqa: F401; # pylint: disable=cyclic-import
+
+
+class SerializedDagModel(Base):
+    """A table for serialized DAGs.
+
+    serialized_dag table is a snapshot of DAG files synchronized by scheduler.
+    This feature is controlled by:
+        [core] dagcached = False: enable this feature
+        [core] dagcached_min_update_interval = 30 (s):
+            serialized DAGs are updated in DB when a file gets processed by scheduler,
+            to reduce DB write rate, there is a minimal interval of updating serialized DAGs.
+        [scheduler] dag_dir_list_interval = 300 (s):
+            interval of deleting serialized DAGs in DB when the files are deleted, suggest
+            to use a smaller interval such as 60
+
+    It is used by webserver to load dagbags when dagcached=True. Because reading from
+    database is lightweight compared to importing from files, it solves the webserver
+    scalability issue.
+    """
+    __tablename__ = 'serialized_dag'
+
+    dag_id = Column(String(ID_LEN), primary_key=True)
+    fileloc = Column(String(2000))
+    # The max length of fileloc exceeds the limit of indexing.
+    fileloc_hash = Column(Integer)
+    data = Column(Text)
+    last_updated = Column(UtcDateTime)
+
+    __table_args__ = (
+        Index('idx_fileloc_hash', fileloc_hash, unique=False),
+    )
+
+    def __init__(self, dag):
+        from airflow.dag.serialization import Serialization
+
+        self.dag_id = dag.dag_id
+        self.fileloc = dag.full_filepath
+        self.fileloc_hash = SerializedDagModel.dag_fileloc_hash(self.fileloc)
+        self.data = Serialization.to_json(dag)
+        self.last_updated = timezone.utcnow()
+
+    @staticmethod
+    def dag_fileloc_hash(full_filepath: str) -> int:
+        """"Hashing file location for indexing.
+
+        :param full_filepath: full filepath of DAG file
+        :return: hashed full_filepath
+        """
+        # hashing is needed because the length of fileloc is 2000 as an Airflow convention,
+        # which is over the limit of indexing. If we can reduce the length of fileloc, then
+        # hashing is not needed.
+        return int(0xFFFF & int(
 
 Review comment:
   Are we happy to use the previous version for Py2 ? @ashb @coufon ?

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