aglinxinyuan opened a new issue, #9365:
URL: https://github.com/apache/texera/issues/9365

   ### Feature Summary
   
   Inside a control block (`Loop Start ... Loop End`), the loop variables 
travel as state messages ahead of the data. When an operator's output feeds 
several operators, the engine sends a copy of each state message on every 
outgoing edge, so an operator where two branches of the loop body meet receives 
one copy per branch. Loop Start marks the state it sends with its own operator 
id, its stamp, which tells the matching Loop End that the state belongs to its 
loop. A state that a body operator sends when its input ends, from 
`produce_state_on_finish`, carries no stamp. Three things a loop body needs do 
not work on main.
   
   A body operator cannot steer the loop with a value it computes from its 
data. It sends such a value when its input ends, so the value has no stamp. 
Loop End keeps the first stamped state, drops later stamped ones, and sends 
unstamped ones on, out of the block, so the value never reaches Loop End's 
update or condition:
   
   ```
   Text Input (3, 5, 4, 6) -> Loop Start -> Python UDF -> Loop End
     Loop Start:  i = 0; prev = None; score = 0                              
output: table.iloc[i]
     Python UDF:  sends score = the row it saw, from produce_state_on_finish
     Loop End:    again = prev is None or score > prev; prev = score; i += 1   
condition: again
   
   main:    Loop End never sees the UDF's score, compares the initial 0 with 
itself, stops after 2 iterations
   wanted:  scores 3, 5, 4; the loop stops after 3 iterations, at the first 
score that does not improve
   ```
   
   A JVM operator in the loop body drops a variable set to `None` when it sends 
the state on. With a Limit between Loop Start and the UDF, the same loop fails 
in Loop End's update with `name 'prev' is not defined`.
   
   An operator where two body branches meet processes a message once per 
branch. Nothing tells a copy of a message from a new one, so every copy is 
registered, handed to `process_state` and sent on again, and the copies 
multiply through each further split and merge:
   
   ```
   Loop Start -> Limit -> Python UDF <- Sleep <- Loop Start
   main:    the UDF's process_state runs twice per iteration, once per branch, 
and sends both copies on
   wanted:  once per message
   ```
   
   ### Proposed Solution or Design
   
   - A state message gets a unique id when an operator issues it, before the 
copies are made, on both engines. Every copy keeps it: across downstream 
channels, the Arrow hop between JVM and Python workers, and the materialized 
state row, which gains a `message_id` column next to `loop_counter` and 
`loop_start_id`. It is not a loop variable.
   - Each worker processes the first copy of an id and drops every later copy 
before registering it, calling `process_state` or sending it on.
   - An operator that sends a state on unchanged keeps its id; a changed state, 
or one sent when the operator's input starts or ends, is a new message. A 
message's depth, the number of inner loops it has entered, changes as it enters 
or leaves an inner loop; its id does not.
   - Loop End collects every distinct message of its own loop, stamped or not, 
until its input ends, a later value replacing an earlier one, and its update 
and condition read the collected values. These messages stay inside the block; 
Loop End still sends on the messages of enclosing loops.
   - For the next iteration, Loop End saves only the loop's own variables: the 
names its stamped state carried and the names its update assigns. A name 
carried only by a state that a body operator sends when its input starts or 
ends lasts for one iteration, unless the update assigns it; the update can also 
copy its value into a loop variable.
   - A JVM operator keeps a variable set to `None` when it sends a state on, as 
Python does. The `None` then counts as a value in the check below, as it 
already does without a JVM operator in between, so a JVM operator that refers 
to a loop variable set to `None` and then sent by a body operator gets two 
different values.
   
   A JVM operator whose setting refers to a loop variable keeps rejecting two 
different values of it from the same loop. Once copies count once, that rule 
can become "the later value wins" in a follow-up.
   
   ### Affected Area
   
   Workflow Engine (Amber)
   


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