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

   ### Search before asking
   
   - [x] I had searched in the 
[issues](https://github.com/apache/seatunnel/issues?q=is%3Aissue+label%3A%22bug%22)
 and found no similar issues.
   
   ### What happened
   
   In the SeaTunnel Zeta engine (`dev` at `39c74ce48b0`), when a failing 
pipeline enters its retry backoff sleep (`job.retry.times > 0`, 
`job.retry.interval.seconds > 0`) and a user calls `cancelJob(jobId)` during 
that backoff window:
   
   1. **`cancelJob(jobId)` blocks for the entire remaining 
`job.retry.interval.seconds` duration** (e.g., 6s in reproduction, or minutes 
in production deployments with longer retry intervals), holding both the 
`SubPlan` monitor lock and the caller/coordinator cancellation thread.
   2. **After the backoff sleep ends, the engine ignores that the job was 
already canceled (`jobMaster.isNeedRestore() == false`, `JobStatus == 
CANCELING`)**, allocates a brand-new set of slots via 
`jobMaster.preApplyResources(this)`, and deploys & starts a zombie retry 
attempt on workers before the blocked `cancelPipeline()` can finally acquire 
the lock and tear it down.
   
   In production, this causes:
   - Unresponsive cancellation requests and thread-pool starvation when 
`job.retry.interval.seconds` is large.
   - Unnecessary slot allocation and contention on clusters with tight 
fixed-slot capacity.
   - Real side effects on external sources/sinks (connections opened, records 
read/written) from a zombie retry attempt launched **after** the user 
explicitly canceled the job.
   
   ### Root Cause Analysis
   
   1. **`SubPlan.stateProcess()` holds the `SubPlan` intrinsic monitor lock 
across `Thread.sleep(...)`:**
      - `SubPlan.stateProcess()` is declared `private synchronized void 
stateProcess()` 
(`seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/dag/physical/SubPlan.java:798`).
      - On `case FAILED:` / `case CANCELED:` (`SubPlan.java:878-885`), it 
directly invokes `prepareRestorePipeline()` (`SubPlan.java:609-639`), which 
resets `currPipelineStatus` to `CREATED` and then executes 
`Thread.sleep(pipelineRestoreIntervalSeconds * 1000)` (`SubPlan.java:628`) 
while still holding **both** `synchronized (this)` and `synchronized 
(restoreLock)`.
      - When `cancelJob(jobId)` arrives, `PhysicalPlan.cancelJob()` calls 
`jobMaster.neverNeedRestore()` and `SubPlan.cancelPipeline()`. Because 
`cancelPipeline()` is `public synchronized void cancelPipeline()` 
(`SubPlan.java:492`), it immediately blocks on `SubPlan`'s monitor lock for the 
entire remaining sleep duration and cannot interrupt or abort the pending retry.
   
   2. **No re-check of `jobMaster.isNeedRestore()` or cancellation state after 
waking up:**
      - Once `Thread.sleep(...)` finishes, `prepareRestorePipeline()` 
unconditionally returns `true` (`SubPlan.java:629`).
      - Still inside the same `synchronized` `stateProcess()` invocation, lines 
`881-883` unconditionally run:
        ```java
        jobMaster.releasePipelineResource(this);
        jobMaster.preApplyResources(this);
        restorePipeline();
        ```
      - `restorePipeline()` calls `startSubPlanStateProcess()` (re-entering 
`synchronized stateProcess()`), driving the pipeline through `CREATED -> 
SCHEDULED -> DEPLOYING -> RUNNING`, acquiring fresh slots from workers, and 
deploying all task groups for an already-canceled job.
      - Only after `stateProcess()` returns and releases the `SubPlan` monitor 
lock does the waiting `cancelPipeline()` finally run and cancel the freshly 
deployed tasks.
   
   ### Expected Behavior
   
   When `cancelJob(jobId)` is called while a pipeline is waiting in its retry 
backoff interval:
   1. The pending retry backoff should be aborted promptly (without waiting for 
the full `job.retry.interval.seconds` sleep).
   2. The pipeline should transition cleanly to `CANCELED` without calling 
`preApplyResources` or deploying any further retry attempt 
(`extraSlotsAllocatedAfterCancel == 0`).
   
   ### How to Reproduce
   
   1. Start a single-node embedded Zeta cluster with fixed slots 
(`dynamic-slot: false`, `slot-num: 10`).
   2. Submit a failing batch job with `job.retry.times = 3` and 
`job.retry.interval.seconds = 6` (for example, a `FakeSource` -> failing SQL 
cast `select cast(name as int) as name, id from fake` -> `Console` job).
   3. Wait until attempt 1 fails and pipeline 1 enters 
`prepareRestorePipeline()` backoff sleep (`subPlan.getPipelineRestoreNum() == 1 
&& subPlan.getCurrPipelineStatus() == PipelineStatus.CREATED`).
   4. Record `slotReqBefore = 
server.getSlotService().getSlotMetrics().getSlotRequestSuccessTotal()` (equals 
`2` for 1 coordinator + 1 task group).
   5. Call `client.cancelJob(jobId)` and measure elapsed time until 
`cancelJob(jobId)` returns and the job terminates.
   6. Observe the actual runtime output:
      - `cancelJob(jobId)` blocks for **`~6100-6200 ms`** (the entire 6-second 
retry backoff interval).
      - `slotRequestSuccessTotal` increases from **`2` to `4`** 
(`extraSlotsAllocatedAfterCancel == 2`), proving that after `cancelJob` was 
invoked and `needRestore` became `false`, the waking retry path still allocated 
fresh slots and deployed a zombie attempt #2 before cancellation took effect.
   
   ### Suggested Fix Direction
   
   1. **Do not hold the `SubPlan` monitor lock across the retry backoff sleep:**
      - Replace the in-lock `Thread.sleep(pipelineRestoreIntervalSeconds * 
1000)` with a cancellable scheduled delay (e.g., `ScheduledFuture<?>` or 
interruptible wait outside `synchronized (this)`) that `cancelPipeline()` / 
`forceStopPipeline()` immediately cancels/interrupts.
   2. **Re-check `jobMaster.isNeedRestore()` and cancellation state before 
allocating resources or redeploying:**
      - After the backoff completes (and before 
`jobMaster.preApplyResources(this)` and `restorePipeline()`), re-verify 
`jobMaster.isNeedRestore()` and that the job/pipeline is not being 
canceled/stopped. If canceled, complete `subPlanDone(PipelineStatus.CANCELED)` 
and `pipelineFuture` directly with zero extra slot requests.
   3. **Add regression tests:**
      - Add a unit/E2E test canceling a failing job during 
`job.retry.interval.seconds` (e.g., 6s) and asserting both prompt cancellation 
(`<< retry interval`) and `slotRequestSuccessTotal` remaining unchanged after 
`cancelJob` (`extraSlotsAllocatedAfterCancel == 0`).
   
   ### Call for Contributors
   
   This is a clean, well-reproducible Zeta engine state-machine bug with high 
practical impact for production cancellation responsiveness and slot hygiene. 
**Community contributors are very welcome to claim and fix this issue!** Feel 
free to leave a comment below if you'd like to take it on or discuss the 
cancellation-safe backoff design.
   
   ### SeaTunnel Version
   
   dev (`39c74ce48b0`)
   
   ### Are you willing to submit a 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