|
Currently Airflow requires DAG files to be present on a file system that is accessible to the scheduler, webserver, and workers. Given that more and more people are running airflow in a distributed setup to achieve higher scalability, it becomes more and more difficult to guarantee a file system is accessible and synchronized amongst services. By allowing Airflow to fetch DAG files from a remote source outside the file system local to the service, this grant a much greater flexibility, eases implementation, and standardizes ways to sync remote sources of DAGs with Airflow.
DAGs are persisted in remote filesystem-like storage and Airflow need to know where to find them. DAG repository is introduced to record the remote root directory of DAG files. Prior to DAG loading, Airflow would download files from the remote DAG repository and cache it under the local filesystem directory under $AIRFLOW_HOME/dags.
It records where the remote filesystem are, it can be s3, git, etc.
dag_repositories: [
"repo1": {
"url": "s3://my-bucket/dags",
"conn_id": "blahblah"
},
"repo2": {
"url": "git://repo_name/dags",
"conn_id": "blahblah2"
}
] |
The following is the DagFetcher interface, we will implement different fetchers for different storage system, GitDagFetcher and S3DagFetcher.
class BaseDagFetcher(): def fetch(repo_id, url, conn_id): """ Download files from remote storage to local directory under $AIRFLOW_HOME/dags/repo_id """ |
Proposed changes
ddd
Since DAG files are stored remotely and Airflow needs to know where to find them, DAG manifest is introduced to record the location of the DAG files. Airflow looks at the DAG manifest and fetches DAG files from remote storage using DagFetcher to local file system. Airflow caches the DAGs on the local filesystem and will only fetch from remote if the local copy is stale.
Currently Airflow assumes valid DAGs are any Dag object that it finds in the python files in the $AIRFLOW_HOME/dags directory. However, to support Airflow fetching DAG files from remote sources, we need a figure out a way to record the remote location of each DAG, which is the DAG manifest. The implementation of DAG manifest can simply be a manifest.yml on the filesystem or we can store the DAG manifest on a database.
The Dag Manifest would be composed of manifest entries, an entry would be defined as follows:
dag_manifest_entry:
dag_id:
uri: where dag can be found, DAG locations will be given via URI, i.e. s3://my-bucket/dag1.zip, local:////dags/day1.zip
conn_id: connection id to use to interact with remote location |
Airflow services will look at $AIRFLOW_HOME/manifest.yml for the DAG manifest. The manifest.yml contains all the DAG entries. We should expect a manifest.yml like:
dag_id_1 - uri: s3://my-bucket/dag_id_1_1.zip - conn_id: some conn id dag_id_2 - uri: s3://my-bucket/dag_id_2_3.zip - conn_id: some conn id |
The manifest can also be generated by a callable supplied in the airflow.cfg that when called would generate a list of entries i.e
[core] # callable to fetch dag manifest list dag_manifest_entries = my_config.get_dag_manifest_entries |
The DAG manifest can be stored on S3 and my_config.get_dag_manifest_entries will read the manifest from S3.
To maintain backward compatibility, we can support a migration script that crawl the DAGs in $AIRFLOE_HOME and populates a manifest.yml file.
DAG URI is also DAG version.
Moving DAGs to a remote location will introduce network overhead so we should cache DAGs and avoid unnecessary fetch. We should only re-fetch a DAG when the URI in the DAG manifest is changed (or we find that the last_update_time of the DAG file is changed).
In order to avoid making a remote fetch every time the dag needs to be run it will be best to keep a local cache of dag files for individual Airflow services to use
The main part of the code base that we will need to change is in DagBag. In collect_dags, we will go through each entries in defined in the DAG manifest and download the DAG files if cache is invalid. In download_dag_file_and_add_to_cache, we will use different fetching implementation for different kind of uri, e.g. s3 or git.
class DagBag():
def collect_dags():
for entry in get_dag_manifest_entries():
if entry.uri is stored locally:
self.process_file(entry.uri, only_if_updated=True)
continue
# the DAG is stored remotely
dag_cache_path = self.get_cache_path(entry.dag_id, entry.uri)
if self.cached_dag_file_is_latest(dag_id, uri, dag_cache_path):
# we have the latest cache of DAG
continue
else:
download_dag_file_and_add_to_cache(dag_id, entry.uri)
self.process_file(cache_path)
def cached_dag_file_is_latest(dag_id, uri, dag_cache_path):
"""
Check if the DAG file on remote storage is changed since the last download.
"""
def download_dag_file_and_add_to_cache(dag_id, uri, dag_cache_path):
"""
Download DAG files from remote location to the local dag_cache_path.
"""
self.dag_fetcher.fetch_and_cache_dag(dag_id, uri, dag_cache_path)
def get_cache_path(dag_id, uri):
return cached_dags_path + "/" + dag_id + "/" + uri
class DagFetcher():
def fetch_and_cache_dag(dag_id, uri, conn_id, dag_cache_path):
"""
Download the DAG file from remote to local file system under dag_cache_path
and record the last_modified_date in a file on local filesystem.
"""
raise NotImplementedError()
def get_last_modified_date(dag_id, uri, conn_id)
"""
When the DAG is last modified on remote system.
"""
raise NotImplementedError() |
Currently the scheduler checks what dags are on disk by calling list_py_file_paths this will need to be changed to instead look at the manifest and load the files on the local filesystem cache. Airflow scheduler will need to persist the URI into the DagRun table when it creates a new DagRun.
If we implement versioning of dags it will require a number of changes to the current scheduler. The biggest issue comes from how the scheduler currently propagates the dag object to it's various function calls for task scheduling. As is the scheduler loads in the dag objects that are found in the filesystem, and these passed along to the resulting functions. In order to implement versions we would need to associate a certain dag version/uri to a DagRun, when previous DagRuns are fetched we'll need to check if they were for an earlier dag version/uri and fetch that version/uri if necessary.