amoghrajesh opened a new pull request, #46719:
URL: https://github.com/apache/airflow/pull/46719

   <!--
    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.
    -->
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   ## Why?
   - While setting xcoms, there were multiple layers of serialisation happening 
/ deserialisation not happening at the right place leading to this issue.
   - We want to keep the interface to the api server simple -> interact only in 
json strings. Native language ser / deser will be taken care by respective sdks.
   
   
   ### Flow for setting an xcom:
   1. Task runner serialises and sends the value for xcom: 
https://github.com/apache/airflow/blob/main/task_sdk/src/airflow/sdk/execution_time/task_runner.py#L353
 (Looking like: `'"Hello, XCom!"'`)
   2. In the API server at the moment, we directly write to the DB (BaseXcom). 
Due to it being a JSON field, it serialises it again which was leading to 
double serialisation. Now that has been eliminated (by assigning the `value` 
variable to `json.loads`)
   3. So, we end up storing a single serialised value for xcom. Also in sync 
with Airflow 2.
   
   ### Flow for getting an xcom:
   1. The task runner requests for an xcom
   2. The client calls the API server, now the api server doesn't deserialise 
and send. Instead, sends raw value from the DB, which is a singly serialised 
value.
   3. The task runner gets the XCOM from the client and performs a deserialise 
on it: 
https://github.com/apache/airflow/blob/main/task_sdk/src/airflow/sdk/execution_time/task_runner.py#L321
   
   ### What impact will this have for the custom xcom backends?
   Flow:
   
   1. When performing a `set`, the custom xcom backend stores the data to 
object store and stores the path in the database, after serialising it: 
https://github.com/apache/airflow/blob/main/airflow/models/xcom.py#L185-L192
   2. When performing a `get_value`, the custom xcom backend does 
this:https://github.com/apache/airflow/blob/main/providers/common/io/src/airflow/providers/common/io/xcom/backend.py#L165
 which means it returns raw data and not serialised data. We should be careful 
to handle this one case `xcom_pull` in task runner when we port custom xcom 
backend.
   
   
   ## Testing:
   DAG used:
   ```
   from airflow import DAG
   from airflow.providers.standard.operators.python import PythonOperator
   
   def push_to_xcom(**kwargs):
       value = "Hello, XCom!"
       return value
   
   def push_to_xcom2(**kwargs):
       value = "Hello, XCom2!"
       return value
   
   def pull_from_xcom(**kwargs):
       ti = kwargs['ti']
       xcom_value1 = ti.xcom_pull(task_ids=["push_xcom_task"])
       xcom_value2 = ti.xcom_pull(task_ids=["push_xcom_task2"])
       return xcom_value1 + xcom_value2
   
   with DAG(
       'xcom_example',
       schedule=None,
       catchup=False,
   ) as dag:
   
       push_xcom_task = PythonOperator(
           task_id='push_xcom_task',
           python_callable=push_to_xcom,
       )
   
       push_xcom_task2 = PythonOperator(
           task_id='push_xcom_task2',
           python_callable=push_to_xcom2,
       )
   
       pull_xcom_task = PythonOperator(
           task_id='pull_xcom_task',
           python_callable=pull_from_xcom,
       )
   
       push_xcom_task >> push_xcom_task2 >> pull_xcom_task
   
   ```
   
   This tests both push and pull behaviour.
   
   State of the DB:
   
![image](https://github.com/user-attachments/assets/0fbcf4f4-b9bf-4eb4-ac79-a5725a234865)
   
   
   
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request 
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
 for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal 
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
 is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party 
License Policy](https://www.apache.org/legal/resolved.html#category-x).
   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](https://github.com/apache/airflow/tree/main/newsfragments).
   


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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to