jason810496 commented on PR #70570:
URL: https://github.com/apache/airflow/pull/70570#issuecomment-7155451740

   Hi @uranusjr, following up on your #70570 review. I built both directions 
end to end so we can pick one before I go further.
   
   The only real difference is **where the mapping arithmetic runs**: in the 
Execution API, or in a Python sidecar on the worker. In both, the runtime gets 
only literals, plus a lazy sequence for a reduce.
   
   > The sidecar is the XCom backend process from #75184, which we'd turn into 
a generic Python sidecar.
   
   **1. Server resolution (what ADR-0016 describes today)**
   
   PoC: 
https://github.com/jason810496/airflow/commits/feature/lang-sdk/mapped-stub-server-resolution
 (one commit per future PR, 48/48 TS e2e passing)
   
   - At `ti_run`, the Execution API works out, for each map index, which 
upstream XCom row and element each argument comes from. The supervisor reads 
those through the XCom backend and sends literals to the runtime.
   - `ti_run` already loads the serialized Dag for every stub task, so mapped 
stubs don't add that load. They do add the upstream length and row lookups per 
mapped task instance. I haven't benchmarked this yet.
   - On duplication: the index arithmetic would move into a `shared/` module 
used by core and task-sdk. That module would serve the scheduler, the server 
and Python's own `_expand_mapped_field`, and the branching would move onto the 
ExpandInput/XComArg classes.
   - Key order still needs a fix in storage, for example keeping order in the 
serialized expand input. Otherwise we keep rejecting a literal dict as an 
expand value.
   
   ```mermaid
   sequenceDiagram
       participant S as Scheduler
       participant API as Execution API
       participant Sup as Supervisor
       participant Side as Python sidecar
       participant RT as Lang SDK runtime
   
       Note over S,RT: Upstream task
       Sup->>API: ti_run
       API-->>Sup: TIRunContext with needs_mapped_length
       Sup->>RT: start task
       RT->>Sup: return value
       Sup->>Sup: measure map length
       Sup->>API: push XCom and map length
       S->>S: expand the mapped task into N task instances
   
       Note over S,RT: Mapped task instance i
       Sup->>API: ti_run
       API->>API: load serialized Dag, compute coordinates for index 
i<br/>(upstream task, XCom row, element index)
       API-->>Sup: arg_bindings with coordinates
       Sup->>API: get upstream XCom row
       API-->>Sup: stored value
       Sup->>Side: load through the XCom backend
       Side-->>Sup: value
       Sup->>Sup: pick element i, narrow to a literal
       Sup->>RT: start task with literal bindings
       opt .map() callable
           RT->>RT: apply its own callable
       end
       RT->>Sup: return value
   ```
   
   **2. Sidecar resolution**
   
   PoC: 
https://github.com/jason810496/airflow/commits/feature/lang-sdk/mapped-stub-sidecar-resolution
   
   - `ti_run` only forwards the stored binding entries and does no arithmetic.
   - The sidecar builds Task SDK objects and resolves them with the real 
`expand()`/`expand_kwargs()` code (`ExpandInput.resolve`). For a Python Dag it 
imports the Dag file. For a TS Dag it rebuilds the task from the serialized Dag 
the runtime sends, with placeholder upstream operators.
   - It reuses one Python implementation with exact Python semantics, and key 
order survives because nothing goes through the DB.
   - The cost is the rebuild adapter for Dag definitions, and importing the Dag 
file in the supervisor for stub TaskHandlers.
   
   ```mermaid
   sequenceDiagram
       participant S as Scheduler
       participant API as Execution API
       participant Sup as Supervisor
       participant Side as Python sidecar
       participant RT as Lang SDK runtime
   
       Note over S,RT: Upstream task (same as server resolution)
       Sup->>API: ti_run
       API-->>Sup: TIRunContext with needs_mapped_length
       Sup->>RT: start task
       RT->>Sup: return value
       Sup->>Sup: measure map length
       Sup->>API: push XCom and map length
       S->>S: expand the mapped task into N task instances
   
       Note over S,RT: Mapped task instance i
       Sup->>API: ti_run
       API-->>Sup: arg_bindings as stored, no arithmetic
       Sup->>RT: start task
       RT->>Sup: ResolveArgBindings with its serialized Dag
       Sup->>Side: resolve_bindings
       Side->>Side: rebuild the task with placeholder upstreams
       Side->>API: get upstream XComs (through Supervisor)
       API-->>Side: values
       Side->>Side: ExpandInput.resolve at index i, leave the outermost .map() 
off
       Side-->>Sup: literal bindings
       Sup-->>RT: literal bindings
       opt .map() callable
           RT->>RT: apply its own callable
       end
       RT->>Sup: return value
   ```
   
   **`.map()` on stub tasks**
   
   This adds the most complexity in both approaches. With a stub TaskHandler, 
the `.map()` callable is Python, so only the Python side can run it. In the 
server approach that means an `ApplyMap` round trip and loading the Dag file in 
the supervisor. In the sidecar approach, TS Dags need a step that takes the 
outermost `.map()` off and leaves the callables to the runtime.
   
   It's a niche case, so we could list it as a known limitation for now, with 
users doing the transform in an upstream task instead, or implement it now. 
Both PoCs implement it today.
   
   Which direction would you prefer? And should `.map()` on stub tasks be a 
known limitation for now, or should we implement it now?
   Thanks.


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