You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "kaxil (via GitHub)" <gi...@apache.org> on 2024/12/02 19:36:45 UTC

[PR] AIP-72: Improve handling of `deferred` state in Supervisor [airflow]

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

   - Added `_terminal_state` in `WatchedSubprocess` to manage task final states like `deferred` directly.
   - Updated `wait` method in `WatchedSubprocess` to finalize task states and call the API with the terminal states (success, failure, etc)
   - Simplified the `final_state` property by removing unnecessary setter logic.
   - Fixed a bug where `make_buffered_socket_reader` was using `memoryview` and `msg = decoder.validate_json(line)` expected bytes.
   
   <!--
    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).
   


-- 
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] AIP-72: Improve handling of `deferred` state in Supervisor [airflow]

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


##########
task_sdk/src/airflow/sdk/execution_time/supervisor.py:
##########
@@ -694,11 +684,7 @@ def cb(sock: socket):
 
         # We could have read multiple lines in one go, yield them all
         while (newline_pos := buffer.find(b"\n")) != -1:
-            if TYPE_CHECKING:
-                # We send in a memoryvuew, but pretend it's a bytes, as Buffer is only in 3.12+
-                line = buffer[: newline_pos + 1]
-            else:
-                line = memoryview(buffer)[: newline_pos + 1]  # Include the newline character

Review Comment:
   ```py
   (Pdbr) line
   <memory at 0xffff88873100>
   
   (Pdbr) bytes(line)
   b'{"state":"deferred","classpath":"airflow.providers.standard.triggers.temporal.DateTimeTrigger","trigger_kwargs":{"moment":"2024-11-07T12:34:59Z","end_from_trigger":false},"next_method":"execute_complete","trigger
   _timeout":null,"type":"DeferTask"}\n'
   
   (Pdbr) decoder.validate_json(line)
   *** pydantic_core._pydantic_core.ValidationError: 1 validation error for tagged-union[TaskState,GetXCom,GetConnection,GetVariable,DeferTask]
     JSON input should be string, bytes or bytearray
       For further information visit https://errors.pydantic.dev/2.10/v/json_type
   
   (Pdbr) decoder.validate_json(bytes(line))
   DeferTask(state='deferred', classpath='airflow.providers.standard.triggers.temporal.DateTimeTrigger', trigger_kwargs={'moment': '2024-11-07T12:34:59Z', 'end_from_trigger': False}, next_method='execute_complete',
   trigger_timeout=None, type='DeferTask')
   
   
   ```



-- 
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] AIP-72: Improve handling of `deferred` state in Supervisor [airflow]

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


##########
task_sdk/src/airflow/sdk/execution_time/supervisor.py:
##########
@@ -694,11 +684,7 @@ def cb(sock: socket):
 
         # We could have read multiple lines in one go, yield them all
         while (newline_pos := buffer.find(b"\n")) != -1:
-            if TYPE_CHECKING:
-                # We send in a memoryvuew, but pretend it's a bytes, as Buffer is only in 3.12+
-                line = buffer[: newline_pos + 1]
-            else:
-                line = memoryview(buffer)[: newline_pos + 1]  # Include the newline character

Review Comment:
   There was a bug where `msg = decoder.validate_json(line)` expected bytes but this sent `memoryview`.



-- 
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] AIP-72: Improve handling of `deferred` state in Supervisor [airflow]

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


##########
task_sdk/src/airflow/sdk/execution_time/supervisor.py:
##########
@@ -653,7 +643,7 @@ def handle_requests(self, log: FilteringBoundLogger) -> Generator[None, bytes, N
                 xcom = self.client.xcoms.get(msg.dag_id, msg.run_id, msg.task_id, msg.key, msg.map_index)
                 resp = xcom.model_dump_json(exclude_unset=True).encode()
             elif isinstance(msg, DeferTask):
-                self.final_state = IntermediateTIState.DEFERRED
+                self._terminal_state = IntermediateTIState.DEFERRED

Review Comment:
   Nice, this works too



-- 
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] AIP-72: Improve handling of `deferred` state in Supervisor [airflow]

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


##########
task_sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -168,13 +168,11 @@ def run(ti: RuntimeTaskInstance, log: Logger):
         next_method = defer.method_name
         timeout = defer.timeout
         msg = DeferTask(
-            state="deferred",
             classpath=classpath,
             trigger_kwargs=trigger_kwargs,
             next_method=next_method,
             trigger_timeout=timeout,
         )
-        global SUPERVISOR_COMMS

Review Comment:
   Yeah this was vestigial code from the poc 



-- 
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] AIP-72: Improve handling of `deferred` state in Supervisor [airflow]

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


##########
task_sdk/src/airflow/sdk/execution_time/supervisor.py:
##########
@@ -694,11 +684,7 @@ def cb(sock: socket):
 
         # We could have read multiple lines in one go, yield them all
         while (newline_pos := buffer.find(b"\n")) != -1:
-            if TYPE_CHECKING:
-                # We send in a memoryvuew, but pretend it's a bytes, as Buffer is only in 3.12+
-                line = buffer[: newline_pos + 1]
-            else:
-                line = memoryview(buffer)[: newline_pos + 1]  # Include the newline character

Review Comment:
   ```py
   
   (Pdbr) bytes(line)
   b'{"state":"deferred","classpath":"airflow.providers.standard.triggers.temporal.DateTimeTrigger","trigger_kwargs":{"moment":"2024-11-07T12:34:59Z","end_from_trigger":false},"next_method":"execute_complete","trigger
   _timeout":null,"type":"DeferTask"}\n'
   
   (Pdbr) decoder.validate_json(line)
   *** pydantic_core._pydantic_core.ValidationError: 1 validation error for tagged-union[TaskState,GetXCom,GetConnection,GetVariable,DeferTask]
     JSON input should be string, bytes or bytearray
       For further information visit https://errors.pydantic.dev/2.10/v/json_type
   
   (Pdbr) decoder.validate_json(bytes(line))
   DeferTask(state='deferred', classpath='airflow.providers.standard.triggers.temporal.DateTimeTrigger', trigger_kwargs={'moment': '2024-11-07T12:34:59Z', 'end_from_trigger': False}, next_method='execute_complete',
   trigger_timeout=None, type='DeferTask')
   
   (Pdbr) line
   <memory at 0xffff88873100>
   ```



-- 
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] AIP-72: Improve handling of `deferred` state in Supervisor [airflow]

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


##########
task_sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -168,13 +168,11 @@ def run(ti: RuntimeTaskInstance, log: Logger):
         next_method = defer.method_name
         timeout = defer.timeout
         msg = DeferTask(
-            state="deferred",

Review Comment:
   Already the default



-- 
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] AIP-72: Improve handling of `deferred` state in Supervisor [airflow]

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


##########
task_sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -168,13 +168,11 @@ def run(ti: RuntimeTaskInstance, log: Logger):
         next_method = defer.method_name
         timeout = defer.timeout
         msg = DeferTask(
-            state="deferred",
             classpath=classpath,
             trigger_kwargs=trigger_kwargs,
             next_method=next_method,
             trigger_timeout=timeout,
         )
-        global SUPERVISOR_COMMS

Review Comment:
   Was redundant as we are only using the method on it



-- 
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] AIP-72: Improve handling of `deferred` state in Supervisor [airflow]

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


##########
task_sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -168,13 +168,11 @@ def run(ti: RuntimeTaskInstance, log: Logger):
         next_method = defer.method_name
         timeout = defer.timeout
         msg = DeferTask(
-            state="deferred",
             classpath=classpath,
             trigger_kwargs=trigger_kwargs,
             next_method=next_method,
             trigger_timeout=timeout,
         )
-        global SUPERVISOR_COMMS

Review Comment:
   Was redundant



-- 
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] AIP-72: Improve handling of `deferred` state in Supervisor [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil merged PR #44579:
URL: https://github.com/apache/airflow/pull/44579


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