From commits-return-60713-archive-asf-public=cust-asf.ponee.io@airflow.apache.org Thu Aug 8 03:12:43 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 221AB180643 for ; Thu, 8 Aug 2019 05:12:43 +0200 (CEST) Received: (qmail 34382 invoked by uid 500); 8 Aug 2019 03:12:42 -0000 Mailing-List: contact commits-help@airflow.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@airflow.apache.org Delivered-To: mailing list commits@airflow.apache.org Received: (qmail 34355 invoked by uid 99); 8 Aug 2019 03:12:41 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 08 Aug 2019 03:12:41 +0000 From: GitBox To: commits@airflow.apache.org Subject: [GitHub] [airflow] milton0825 commented on a change in pull request #5743: [AIRFLOW-5088] Persisting serialized DAG in DB for webserver scalability Message-ID: <156523396180.22819.2239784199255083069.gitbox@gitbox.apache.org> Date: Thu, 08 Aug 2019 03:12:41 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit milton0825 commented on a change in pull request #5743: [AIRFLOW-5088] Persisting serialized DAG in DB for webserver scalability URL: https://github.com/apache/airflow/pull/5743#discussion_r311840968 ########## File path: airflow/models/serialized_dag.py ########## @@ -0,0 +1,143 @@ +# -*- 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 timezone +from airflow.utils.db import provide_session +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 database table for serialized DAGs.""" + + __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 + """ + # Truncates hash to 4 bytes. + # TODO(coufon): hashing is needed because the length of fileloc is 2000 as + # an Airflow convention, which is over the limit of indexing. If we can + return int(0xFFFF & int( + hashlib.sha1(full_filepath.encode('utf-8')).hexdigest(), 16)) + + @classmethod + @provide_session + def write_dag(cls, dag: 'DAG', min_update_interval: Optional[int] = None, session=None): + """Serializes a DAG and writes it into database. + + :param dag: a DAG to be written into database + :param min_update_interval: minimal interval in seconds to update serialized DAG + """ + if min_update_interval is not None: + result = session.query(cls.last_updated).filter( + cls.dag_id == dag.dag_id).first() + if result is not None and ( + timezone.utcnow() - result.last_updated).total_seconds() < min_update_interval: + return + session.merge(cls(dag)) + session.commit() Review comment: Should we do a `session.rollback()` in cased of encountering an exception? ---------------------------------------------------------------- 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