Skip to content

Commit

Permalink
Polish "Add async task execution cleanup"
Browse files Browse the repository at this point in the history
  • Loading branch information
onobc committed Nov 1, 2023
1 parent 79845a0 commit 6c63af8
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 70 deletions.
5 changes: 5 additions & 0 deletions spring-cloud-dataflow-server-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mariadb</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,51 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
import org.springframework.boot.task.TaskExecutorBuilder;
import org.springframework.cloud.dataflow.core.DataFlowPropertyKeys;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import static org.springframework.cloud.dataflow.server.config.DataflowAsyncConfiguration.ASYNC_PREFIX;
import static org.springframework.cloud.dataflow.server.config.DataflowAsyncAutoConfiguration.ASYNC_PROPS_PREFIX;

/**
* Class to override the executor at the application level. It also enables async executions for the Spring Cloud Data Flow Server.
* Enables async executions for the Spring Cloud Dataflow server.
* Uses the Spring Boot autoconfigured {@code TaskExecutorBuilder} to create an async executor and register it
* with name {@link #DATAFLOW_ASYNC_EXECUTOR}.
*
* @author Tobias Soloschenko
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = ASYNC_PREFIX, name = "enabled")
@ConditionalOnBean({EnableDataFlowServerConfiguration.Marker.class})
@ConditionalOnProperty(prefix = ASYNC_PROPS_PREFIX, name = "enabled", havingValue = "true")
@AutoConfigureAfter(TaskExecutionAutoConfiguration.class)
@EnableAsync
class DataflowAsyncConfiguration implements AsyncConfigurer {
public class DataflowAsyncAutoConfiguration implements AsyncConfigurer {

private static final Logger logger = LoggerFactory.getLogger(DataflowAsyncConfiguration.class);
private static final Logger logger = LoggerFactory.getLogger(DataflowAsyncAutoConfiguration.class);

public static final String ASYNC_PREFIX = DataFlowPropertyKeys.PREFIX + "async";
public static final String ASYNC_PROPS_PREFIX = DataFlowPropertyKeys.PREFIX + "async";

public static final String DATAFLOW_ASYNC_EXECUTOR = "dataflowAsyncExecutor";

private static final String THREAD_NAME_PREFIX = "scdf-async-";

@Bean(name = "asyncExecutor")
Executor getAsyncExecutor(TaskExecutorBuilder taskExecutorBuilder) {
return taskExecutorBuilder.threadNamePrefix(THREAD_NAME_PREFIX).build();
private final TaskExecutorBuilder taskExecutorBuilder;

public DataflowAsyncAutoConfiguration(TaskExecutorBuilder taskExecutorBuilder) {
this.taskExecutorBuilder = taskExecutorBuilder;
}

@Bean(name = DATAFLOW_ASYNC_EXECUTOR)
@Override
public Executor getAsyncExecutor() {
return this.taskExecutorBuilder.threadNamePrefix(THREAD_NAME_PREFIX).build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.springframework.cloud.dataflow.rest.util.TaskSanitizer;
import org.springframework.cloud.dataflow.schema.AggregateTaskExecution;
import org.springframework.cloud.dataflow.schema.SchemaVersionTarget;
import org.springframework.cloud.dataflow.server.config.DataflowAsyncAutoConfiguration;
import org.springframework.cloud.dataflow.server.controller.support.TaskExecutionControllerDeleteAction;
import org.springframework.cloud.dataflow.server.repository.NoSuchTaskDefinitionException;
import org.springframework.cloud.dataflow.server.repository.NoSuchTaskExecutionException;
Expand Down Expand Up @@ -337,6 +338,9 @@ public void cleanup(
* Cleanup resources associated with one or more task executions. The
* optional {@code actions} and {@code completed} parameters can be used to not only clean up task execution resources,
* but can also trigger the deletion of task execution and job data in the persistence store.
* <p>
* When the {@code spring.cloud.dataflow.async.enabled} property is set to {@code true} the cleanup will happen
* asynchronously.
*
* @param actions the actions to perform (default 'CLEANUP')
* @param completed whether to include only completed task executions (default false)
Expand All @@ -345,7 +349,7 @@ public void cleanup(
*/
@RequestMapping(method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.OK)
@Async
@Async(DataflowAsyncAutoConfiguration.DATAFLOW_ASYNC_EXECUTOR)
public void cleanupAll(
@RequestParam(defaultValue = "CLEANUP", name = "action") TaskExecutionControllerDeleteAction[] actions,
@RequestParam(defaultValue = "false", name = "completed") boolean completed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.dataflow.server.config.DataFlowServerAutoConfiguration,\
org.springframework.cloud.dataflow.server.config.DataFlowControllerAutoConfiguration, \
org.springframework.cloud.dataflow.server.config.SpringDocAutoConfiguration, \
org.springframework.cloud.dataflow.server.config.DataflowAsyncConfiguration
org.springframework.cloud.dataflow.server.config.DataflowAsyncAutoConfiguration

org.springframework.context.ApplicationContextInitializer=\
org.springframework.cloud.dataflow.common.flyway.FlywayVendorReplacingApplicationContextInitializer
Loading

0 comments on commit 6c63af8

Please sign in to comment.