-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AIP-58: Add object storage backend for xcom (#37058)
* Add object storage backend for xcom This adds the possibility to store xcoms on a configurable object storage supported backend. --------- Co-authored-by: Ephraim Anierobi <splendidzigy24@gmail.com>
- Loading branch information
1 parent
7324400
commit 573d650
Showing
13 changed files
with
631 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# 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 __future__ import annotations | ||
|
||
import packaging.version | ||
|
||
try: | ||
from airflow import __version__ as airflow_version | ||
except ImportError: | ||
from airflow.version import version as airflow_version | ||
|
||
if packaging.version.parse(packaging.version.parse(airflow_version).base_version) < packaging.version.parse( | ||
"2.9.0" | ||
): | ||
raise RuntimeError( | ||
"The package xcom backend feature of `apache-airflow-providers-common-io` needs " | ||
"Apache Airflow 2.9.0+" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
# 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 __future__ import annotations | ||
|
||
import json | ||
import uuid | ||
from typing import TYPE_CHECKING, Any, TypeVar | ||
from urllib.parse import urlsplit | ||
|
||
import fsspec.utils | ||
|
||
from airflow.configuration import conf | ||
from airflow.io.path import ObjectStoragePath | ||
from airflow.models.xcom import BaseXCom | ||
from airflow.utils.json import XComDecoder, XComEncoder | ||
|
||
if TYPE_CHECKING: | ||
from sqlalchemy.orm import Session | ||
|
||
from airflow.models import XCom | ||
|
||
T = TypeVar("T") | ||
|
||
SECTION = "common.io" | ||
|
||
|
||
def _is_relative_to(o: ObjectStoragePath, other: ObjectStoragePath) -> bool: | ||
"""This is a port of the pathlib.Path.is_relative_to method. It is not available in python 3.8.""" | ||
if hasattr(o, "is_relative_to"): | ||
return o.is_relative_to(other) | ||
|
||
try: | ||
o.relative_to(other) | ||
return True | ||
except ValueError: | ||
return False | ||
|
||
|
||
def _get_compression_suffix(compression: str) -> str: | ||
"""This returns the compression suffix for the given compression. | ||
:raises ValueError: if the compression is not supported | ||
""" | ||
for suffix, c in fsspec.utils.compressions.items(): | ||
if c == compression: | ||
return suffix | ||
|
||
raise ValueError(f"Compression {compression} is not supported. Make sure it is installed.") | ||
|
||
|
||
class XComObjectStoreBackend(BaseXCom): | ||
"""XCom backend that stores data in an object store or database depending on the size of the data. | ||
If the value is larger than the configured threshold, it will be stored in an object store. | ||
Otherwise, it will be stored in the database. If it is stored in an object store, the path | ||
to the object in the store will be returned and saved in the database (by BaseXCom). Otherwise, the value | ||
itself will be returned and thus saved in the database. | ||
""" | ||
|
||
@staticmethod | ||
def _get_key(data: str) -> str: | ||
"""This gets the key from the url and normalizes it to be relative to the configured path. | ||
:raises ValueError: if the key is not relative to the configured path | ||
:raises TypeError: if the url is not a valid url or cannot be split | ||
""" | ||
path = conf.get(SECTION, "xcom_objectstore_path", fallback="") | ||
p = ObjectStoragePath(path) | ||
|
||
try: | ||
url = urlsplit(data) | ||
except AttributeError: | ||
raise TypeError(f"Not a valid url: {data}") | ||
|
||
if url.scheme: | ||
k = ObjectStoragePath(data) | ||
|
||
if _is_relative_to(k, p) is False: | ||
raise ValueError(f"Invalid key: {data}") | ||
else: | ||
return data.replace(path, "", 1).lstrip("/") | ||
|
||
raise ValueError(f"Not a valid url: {data}") | ||
|
||
@staticmethod | ||
def serialize_value( | ||
value: T, | ||
*, | ||
key: str | None = None, | ||
task_id: str | None = None, | ||
dag_id: str | None = None, | ||
run_id: str | None = None, | ||
map_index: int | None = None, | ||
) -> bytes | str: | ||
# we will always serialize ourselves and not by BaseXCom as the deserialize method | ||
# from BaseXCom accepts only XCom objects and not the value directly | ||
s_val = json.dumps(value, cls=XComEncoder).encode("utf-8") | ||
path = conf.get(SECTION, "xcom_objectstore_path", fallback="") | ||
compression = conf.get(SECTION, "xcom_objectstore_compression", fallback=None) | ||
|
||
if compression: | ||
suffix = "." + _get_compression_suffix(compression) | ||
else: | ||
suffix = "" | ||
|
||
threshold = conf.getint(SECTION, "xcom_objectstore_threshold", fallback=-1) | ||
|
||
if path and -1 < threshold < len(s_val): | ||
# safeguard against collisions | ||
while True: | ||
p = ObjectStoragePath(path) / f"{dag_id}/{run_id}/{task_id}/{str(uuid.uuid4())}{suffix}" | ||
if not p.exists(): | ||
break | ||
|
||
if not p.parent.exists(): | ||
p.parent.mkdir(parents=True, exist_ok=True) | ||
|
||
with p.open("wb", compression=compression) as f: | ||
f.write(s_val) | ||
|
||
return BaseXCom.serialize_value(str(p)) | ||
else: | ||
return s_val | ||
|
||
@staticmethod | ||
def deserialize_value( | ||
result: XCom, | ||
) -> Any: | ||
"""Deserializes the value from the database or object storage. | ||
Compression is inferred from the file extension. | ||
""" | ||
data = BaseXCom.deserialize_value(result) | ||
path = conf.get(SECTION, "xcom_objectstore_path", fallback="") | ||
|
||
try: | ||
p = ObjectStoragePath(path) / XComObjectStoreBackend._get_key(data) | ||
return json.load(p.open("rb", compression="infer"), cls=XComDecoder) | ||
except TypeError: | ||
return data | ||
except ValueError: | ||
return data | ||
|
||
@staticmethod | ||
def purge(xcom: XCom, session: Session) -> None: | ||
path = conf.get(SECTION, "xcom_objectstore_path", fallback="") | ||
if isinstance(xcom.value, str): | ||
try: | ||
p = ObjectStoragePath(path) / XComObjectStoreBackend._get_key(xcom.value) | ||
p.unlink(missing_ok=True) | ||
except TypeError: | ||
pass | ||
except ValueError: | ||
pass |
18 changes: 18 additions & 0 deletions
18
docs/apache-airflow-providers-common-io/configurations-ref.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.. 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. | ||
.. include:: ../exts/includes/providers-configurations-ref.rst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
.. 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. | ||
Object Storage XCom Backend | ||
=========================== | ||
|
||
The default XCom backend is the :class:`~airflow.models.xcom.BaseXCom` class, which stores XComs in the Airflow database. This is fine for small values, but can be problematic for large values, or for large numbers of XComs. | ||
|
||
To enable storing XComs in an object store, you can set the ``xcom_backend`` configuration option to ``airflow.providers.common.io.xcom.backend.XComObjectStoreBackend``. You will also need to set ``xcom_objectstorage_path`` to the desired location. The connection | ||
id is obtained from the user part of the url the you will provide, e.g. ``xcom_objectstorage_path = s3://conn_id@mybucket/key``. Furthermore, ``xcom_objectstorage_threshold`` is required | ||
to be something larger than -1. Any object smaller than the threshold in bytes will be stored in the database and anything larger will be be | ||
put in object storage. This will allow a hybrid setup. If an xcom is stored on object storage a reference will be | ||
saved in the database. Finally, you can set ``xcom_objectstorage_compression`` to fsspec supported compression methods like ``zip`` or ``snappy`` to | ||
compress the data before storing it in object storage. | ||
|
||
So for example the following configuration will store anything above 1MB in S3 and will compress it using gzip:: | ||
|
||
[core] | ||
xcom_backend = airflow.providers.common.io.xcom.backend.XComObjectStoreBackend | ||
|
||
[common.io] | ||
xcom_objectstorage_path = s3://conn_id@mybucket/key | ||
xcom_objectstorage_threshold = 1048576 | ||
xcom_objectstorage_compression = gzip | ||
|
||
.. note:: | ||
|
||
Compression requires the support for it is installed in your python environment. For example, to use ``snappy`` compression, you need to install ``python-snappy``. Zip, gzip and bz2 work out of the box. |
Oops, something went wrong.