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-12149-93328e4a2e0ecb08d943c30b56ecb0c5a9cfeda1 in repository https://gitbox.apache.org/repos/asf/seatunnel.git
commit 2847c8c5c7cea5ead83b0a2093dde8f36cee32e4 Author: Yohanes <[email protected]> AuthorDate: Tue Nov 24 12:55:44 2026 +0000 [Fix][Zeta] Stop checkpoint scheduler after terminal cleanup (#12149) Co-authored-by: Yohanes <[email protected]> Co-authored-by: David Zollo <[email protected]> Co-authored-by: zhiweiniu <[email protected]> --- .../e2e/CheckpointCoordinatorFailoverIT.java | 26 ++++ .../server/checkpoint/CheckpointCoordinator.java | 159 ++++++++++++--------- .../checkpoint/CheckpointCoordinatorTest.java | 64 +++++++++ 3 files changed, 184 insertions(+), 65 deletions(-) diff --git a/seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/src/test/java/org/apache/seatunnel/engine/e2e/CheckpointCoordinatorFailoverIT.java b/seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/src/test/java/org/apache/seatunnel/engine/e2e/CheckpointCoordinatorFailoverIT.java index 3085426fb6..8ff4adaa47 100644 --- a/seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/src/test/java/org/apache/seatunnel/engine/e2e/CheckpointCoordinatorFailoverIT.java +++ b/seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/src/test/java/org/apache/seatunnel/engine/e2e/CheckpointCoordinatorFailoverIT.java @@ -69,6 +69,7 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; @@ -379,9 +380,34 @@ public class CheckpointCoordinatorFailoverIT { + " one pipeline (pipelineId=%d, before=%d, after=%d)", observedPipelineId.get(), ckIdBefore.get(), ckIdAfter.get())); + CheckpointCoordinator recoveredCoordinator = + getJobMaster(masterNode2, jobId) + .getCheckpointManager() + .getCheckpointCoordinator(observedPipelineId.get()); + Assertions.assertNotNull(recoveredCoordinator); clientJobProxy.cancelJob(); // Recovery must also resolve cancellation through the job completion future. Assertions.assertEquals(JobStatus.CANCELED, clientJobProxy.waitForJobComplete()); + // Check before member shutdown, which would otherwise hide a replacement scheduler. + Awaitility.await() + .atMost(10, TimeUnit.SECONDS) + .untilAsserted( + () -> { + ScheduledExecutorService scheduler = + (ScheduledExecutorService) + ReflectionUtils.getField( + recoveredCoordinator, "scheduler") + .orElseThrow( + () -> + new IllegalStateException( + "Missing checkpoint scheduler")); + Assertions.assertTrue( + scheduler.isShutdown(), + "A canceled recovered pipeline must not recreate its scheduler"); + Assertions.assertTrue( + scheduler.isTerminated(), + "All recovered checkpoint scheduler workers must exit after cancellation"); + }); } finally { if (engineClient != null) { engineClient.close(); diff --git a/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/checkpoint/CheckpointCoordinator.java b/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/checkpoint/CheckpointCoordinator.java index 2c8bab5a88..ecb4e2c6fc 100644 --- a/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/checkpoint/CheckpointCoordinator.java +++ b/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/checkpoint/CheckpointCoordinator.java @@ -71,7 +71,7 @@ import java.util.concurrent.CompletionException; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -238,18 +238,7 @@ public class CheckpointCoordinator { this.pendingCheckpoints = new ConcurrentHashMap<>(); this.completedCheckpointIds = new ArrayDeque<>(coordinatorConfig.getStorage().getMaxRetainedCheckpoints() + 1); - this.scheduler = - Executors.newScheduledThreadPool( - 2, - runnable -> { - Thread thread = new Thread(runnable); - thread.setName( - String.format( - "checkpoint-coordinator-%s/%s", pipelineId, jobId)); - return thread; - }); - ((ScheduledThreadPoolExecutor) this.scheduler).setRemoveOnCancelPolicy(true); - this.scheduler = MDCTracer.tracing(scheduler); + this.scheduler = createScheduler(); this.serializer = new ProtoStuffSerializer(); this.pipelineTasks = getPipelineTasks(plan.getPipelineSubtasks()); this.actionParallelism = getActionParallelism(plan.getSubtaskActions()); @@ -299,6 +288,28 @@ public class CheckpointCoordinator { } } + /** + * Builds a scheduler with capacity for two workers, started lazily when tasks are scheduled. + * + * <p>Only called from the constructor and from a {@code CHECKPOINT_COORDINATOR_RESET} cleanup. + * Non-reset cleanup leaves the scheduler shut down until {@link #restoreCoordinator(boolean)} + * revives it for pipeline restore or master failover. + */ + private ScheduledExecutorService createScheduler() { + ScheduledThreadPoolExecutor executor = + new ScheduledThreadPoolExecutor( + 2, + runnable -> { + Thread thread = new Thread(runnable); + thread.setName( + String.format( + "checkpoint-coordinator-%s/%s", pipelineId, jobId)); + return thread; + }); + executor.setRemoveOnCancelPolicy(true); + return MDCTracer.tracing(executor); + } + public int getPipelineId() { return pipelineId; } @@ -549,23 +560,32 @@ public class CheckpointCoordinator { @VisibleForTesting protected void scheduleTriggerPendingCheckpoint( CheckpointType checkpointType, long delayMills) { - scheduler.schedule( - () -> { - try { - tryTriggerPendingCheckpoint(checkpointType); - } catch (Throwable e) { - // A thrown trigger would otherwise drop the scheduled checkpoint chain - // silently; fail the coordinator so the job can handle the error. - handleCoordinatorError( - String.format( - "Failed to trigger %s for job id: %s, pipeline id: %s", - checkpointType, jobId, pipelineId), - e, - CheckpointCloseReason.CHECKPOINT_INSIDE_ERROR); - } - }, - delayMills, - TimeUnit.MILLISECONDS); + try { + scheduler.schedule( + () -> { + try { + tryTriggerPendingCheckpoint(checkpointType); + } catch (Throwable e) { + // A thrown trigger would otherwise drop the scheduled checkpoint chain + // silently; fail the coordinator so the job can handle the error. + handleCoordinatorError( + String.format( + "Failed to trigger %s for job id: %s, pipeline id: %s", + checkpointType, jobId, pipelineId), + e, + CheckpointCloseReason.CHECKPOINT_INSIDE_ERROR); + } + }, + delayMills, + TimeUnit.MILLISECONDS); + } catch (RejectedExecutionException e) { + // Terminal cleanup keeps the scheduler shut down; a concurrent trigger may arrive late. + LOG.info( + "Job {} pipeline {} scheduler is already shut down, skip scheduling a {} checkpoint.", + jobId, + pipelineId, + checkpointType); + } } /** @@ -1061,26 +1081,38 @@ public class CheckpointCoordinator { checkpointTimeout = coordinatorConfig.getSchemaChangeCheckpointTimeout(); } - pendingCheckpoint.setCheckpointTimeOutFuture( - scheduler.schedule( - () -> { - // If any task is not acked within the checkpoint - // timeout - if (pendingCheckpoints.get( - pendingCheckpoint - .getCheckpointId()) - != null - && !pendingCheckpoint.isFullyAcknowledged()) { - LOG.info( - "timeout checkpoint: {}", - pendingCheckpoint.getInfo()); - handleCoordinatorError( - CheckpointCloseReason.CHECKPOINT_EXPIRED, - null); - } - }, - checkpointTimeout, - TimeUnit.MILLISECONDS)); + try { + pendingCheckpoint.setCheckpointTimeOutFuture( + scheduler.schedule( + () -> { + // If any task is not acked within the checkpoint + // timeout + if (pendingCheckpoints.get( + pendingCheckpoint + .getCheckpointId()) + != null + && !pendingCheckpoint + .isFullyAcknowledged()) { + LOG.info( + "timeout checkpoint: {}", + pendingCheckpoint.getInfo()); + handleCoordinatorError( + CheckpointCloseReason + .CHECKPOINT_EXPIRED, + null); + } + }, + checkpointTimeout, + TimeUnit.MILLISECONDS)); + } catch (RejectedExecutionException e) { + // Terminal cleanup may finish while the asynchronous barrier RPC is in + // flight. + LOG.info( + "Job {} pipeline {} scheduler is already shut down, skip arming timeout for checkpoint {}.", + jobId, + pipelineId, + pendingCheckpoint.getCheckpointId()); + } } }); pendingCounter.incrementAndGet(); @@ -1231,14 +1263,15 @@ public class CheckpointCoordinator { * caused by a coordinator reset) * <li>Clearing all internal tracking structures * <li>Resetting counters and schema change flags - * <li>Stopping and recreating the scheduler thread pool + * <li>Stopping the scheduler thread pool; recreating it only for {@code + * CHECKPOINT_COORDINATOR_RESET} * </ul> * * <p>If the close reason is {@code CHECKPOINT_COORDINATOR_RESET}, the monitor service will * clear all in-progress checkpoint metadata without reporting them as failures. * - * <p>This method ensures that no residual checkpoint state remains in memory and that the - * coordinator is ready for a clean restart if necessary. + * <p>This method clears residual checkpoint state. Only reset cleanup prepares the scheduler + * for a restart; other close reasons leave it shut down until {@code restoreCoordinator} runs. * * @param closedReason the reason why pending checkpoints are being closed; determines how * monitoring and cleanup are handled @@ -1278,22 +1311,18 @@ public class CheckpointCoordinator { pendingCounter.set(0); schemaChanging.set(false); // Only remove the persisted ready-to-close state when the coordinator truly ends - // (completed/failed/cancelled). During a reset (master failover), the IMap entry - // must be preserved so restoreCoordinator() can recover from it. + // (completed/failed/cancelled). During pipeline restore or master failover, RESET + // preserves the IMap entry so restoreCoordinator() can recover from it. if (closedReason != CheckpointCloseReason.CHECKPOINT_COORDINATOR_RESET) { runningJobStateIMap.remove(readyToCloseImapKey); } scheduler.shutdownNow(); - scheduler = - Executors.newScheduledThreadPool( - 2, - runnable -> { - Thread thread = new Thread(runnable); - thread.setName( - String.format( - "checkpoint-coordinator-%s/%s", pipelineId, jobId)); - return thread; - }); + // Non-reset cleanup leaves the scheduler shut down, so late callbacks cannot start + // workers on a replacement after the pipeline ends. restoreCoordinator() revives it + // through RESET for either pipeline restore or master failover. + if (closedReason == CheckpointCloseReason.CHECKPOINT_COORDINATOR_RESET) { + scheduler = createScheduler(); + } } if (checkpointMonitorService != null && closedReason == CheckpointCloseReason.CHECKPOINT_COORDINATOR_RESET) { diff --git a/seatunnel-engine/seatunnel-engine-server/src/test/java/org/apache/seatunnel/engine/server/checkpoint/CheckpointCoordinatorTest.java b/seatunnel-engine/seatunnel-engine-server/src/test/java/org/apache/seatunnel/engine/server/checkpoint/CheckpointCoordinatorTest.java index a37f2cdcf7..b3e8898b64 100644 --- a/seatunnel-engine/seatunnel-engine-server/src/test/java/org/apache/seatunnel/engine/server/checkpoint/CheckpointCoordinatorTest.java +++ b/seatunnel-engine/seatunnel-engine-server/src/test/java/org/apache/seatunnel/engine/server/checkpoint/CheckpointCoordinatorTest.java @@ -67,6 +67,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; @@ -1135,6 +1136,69 @@ public class CheckpointCoordinatorTest } } + @Test + void testCancelDoesNotRecreateScheduler() { + ExecutorService executorService = Executors.newCachedThreadPool(); + CheckpointCoordinator coordinator = null; + try { + coordinator = buildMinimalCoordinator(executorService); + coordinator.cancelCheckpoint().join(); + + ScheduledExecutorService scheduler = + (ScheduledExecutorService) + ReflectionUtils.getField(coordinator, "scheduler") + .orElseThrow( + () -> + new IllegalStateException( + "scheduler field not found")); + Assertions.assertTrue( + scheduler.isShutdown(), + "terminal cleanup must not replace the shutdown checkpoint scheduler"); + } finally { + try { + if (coordinator != null) { + coordinator.cancelCheckpoint().join(); + } + } finally { + executorService.shutdownNow(); + } + } + } + + /** + * Pipeline restore and master failover both revive the scheduler through {@code + * CHECKPOINT_COORDINATOR_RESET}; reset cleanup must leave a live scheduler. + */ + @Test + void testResetRecreatesLiveScheduler() { + ExecutorService executorService = Executors.newCachedThreadPool(); + CheckpointCoordinator coordinator = null; + try { + coordinator = buildMinimalCoordinator(executorService); + + coordinator.restoreCoordinator(true); + + ScheduledExecutorService scheduler = + (ScheduledExecutorService) + ReflectionUtils.getField(coordinator, "scheduler") + .orElseThrow( + () -> + new IllegalStateException( + "scheduler field not found")); + Assertions.assertFalse( + scheduler.isShutdown(), + "a coordinator reset must leave a schedulable checkpoint scheduler behind"); + } finally { + try { + if (coordinator != null) { + coordinator.cancelCheckpoint().join(); + } + } finally { + executorService.shutdownNow(); + } + } + } + /** * Regression: when {@code notifyCompleted()} fails (returns {@code false}), {@code * completePendingCheckpoint} must return immediately without decrementing {@code
