PatrickEifler opened a new pull request, #1516:
URL: https://github.com/apache/flink-agents/pull/1516
Linked issue: #1515
### Purpose of change
When a Java agent's output type is declared with
`toDataStream(TypeInformation)` / `toDataStream(Class)` /
`toTable(TypeInformation)` / `toTable(Class)`, an action that emits an
`OutputEvent` of a different type now fails the job with an
`IllegalStateException` naming the agent, the event type, the emitted class and
the declared class.
The declared type is applied directly to the agent operator, which
promises that type to Flink but emitted whatever the `OutputEvent` held. A
wrong payload surfaced as a
`ClassCastException` inside a serializer or a chained downstream operator,
with no hint of which agent or event produced it.
#### Runtime flow
1. Graph build: Flink calls
`ActionExecutionOperatorFactory.setOutputType(type, config)` (enabled by
`isOutputTypeConfigurable() == true`); the factory stores
`type.getTypeClass()`.
2. Task start: `createStreamOperator` passes that class to
`ActionExecutionOperator.setDeclaredOutputClass`, which forwards it to
`EventRouter.setDeclaredOutputClass`. The router
keeps it only for Java pipelines and non-`Object`, non-primitive classes;
otherwise no check applies.
3. Per output: `ActionExecutionOperator.processEvent` →
`EventRouter.getOutputFromOutputEvent`. On a Java pipeline,
`checkDeclaredOutputType` runs before the value is returned to
`output.collect`.
#### Key decisions
- The check sits in `EventRouter.getOutputFromOutputEvent`, the single
path by which outputs leave the operator, so no emission path bypasses it.
- The class comes from Flink's `setOutputType` hook rather than a new
constructor parameter, so it is the output type Flink itself uses for the
operator's serializer.
- Subtypes of the declared class pass (`Class.isInstance`).
- `null` payloads are not checked; whether they are accepted stays with
the declared type's serializer.
### Behavioral Semantics
#### Interaction decisions
| Pipeline | Declared output class | Payload | Result |
|---|---|---|---|
| Java | concrete (e.g. `String`) | instance or subtype | emitted
unchanged |
| Java | concrete | other class | `IllegalStateException`, job fails |
| Java | concrete | `null` | passed to the serializer (unchanged behavior)
|
| Java | `Object` (untyped terminals) | any | no check |
| Java | primitive class | any | no check (payloads are boxed) |
| Java | not set (operator built without a factory) | any | no check |
| Python | any | pickled bytes | no check |
#### Behavioral contracts
1. Java pipeline, concrete declared type, payload of another class: the
job fails with `IllegalStateException` whose message contains the agent name,
the emitted class name, the
event type and the declared class name.
2. Java pipeline, payload that is an instance (or subtype instance) of the
declared class: emitted unchanged.
3. Java pipeline, `null` payload: not rejected by the check.
4. Declared type `Object`: outputs are unrestricted.
5. Python pipeline: outputs are never checked against the declared type.
6. The type declared at graph build reaches the running operator.
#### Failure behavior
- Mismatch: raises `IllegalStateException` from the operator's event
processing, outside action-failure handling, failing the task under the job's
restart strategy. It is not
retried or absorbed.
- No other new failure paths; a missing declared class disables the check.
### Tests
| Contract | Tests |
|---|---|
| 1. Mismatch fails with descriptive error |
`EventRouterTest#getOutputFromOutputEventRejectsOutputNotMatchingDeclaredType`
(classes, event type);
`DeclaredOutputTypeExecutionTest#rejectsOutputsNotMatchingTheDeclaredType`
(real job) |
| 2. Matching / subtype payload passes |
`EventRouterTest#getOutputFromOutputEventAcceptsOutputMatchingDeclaredType`
(`Long` vs `Number`);
`DeclaredOutputTypeExecutionTest#emitsOutputsMatchingTheDeclaredType`
(real job) |
| 3. `null` payload not rejected |
`EventRouterTest#getOutputFromOutputEventAcceptsOutputMatchingDeclaredType` |
| 4. `Object` unrestricted |
`EventRouterTest#getOutputFromOutputEventSkipsCheckForObjectOutputType` |
| 5. Python pipeline unchecked |
`EventRouterTest#getOutputFromOutputEventSkipsCheckOnPythonPipeline` |
| 6. Declared type reaches the running operator | both
`DeclaredOutputTypeExecutionTest` cases (MiniCluster jobs via
`toDataStream(Class)`) |
Coverage by risk: the declared type reaching the running operator, and the
check not firing on valid output, are covered by real MiniCluster jobs. Skip
conditions are unit-tested.
Not verified:
- The agent name in the message is not asserted (the unit test plan has no
agent name).
- The primitive-class skip has no test.
- POJO / user-classloader classes in a job (job tests use `Integer`,
`String`).
- Behavior on restart when the failing action's result has been persisted
by durable execution.
- Python pipelines end to end (no Python code changes).
Ran `mvn -pl runtime test` (1963 tests, 0 failures), `./tools/lint.sh -c`,
`./tools/check-license.sh`.
### API
No public API changes. `ActionExecutionOperatorFactory` (runtime, outside
the scope in `API.md`) now overrides `isOutputTypeConfigurable` /
`setOutputType`; the new operator and
router setters are package-private.
Compatibility impact: Java pipelines with a typed terminal that already
emitted matching payloads, untyped `toDataStream()` / `toTable(Schema)`
pipelines, and all Python pipelines
behave as before. Pipelines that emitted a mismatching payload already
failed, but now with an `IllegalStateException` instead of a
`ClassCastException`. One stricter case: a
declared generic (Kryo) type such as `GenericTypeInfo<Foo>` now rejects
non-`Foo` payloads that Kryo might previously have serialized.
### Documentation
- [ ] `doc-needed`
- [x] `doc-not-needed`
- [ ] `doc-included`
### Was this patch authored or co-authored using generative AI tooling?
- [x] Yes
- [ ] No
Generated-by: Claude Code 2.1.335 (Claude Opus 5.5)
--
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]