You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "shubham-pyc (via GitHub)" <gi...@apache.org> on 2025/03/19 06:14:40 UTC

[PR] Added Fastapi endpoint for reserialize_dags [airflow]

shubham-pyc opened a new pull request, #47943:
URL: https://github.com/apache/airflow/pull/47943

   <!--
    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/
   -->
   
   
   
   <!-- 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).
   
   
   --
   Closes: #47844
   Added Rest port to replicate CLI functionality of  airflow dags reserialize
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Added Fastapi endpoint for reserialize_dags [airflow]

Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #47943:
URL: https://github.com/apache/airflow/pull/47943#issuecomment-2735443930

   Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
   Here are some useful points:
   - Pay attention to the quality of your code (ruff, mypy and type annotations). Our [pre-commits]( https://github.com/apache/airflow/blob/main/contributing-docs/08_static_code_checks.rst#prerequisites-for-pre-commit-hooks) will help you with that.
   - In case of a new feature add useful documentation (in docstrings or in `docs/` directory). Adding a new operator? Check this short [guide](https://github.com/apache/airflow/blob/main/docs/apache-airflow/howto/custom-operator.rst) Consider adding an example DAG that shows how users should use it.
   - Consider using [Breeze environment](https://github.com/apache/airflow/blob/main/dev/breeze/doc/README.rst) for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
   - Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
   - Please follow [ASF Code of Conduct](https://www.apache.org/foundation/policies/conduct) for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
   - Be sure to read the [Airflow Coding style]( https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#coding-style-and-best-practices).
   - Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
   Apache Airflow is a community-driven project and together we are making it better 🚀.
   In case of doubts contact the developers at:
   Mailing List: dev@airflow.apache.org
   Slack: https://s.apache.org/airflow-slack
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Added Fastapi endpoint for reserialize_dags [airflow]

Posted by "shubham-pyc (via GitHub)" <gi...@apache.org>.
shubham-pyc commented on PR #47943:
URL: https://github.com/apache/airflow/pull/47943#issuecomment-2738577032

   @jedcunningham thanks for the clarification. Is it okay if I keep working on this feature and it just gets merged next month? 
    I am open to addressing other open issues but any feature would only be able to get merged next month.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Added Fastapi endpoint for reserialize_dags [airflow]

Posted by "shubham-pyc (via GitHub)" <gi...@apache.org>.
shubham-pyc commented on code in PR #47943:
URL: https://github.com/apache/airflow/pull/47943#discussion_r2002750203


##########
airflow/api_fastapi/core_api/routes/public/dags.py:
##########
@@ -339,3 +342,68 @@ def delete_dag(
             status.HTTP_409_CONFLICT, f"Task instances of dag with id: '{dag_id}' are still running"
         )
     return Response(status_code=status.HTTP_204_NO_CONTENT)
+
+
+@dags_router.post(
+    "/manage/reserialize",

Review Comment:
   In that case you won't be able to have a DAG with ID: reserialize
   
   because we have an API router with /public/dags/{dag_id}



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Added Fastapi endpoint for reserialize_dags [airflow]

Posted by "shubham-pyc (via GitHub)" <gi...@apache.org>.
shubham-pyc commented on PR #47943:
URL: https://github.com/apache/airflow/pull/47943#issuecomment-2742893726

   @jedcunningham i looked into the code for[reparse a file feature](https://github.com/apache/airflow/blob/2500dcf20d2782d16da53ee857c0aab21bfdfbf2/airflow/api_fastapi/core_api/routes/public/dag_parsing.py#L47)
   
   For this feature if we fetch all the file locations of bundles and create a batch insert DagPriorityParsingRequest. Will it work? Since this would create a parsing results for all of our bundles.
   
   `file_locations = session.scalars(select(DagModel.fileloc).where(DagModel.name.in_(list(bundles_to_process))))
           # Process each bundle
           parsing_requests = [DagPriorityParsingRequest(fileloc=fileloc) for fileloc in file_locations]
           
           session.add_all(parsing_requests)`
           
     lemme know your thoughts


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Added Fastapi endpoint for reserialize_dags [airflow]

Posted by "shubham-pyc (via GitHub)" <gi...@apache.org>.
shubham-pyc commented on PR #47943:
URL: https://github.com/apache/airflow/pull/47943#issuecomment-2738426029

   @jedcunningham, thank you for your clarifications. I will review the security feature and its functionality. It makes sense that user-defined code is not present with the webserver and scheduler, as they can be deployed on separate machines.
   
   I will go through the code to understand how the file is re-parsed, and we can discuss your ideas on improving its implementation.
   
   On a side note, do you have any updates on the 3.1 release timeline?


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Added Fastapi endpoint for reserialize_dags [airflow]

Posted by "jedcunningham (via GitHub)" <gi...@apache.org>.
jedcunningham commented on PR #47943:
URL: https://github.com/apache/airflow/pull/47943#issuecomment-2738432977

   > On a side note, do you have any updates on the 3.1 release timeline?
   
   Unfortunately, not at this time. We can merge new features once 3.0 is out though, so the period of time where we cannot get new things like this merged will be approximately 1 month long.


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Added Fastapi endpoint for reserialize_dags [airflow]

Posted by "bugraoz93 (via GitHub)" <gi...@apache.org>.
bugraoz93 commented on code in PR #47943:
URL: https://github.com/apache/airflow/pull/47943#discussion_r2002655654


##########
airflow/api_fastapi/core_api/datamodels/dags.py:
##########
@@ -113,6 +114,32 @@ class DAGPatchBody(StrictBaseModel):
     is_paused: bool
 
 
+class DagReserializePostBody(BaseModel):
+    """Dag Serializer for reserialzed bodies."""
+
+    bundle_names: list[str] | None
+
+    @field_validator("bundle_names", mode="before")
+    def validate_bundle_names(cls, value):
+        """Validate bundle names format and check for duplicates."""
+        manager = DagBundlesManager()
+        all_bundles = list(manager.get_all_dag_bundles())
+        all_bundle_names = {b.name for b in all_bundles}
+
+        if value is not None:

Review Comment:
   ```suggestion
           if value:
   ```
   



##########
airflow/api_fastapi/core_api/routes/public/dags.py:
##########
@@ -339,3 +342,68 @@ def delete_dag(
             status.HTTP_409_CONFLICT, f"Task instances of dag with id: '{dag_id}' are still running"
         )
     return Response(status_code=status.HTTP_204_NO_CONTENT)
+
+
+@dags_router.post(
+    "/manage/reserialize",

Review Comment:
   ```suggestion
       "/reserialize",
   ```
   I think we don't need additional `manage`. It would be simpler 



##########
airflow/api_fastapi/core_api/datamodels/dags.py:
##########
@@ -113,6 +114,32 @@ class DAGPatchBody(StrictBaseModel):
     is_paused: bool
 
 
+class DagReserializePostBody(BaseModel):
+    """Dag Serializer for reserialzed bodies."""
+
+    bundle_names: list[str] | None
+
+    @field_validator("bundle_names", mode="before")
+    def validate_bundle_names(cls, value):
+        """Validate bundle names format and check for duplicates."""
+        manager = DagBundlesManager()
+        all_bundles = list(manager.get_all_dag_bundles())

Review Comment:
   I am not sure if we need to init and load all the DAG bundles for each request. Maybe we can have a method in it to get_all_dag_bundles_by_name()



##########
airflow/api_fastapi/core_api/routes/public/dags.py:
##########
@@ -339,3 +342,68 @@ def delete_dag(
             status.HTTP_409_CONFLICT, f"Task instances of dag with id: '{dag_id}' are still running"
         )
     return Response(status_code=status.HTTP_204_NO_CONTENT)
+
+
+@dags_router.post(
+    "/manage/reserialize",
+    responses=create_openapi_http_exception_doc(
+        [
+            status.HTTP_400_BAD_REQUEST,
+            status.HTTP_404_NOT_FOUND,
+            status.HTTP_422_UNPROCESSABLE_ENTITY,
+        ]
+    ),
+    dependencies=[Depends(action_logging())],
+)
+async def reserialize_dags(
+    request: DagReserializePostBody,
+    session: SessionDep,  # Add your session dependency
+):
+    """
+    Reserialize DAG bundles in Airflow.
+
+    - **bundle_names**: List of specific bundles to reserialize (all if empty)
+    """
+    try:
+        manager = DagBundlesManager()
+        manager.sync_bundles_to_db(session=session)
+        session.commit()
+
+        # Get all available bundles
+        all_bundles = list(manager.get_all_dag_bundles())
+        all_bundle_names = {b.name for b in all_bundles}
+
+        # Validate bundle names if specified
+        if request.bundle_names:
+            bundles_to_process = set(request.bundle_names)
+        else:
+            bundles_to_process = all_bundle_names
+
+        # Process each bundle
+        processed = []
+        for bundle in all_bundles:
+            if bundle.name not in bundles_to_process:
+                continue
+
+            try:
+                bundle.initialize()
+                dag_bag = DagBag(bundle.path, bundle_path=bundle.path, include_examples=False)
+                dag_bag.sync_to_db(bundle.name, bundle_version=bundle.get_current_version(), session=session)
+                processed.append(bundle.name)
+            except Exception as e:
+                raise HTTPException(
+                    status_code=status.HTTP_400_BAD_REQUEST,
+                    detail=f"Error processing bundle {bundle.name}: {str(e)}",
+                )
+
+        session.commit()
+        return ReserializeResponse(
+            message="DAG bundles reserialized successfully", processed_bundles=processed
+        )
+
+    except Exception as e:
+        session.rollback()

Review Comment:
   This could maybe go to finally?



##########
airflow/api_fastapi/core_api/routes/public/dags.py:
##########
@@ -339,3 +342,68 @@ def delete_dag(
             status.HTTP_409_CONFLICT, f"Task instances of dag with id: '{dag_id}' are still running"
         )
     return Response(status_code=status.HTTP_204_NO_CONTENT)
+
+
+@dags_router.post(
+    "/manage/reserialize",
+    responses=create_openapi_http_exception_doc(
+        [
+            status.HTTP_400_BAD_REQUEST,
+            status.HTTP_404_NOT_FOUND,
+            status.HTTP_422_UNPROCESSABLE_ENTITY,
+        ]
+    ),
+    dependencies=[Depends(action_logging())],
+)
+async def reserialize_dags(
+    request: DagReserializePostBody,
+    session: SessionDep,  # Add your session dependency
+):
+    """
+    Reserialize DAG bundles in Airflow.
+
+    - **bundle_names**: List of specific bundles to reserialize (all if empty)
+    """
+    try:
+        manager = DagBundlesManager()
+        manager.sync_bundles_to_db(session=session)
+        session.commit()
+
+        # Get all available bundles
+        all_bundles = list(manager.get_all_dag_bundles())
+        all_bundle_names = {b.name for b in all_bundles}
+
+        # Validate bundle names if specified
+        if request.bundle_names:
+            bundles_to_process = set(request.bundle_names)
+        else:
+            bundles_to_process = all_bundle_names
+
+        # Process each bundle
+        processed = []
+        for bundle in all_bundles:
+            if bundle.name not in bundles_to_process:
+                continue
+
+            try:
+                bundle.initialize()
+                dag_bag = DagBag(bundle.path, bundle_path=bundle.path, include_examples=False)
+                dag_bag.sync_to_db(bundle.name, bundle_version=bundle.get_current_version(), session=session)
+                processed.append(bundle.name)
+            except Exception as e:
+                raise HTTPException(
+                    status_code=status.HTTP_400_BAD_REQUEST,
+                    detail=f"Error processing bundle {bundle.name}: {str(e)}",
+                )
+
+        session.commit()
+        return ReserializeResponse(
+            message="DAG bundles reserialized successfully", processed_bundles=processed
+        )
+
+    except Exception as e:
+        session.rollback()
+        raise HTTPException(
+            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,

Review Comment:
   I'm not sure about this broad status code. It should also be documented in openapi spec. 



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Added Fastapi endpoint for reserialize_dags [airflow]

Posted by "shubham-pyc (via GitHub)" <gi...@apache.org>.
shubham-pyc commented on PR #47943:
URL: https://github.com/apache/airflow/pull/47943#issuecomment-2746627472

   Because the rebase messed up codefile I have created a new pull request with latest changes and per suggestions.
   
   https://github.com/apache/airflow/pull/48174
   
   I am closing this pull request


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Added Fastapi endpoint for reserialize_dags [airflow]

Posted by "shubham-pyc (via GitHub)" <gi...@apache.org>.
shubham-pyc closed pull request #47943: Added Fastapi endpoint for reserialize_dags
URL: https://github.com/apache/airflow/pull/47943


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org