Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.stream.Stream;

import static com.hazelcast.jet.impl.util.ExceptionUtil.withTryCatch;
Expand Down Expand Up @@ -443,6 +444,8 @@ public TaskDeployState deployTask(@NonNull TaskGroupImmutableInformation taskImm
"received deploying task executionId [%s]",
taskImmutableInfo.getExecutionId()));
TaskGroup taskGroup = null;
// References owned by this deployment attempt until TaskGroupContext is published.
List<Collection<URL>> acquiredClassLoaderJars = new ArrayList<>();
try {
List<Set<ConnectorJarIdentifier>> connectorJarIdentifiersList =
taskImmutableInfo.getConnectorJarIdentifiers();
Expand All @@ -465,9 +468,11 @@ public TaskDeployState deployTask(@NonNull TaskGroupImmutableInformation taskImm
} else if (!CollectionUtils.isEmpty(taskImmutableInfo.getJars().get(i))) {
jars = taskImmutableInfo.getJars().get(i);
}
List<URL> classLoaderJars = Lists.newArrayList(jars);
ClassLoader classLoader =
classLoaderService.getClassLoader(
taskImmutableInfo.getJobId(), Lists.newArrayList(jars));
taskImmutableInfo.getJobId(), classLoaderJars);
acquiredClassLoaderJars.add(classLoaderJars);
Task task;
if (jars.isEmpty()) {
task = nodeEngine.getSerializationService().toObject(taskData.get(i));
Expand All @@ -480,7 +485,7 @@ public TaskDeployState deployTask(@NonNull TaskGroupImmutableInformation taskImm
}
tasks.add(task);
classLoaders.put(task.getTaskID(), classLoader);
taskJars.put(task.getTaskID(), jars);
taskJars.put(task.getTaskID(), classLoaderJars);
}
taskGroup =
TaskGroupUtils.createTaskGroup(
Expand Down Expand Up @@ -512,12 +517,30 @@ public TaskDeployState deployTask(@NonNull TaskGroupImmutableInformation taskImm
classLoaderService.releaseClassLoader(
taskImmutableInfo.getJobId(), entry.getValue());
}
acquiredClassLoaderJars.clear();
return TaskDeployState.success();
}
deployLocalTask(taskGroup, classLoaders, taskJars);
AtomicBoolean classLoaderOwnershipTransferred = new AtomicBoolean();
deployLocalTask(
taskGroup,
classLoaders,
taskJars,
() -> classLoaderOwnershipTransferred.set(true),
failure -> {
releaseClassLoadersAfterFailedDeployment(
taskImmutableInfo.getJobId(), acquiredClassLoaderJars, failure);
acquiredClassLoaderJars.clear();
});
// Publication is a monotonic ownership transfer. The context may already have
// completed and left executionContexts by the time deployment returns.
if (classLoaderOwnershipTransferred.get()) {
acquiredClassLoaderJars.clear();
}
return TaskDeployState.success();
}
} catch (Throwable t) {
releaseClassLoadersAfterFailedDeployment(
taskImmutableInfo.getJobId(), acquiredClassLoaderJars, t);
logger.severe(
String.format(
"TaskGroupID : %s deploy error with Exception: %s",
Expand All @@ -529,6 +552,30 @@ public TaskDeployState deployTask(@NonNull TaskGroupImmutableInformation taskImm
}
}

/**
* Releases classloader references acquired by the current deployment attempt.
*
* <p>This cleanup only releases references that have not been transferred to a published {@link
* TaskGroupContext}. Cleanup failures are added to the deployment failure so all references are
* attempted without replacing the original error.
*/
private void releaseClassLoadersAfterFailedDeployment(
long jobId,
List<Collection<URL>> acquiredClassLoaderJars,
Throwable deploymentFailure) {
// Release in reverse acquisition order, matching the ownership stack built above.
for (int i = acquiredClassLoaderJars.size() - 1; i >= 0; i--) {
Collection<URL> jars = acquiredClassLoaderJars.get(i);
try {
classLoaderService.releaseClassLoader(jobId, jars);
} catch (Throwable cleanupFailure) {
deploymentFailure.addSuppressed(cleanupFailure);
logger.severe(
"Release classloader after failed task deployment failed", cleanupFailure);
}
}
}

/**
* Deploys a task group locally. This method initializes the task group, creates execution
* contexts, and submits tasks for execution based on the configured thread share mode.
Expand All @@ -542,7 +589,42 @@ public PassiveCompletableFuture<TaskExecutionState> deployLocalTask(
@NonNull TaskGroup taskGroup,
@NonNull ConcurrentHashMap<Long, ClassLoader> classLoaders,
ConcurrentHashMap<Long, Collection<URL>> jars) {
return deployLocalTask(taskGroup, classLoaders, jars, () -> {}, failure -> {});
}

private PassiveCompletableFuture<TaskExecutionState> deployLocalTask(
@NonNull TaskGroup taskGroup,
@NonNull ConcurrentHashMap<Long, ClassLoader> classLoaders,
ConcurrentHashMap<Long, Collection<URL>> jars,
Runnable onContextPublished,
Consumer<Throwable> onFailureBeforeContextPublished) {
CompletableFuture<TaskExecutionState> resultFuture = new CompletableFuture<>();
resultFuture.whenCompleteAsync(
withTryCatch(
logger,
(r, s) -> {
if (s != null) {
logger.severe(
String.format(
"Task %s complete with error %s",
taskGroup.getTaskGroupLocation(),
ExceptionUtils.getMessage(s)));
}
if (r == null) {
r =
new TaskExecutionState(
taskGroup.getTaskGroupLocation(),
ExecutionState.FAILED,
s);
}
logger.info(
String.format(
"Task %s complete with state %s",
r.getTaskGroupLocation(), r.getExecutionState()));
notifyTaskStatusToMaster(taskGroup.getTaskGroupLocation(), r);
}),
MDCTracer.tracing(executorService));
boolean contextPublished = false;
try {
taskGroup.init();
logger.info(
Expand Down Expand Up @@ -587,6 +669,8 @@ public PassiveCompletableFuture<TaskExecutionState> deployLocalTask(
executionContexts.put(
taskGroup.getTaskGroupLocation(),
new TaskGroupContext(taskGroup, classLoaders, jars));
contextPublished = true;
onContextPublished.run();
cancellationFutures.put(taskGroup.getTaskGroupLocation(), cancellationFuture);
submitThreadShareTask(executionTracker, byCooperation.get(true));
submitBlockingTask(executionTracker, byCooperation.get(false));
Expand All @@ -596,33 +680,11 @@ public PassiveCompletableFuture<TaskExecutionState> deployLocalTask(
"deploying TaskGroup %s success", taskGroup.getTaskGroupLocation()));
} catch (Throwable t) {
logger.severe(ExceptionUtils.getMessage(t));
if (!contextPublished) {
onFailureBeforeContextPublished.accept(t);
}
resultFuture.completeExceptionally(t);
}
resultFuture.whenCompleteAsync(
withTryCatch(
logger,
(r, s) -> {
if (s != null) {
logger.severe(
String.format(
"Task %s complete with error %s",
taskGroup.getTaskGroupLocation(),
ExceptionUtils.getMessage(s)));
}
if (r == null) {
r =
new TaskExecutionState(
taskGroup.getTaskGroupLocation(),
ExecutionState.FAILED,
s);
}
logger.info(
String.format(
"Task %s complete with state %s",
r.getTaskGroupLocation(), r.getExecutionState()));
notifyTaskStatusToMaster(taskGroup.getTaskGroupLocation(), r);
}),
MDCTracer.tracing(executorService));
return new PassiveCompletableFuture<>(resultFuture);
}

Expand All @@ -633,7 +695,7 @@ public PassiveCompletableFuture<TaskExecutionState> deployLocalTask(
* @param taskGroupLocation the location of the task group
* @param taskExecutionState the execution state to report
*/
private void notifyTaskStatusToMaster(
void notifyTaskStatusToMaster(
TaskGroupLocation taskGroupLocation, TaskExecutionState taskExecutionState) {
long sleepTime = 1000;
boolean notifyStateSuccess = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@

import org.apache.seatunnel.engine.common.utils.PassiveCompletableFuture;
import org.apache.seatunnel.engine.common.utils.concurrent.CompletableFuture;
import org.apache.seatunnel.engine.core.classloader.DefaultClassLoaderService;
import org.apache.seatunnel.engine.server.exception.TaskGroupContextNotFoundException;
import org.apache.seatunnel.engine.server.execution.BlockTask;
import org.apache.seatunnel.engine.server.execution.ExceptionTestTask;
import org.apache.seatunnel.engine.server.execution.FixedCallTestTimeTask;
import org.apache.seatunnel.engine.server.execution.ProgressState;
import org.apache.seatunnel.engine.server.execution.StopTimeTestTask;
import org.apache.seatunnel.engine.server.execution.Task;
import org.apache.seatunnel.engine.server.execution.TaskDeployState;
import org.apache.seatunnel.engine.server.execution.TaskExecutionContext;
import org.apache.seatunnel.engine.server.execution.TaskExecutionState;
import org.apache.seatunnel.engine.server.execution.TaskGroup;
import org.apache.seatunnel.engine.server.execution.TaskGroupContext;
Expand All @@ -41,6 +45,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import com.hazelcast.flakeidgen.FlakeIdGenerator;
import com.hazelcast.internal.serialization.Data;
Expand All @@ -54,6 +59,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ScheduledFuture;
Expand Down Expand Up @@ -234,6 +240,101 @@ public void testClassloaderSplit() throws IOException {
console.delete();
}

/**
* Verifies that a partially constructed task group does not retain classloader references when
* a later task cannot be deserialized.
*/
@Test
public void testDeployTaskReleasesClassLoadersWhenDeserializationFails() throws IOException {
TaskExecutionService taskExecutionService = server.getTaskExecutionService();
DefaultClassLoaderService classLoaderService =
(DefaultClassLoaderService) server.getClassLoaderService();

File testJar = File.createTempFile("failed-deployment", ".jar");
testJar.deleteOnExit();
URL testJarUrl = testJar.toURI().toURL();
Set<URL> testJars = Collections.singleton(testJarUrl);
long testJobId = System.currentTimeMillis();
TestTask validTask = new TestTask(new AtomicBoolean(false), 300, true);
TaskGroupImmutableInformation taskGroupImmutableInformation =
new TaskGroupImmutableInformation(
testJobId,
1,
TaskGroupType.INTERMEDIATE_BLOCKING_QUEUE,
new TaskGroupLocation(testJobId, 1, 1),
"testDeployTaskReleasesClassLoadersWhenDeserializationFails",
Arrays.asList(
nodeEngine.getSerializationService().toData(validTask),
nodeEngine.getSerializationService().toData("not a task")),
Arrays.asList(testJars, testJars),
Arrays.asList(emptySet(), emptySet()));

TaskDeployState taskDeployState =
taskExecutionService.deployTask(taskGroupImmutableInformation);

Assertions.assertFalse(taskDeployState.isSuccess());
Assertions.assertThrows(
TaskGroupContextNotFoundException.class,
() ->
taskExecutionService.getActiveExecutionContext(
taskGroupImmutableInformation.getTaskGroupLocation()));
Assertions.assertTrue(
classLoaderService.queryClassLoaderById(testJobId, testJars).isPresent());
Assertions.assertEquals(
0, classLoaderService.queryClassLoaderReferenceCount(testJobId, testJars));
testJar.delete();
}

/**
* Verifies that a failure before context publication releases the acquired classloader and is
* still reported to the master.
*/
@Test
public void testDeployTaskHandlesFailureBeforeContextPublication() throws IOException {
TaskExecutionService taskExecutionService = Mockito.spy(server.getTaskExecutionService());
Mockito.doNothing()
.when(taskExecutionService)
.notifyTaskStatusToMaster(Mockito.any(), Mockito.any());
DefaultClassLoaderService classLoaderService =
(DefaultClassLoaderService) server.getClassLoaderService();

File testJar = File.createTempFile("failed-context-publication", ".jar");
testJar.deleteOnExit();
URL testJarUrl = testJar.toURI().toURL();
Set<URL> testJars = Collections.singleton(testJarUrl);
long testJobId = System.currentTimeMillis();
TaskGroupLocation location = new TaskGroupLocation(testJobId, 1, 1);
Task task = new ContextInitializationFailureTask();
TaskGroupImmutableInformation taskGroupImmutableInformation =
new TaskGroupImmutableInformation(
testJobId,
1,
TaskGroupType.DEFAULT,
location,
"testDeployTaskHandlesFailureBeforeContextPublication",
Collections.singletonList(
nodeEngine.getSerializationService().toData(task)),
Collections.singletonList(testJars),
Collections.singletonList(emptySet()));

TaskDeployState taskDeployState =
taskExecutionService.deployTask(taskGroupImmutableInformation);

Assertions.assertEquals(TaskDeployState.success(), taskDeployState);
Assertions.assertThrows(
TaskGroupContextNotFoundException.class,
() -> taskExecutionService.getActiveExecutionContext(location));
Assertions.assertTrue(
classLoaderService.queryClassLoaderById(testJobId, testJars).isPresent());
Assertions.assertEquals(
0, classLoaderService.queryClassLoaderReferenceCount(testJobId, testJars));
Mockito.verify(taskExecutionService, Mockito.timeout(5000))
.notifyTaskStatusToMaster(
Mockito.eq(location),
Mockito.argThat(state -> state.getExecutionState() == FAILED));
testJar.delete();
}

/** Test task execution time is the same as the timer timeout */
@Test
public void testCriticalCallTime() throws InterruptedException {
Expand Down Expand Up @@ -539,4 +640,22 @@ public void testCloseTimerFlushOnUnknownLocationIsNoop() {

Assertions.assertDoesNotThrow(() -> taskExecutionService.closeTimerFlushTask(unknown));
}

private static class ContextInitializationFailureTask implements Task {

@Override
public void setTaskExecutionContext(TaskExecutionContext taskExecutionContext) {
throw new IllegalStateException("context initialization failed");
}

@Override
public ProgressState call() {
return ProgressState.DONE;
}

@Override
public Long getTaskID() {
return 1L;
}
}
}
Loading