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

   ### 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: #12985 (fixed-slot admission), #13349 
(snapshot-based allocation).
   
   ### What happened
   
   `JobMaster.preApplyResources()` resets the **global** allocation-strategy 
state for every job submission and every single-pipeline restore:
   
   ```java
   SlotAllocationStrategy slotAllocationStrategy =
           ((AbstractResourceManager) 
resourceManager).getSlotAllocationStrategy();
   if (slotAllocationStrategy instanceof SlotRatioStrategy) {
       ((SlotRatioStrategy) slotAllocationStrategy)
               .setWorkerAssignedSlots(new ConcurrentHashMap<>());
   } else if (slotAllocationStrategy instanceof SystemLoadStrategy) {
       ((SystemLoadStrategy) slotAllocationStrategy)
               .setWorkerAssignedSlots(new ConcurrentHashMap<>());
   }
   ```
   
   The `SlotAllocationStrategy` instance belongs to the `ResourceManager`, not 
to a job. Resetting its `workerAssignedSlots` map wipes the in-flight manual 
counters of **all other jobs** that are currently allocating. With fixed slots 
and concurrent submissions/pipeline restores, `SLOT_RATIO` and `SYSTEM_LOAD` 
can then repeatedly re-learn the same assignments or revert to stale 
`WorkerProfile` counts, which distorts placement and can concentrate requests 
on a subset of workers.
   
   There is also a direct NPE risk: `SlotRatioStrategy.calculateSlotUsage()` 
calls `workerAssignedSlots.get(...)` / `put(...)` without a null check. If 
`applyResources()` is invoked before any `preApplyResources()` has initialized 
the map (for example a direct `ResourceManager` API call, a test, or a future 
caller), it throws `NullPointerException`.
   
   ### Code path
   
   
[`JobMaster.java#L518-L536`](https://github.com/apache/seatunnel/blob/5e0e9c8e3f5c8b892df4392ca7567a06ea7f3dff/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/master/JobMaster.java#L518-L536)
   
   
[`SlotRatioStrategy.java#L28-L60`](https://github.com/apache/seatunnel/blob/5e0e9c8e3f5c8b892df4392ca7567a06ea7f3dff/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/resourcemanager/allocation/strategy/SlotRatioStrategy.java#L28-L60)
   
   
[`SystemLoadStrategy.java#L43-L70`](https://github.com/apache/seatunnel/blob/5e0e9c8e3f5c8b892df4392ca7567a06ea7f3dff/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/resourcemanager/allocation/strategy/SystemLoadStrategy.java#L43-L70)
   
   `workerAssignedSlots` is declared as a plain field (Lombok 
`@Getter/@Setter`), not `volatile`:
   
   
[`SlotRatioStrategy.java#L28`](https://github.com/apache/seatunnel/blob/5e0e9c8e3f5c8b892df4392ca7567a06ea7f3dff/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/resourcemanager/allocation/strategy/SlotRatioStrategy.java#L28)
   
   ### Impact
   
   - `SLOT_RATIO` / `SYSTEM_LOAD` lose their in-flight allocation counters 
whenever any other job is submitted or any pipeline restores.
   - Placement drifts back to cached `WorkerProfile` counts, which are already 
up to 5s stale; fixed-slot bursts then over-select the same worker and hit the 
retry path (#13349).
   - Direct `ResourceManager.applyResources()` callers can NPE depending on 
timing.
   - The reset happens even for a *single-pipeline* restore 
(`preApplyResources(subPlan)`), so a running job can disturb the placement 
state of unrelated jobs.
   
   ### Expected behavior
   
   - Allocation strategy state should be per-job or per-allocation-request, not 
a global mutable map reset by unrelated jobs.
   - At minimum, `workerAssignedSlots` must be initialized defensively and 
updated with a stable concurrency contract; a job's pre-apply must not clear 
another job's in-flight state.
   - If the strategy needs a fresh view for a new job, scope the reset to that 
job's allocation context or snapshot the current counts instead of wiping them.
   
   ### Suggested fix direction
   
   1. Move `workerAssignedSlots` into the `ResourceRequestHandler` (per 
job/allocation) or pass an immutable snapshot into `selectWorker`.
   2. If a global map is kept, make it `volatile`/`ConcurrentHashMap` and never 
replace it from `preApplyResources`; merge counts instead of resetting.
   3. Initialize `workerAssignedSlots` lazily in the strategy constructor or in 
`selectWorker` (`computeIfAbsent`), so direct `applyResources()` calls cannot 
NPE.
   4. Add a test with two concurrent `preApplyResources()` calls and assert 
that both jobs' counters are preserved.
   
   ### 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