Skip to content

Fix unfinished step in parallel flow #4567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
Expand Down Expand Up @@ -119,7 +120,7 @@ public FlowExecutionStatus handle(final FlowExecutor executor) throws Exception
FlowExecutionStatus parentSplitStatus = parentSplit == null ? null : parentSplit.handle(executor);

Collection<FlowExecution> results = new ArrayList<>();

List<Exception> exceptions = new ArrayList<>();
// Could use a CompletionService here?
for (Future<FlowExecution> task : tasks) {
try {
Expand All @@ -129,14 +130,18 @@ public FlowExecutionStatus handle(final FlowExecutor executor) throws Exception
// Unwrap the expected exceptions
Throwable cause = e.getCause();
if (cause instanceof Exception) {
throw (Exception) cause;
exceptions.add((Exception) cause);
}
else {
throw e;
exceptions.add(e);
}
}
}

if (!exceptions.isEmpty()) {
throw exceptions.get(0);
Copy link
Contributor Author

@doontagi doontagi Mar 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As before, the first caught exception would be thrown.

}

FlowExecutionStatus flowExecutionStatus = doAggregation(results, executor);
if (parentSplitStatus != null) {
return Collections.max(Arrays.asList(flowExecutionStatus, parentSplitStatus));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
package org.springframework.batch.core.job.builder;

import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.sql.DataSource;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
Expand All @@ -45,6 +48,8 @@
import org.springframework.batch.core.step.StepSupport;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
Expand Down Expand Up @@ -419,4 +424,38 @@ public JdbcTransactionManager transactionManager(DataSource dataSource) {

}

@Test
public void testBuildSplitWithParallelFlow() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
Step longExecutingStep = new StepBuilder("longExecutingStep", jobRepository).tasklet((stepContribution, b) -> {
Thread.sleep(500L);
return RepeatStatus.FINISHED;
}, new ResourcelessTransactionManager()).build();

Step interruptedStep = new StepBuilder("interruptedStep", jobRepository).tasklet((stepContribution, b) -> {
stepContribution.getStepExecution().setTerminateOnly();
return RepeatStatus.FINISHED;
}, new ResourcelessTransactionManager()).build();

Step nonExecutableStep = new StepBuilder("nonExecutableStep", jobRepository).tasklet((stepContribution, b) -> {
countDownLatch.countDown();
return RepeatStatus.FINISHED;
}, new ResourcelessTransactionManager()).build();

Flow twoStepFlow = new FlowBuilder<SimpleFlow>("twoStepFlow").start(longExecutingStep)
.next(nonExecutableStep)
.build();
Flow interruptedFlow = new FlowBuilder<SimpleFlow>("interruptedFlow").start(interruptedStep).build();

Flow splitFlow = new FlowBuilder<Flow>("splitFlow").split(new SimpleAsyncTaskExecutor())
.add(interruptedFlow, twoStepFlow)
.build();
FlowJobBuilder jobBuilder = new JobBuilder("job", jobRepository).start(splitFlow).build();
jobBuilder.preventRestart().build().execute(execution);

boolean isExecutedNonExecutableStep = countDownLatch.await(1, TimeUnit.SECONDS);
assertEquals(BatchStatus.STOPPED, execution.getStatus());
Copy link
Contributor Author

@doontagi doontagi Mar 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finished with BatchStatus.STOPPED without executing nonExecutableStep.
This test would fail with executing nonExecutableStep if it runs on the current main branch.

Assertions.assertFalse(isExecutedNonExecutableStep);
}

}