Skip to content

[Fix][Transform-V2] Forward lifecycle callbacks to per-table transforms - #13180

Open
goutamadwant wants to merge 1 commit into
apache:devfrom
goutamadwant:fix/multi-table-transform-lifecycle
Open

goutamadwant wants to merge 1 commit into
apache:devfrom
goutamadwant:fix/multi-table-transform-lifecycle

Conversation

@goutamadwant

Copy link
Copy Markdown
Collaborator

Purpose of this pull request

I’m contributing this fix to SeaTunnel so multi-table wrappers initialize and release the per-table transforms they own.

Fixes #13131.

  • Forward lifecycle callbacks once per owned child, including composed Map and FlatMap rules.
  • Stop initialization on failure and clean up partially built or initialized children. Continue sibling cleanup while retaining the original failure.
  • Reuse schema-initialized SQL engines and model clients, prevent late initialization after close, and preserve Python startup cancellation.
  • Keep lifecycle state transient and retain existing routing and serialization behavior. Update the English and Chinese contributor guides.

Engine-side failure propagation remains in #13132 / #13175. This patch contains no engine production changes.

Does this PR introduce any user-facing change?

Yes. On dev, affected multi-table wrappers currently skip child initialization and cleanup. They now forward both callbacks, release schema-time resources and retain startup/cleanup failures without duplicate callbacks.

No configuration options, dependencies or public method signatures change. All seven affected serialization UIDs remain unchanged.

How was this patch tested?

Validated locally with JDK 11:

  • 1,998 transform tests and 498 API/common tests passed.
  • Extended existing tests for successful startup, partial failures, constructor cleanup, repeated/concurrent callbacks, identity children, composed rules and serialization.
  • Real loopback Embedding and Python subprocess checks verified client reuse, connection-pool shutdown, startup cancellation and process/thread/script cleanup.
  • Eight native Zeta jobs passed with unchanged engine code, covering Map/FlatMap, empty/nonempty input and ALL/OFF thread sharing. Checked callbacks, output counts and released resources.
  • Spotless checks passed. The full Docker/Failsafe suite was not run.

Unit-test commands:

export MAVEN_OPTS="-Xmx3g -XX:ActiveProcessorCount=4"

./mvnw -Pci -pl seatunnel-transforms-v2 -am \
  "-Dsurefire.jvm.args=-Xmx3g -XX:ActiveProcessorCount=4 -javaagent:$HOME/.m2/repository/net/bytebuddy/byte-buddy-agent/1.12.19/byte-buddy-agent-1.12.19.jar" verify

./mvnw -Pci -pl seatunnel-transforms-v2 \
  "-Dsurefire.jvm.args=-Xmx3g -XX:ActiveProcessorCount=4 -javaagent:$HOME/.m2/repository/net/bytebuddy/byte-buddy-agent/1.12.19/byte-buddy-agent-1.12.19.jar" verify

Native integration exercised LocalModeIT.multiTableLifecycleForEmptyAndNonemptyJobs through JUnit Platform, with assertions for resource and output counts.

Check list

@DanielLeens DanielLeens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this careful fix, @goutamadwant. The lifecycle gap in the multi-table wrapper is real, and the PR closes it with a clear ownership model, solid partial-failure handling and a native Zeta test. I reviewed the current head c085127c6d0d721211c654a7f5c7d7865e7a7537.

What Problem Does This PR Solve?

AbstractMultiCatalogTransform builds and owns one transform per table in transformMap, but it did not override open() / close(). On dev, the wrapper therefore inherited the no-op defaults from SeaTunnelTransform, so its per-table children never got startup or cleanup callbacks from the engine. PythonMultiCatalogTransform was the only wrapper with a hand-written close(). This PR forwards both callbacks to the owned children (Fixes #13131).

1 Code Change Review

1.1 Core logic

  • New TransformLifecycle (seatunnel-transforms-v2/.../common/TransformLifecycle.java:33-180): identity-deduplicated child list, open() runs once, stops at the first failure, records it, closes all children and rethrows it. close() runs once, tries every child, keeps the first failure and adds the rest as suppressed. Plugin calls run outside the monitor (:131-145), so a Python close() can still interrupt an open() that is blocked.
  • AbstractMultiCatalogTransform.java:172-188 now overrides open() / close() and delegates to a lazily created, transient lifecycle. orderedChildren() (:190-203) uses input-table order and then the remaining map entries. Duplicates are removed by identity in the lifecycle constructor.
  • Constructor and composed-rule build failures release children that were already built (:165-168, :228-231).
  • ChainedMapTransform / ChainedFlatMapTransform now use the same owner, so composed children are closed once and close failures are no longer lost.
  • SQLTransform, CalciteTransform, EmbeddingTransform and LLMTransform now make open() idempotent. It reuses a resource that schema discovery already built and refuses to open after close(). close() is idempotent and nulls the resource.

Runtime flow on Zeta after this PR:

client: wrapper ctor -> buildTransform per table (schema discovery may call child tryOpen())
     -> serialized in LogicalDag (lifecycle/closed/engine fields are transient)
worker: TransformFlowLifeCycle.open() -> wrapper.open() -> TransformLifecycle.open()
     -> child.open() for each owned child, in order (SQL/Calcite/LLM/Embedding: init once)
     -> on failure: close all children, rethrow original failure -> task fails
worker: TransformFlowLifeCycle.close() -> wrapper.close() -> each child.close() once,
     first failure kept, the rest suppressed

On the normal path, every Zeta job that uses a multi-table transform reaches this change (TransformFlowLifeCycle.open() calls t.open() on the wrapper). So this is a main-path change, not only an edge-case fix.

1.2 Compatibility

  • No option, default, public signature or SPI change. All new fields are transient, and the serialVersionUIDs stay the same, so the serialized form of the wrappers and children is unchanged.
  • transformMap is only assigned in the constructor, so caching the child list on the first lifecycle() call is safe.
  • There is one intended behavior change: a child open() failure now fails the task at startup instead of surfacing lazily on the first row. That is the correct contract, and the PR description explains it.
  • The new "closed means no reopen" guard is safe for Zeta, Flink and Spark, because each runtime instance comes from deserialization. The new LocalModeIT covers this for Zeta.

1.3 Side effects

  • SQL / Calcite engines and model clients are now created eagerly in open() instead of on the first row. The cost is the same, only earlier.
  • PythonMultiCatalogTransform.close() now goes through the shared owner, so it also covers partial-startup cleanup.

1.4 Error handling / logging

Failure retention and suppression look correct. Two small non-blocking observations:

Issue 1

  • Location: seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/common/TransformLifecycle.java:89
  • Problem: open() catches only Exception. If a child throws an Error, the children that already opened are not closed, and startupFailure stays unset. A LinkageError / NoClassDefFoundError is a realistic case for plugins with missing jars. The finally block resets opening, so a later open() would retry.
  • Risk: Low. The task fails anyway, but resources opened by earlier children may be released only by the later close() from the engine.
  • Best improvement: catch Throwable here (rethrowing as-is), or add an Error branch that also runs close().
  • Severity: Low (non-blocking)
  • Raised by another reviewer: No

Issue 2

  • Location: seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/nlpmodel/embedding/EmbeddingTransform.java:93-214 (similarly LLMTransform.java:80)
  • Problem: open() holds synchronized (this) while it builds the remote client and calls model.dimension(), which can make a network call. close() takes the same monitor (:416). A cancel that arrives during a slow open() therefore waits until the HTTP call returns or times out. This differs a little from the guide text, which says a child that supports cancellation must allow close() during initialization.
  • Risk: Low. Cancellation is delayed, but nothing leaks.
  • Best improvement: none required for this PR. Optionally, add a short comment that these model transforms do not support cancellation during open().
  • Severity: Low (non-blocking)
  • Raised by another reviewer: No

2 Quality

2.1 Standards

  • Nit: AbstractMultiCatalogTransform.java:57 and :196 use the fully qualified @lombok.SneakyThrows. The sibling TransformLifecycle imports lombok.SneakyThrows, so importing it here too would be consistent. Also, the constructor's lambda can only throw unchecked exceptions, so catch (RuntimeException failure) would remove the need for @SneakyThrows on the constructor. Non-blocking.
  • New class has the ASF header and class-level Javadoc. Key methods carry comments that explain the ownership and failure rules.

2.2 Tests

  • AbstractMultiCatalogTransformTest (+429) covers successful startup, partial failure, constructor cleanup, repeated and concurrent callbacks, identity children, composed rules and serialization. The new per-transform tests (SQL, Calcite, Embedding, LLM, Python) check reuse and once-only close.
  • LocalModeIT.multiTableLifecycleForEmptyAndNonemptyJobs (seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/.../LocalModeIT.java) runs real Zeta jobs for Map/FlatMap × empty/non-empty × ALL/OFF thread sharing. It asserts exact open/close/map/output counts and that no resources leak. Each run uses its own cluster name and checkpoint dir, and the static observation map is cleaned up in finally.
  • Stability: Stable. Each run starts its own Hazelcast node, and the 60s/10s Awaitility budgets give enough headroom for a single-row batch job. It passed in engine-v2-it on both JDK 11 and 17 in the fork run.

2.3 Docs

The EN and ZH contribute-transform-v2-guide.md sections match in content and describe the ownership rules accurately.

3 Architecture

The fix sits in the right place: the generic owner (AbstractMultiCatalogTransform) and the composed chains, with no engine changes. Per-transform changes stay inside each transform. Putting the lifecycle in one package-private class avoids copying the close-all-and-suppress loop into every wrapper.

4 Issue table

# Location Severity Blocking
1 TransformLifecycle.java:89 (Error not covered by startup cleanup) Low No
2 EmbeddingTransform.java:93, LLMTransform.java:80 (monitor held across remote init) Low No
nit AbstractMultiCatalogTransform.java:57,196 (FQN @lombok.SneakyThrows) Nit No

CI status

The apache-side Build check is red. The real run is in your fork: https://github.com/goutamadwant/seatunnel/actions/runs/42435490469. Two jobs failed, and neither failure comes from this diff:

The jobs this change can affect are all green on JDK 11 and 17: unit-test (ubuntu and windows), engine-v2-it, transform-v2-it parts 1 and 2. Could you re-run the failed jobs, or sync the latest dev (the branch is 1 commit behind) and push to trigger CI again? I will approve once the Build check is green.

5 Merge recommendation

Ready to merge after fixes. The only blocker is CI: the two failures above are infrastructure problems and need a green re-run. There are no blocking code findings. Issues 1–2 and the nit are optional improvements.

Thanks again for the thorough work on this, especially the native Zeta coverage.

@DanielLeens DanielLeens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @goutamadwant for re-running CI. CI was previously the only remaining blocker; CI is now passing, so I am changing my conclusion to approve.

The fork run https://github.com/goutamadwant/seatunnel/actions/runs/42435490469 (attempt 2) on head c085127c is fully green: 85 jobs passed, 11 skipped, none failed. That includes all-connectors-it-7 (11, ubuntu-latest) and doris-connector-it (11, ubuntu-latest), the two jobs that failed on infrastructure last time. The apache-side Build check is green as well.

My source review on this head is unchanged: no blocking findings. Issues 1-2 (catching Error in TransformLifecycle.open(), and the monitor held during remote model init) and the @lombok.SneakyThrows nit are still optional follow-ups.

Conclusion: Approve

@JAEKWANG97

Copy link
Copy Markdown
Contributor

Thanks for working on this, @goutamadwant. I opened #13131 while working through the lifecycle prerequisites for STIP-44 (#13099), and I was also preparing a fix.

I reproduced the LinkageError cleanup gap noted in the review and prepared a small follow-up on top of c085127c. It covers construction, startup and shutdown, preserves the original failure, and continues cleanup of the remaining children. The regression tests and native Zeta checks pass locally.

To avoid overlapping work, would you prefer that I share the patch for inclusion here, or open a focused follow-up after this PR merges? Happy to coordinate, and this does not need to hold up the current PR.

@DanielLeens

Copy link
Copy Markdown
Contributor

Thanks @JAEKWANG97 for reproducing this and for coordinating instead of pushing a competing change.

For context, this is Issue 1 from my review: TransformLifecycle.open() (seatunnel-transforms-v2/src/main/java/org/apache/seatunnel/transform/common/TransformLifecycle.java:89) catches only Exception, so an Error such as LinkageError skips cleanup of children that already opened. I rated it low and non-blocking because the task fails anyway and the engine's later close() still releases those children.

From the reviewer side, my suggestion is a focused follow-up PR after this one merges. The current head c085127c is approved and its fork CI is fully green, and your patch also widens the construction and shutdown paths, which is easier to review on its own with its regression tests. Please link it to #13131 / #13099 so the lifecycle work stays traceable. Of course, if @goutamadwant prefers to fold it in here, that works too; I would re-review the new head.

@JAEKWANG97

Copy link
Copy Markdown
Contributor

Thanks for the guidance. I’ll prepare a focused follow-up after #13180 merges and link it to #13131 and #13099.

@DanielLeens

Copy link
Copy Markdown
Contributor

Thanks @JAEKWANG97. That follow-up scope and the links to #13131 / #13099 match the plan we discussed. No additional change is needed in this PR from my side; the existing technical approval stands, subject to the maintainer merge gate.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] [Transform-V2] Forward lifecycle callbacks to per-table transforms

3 participants