This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/dev/pr-13487-3fabb5d18c0c8da5eb405e935189e4ac66935cec in repository https://gitbox.apache.org/repos/asf/seatunnel.git
commit 6a6d95458775637cf616bb20329b289dd2f4bed0 Author: Jast <[email protected]> AuthorDate: Sun Nov 29 11:19:56 2026 +0000 [Test][Zeta] Guard that a cancelled queued job is never dispatched (#13487) Co-authored-by: zhangshenghang <[email protected]> --- .../seatunnel/engine/e2e/SeaTunnelSlotIT.java | 261 +++++++++++++++++++++ .../test/resources/cancel_queued_job_stream.conf | 53 +++++ 2 files changed, 314 insertions(+) diff --git a/seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/src/test/java/org/apache/seatunnel/engine/e2e/SeaTunnelSlotIT.java b/seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/src/test/java/org/apache/seatunnel/engine/e2e/SeaTunnelSlotIT.java index 4186b54568f..b76231afba6 100644 --- a/seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/src/test/java/org/apache/seatunnel/engine/e2e/SeaTunnelSlotIT.java +++ b/seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/src/test/java/org/apache/seatunnel/engine/e2e/SeaTunnelSlotIT.java @@ -25,6 +25,7 @@ import org.apache.seatunnel.engine.client.job.ClientJobProxy; import org.apache.seatunnel.engine.common.config.ConfigProvider; import org.apache.seatunnel.engine.common.config.JobConfig; import org.apache.seatunnel.engine.common.config.SeaTunnelConfig; +import org.apache.seatunnel.engine.common.config.server.ScheduleStrategy; import org.apache.seatunnel.engine.common.job.JobStatus; import org.apache.seatunnel.engine.server.SeaTunnelServerStarter; @@ -36,9 +37,23 @@ import com.hazelcast.client.config.ClientConfig; import com.hazelcast.instance.impl.HazelcastInstanceImpl; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; public class SeaTunnelSlotIT { + + /** + * Upper bound for one job cancellation request. {@link ClientJobProxy#cancelJob()} joins the + * client response future without any timeout, so the test has to own that wait: a stalled + * cancellation must fail the assertion with a clear message instead of hanging the suite and + * skipping the client/node shutdown. + */ + private static final long CANCEL_REQUEST_TIMEOUT_SECONDS = 60; + @Test public void testSlotNotEnough() throws Exception { HazelcastInstanceImpl node1 = null; @@ -241,4 +256,250 @@ public class SeaTunnelSlotIT { } } } + + /** + * Guards the finality of cancelling a job that is only queued, not running: under {@link + * ScheduleStrategy#WAIT} a job that does not fit into the slot pool stays in the master's + * pending queue, and cancelling it must remove it from that queue for good. In particular the + * scheduler must never dispatch it later, after the capacity it was waiting for is released. + * + * <p><b>Why this is a boundary with real regression risk.</b> The pending scheduler is a + * long-lived thread that peeks the queue head, evaluates the job's slots through {@code + * JobMaster#preApplyResources} and only then dispatches it (see {@code + * CoordinatorService#pendingJobSchedule}). Cancelling a queued job happens on a different + * thread and only removes the entry from {@code pendingJobQueue} before flipping the {@code + * JobMaster} to {@code CANCELED}. If the scheduler already holds a reserved reference to that + * entry, or if a later iteration re-reads stale queue state, the cancelled job can be + * dispatched anyway once capacity appears: Zeta then reports a job the user cancelled as {@code + * FAILED} (leaving terminal state is rejected) or, worse, deploys its tasks. The window is + * invisible to a client that saw {@code CANCELED}, and it only closes once the cluster has free + * capacity — which in a full fixed-slot cluster can be much later. + * + * <p><b>Why the existing coverage does not reach it.</b> {@code + * CoordinatorServiceWithCancelPendingJobTest#testCancelPendingJob} cancels a hand-injected + * pending {@code JobMaster} and asserts the queue entry and IMaps are gone at that moment, but + * no scheduler runs against real resources in that test, so nothing can be dispatched after the + * cancel and the release-then-dispatch path is never exercised. The pending-job failover E2E ( + * {@code SplitClusterPendingJobLifecycleFailoverIT}) only cancels jobs that are already + * running, and its queue tests keep their queued job alive. {@link #testSlotNotEnough()} and + * {@link #testSlotReleasedAfterNotEnoughResourceFailure()} use the {@code FAIL} strategy, where + * an oversize request never enters the queue. + * + * <p><b>How the contract is pinned.</b> The fixture pipeline needs three slots (one split + * enumerator plus one chain task group per parallelism index), so a fixed three-slot pool lets + * exactly one such job run. The holder fills the pool, the victim is queued and cancelled while + * {@code PENDING}. The test then releases the holder and keeps observing the victim for longer + * than a full scheduler retry cycle: it must stay {@code CANCELED} and must never read a single + * row, which is only true if the scheduler dropped it instead of dispatching it with the freed + * slots. A fresh job submitted afterwards must still start and read rows, which proves the + * cancellation did not break the queue or leak a slot. + */ + @Test + public void testCanceledQueuedJobIsNeverDispatchedAfterCapacityIsReleased() throws Exception { + HazelcastInstanceImpl node1 = null; + SeaTunnelClient engineClient = null; + ClientJobProxy holder = null; + ClientJobProxy canceledQueuedJob = null; + ClientJobProxy follower = null; + // Owns every cancellation request so that a blocked cancel can be abandoned on timeout + // instead of wedging the test thread; the threads are daemons so a cancel that ignores + // interruption can never keep the JVM alive. + ExecutorService cancelExecutor = + Executors.newCachedThreadPool( + runnable -> { + Thread thread = new Thread(runnable, "seatunnel-slot-cancel"); + thread.setDaemon(true); + return thread; + }); + + try { + String testClusterName = + "testCanceledQueuedJobIsNeverDispatchedAfterCapacityIsReleased"; + SeaTunnelConfig seaTunnelConfig = ConfigProvider.locateAndGetSeaTunnelConfig(); + seaTunnelConfig.getHazelcastConfig().setClusterName(testClusterName); + seaTunnelConfig.getEngineConfig().getHttpConfig().setEnabled(false); + seaTunnelConfig.getEngineConfig().getSlotServiceConfig().setDynamicSlot(false); + seaTunnelConfig.getEngineConfig().getSlotServiceConfig().setSlotNum(3); + seaTunnelConfig.getEngineConfig().setScheduleStrategy(ScheduleStrategy.WAIT); + + node1 = SeaTunnelServerStarter.createHazelcastInstance(seaTunnelConfig); + + Common.setDeployMode(DeployMode.CLIENT); + ClientConfig clientConfig = ConfigProvider.locateAndGetClientConfig(); + clientConfig.setClusterName(testClusterName); + engineClient = new SeaTunnelClient(clientConfig); + + holder = submitStreamingJob(engineClient, seaTunnelConfig, "cancel-queued-holder"); + awaitJobStatus(holder, JobStatus.RUNNING); + awaitSourceRowsRead(engineClient, holder.getJobId()); + + canceledQueuedJob = + submitStreamingJob(engineClient, seaTunnelConfig, "cancel-queued-victim"); + final long victimJobId = canceledQueuedJob.getJobId(); + final SeaTunnelClient metricsClient = engineClient; + final ClientJobProxy victim = canceledQueuedJob; + awaitJobStatus(victim, JobStatus.PENDING); + + cancelJobOrFail(cancelExecutor, victim); + awaitJobStatus(victim, JobStatus.CANCELED); + Awaitility.await() + .atMost(30, TimeUnit.SECONDS) + .during(5, TimeUnit.SECONDS) + .untilAsserted( + () -> + Assertions.assertEquals( + JobStatus.CANCELED, + victim.getJobStatus(), + "A cancelled queued job must not be resurrected while" + + " the cluster is still full")); + + cancelJobOrFail(cancelExecutor, holder); + awaitJobStatus(holder, JobStatus.CANCELED); + + // Wait longer than one full 3s scheduler retry cycle with free capacity available: the + // scheduler must never turn the cancelled queue entry into a dispatched job. + Awaitility.await() + .atMost(40, TimeUnit.SECONDS) + .during(10, TimeUnit.SECONDS) + .untilAsserted( + () -> { + Assertions.assertEquals( + JobStatus.CANCELED, + victim.getJobStatus(), + "A cancelled queued job must stay CANCELED after the" + + " capacity it waited for was released"); + Assertions.assertEquals( + 0, + metricsClient + .getJobMetricsSummary(victimJobId) + .getSourceReadCount(), + "A cancelled queued job must never deploy its source"); + }); + + follower = submitStreamingJob(engineClient, seaTunnelConfig, "cancel-queued-follower"); + awaitJobStatus(follower, JobStatus.RUNNING); + awaitSourceRowsRead(engineClient, follower.getJobId()); + cancelJobOrFail(cancelExecutor, follower); + awaitJobStatus(follower, JobStatus.CANCELED); + } finally { + // Teardown must always reach client/node shutdown, even when a cancellation request or + // a previous close fails: hence one best-effort block per step, nested finally blocks. + try { + cancelQuietly(cancelExecutor, holder); + cancelQuietly(cancelExecutor, canceledQueuedJob); + cancelQuietly(cancelExecutor, follower); + } finally { + try { + if (engineClient != null) { + engineClient.close(); + } + } finally { + try { + if (node1 != null) { + node1.shutdown(); + } + } finally { + cancelExecutor.shutdownNow(); + } + } + } + } + } + + private ClientJobProxy submitStreamingJob( + SeaTunnelClient engineClient, SeaTunnelConfig seaTunnelConfig, String jobName) + throws Exception { + JobConfig jobConfig = new JobConfig(); + jobConfig.setName(jobName); + ClientJobExecutionEnvironment jobExecutionEnv = + engineClient.createExecutionContext( + TestUtils.getResource("cancel_queued_job_stream.conf"), + jobConfig, + seaTunnelConfig); + return jobExecutionEnv.execute(); + } + + private void awaitJobStatus(ClientJobProxy jobProxy, JobStatus expectedStatus) { + Awaitility.await() + .atMost(180, TimeUnit.SECONDS) + .untilAsserted( + () -> + Assertions.assertEquals( + expectedStatus, + jobProxy.getJobStatus(), + "Job " + + jobProxy.getJobId() + + " did not reach " + + expectedStatus)); + } + + private void awaitSourceRowsRead(SeaTunnelClient engineClient, long jobId) { + Awaitility.await() + .atMost(120, TimeUnit.SECONDS) + .untilAsserted( + () -> + Assertions.assertTrue( + engineClient + .getJobMetricsSummary(jobId) + .getSourceReadCount() + > 0, + "Job " + + jobId + + " is deployed but its source read no" + + " rows")); + } + + /** + * Cancels a job and requires the request itself to be served within {@link + * #CANCEL_REQUEST_TIMEOUT_SECONDS}. {@link ClientJobProxy#cancelJob()} blocks on the client + * response future with no timeout of its own, so waiting on it directly would skip every status + * assertion and the client/node shutdown; bounding it turns a stalled cancellation into a clear + * test failure, and the abandoned thread is interrupted so it cannot leak. + */ + private void cancelJobOrFail(ExecutorService cancelExecutor, ClientJobProxy jobProxy) + throws InterruptedException { + Assertions.assertNotNull(jobProxy, "The job to cancel must be submitted first"); + Future<?> cancelRequest = cancelExecutor.submit(jobProxy::cancelJob); + try { + cancelRequest.get(CANCEL_REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS); + } catch (TimeoutException e) { + cancelRequest.cancel(true); + Assertions.fail( + "Cancelling job " + + jobProxy.getJobId() + + " did not complete within " + + CANCEL_REQUEST_TIMEOUT_SECONDS + + " seconds, so the regression cannot be observed: a stalled" + + " cancellation must fail the test instead of hanging the suite"); + } catch (ExecutionException e) { + cancelRequest.cancel(true); + Assertions.fail("Cancelling job " + jobProxy.getJobId() + " failed", e.getCause()); + } + } + + /** + * Best-effort cancellation used by teardown: bounded like {@link + * #cancelJobOrFail(ExecutorService, ClientJobProxy)}, but it never throws so it cannot mask the + * failure that brought the test down, and the pending request is always released. + */ + private void cancelQuietly(ExecutorService cancelExecutor, ClientJobProxy jobProxy) { + if (jobProxy == null) { + return; + } + Future<?> cancelRequest = null; + try { + cancelRequest = cancelExecutor.submit(jobProxy::cancelJob); + cancelRequest.get(CANCEL_REQUEST_TIMEOUT_SECONDS, TimeUnit.SECONDS); + } catch (InterruptedException e) { + // The interrupt status is deliberately not restored: teardown still has to close the + // client and stop the node, and this request is being abandoned anyway. + } catch (Exception ignored) { + // The job may already be cancelled, the cluster may already be shut down, or the + // bounded request timed out; cleanup must not replace the original failure. + } finally { + if (cancelRequest != null && !cancelRequest.isDone()) { + cancelRequest.cancel(true); + } + } + } } diff --git a/seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/src/test/resources/cancel_queued_job_stream.conf b/seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/src/test/resources/cancel_queued_job_stream.conf new file mode 100644 index 00000000000..b131aea1567 --- /dev/null +++ b/seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/src/test/resources/cancel_queued_job_stream.conf @@ -0,0 +1,53 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# Job fixture for the queued-job lifecycle test in SeaTunnelSlotIT. An unbounded FakeSource +# feeding a Console sink that discards every row keeps the job running (and holding its slots) +# until the test cancels it, while the source metric proves the deployed tasks really read data. +# Parallelism 2 means the pipeline needs three slots (one split enumerator plus one chain task +# group per parallelism index), so a second identical job cannot be deployed on a cluster whose +# slot pool is already consumed. +env { + parallelism = 2 + job.mode = "STREAMING" + checkpoint.interval = 5000 +} + +source { + FakeSource { + plugin_output = "fake" + row.num = 1000000 + split.num = 4 + split.read-interval = 100 + schema = { + fields { + id = "bigint" + name = "string" + } + } + } +} + +transform { +} + +sink { + Console { + plugin_input = "fake" + log.print.data = false + } +}
