aglinxinyuan opened a new pull request, #9366:
URL: https://github.com/apache/texera/pull/9366
### What changes were proposed in this PR?
Inside a control block (`Loop Start ... Loop End`), the loop variables
travel as state messages ahead of the data. The engine sends a copy of each
state message on every outgoing edge and to every downstream worker, so an
operator where two body branches meet receives one copy per branch. Loop Start
marks the state it sends with its own operator id, its stamp. A state that a
body operator sends when its input starts or ends (`produce_state_on_start` or
`produce_state_on_finish`), called a publication here, has no stamp.
With this PR every issued state message carries an id that all its copies
keep, so a worker processes a message once however many paths deliver it. Loop
End collects every distinct message of its own loop, so in the first example
below the score that a body operator publishes reaches Loop End's update and
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: publishes score = the row it saw, when its input ends
Loop End: again = prev is None or score > prev; prev = score; i += 1
condition: again
before: Loop End never sees the UDF's score and stops after 2 iterations;
with a Limit before the UDF, Loop End's update fails on prev
after: it reads 3, 5, then 4, and stops after 3 iterations, with or
without the Limit
Loop Start -> Limit -> Python UDF <- Sleep <- Loop Start
before: the UDF's process_state runs twice per iteration, once per branch
after: once
```
- **The id.** A new message gets a random id before its copies are made. On
the JVM, `DataProcessor.processInputState` issues it for a state that an
operator's `processState` changed, and `OutputManager.emitState` issues it for
a state that the start or end channel handler sends. In Python,
`MainLoop._emit_and_save_state` issues it. Every copy keeps the id: one copy
per downstream channel, 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`. The id is not part of the state's content,
so it never becomes a loop variable.
- **Copies count once.** Each worker remembers the ids it has processed and
drops a later copy before registering it, running `process_state` or sending it
on. A state that nobody issued, such as the back-edge write (the variables Loop
End writes into Loop Start's input storage for the next iteration), has an
empty id and is never taken for a copy.
- **New or not.** An operator that sends a state on unchanged sends the same
message, with its id. A changed state, or a publication, is a new message.
Python compares the serialized values, so `2` becoming `2.0` is a change. A
message's depth (`loop_counter`, the number of inner loops it has entered
beyond its own) changes as it enters or leaves an inner loop; its id does not.
- **Loop End collects.** Loop End takes every distinct message of its own
loop (depth 0), stamped or not, until its input ends. A later value replaces an
earlier one, and the user's update and condition run on the collected values.
Before, Loop End kept the first stamped state, dropped the rest, and sent
publications on, out of the block. Messages of enclosing loops are sent on as
before.
- **What the back-edge saves.** For the next iteration Loop End saves the
loop's own variables, with the values they ended with: the names its stamped
state carried, including any that a body operator's `process_state` added to
it, and every name the update assigns, read from the update's code. A name that
only a publication carried, and that the update does not assign, lasts for one
iteration. The update and condition read it, and saving it would send the old
value with Loop Start's next message, so a JVM operator that refers to it would
get two different values in one iteration. The update keeps such a value by
assigning the name (`best = max(best, score)`) or by copying it into a loop
variable (`prev = score`).
- **`None` through a JVM operator.** A JVM operator dropped a variable set
to `None` when it sent the state on, so its copy differed from the other copies
and Loop End's update could not read the variable. `State`'s JSON now keeps
null values, as Python's does.
State tables are created per execution under the execution's storage path,
on both engines from the same four-column schema, so no stored table needs
migrating. The JVM's error when two different values of one loop variable
arrive from the same loop (`StateReferenceBinding`) is unchanged, and copies of
one message are dropped before that check. Two cases still reach it, for a JVM
operator after the publisher that refers to the variable: a loop variable set
to `None` that the body publishes, now also when a JVM operator sits between
Loop Start and the publisher, since the `None` survives it; and, from the
second iteration, a name the update assigns that the body publishes again,
since Loop Start's next message carries it too.
### Any related issues, documentation, discussions?
Closes #9365.
### How was this PR tested?
The test commit, 11e78f8b85, fails without the feat commit, f65eb4a656: the
Scala specs do not compile, and 27 of the 169 tests in the seven touched Python
test files fail, 25 on what the feat commit adds or renames (the message-id
API, `LoopEndOperator.assigned_names` and the `_took_unstamped_state` flag) and
2 on the values they check. On main's source with only the new
`LoopIntegrationSpec` and `StateSpec`'s null case added, the stopping example
stops after 2 iterations instead of 3, the merge UDF counts 12 rows instead of
9, the Limit variant fails on `prev` and the null case fails on the dropped
nulls; the Filter case passes there, as it does here. Each of 24 Python and 14
JVM mutants of the feat commit makes at least one test fail.
- `test_main_loop.py`: a copy is dropped before the callback, and a late
copy cannot undo a newer message. Forwarding keeps the id, while a change
(including `2` to `2.0`) or a start/finish state gets a new one, with the same
id on the sent and the stored copy. A value that cannot be serialized is
reported as an operator error. Entering and leaving an inner loop keeps the id.
Loop End collects a publication without losing the Loop Start it jumps back to,
drops a late copy of its loop's state after a changed one, and saves the loop's
variables but no name that only publications carried. A name the update
assigns, as in `best = max(best, score)`, is saved in every iteration, also
when only a publication carried it. The stopping example runs on two fresh
workers, the second starting from what the first saved.
- `test_loop_operators.py`: Loop End's update records every name it assigns,
also a walrus target in a comprehension, a global set in a function and an
import, and no name it only reads or deletes.
- `test_output_manager.py`, `test_state.py`, `test_network_receiver.py`,
`test_input_port_materialization_reader_runnable.py`,
`test_state_materialization_e2e.py`: the given id reaches every receiver and
the stored row, goes through the Python sender and receiver, the reader and a
real Iceberg table, and stays separate from a user variable named `message_id`.
- `DataProcessorSpec`: a copy arriving on another input port is processed
once, and a late copy registers nothing after a distinct message. An unchanged
state keeps its id and a changed one gets a new id. Two distinct messages that
change a loop variable the operator's setting refers to still fail.
- `OutputManagerSpec`, `NetworkOutputBufferSpec`, `StartChannelHandlerSpec`,
`EndChannelHandlerSpec`: one id per `emitState`, shared by every buffer; a
handler's state is a new message.
- `StateSpec`, `ArrowUtilsSpec`, `PythonProxyClientSpec`,
`PythonProxyServerSpec`, `InputPortMaterializationReaderThreadSpec`: null
values survive `State`'s JSON, nested ones too; the id goes through the state
row, the Arrow hop and real Iceberg rows, where a stored row replays with the
id its network copies carried.
- `LoopIntegrationSpec`: the two workflows above, the stopping example with
a Limit before the UDF, and a Filter that refers to the score the body
publishes in each iteration.
```
sbt "WorkflowCore/testOnly org.apache.texera.amber.core.state.*
org.apache.texera.amber.core.executor.*
org.apache.texera.amber.util.ArrowUtilsSpec"
# 118 passed
sbt "WorkflowExecutionService/testOnly
org.apache.texera.amber.engine.architecture.worker.DataProcessorSpec
org.apache.texera.amber.engine.common.ambermessage.DataPayloadSpec
org.apache.texera.amber.engine.architecture.messaginglayer.OutputManagerSpec
org.apache.texera.amber.engine.architecture.pythonworker.PythonProxyClientSpec
org.apache.texera.amber.engine.architecture.pythonworker.PythonProxyServerSpec
org.apache.texera.amber.engine.architecture.worker.managers.InputPortMaterializationReaderThreadSpec
org.apache.texera.amber.engine.architecture.worker.promisehandlers.EndChannelHandlerSpec
org.apache.texera.amber.engine.architecture.worker.promisehandlers.StartChannelHandlerSpec
org.apache.texera.amber.engine.architecture.messaginglayer.NetworkInputGatewaySpec
org.apache.texera.amber.engine.architecture.sendsemantics.partitioners.NetworkOutputBufferSpec
org.apache.texera.amber.engine.architecture.sendsemantics.partitioners.PartitionersSpec"
# 131 passed, 1 pending (NetworkOutputBufferSpec's pendingUntilFixed, as on
main)
sbt "WorkflowExecutionService/testOnly
org.apache.texera.amber.engine.e2e.LoopIntegrationSpec"
# 12 passed: the eight cases already there and the four new ones above
sbt scalafmtCheckAll "scalafixAll --check"
# clean
cd amber && pytest src/test/python/core
# 1214 passed, 9 skipped, 1 xfailed; 13 fail on Windows and local Iceberg
catalogs, as on main
ruff check src/main/python src/test/python && ruff format --check
src/main/python src/test/python
# clean
```
### Was this PR authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 5.5)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
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]