SEZ9 opened a new pull request, #13433:
URL: https://github.com/apache/seatunnel/pull/13433
Yes — glad to contribute this to Apache SeaTunnel.
Closes #13351.
### What Problem Does This PR Solve?
`PhysicalPlan.preApplyResourceFutures` was a plain `HashMap` that every
restoring pipeline merges into.
`SubPlan.stateProcess()` is `synchronized`, but per pipeline — each pipeline
is a different `SubPlan` instance. Two pipelines of the same job can therefore
restore at the same time and both call
```java
physicalPlan.getPreApplyResourceFutures().putAll(preApplyResourceFutures);
// JobMaster
```
from their own threads, while `ResourceUtils.applyResourceForPipeline()`
reads the same map for a third and `PendingDiagnosticsCollector` copies it for
the REST view.
A `HashMap` does not support that access pattern. A lost entry leaves a task
group with no pre-applied future, so the pipeline fails to allocate and the
slot already acquired for it is neither consumed nor released — a capacity leak
in fixed-slot mode, where capacity is not elastic. A resize racing with a read
can corrupt the table outright.
### What Changed
The field is a `ConcurrentHashMap`, and **the setter copies into one rather
than storing the map it is handed**:
```java
this.preApplyResourceFutures =
preApplyResourceFutures == null
? new ConcurrentHashMap<>()
: new ConcurrentHashMap<>(preApplyResourceFutures);
```
That second half is the part that makes the fix hold.
`JobMaster.preApplyResources()` has two paths: the per-`SubPlan` path merges
with `putAll()` into whatever map is stored, while the whole-job path calls
`setPreApplyResourceFutures()` with a locally built `HashMap`. Changing only
the field initialiser would leave the second path silently putting a `HashMap`
back in place, undoing the contract for every pipeline that restores afterwards.
I checked the null question before choosing a concurrent map, since
`ConcurrentHashMap` rejects null values: both construction sites in `JobMaster`
store a non-null future on every branch (`reusableSlot == null ?
applyResourceForTask(...) : completedFuture(reusableSlot)`). The nulls in this
area live in the separate per-`SubPlan` map that
`ResourceUtils.allocateResources()` fills from
`preApplyResourceFutures.get(...)`, which is unaffected. I also confirmed the
local map handed to the setter is not mutated afterwards on that path, so
copying cannot diverge from what the caller expects.
This is the "minimum fix" the issue lists. The richer options it offers —
one immutable map per `PipelineLocation`, or moving pre-apply/consume behind a
per-job lock — are better designs but change the ownership model, so they
seemed wrong to fold into a correctness fix.
### How Was This Patch Tested?
Built and run locally — JDK 11.0.32, Maven 3.8.4, `seatunnel-engine-server`
module:
```
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
- in
org.apache.seatunnel.engine.server.dag.physical.PhysicalPlanPreApplyFuturesTest
```
`spotless:apply` leaves the change unmodified. Both halves of the fix are
covered, checked by reverting each:
| Mutation | Result |
| --- | --- |
| field back to a plain `HashMap` | 2 failed |
| setter stores the caller's map as handed over | 1 failed, 1 error |
| none | 4 passed |
New file `PhysicalPlanPreApplyFuturesTest`, since there is no existing
`PhysicalPlan` test class and this repo already keeps one file per concern here
(`PhysicalVertexDeployStateTest`, `StateTransitionCleanupTest`,
`SubPlanDeploymentTest`). `PhysicalPlan` is built from Mockito mocks for the
two `IMap`s and `JobImmutableInformation`, so the test needs no Hazelcast
instance and runs in ~3s.
On test design: the deterministic assertions are the ones that guard the fix
— the map is a `ConcurrentMap`, the setter does not install a plain one, and
contents survive the copy. The concurrent test asserts only that **no entry is
lost** when four pipelines merge 250 entries each; that holds by construction
on a correct implementation, so it cannot flake. I deliberately did not assert
that a `HashMap` *does* lose entries, since that is probabilistic and would
make the suite unstable.
### Compatibility
- No new or changed configuration options, no changed defaults, no signature
change.
- Readers are unaffected: `getPreApplyResourceFutures()` still returns
`Map`, and `PendingDiagnosticsCollector`'s `HashMap::new` copy and the existing
`ResourceUtilsTest` / `PendingDiagnosticsCollectorTest` mocks all keep working.
- One behaviour change worth naming: a map containing a null value passed to
the setter would now throw instead of being stored. No current caller can
produce one (see above), and failing at the boundary is better than storing a
value that breaks allocation later — but it is a change, not a no-op.
### Docs
No documentation change: internal concurrency contract, no user-facing
option, command or output. The contract is documented on the field itself.
### Check list
* [x] Code changed are covered with tests, or it does not need tests for
reason
* [x] If any new Jar binary package adding in your PR, please add License
Notice according [New License
Guide](https://github.com/apache/seatunnel/blob/dev/docs/en/contribution/new-license.md)
— no new dependency
* [x] If necessary, please update the documentation to describe the new
feature — not user-visible
* [x] If you are contributing the connector code, please check that the
following files are updated — not a connector change
🤖 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]