zhangshenghang opened a new issue, #13351:
URL: https://github.com/apache/seatunnel/issues/13351

   ### Search before asking
   
   - [x] I searched existing issues and PRs before filing this report.
   - [x] This report is based on source analysis at commit 
[`5e0e9c8e3f`](https://github.com/apache/seatunnel/commit/5e0e9c8e3f5c8b892df4392ca7567a06ea7f3dff);
 line numbers may drift on `dev`.
   - [x] Related existing work: #12509 / PR #12510 (pipeline restore allocation 
failure), #12947 (partial allocation rollback), #13348 (restore retry budget).
   
   ### What happened
   
   `PhysicalPlan.preApplyResourceFutures` is a plain `java.util.HashMap`. It is 
exposed through a getter and mutated by `JobMaster.preApplyResources(SubPlan)` 
from the `SubPlan.stateProcess()` thread. `stateProcess()` is `synchronized` on 
the `SubPlan` instance, but different pipelines are different `SubPlan` 
instances, so two pipelines of the same job can restore concurrently and call 
`physicalPlan.getPreApplyResourceFutures().putAll(...)` at the same time. 
`ResourceUtils.applyResourceForPipeline()` also reads the same map concurrently.
   
   A `HashMap` does not support concurrent structural modification. Two 
simultaneous `putAll()` calls (or a `putAll()` racing with `get()`) can lose 
entries, observe a partially resized table, or in the worst case cause the JDK 
8 `HashMap` resize loop. Even without corruption, a lost entry means a task 
group has no pre-applied future, so `applyResourceForPipeline()` sees a null 
future and fails the pipeline; any slot already acquired for that entry is then 
not tracked.
   
   ### Code path
   
   Map declaration and direct getter:
   
   
[`PhysicalPlan.java#L86`](https://github.com/apache/seatunnel/blob/5e0e9c8e3f5c8b892df4392ca7567a06ea7f3dff/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/dag/physical/PhysicalPlan.java#L86)
   
   
[`PhysicalPlan.java#L452-L458`](https://github.com/apache/seatunnel/blob/5e0e9c8e3f5c8b892df4392ca7567a06ea7f3dff/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/dag/physical/PhysicalPlan.java#L452-L458)
   
   Concurrent writers (one per restoring pipeline):
   
   
[`JobMaster.java#L690-L731`](https://github.com/apache/seatunnel/blob/5e0e9c8e3f5c8b892df4392ca7567a06ea7f3dff/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/master/JobMaster.java#L690-L731)
   
   Reader:
   
   
[`ResourceUtils.java#L47-L56`](https://github.com/apache/seatunnel/blob/5e0e9c8e3f5c8b892df4392ca7567a06ea7f3dff/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/dag/physical/ResourceUtils.java#L47-L56)
   
   `SubPlan.stateProcess()` is only synchronized per pipeline:
   
   
[`SubPlan.java#L798`](https://github.com/apache/seatunnel/blob/5e0e9c8e3f5c8b892df4392ca7567a06ea7f3dff/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/dag/physical/SubPlan.java#L798)
   
   ### Reproduction
   
   - A multi-pipeline job (for example 4 independent FakeSource -> LocalFile 
chains).
   - Kill a worker so more than one pipeline fails and restores at roughly the 
same time.
   - Both pipelines enter `FAILED` -> `prepareRestorePipeline()` -> 
`preApplyResources(subPlan)` and call `putAll()` concurrently on the shared 
`HashMap`.
   - The race is timing-dependent, but the CI shape is exactly the one used by 
`SplitClusterFaultToleranceIT#testManyPipelinesRestoreContentionInWorkerDown` 
(currently disabled for #12202); a focused unit test can drive two `SubPlan` 
restores concurrently against the same `PhysicalPlan`.
   
   ### Impact
   
   - Lost pre-applied futures -> `ResourceUtils.applyResourceForPipeline()` 
throws `NoEnoughResourceException` -> pipeline restore consumes a retry (see 
#13348) and may permanently fail.
   - An entry can be replaced/removed while another pipeline is reading it; the 
corresponding slot is not consumed and is not released.
   - Worst case: `HashMap` internal corruption (resize race) causing 
unpredictable behavior in the scheduler thread.
   - Fixed-slot mode turns any lost entry into a capacity leak; dynamic mode 
turns it into an unnecessary new allocation.
   
   ### Expected behavior
   
   - The pre-applied map must be safe for concurrent access from multiple 
pipeline restore threads, or each pipeline must own its own map with an atomic 
merge.
   - A failed restore must not be able to corrupt or drop another pipeline's 
pre-applied entry.
   
   ### Suggested fix direction
   
   1. Replace the `HashMap` with a `ConcurrentHashMap` (minimum fix), and 
document the concurrency contract on the getter.
   2. Better: keep one immutable `Map` per `PipelineLocation` and replace the 
per-pipeline entry atomically; readers then snapshot the map for their pipeline.
   3. Guard `putAll()` and the read in `applyResourceForPipeline()` with the 
same lock, or move the whole pre-apply/consume operation into a per-job 
scheduler.
   4. Add a stress test with two or more pipelines restoring concurrently and 
assert that every task group gets a non-null future and every allocated slot is 
either consumed or released.
   
   ### SeaTunnel version
   
   `dev` (analyzed at `5e0e9c8e3f`).
   
   ### Zeta or Flink or Spark Version
   
   Zeta.
   
   ### Are you willing to submit PR?
   
   - [ ] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


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