uranusjr commented on code in PR #73783:
URL: https://github.com/apache/airflow/pull/73783#discussion_r4509383261


##########
task-sdk/src/airflow/sdk/execution_time/comms.py:
##########
@@ -177,14 +181,42 @@ def _new_encoder() -> msgspec.msgpack.Encoder:
     return msgspec.msgpack.Encoder(enc_hook=_msgpack_enc_hook)
 
 
+def _encode_large_ints(value: Any) -> Any:
+    if isinstance(value, int) and not -(2**63) <= value < 2**64:
+        return msgspec.msgpack.Ext(
+            MSGPACK_HUGEINT, value.to_bytes((value.bit_length() + 8) // 8, 
"big", signed=True)
+        )
+    if isinstance(value, dict):
+        return {key: _encode_large_ints(item) for key, item in value.items()}
+    if isinstance(value, (list, tuple)):
+        return [_encode_large_ints(item) for item in value]
+    return value
+
+
+def _decode_msgpack_ext(code: int, data: memoryview) -> Any:
+    if code == MSGPACK_HUGEINT:
+        return int.from_bytes(data, "big", signed=True)
+    return msgspec.msgpack.Ext(code, data)

Review Comment:
   `msgspec.msgpack.Ext(code, data)` keeps the `memoryview` that msgspec passes 
to `ext_hook`, and that view points into the caller's receive buffer. The data 
can be overwritten when the buffer is reused. Do `bytes(data)`.



##########
task-sdk/src/airflow/sdk/execution_time/comms.py:
##########
@@ -177,14 +181,42 @@ def _new_encoder() -> msgspec.msgpack.Encoder:
     return msgspec.msgpack.Encoder(enc_hook=_msgpack_enc_hook)
 
 
+def _encode_large_ints(value: Any) -> Any:
+    if isinstance(value, int) and not -(2**63) <= value < 2**64:
+        return msgspec.msgpack.Ext(
+            MSGPACK_HUGEINT, value.to_bytes((value.bit_length() + 8) // 8, 
"big", signed=True)
+        )
+    if isinstance(value, dict):
+        return {key: _encode_large_ints(item) for key, item in value.items()}
+    if isinstance(value, (list, tuple)):
+        return [_encode_large_ints(item) for item in value]
+    return value
+
+
+def _decode_msgpack_ext(code: int, data: memoryview) -> Any:
+    if code == MSGPACK_HUGEINT:
+        return int.from_bytes(data, "big", signed=True)
+    return msgspec.msgpack.Ext(code, data)
+
+
 class _FrameMixin:
     _encoder: ClassVar[msgspec.msgpack.Encoder] = _new_encoder()
 
-    def as_bytes(self) -> bytearray:
+    def as_bytes(self, *, allow_large_ints: bool = True) -> bytearray:
         # https://jcristharif.com/msgspec/perf-tips.html#length-prefix-framing 
for inspiration
         buffer = bytearray(256)
 
-        self._encoder.encode_into(self, buffer, 4)  # type: ignore[arg-type]
+        try:
+            self._encoder.encode_into(self, buffer, 4)  # type: 
ignore[arg-type]
+        except OverflowError:
+            if not allow_large_ints:
+                raise
+            # Keep the usual path free of a Python traversal of every message.
+            frame = cast("_RequestFrame | _ResponseFrame", self)
+            fields = {"body": _encode_large_ints(frame.body)}
+            if isinstance(frame, _ResponseFrame):
+                fields["error"] = _encode_large_ints(frame.error)

Review Comment:
   Let each frame declare which of its fields carry payloads, for example 
`_payload_fields: ClassVar[tuple[str, ...]] = ("body",)` on `_RequestFrame` and 
`("body", "error")` on `_ResponseFrame`, and replace those fields generically.



##########
task-sdk/tests/task_sdk/execution_time/test_supervisor.py:
##########
@@ -1026,13 +1026,27 @@ def _on_child_started(self, *args, **kwargs):
         spy_agency.assert_spy_not_called(heartbeat_spy)
 
     @pytest.mark.parametrize("log_to_file", [False, True])
+    @pytest.mark.parametrize(
+        "conf_value",
+        [-(2**63), 2**64 - 1, -(2**63) - 1, 2**64, str(2**64)],
+        ids=["min-int64", "max-uint64", "below-int64", "above-uint64", 
"large-number-string"],
+    )

Review Comment:
   This test is on the heavy side to be run 10 times. Maybe convert into a test 
on smaller chunks, and do the subprocess only once?



##########
airflow-core/tests/unit/api_fastapi/execution_api/versions/head/test_task_instances.py:
##########
@@ -742,6 +744,46 @@ def 
test_startup_xcom_cleanup_keys_belong_to_the_starting_try(
         assert response.status_code == 200
         assert response.json()["xcom_keys_to_clear"] == ["own"]
 
+    @pytest.mark.parametrize(
+        "conf_value",
+        [
+            pytest.param(-(2**63), id="min-int64"),
+            pytest.param(2**64 - 1, id="max-uint64"),
+            pytest.param(
+                -(2**63) - 1,
+                id="below-int64",
+                marks=pytest.mark.backend("postgres", "sqlite"),
+            ),
+            pytest.param(
+                2**64,
+                id="above-uint64",
+                marks=pytest.mark.backend("postgres", "sqlite"),
+            ),
+            pytest.param(str(2**64), id="large-number-string"),
+        ],
+    )
+    def test_run_conf_can_be_sent_to_task_runner(self, client, session, 
create_task_instance, conf_value):
+        ti = create_task_instance(task_id="large_integer_conf", 
state=State.QUEUED)
+        ti.dag_run.conf = {"large_integer": conf_value}
+        session.commit()
+        session.expire_all()
+        assert ti.dag_run.conf == {"large_integer": conf_value}
+
+        response = client.patch(
+            f"/execution/task-instances/{ti.id}/run",
+            json={
+                "state": "running",
+                "hostname": "test-worker",
+                "unixname": "airflow",
+                "pid": 100,
+                "start_date": "2024-10-31T12:00:00Z",
+            },
+        )
+        assert response.status_code == 200, response.text
+        context = TIRunContext.model_validate_json(response.content)
+        assert context.dag_run.conf == {"large_integer": conf_value}
+        _ResponseFrame(id=0, body={"ti_context": 
context.model_dump()}).as_bytes()

Review Comment:
   This last line is leftover from somewhere else?



##########
airflow-core/tests/unit/api_fastapi/execution_api/versions/head/test_task_instances.py:
##########
@@ -742,6 +744,46 @@ def 
test_startup_xcom_cleanup_keys_belong_to_the_starting_try(
         assert response.status_code == 200
         assert response.json()["xcom_keys_to_clear"] == ["own"]
 
+    @pytest.mark.parametrize(
+        "conf_value",
+        [
+            pytest.param(-(2**63), id="min-int64"),
+            pytest.param(2**64 - 1, id="max-uint64"),
+            pytest.param(
+                -(2**63) - 1,
+                id="below-int64",
+                marks=pytest.mark.backend("postgres", "sqlite"),

Review Comment:
   Why not on MySQL?



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