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

   ### 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 but distinct: #12125 (`SeaTunnelServer#reset()` no-op after 
cluster merge), #11437 (fixed-slot failover leak).
   
   ### What happened
   
   `DefaultSlotService` uses a boolean named `initStatus` to decide whether 
`reset()` should reinitialize the slot service, but the flag is set in the 
opposite direction from what the name and `reset()` logic imply:
   
   - `init()` sets `initStatus = true` after initialization.
   - `requestSlot()` sets `initStatus = false` on its first line, **even when 
the request fails**.
   - `reset()` only reinitializes when `initStatus == false`.
   
   So the first slot request flips the service into "reset me on the next 
`ResetResourceOperation`" state, and it never flips back. A worker that has 
ever served a slot will have all of its slot state destroyed the next time a 
master sends `ResetResourceOperation`; a worker that has never served a slot 
does nothing on reset.
   
   ### Code path
   
   
[`DefaultSlotService.java#L91-L110`](https://github.com/apache/seatunnel/blob/5e0e9c8e3f5c8b892df4392ca7567a06ea7f3dff/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/service/slot/DefaultSlotService.java#L91-L110)
   
   
[`DefaultSlotService.java#L158-L168`](https://github.com/apache/seatunnel/blob/5e0e9c8e3f5c8b892df4392ca7567a06ea7f3dff/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/service/slot/DefaultSlotService.java#L158-L168)
   
   
[`DefaultSlotService.java#L171-L193`](https://github.com/apache/seatunnel/blob/5e0e9c8e3f5c8b892df4392ca7567a06ea7f3dff/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/service/slot/DefaultSlotService.java#L171-L193)
   
   `reset()` does `close(); init();`, which:
   
   - recreates `assignedSlots`, `unassignedSlots`, and `contexts`;
   - regenerates `slotServiceSequence`;
   - creates a new `unassignedResource` snapshot;
   - leaves the existing `TaskExecutionService` running.
   
   ### Trigger
   
   `ResetResourceOperation` is sent by the master's 
`AbstractResourceManager.heartbeat()` whenever a heartbeat arrives from a 
worker address that is not in `registerWorker`:
   
   
[`AbstractResourceManager.java#L324-L331`](https://github.com/apache/seatunnel/blob/5e0e9c8e3f5c8b892df4392ca7567a06ea7f3dff/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/resourcemanager/AbstractResourceManager.java#L324-L331)
   
   Realistic cases:
   
   - a worker leaves and rejoins the Hazelcast cluster without its JVM 
restarting (network partition / merge / transient eviction);
   - a new active master misses the worker during 
`syncExistingWorkerProfiles()` (for example, a `SyncWorkerProfileOperation` 
failure or a membership race), so the first heartbeat is treated as an unknown 
worker;
   - any other path where the master loses the worker registration while the 
worker still executes tasks.
   
   ### Impact
   
   - `assignedSlots` and `contexts` are cleared. A task that is still running 
on the worker now fails `getSlotContext()` with `WrongTargetSlotException` on 
its next deploy/restore, because `contexts.remove()` happened:
     
[`DefaultSlotService.java#L196-L202`](https://github.com/apache/seatunnel/blob/5e0e9c8e3f5c8b892df4392ca7567a06ea7f3dff/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/service/slot/DefaultSlotService.java#L196-L202)
     
[`PhysicalVertex.java#L277`](https://github.com/apache/seatunnel/blob/5e0e9c8e3f5c8b892df4392ca7567a06ea7f3dff/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/dag/physical/PhysicalVertex.java#L277)
   - `slotServiceSequence` changes, so every persisted `SlotProfile` for this 
worker fails `slotActiveCheck()`. The master-failover reuse fix (#11458) no 
longer works for this worker.
   - `unassignedSlots` is repopulated, so the worker can hand out the same 
fixed slot IDs again while the old task is still running. That is a slot 
double-assignment risk.
   - Because `requestSlot()` and `reset()` are both `synchronized`, a reset can 
also run immediately after a successful assignment and invalidate the 
`SlotProfile` that the master just received.
   
   ### Expected behavior
   
   - `ResetResourceOperation` must not destroy live assignments. Either:
     - reject/reset only when the service has no assigned slots and no active 
contexts, or
     - cancel the local task groups through `TaskExecutionService` first, then 
release each slot exactly once through the normal completion path, and only 
then reinitialize.
   - The lifecycle flag should be named and updated so that "initialized" and 
"safe to reset" are not conflated. A dedicated 
`hasAssignedSlots()`/`hasLiveContexts()` check is clearer than the current 
boolean.
   
   ### Suggested fix direction
   
   1. Introduce an explicit state: `UNINITIALIZED`, `INITIALIZED`, `IN_USE`, 
`RESETTING`, and make `reset()` a no-op (with a warning) in `IN_USE`.
   2. If a hard reset is required, first stop the task groups owned by this 
slot service, wait for their completion/release, then rebuild.
   3. Add a regression test: assign a slot, send `ResetResourceOperation`, 
assert the assignment/context/sequence are preserved or the owning tasks are 
cleanly stopped; do not silently clear.
   4. Consider not using `ResetResourceOperation` at all for a worker that 
already registered; use `SyncWorkerProfileOperation` for state exchange and 
reserve reset for a genuinely new/empty worker.
   
   ### 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