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-12446-efecb179a100d0a621566ba1b0c015357f55c168 in repository https://gitbox.apache.org/repos/asf/seatunnel.git
commit 86bef7694d68fb8c167b79626a30a4236d18208f Author: Daniel <[email protected]> AuthorDate: Sun Nov 22 11:49:20 2026 +0000 [Test][E2E] Bound Flink E2E jobs and capture JVM thread dumps when one hangs (#12446) Co-authored-by: DanielLeens <[email protected]> Co-authored-by: Claude Fable 5.1 <[email protected]> Co-authored-by: David Zollo <[email protected]> --- .../flink/AbstractTestFlinkContainer.java | 140 ++++++++++++++++++++- .../flink/AbstractTestFlinkContainerTest.java | 133 +++++++++++++++++++- 2 files changed, 271 insertions(+), 2 deletions(-) diff --git a/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/container/flink/AbstractTestFlinkContainer.java b/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/container/flink/AbstractTestFlinkContainer.java index 95e05d4f98..9c722df8f4 100644 --- a/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/container/flink/AbstractTestFlinkContainer.java +++ b/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/container/flink/AbstractTestFlinkContainer.java @@ -40,6 +40,12 @@ import java.time.Duration; import java.util.Arrays; import java.util.Collections; import java.util.List; +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; import java.util.stream.Stream; /** @@ -199,11 +205,143 @@ public abstract class AbstractTestFlinkContainer extends AbstractTestContainer { return executeJob(confFile, Collections.emptyList()); } + /** + * Upper bound for one SeaTunnel job submitted through {@link #executeJob}. Every green Flink + * E2E job finishes within minutes, so a job still running after this long has hung. Letting it + * run until the workflow's timeout-minutes kills the whole job discards the only evidence that + * explains the hang, the JVM thread dumps, and burns the rest of the job budget. + */ + private static final long JOB_TIMEOUT_MINUTES = 30; + + /** + * Upper bound, enforced on the host, for one thread dump request. The request is itself a + * {@code docker exec}; if the Docker transport stalls instead of failing, an unbounded request + * would block the test past {@link #JOB_TIMEOUT_MINUTES} and recreate the silent workflow-level + * cancellation this class exists to prevent. The in-container command only needs a few seconds + * (signal the JVMs, then sleep 5 so HotSpot can write the dumps), so a request that misses this + * deadline is abandoned and logged. Stopping the containers in teardown remains the cleanup + * boundary for whatever the abandoned exec left behind. + */ + private static final long THREAD_DUMP_TIMEOUT_SECONDS = 60; + + /** + * Shell command that asks every JVM in a container for a thread dump. SIGQUIT makes HotSpot + * print the dump to the process's own stdout, which testcontainers already forwards into the + * test log, so this works on images without jstack or jcmd. The JVMs are found through /proc + * because the Flink images do not ship pgrep. + */ + private static final String THREAD_DUMP_COMMAND = + "for proc in /proc/[0-9]*; do" + + " if [ \"$(cat \"$proc/comm\" 2>/dev/null)\" = java ]; then" + + " kill -QUIT \"${proc#/proc/}\"; fi; done; sleep 5"; + @Override public Container.ExecResult executeJob(String confFile, List<String> variables) throws IOException, InterruptedException { log.info("test in container: {}", identifier()); - return executeJob(jobManager, confFile, null, variables); + ExecutorService executor = newDaemonExecutor("flink-e2e-job-" + identifier()); + Future<Container.ExecResult> result = + executor.submit(() -> executeJob(jobManager, confFile, null, variables)); + try { + return result.get(getJobTimeout().toMillis(), TimeUnit.MILLISECONDS); + } catch (TimeoutException e) { + // Capture the state of both JVMs before anything is torn down. Each request is bounded + // on the host (see THREAD_DUMP_TIMEOUT_SECONDS), so a stalled first request cannot + // prevent the second one or the timeout failure below. + dumpJvmThreads("jobmanager", jobManager); + dumpJvmThreads("taskmanager", taskManager); + result.cancel(true); + throw new IllegalStateException( + "SeaTunnel job " + + confFile + + " did not finish within " + + getJobTimeout().toMinutes() + + " minutes on " + + identifier() + + "; thread dumps of the JobManager and TaskManager JVMs were" + + " requested and are in their container logs above", + e); + } catch (ExecutionException e) { + Throwable cause = e.getCause(); + if (cause instanceof IOException) { + throw (IOException) cause; + } + if (cause instanceof InterruptedException) { + throw (InterruptedException) cause; + } + if (cause instanceof RuntimeException) { + throw (RuntimeException) cause; + } + throw new IllegalStateException(cause); + } finally { + executor.shutdownNow(); + } + } + + /** + * How long {@link #executeJob} waits for the job before treating it as hung. Overridden by unit + * tests so the timeout path can be exercised without a real 30-minute job. + */ + Duration getJobTimeout() { + return Duration.ofMinutes(JOB_TIMEOUT_MINUTES); + } + + /** + * How long {@link #dumpJvmThreads} waits for one thread dump request on the host side. + * Overridden by unit tests so a stalled request can be simulated within a test budget. + */ + Duration getThreadDumpTimeout() { + return Duration.ofSeconds(THREAD_DUMP_TIMEOUT_SECONDS); + } + + /** + * Requests thread dumps from every JVM in the container and waits at most {@link + * #getThreadDumpTimeout()} for the request to return. Failures and timeouts are only logged: + * the dumps are diagnostics for the job timeout, and must never replace or delay it. + */ + private void dumpJvmThreads(String role, GenericContainer<?> container) { + ExecutorService executor = newDaemonExecutor("flink-e2e-thread-dump-" + role); + Future<Container.ExecResult> dump = + executor.submit(() -> container.execInContainer("bash", "-c", THREAD_DUMP_COMMAND)); + try { + Container.ExecResult result = + dump.get(getThreadDumpTimeout().toMillis(), TimeUnit.MILLISECONDS); + log.warn( + "Requested thread dumps from the {} JVMs (exit code {}) {}{}", + role, + result.getExitCode(), + result.getStdout(), + result.getStderr()); + } catch (TimeoutException e) { + log.warn( + "Thread dump request to the {} JVMs did not return within {}; abandoning it so" + + " the job timeout can still be reported", + role, + getThreadDumpTimeout()); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + log.warn("Interrupted while requesting thread dumps from the {} JVMs", role, e); + } catch (Exception e) { + log.warn("Could not request thread dumps from the {} JVMs", role, e); + } finally { + // Also interrupts a request that missed the deadline, so the daemon thread does not + // keep a stalled docker exec alive for the rest of the JVM's life. + dump.cancel(true); + executor.shutdownNow(); + } + } + + /** + * Daemon threads keep a stalled docker exec from pinning the JVM after the test has already + * failed; the surefire/failsafe fork must still be able to exit. + */ + private static ExecutorService newDaemonExecutor(String threadName) { + return Executors.newSingleThreadExecutor( + runnable -> { + Thread thread = new Thread(runnable, threadName); + thread.setDaemon(true); + return thread; + }); } @Override diff --git a/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/container/flink/AbstractTestFlinkContainerTest.java b/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/container/flink/AbstractTestFlinkContainerTest.java index 77ca4e9c02..93a6b5ca57 100644 --- a/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/container/flink/AbstractTestFlinkContainerTest.java +++ b/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/container/flink/AbstractTestFlinkContainerTest.java @@ -23,8 +23,17 @@ import org.mockito.Mockito; import org.testcontainers.containers.Container; import org.testcontainers.containers.GenericContainer; +import java.io.IOException; +import java.time.Duration; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeoutException; + class AbstractTestFlinkContainerTest { + /** Generous host-side budget for the whole timeout path; the stubbed deadlines are 200 ms. */ + private static final Duration TEST_BUDGET = Duration.ofSeconds(10); + /** * A TaskManager that failed to start must not stop the JobManager from being stopped. A leaked * JobManager keeps the {@code jobmanager} alias on the shared network, so the TaskManager of @@ -123,6 +132,98 @@ class AbstractTestFlinkContainerTest { Assertions.assertTrue(container.hostVolumeDeleted); } + /** + * The job deadline and the thread dump deadline must both be enforced on the host. The job + * never returns, the JobManager dump request stalls forever and the TaskManager dump request + * fails, which is the worst case for a stalled Docker transport. The test fails when the job + * deadline is removed, when the stalled dump is not bounded (the whole call would hang past the + * test budget), when the TaskManager dump is skipped after the JobManager dump stalled, or when + * a failing dump replaces the original timeout failure. + */ + @Test + void shouldFailAfterJobDeadlineAndBoundEachThreadDumpRequest() throws Exception { + GenericContainer<?> jobManager = runningContainer(); + GenericContainer<?> taskManager = runningContainer(); + CountDownLatch never = new CountDownLatch(1); + Mockito.when( + jobManager.execInContainer( + Mockito.eq("bash"), Mockito.eq("-c"), Mockito.anyString())) + .thenAnswer( + invocation -> { + never.await(); + return null; + }); + Mockito.when( + taskManager.execInContainer( + Mockito.eq("bash"), Mockito.eq("-c"), Mockito.anyString())) + .thenThrow(new IOException("docker exec failed")); + TestFlinkContainer container = new TestFlinkContainer(jobManager, taskManager); + container.jobSubmission = + () -> { + never.await(); + return null; + }; + + IllegalStateException thrown = + Assertions.assertTimeoutPreemptively( + TEST_BUDGET, + () -> + Assertions.assertThrows( + IllegalStateException.class, + () -> container.executeJob("hang.conf"))); + + Assertions.assertTrue( + thrown.getCause() instanceof TimeoutException, String.valueOf(thrown.getCause())); + Assertions.assertTrue(thrown.getMessage().contains("hang.conf"), thrown.getMessage()); + Mockito.verify(jobManager) + .execInContainer( + Mockito.eq("bash"), Mockito.eq("-c"), Mockito.contains("kill -QUIT")); + Mockito.verify(taskManager) + .execInContainer( + Mockito.eq("bash"), Mockito.eq("-c"), Mockito.contains("kill -QUIT")); + } + + /** A job that finishes in time returns its own result and triggers no diagnostics. */ + @Test + void shouldReturnJobResultWithoutThreadDumpsWhenJobFinishesInTime() throws Exception { + GenericContainer<?> jobManager = runningContainer(); + GenericContainer<?> taskManager = runningContainer(); + TestFlinkContainer container = new TestFlinkContainer(jobManager, taskManager); + Container.ExecResult result = execResult(0); + container.jobSubmission = () -> result; + + Assertions.assertSame(result, container.executeJob("finishes.conf")); + + Mockito.verify(jobManager, Mockito.never()) + .execInContainer(Mockito.eq("bash"), Mockito.eq("-c"), Mockito.anyString()); + Mockito.verify(taskManager, Mockito.never()) + .execInContainer(Mockito.eq("bash"), Mockito.eq("-c"), Mockito.anyString()); + } + + /** + * Running the submission on another thread must not change what callers see: the original + * exception instance is rethrown, not an ExecutionException or a wrapper around it. + */ + @Test + void shouldRethrowJobFailureUnchanged() throws Exception { + GenericContainer<?> jobManager = runningContainer(); + GenericContainer<?> taskManager = runningContainer(); + TestFlinkContainer container = new TestFlinkContainer(jobManager, taskManager); + IOException failure = new IOException("submission failed"); + container.jobSubmission = + () -> { + throw failure; + }; + + IOException thrown = + Assertions.assertThrows( + IOException.class, () -> container.executeJob("fails.conf")); + + Assertions.assertSame(failure, thrown); + Mockito.verify(jobManager, Mockito.never()) + .execInContainer(Mockito.eq("bash"), Mockito.eq("-c"), Mockito.anyString()); + } + private static GenericContainer<?> runningContainer() throws Exception { GenericContainer<?> container = Mockito.mock(GenericContainer.class); Mockito.when(container.isRunning()).thenReturn(true); @@ -138,13 +239,23 @@ class AbstractTestFlinkContainerTest { return result; } - /** Uses mocked containers and records the host cleanup instead of deleting a real path. */ + /** Stands in for the docker exec that submits a job, so a test can finish, fail or hang it. */ + private interface JobSubmission { + Container.ExecResult submit() throws IOException, InterruptedException; + } + + /** + * Uses mocked containers and records the host cleanup instead of deleting a real path. The job + * and thread dump deadlines are shortened so the timeout path runs within a unit test. + */ private static class TestFlinkContainer extends Flink18Container { static final String VOLUME = CONTAINER_VOLUME_MOUNT_PATH; boolean hostVolumeDeleted; + JobSubmission jobSubmission; + TestFlinkContainer(GenericContainer<?> jobManager, GenericContainer<?> taskManager) { this.jobManager = jobManager; this.taskManager = taskManager; @@ -154,5 +265,25 @@ class AbstractTestFlinkContainerTest { protected void deleteHostVolumeMountPath() { hostVolumeDeleted = true; } + + @Override + protected Container.ExecResult executeJob( + GenericContainer<?> container, + String confFile, + String jobId, + List<String> variables) + throws IOException, InterruptedException { + return jobSubmission.submit(); + } + + @Override + Duration getJobTimeout() { + return Duration.ofMillis(200); + } + + @Override + Duration getThreadDumpTimeout() { + return Duration.ofMillis(200); + } } }
