Skip to content

Commit

Permalink
Complete task task resource cleanup on task deletion.
Browse files Browse the repository at this point in the history
If there is no task deployment when a task definition is deleted SCDF will delete all app resources for the task definition name across all platforms.
If there is a task deployment for that task definition, then it will only delete those app resources associated with that task deployment.
This is to resolve the case where a task launch failed on the platform and thus no task deployment record is created.  Hence we don't know what platform the launch attempt occured.

resolves spring-cloud#4574
  • Loading branch information
cppwfs authored and jvalkeal committed Jun 14, 2021
1 parent a1a26fb commit 3fcbbed
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.cloud.dataflow.server.service.impl;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -398,8 +399,28 @@ private void destroyTask(TaskDefinition taskDefinition) {
}
}
else {
logger.info("TaskLauncher.destroy not invoked for task " +
taskDefinition.getTaskName() + ". Did not find a previously launched task to destroy.");
if(!findAndDeleteTaskResourcesAcrossPlatforms(taskDefinition)) {
logger.info("TaskLauncher.destroy not invoked for task " +
taskDefinition.getTaskName() + ". Did not find a previously launched task to destroy.");
}
}
}

private boolean findAndDeleteTaskResourcesAcrossPlatforms(TaskDefinition taskDefinition) {
boolean result = false;
Iterable<Launcher> launchers = launcherRepository.findAll();
Iterator<Launcher> launcherIterator = launchers.iterator();
while(launcherIterator.hasNext()) {
Launcher launcher = launcherIterator.next();
try {
launcher.getTaskLauncher().destroy(taskDefinition.getName());
logger.info(String.format("Deleted task app resources for %s in platform %s", taskDefinition.getName(), launcher.getName()));
result = true;
}
catch (Exception ex) {
logger.info(String.format("Attempted delete of app resources for %s but none found on platform %s.", taskDefinition.getName(), launcher.getName()));
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,9 @@ public void createSimpleTask() {
initializeSuccessfulRegistry(appRegistry);
taskSaveService.saveTaskDefinition(new TaskDefinition("simpleTask", "AAA --foo=bar"));
verifyTaskExistsInRepo("simpleTask", "AAA --foo=bar", taskDefinitionRepository);
taskDeleteService.deleteTaskDefinition("simpleTask", true);
String logEntries = outputCapture.toString();
assertTrue(logEntries.contains("Deleted task app resources for"));
}

@Test
Expand All @@ -749,6 +752,10 @@ public void executeSingleTaskTest() {
assertEquals(TASK_NAME_ORIG, taskDeployment.getTaskDefinitionName());
assertEquals("default", taskDeployment.getPlatformName());
assertNotNull("TaskDeployment createdOn field should not be null", taskDeployment.getCreatedOn());
taskDeleteService.deleteTaskDefinition(TASK_NAME_ORIG, true);
String logEntries = outputCapture.toString();
assertTrue(!logEntries.contains("Deleted task app resources for"));
assertTrue(!logEntries.contains("Attempted delete of app resources for"));
}

@Test
Expand Down Expand Up @@ -977,6 +984,19 @@ public void executeSameTaskDefinitionOnMultiplePlatforms() {
}
}

@Test
@DirtiesContext
public void executeDeleteNoDeploymentWithMultiplePlatforms() {
initializeSuccessfulRegistry(appRegistry);
when(taskLauncher.launch(any())).thenReturn("0");
this.launcherRepository.save(new Launcher("anotherPlatform", "local", taskLauncher));
taskDeleteService.deleteTaskDefinition(TASK_NAME_ORIG, true);
String logEntries = outputCapture.toString();
assertTrue(logEntries.contains("Deleted task app resources for myTask_ORIG in platform anotherPlatform"));
assertTrue(logEntries.contains("Deleted task app resources for myTask_ORIG in platform default"));
assertTrue(logEntries.contains("Deleted task app resources for myTask_ORIG in platform MyPlatform"));
}

@Test
@DirtiesContext
public void executeTaskWithNullIDReturnedTest() {
Expand Down

0 comments on commit 3fcbbed

Please sign in to comment.