Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove XCom pickling #43905

Merged
merged 1 commit into from
Nov 18, 2024
Merged

Remove XCom pickling #43905

merged 1 commit into from
Nov 18, 2024

Conversation

kaxil
Copy link
Member

@kaxil kaxil commented Nov 12, 2024

XCom pickling was disabled by default in Airflow 2.0.0: https://airflow.apache.org/docs/apache-airflow/1.10.15/configurations-ref.html#enable-xcom-pickling

Discussion

To avoid a time-consuming DB migration, I have not changed the column type of value; it is still LargeBinary/LONGBLOB (MySQL).

As part of Airflow 3, we should strongly recommend users to use the airflow db clean command to prune the DBs to the minimum required. If we assume, most users would do that, we can run the following migration:

Option 1: Try to migrate pickle to JSON

def upgrade():
    bind = op.get_bind()
    session = Session(bind=bind)

    for row in session.query(XCom).all():
        try:
            unpickled_data = pickle.loads(row.value)
            row.value = json.dumps(unpickled_data).encode('utf-8')
        except (pickle.UnpicklingError, json.JSONDecodeError):
            # If unpickling fails, assume it's already JSON and skip
            continue

    op.alter_column("xcom", "value", type_=sa.Text)

Option 2: Delete XCom rows with pickle

from airflow.models.xcom import BaseXCom

def upgrade():
    bind = op.get_bind()
    session = Session(bind=bind)

    pickled_xcoms = session.query(BaseXCom).filter(BaseXCom.value.isnot(None))
    
    deleted_count = 0
    for xcom in pickled_xcoms:
        if xcom.value.startswith(b'\x80'):  # Identify as pickled by protocol marker: https://github.com/python/cpython/blob/494360afd00dc8f6b549f160525c3e86ec14905d/Lib/pickletools.py#L2122-L2133
            session.delete(xcom)

Option 3: Keep the current column type

Not optimal, but we can keep the current column type to binary/long-blob

Any thoughts?


^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in newsfragments.

@kaxil kaxil added the airflow3.0:breaking Candidates for Airflow 3.0 that contain breaking changes label Nov 12, 2024
@boring-cyborg boring-cyborg bot added the area:API Airflow's REST/HTTP API label Nov 12, 2024
@kaxil kaxil requested review from potiuk and eladkal November 12, 2024 01:09
@kaxil kaxil force-pushed the remove-xcom-pickling branch 2 times, most recently from 39d2306 to fd62961 Compare November 12, 2024 01:16
@kaxil kaxil added the legacy api Whether legacy API changes should be allowed in PR label Nov 12, 2024
@pgagnon
Copy link
Contributor

pgagnon commented Nov 12, 2024

@kaxil

Option 1: Try to migrate pickle to JSON

# If unpickling fails, assume it's already JSON and skip

If JSON decoding fails, maybe we could save the value in a backup blob field (to be deprecated and removed in a specified future version).

While XComs are generally not that important I feel it might be better to soft-delete wherever there's a risk of data loss as a general practice.

@jscheffl
Copy link
Contributor

jscheffl commented Nov 12, 2024

I like the drop of the pickle type - but I'd favor not keeping this as blob/binary in the DB. Then we can not use any DB feature to efficiently "use" the data other than a blob.

If not in this PR, can we have a follow-up that converts the data into json data type as described in https://www.postgresql.org/docs/17/datatype-json.html ?

(MySQL is: https://dev.mysql.com/doc/refman/8.4/en/json.html)

And totally forgot about the vote: I think we can also consider to provide an offline migration tool which we request to be executed by everybody manually prior upgrade such that we don't need to carry complex inline migration. Everybody who uses XCom pickling should know this from the config. So Option 1 or Option 1a (a=offline tool)

Copy link
Member

@hussein-awala hussein-awala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@kaxil kaxil added this to the Airflow 3.0.0 milestone Nov 13, 2024
@kaxil
Copy link
Member Author

kaxil commented Nov 18, 2024

I will create a separate PR to handle the migration so that can be reviewed independently -- will have a PR by EOD today

@kaxil kaxil merged commit 6faa720 into apache:main Nov 18, 2024
63 checks passed
@kaxil kaxil deleted the remove-xcom-pickling branch November 18, 2024 17:44
@kaxil
Copy link
Member Author

kaxil commented Nov 18, 2024

PR created: #44166

kandharvishnu pushed a commit to kandharvishnu/airflow that referenced this pull request Nov 19, 2024
kaxil added a commit that referenced this pull request Nov 19, 2024
follow-up of #43905

Changes:
- Changed `XCom.value` column to JSON for all dbs.
- Archived pickled XCom data to `_xcom_archive` and removed it from the `xcom` table.
- Removed encoded string in XCom serialization and deserialization logic.
- Updated logic for `XComObjectStorageBackend` to make it compatible for AF 2 & 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
airflow3.0:breaking Candidates for Airflow 3.0 that contain breaking changes area:API Airflow's REST/HTTP API legacy api Whether legacy API changes should be allowed in PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants