Skip to content

Fix FlowBuilder.next().end() infinite loop #4475

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
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 @@ -48,6 +48,7 @@
* @author Dave Syer
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Injae Kim
* @since 2.2
* @param <Q> the type of object returned by the builder (by default a Flow)
*
Expand Down Expand Up @@ -107,7 +108,7 @@ public Q build() {

/**
* Transition to the next step on successful completion of the current step. All other
* outcomes are treated as failures.
* outcomes are treated as failures. If no steps are registered yet just a synonym for {@link #start(Step)}.
* @param step the next step
* @return this to enable chaining
*/
Expand Down Expand Up @@ -247,29 +248,32 @@ protected Flow flow() {
}

private void doNext(Object input) {
if (this.currentState == null) {
if (currentState == null) {
doStart(input);
} else {
State next = createState(input);
addTransition("COMPLETED", next);
addTransition("*", failedState);
currentState = next;
}
State next = createState(input);
addTransition("COMPLETED", next);
addTransition("*", failedState);
this.currentState = next;
Comment on lines -253 to -256
Copy link
Contributor Author

Choose a reason for hiding this comment

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

		if (this.currentState == null) {
			doStart(input); // 1. currentState: input
		}

		State next = createState(input);
        // 2. input -> COMPLETED -> input -> COMPLETED -> input ..
		addTransition("COMPLETED", next); 

when currentState == null, I think we should not addTransition("COMPLETED", next); cause it makes infinite loop 🤔

}

private void doStart(Object input) {
if (this.currentState != null) {
if (currentState == null) {
currentState = createState(input);
} else {
doFrom(input);
}
this.currentState = createState(input);
}

private void doFrom(Object input) {
if (currentState == null) {
doStart(input);
} else {
State state = createState(input);
tos.put(currentState.getName(), currentState);
currentState = state;
Comment on lines +272 to +275
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When currrent == null, previous doFrom logic seems createState(input) twice (inside of doStart() and L273) so I fix it~!

}
State state = createState(input);
tos.put(currentState.getName(), currentState);
this.currentState = state;
}

private State createState(Object input) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@

import org.junit.jupiter.api.Test;

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInterruptedException;
Expand All @@ -34,26 +35,80 @@
import org.springframework.batch.core.step.StepSupport;

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

/**
* @author Dave Syer
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Injae Kim
*
*/
class FlowBuilderTests {

@Test
void test() throws Exception {
void testNext() throws Exception {
FlowBuilder<Flow> builder = new FlowBuilder<>("flow");
JobRepository jobRepository = new JobRepositorySupport();
JobExecution execution = jobRepository.createJobExecution("foo", new JobParameters());
builder.start(new StepSupport("step") {
@Override
public void execute(StepExecution stepExecution)
throws JobInterruptedException, UnexpectedJobExecutionException {
}
}).end().start(new JobFlowExecutor(jobRepository, new SimpleStepHandler(jobRepository), execution));

builder.next(createCompleteStep("stepA"))
.end()
.start(new JobFlowExecutor(jobRepository, new SimpleStepHandler(jobRepository), execution));

Iterator<StepExecution> stepExecutions = execution.getStepExecutions().iterator();
assertEquals(stepExecutions.next().getStepName(), "stepA");
assertFalse(stepExecutions.hasNext());
}

@Test
void testMultipleNext() throws Exception {
FlowBuilder<Flow> builder = new FlowBuilder<>("flow");
JobRepository jobRepository = new JobRepositorySupport();
JobExecution execution = jobRepository.createJobExecution("foo", new JobParameters());

builder.next(createCompleteStep("stepA"))
.next(createCompleteStep("stepB"))
.next(createCompleteStep("stepC"))
.end()
.start(new JobFlowExecutor(jobRepository, new SimpleStepHandler(jobRepository), execution));

Iterator<StepExecution> stepExecutions = execution.getStepExecutions().iterator();
assertEquals(stepExecutions.next().getStepName(), "stepA");
assertEquals(stepExecutions.next().getStepName(), "stepB");
assertEquals(stepExecutions.next().getStepName(), "stepC");
assertFalse(stepExecutions.hasNext());
}

@Test
void testStart() throws Exception {
FlowBuilder<Flow> builder = new FlowBuilder<>("flow");
JobRepository jobRepository = new JobRepositorySupport();
JobExecution execution = jobRepository.createJobExecution("foo", new JobParameters());

builder.start(createCompleteStep("stepA"))
.end()
.start(new JobFlowExecutor(jobRepository, new SimpleStepHandler(jobRepository), execution));

Iterator<StepExecution> stepExecutions = execution.getStepExecutions().iterator();
assertEquals(stepExecutions.next().getStepName(), "stepA");
assertFalse(stepExecutions.hasNext());
}

@Test
void testFrom() throws Exception {
FlowBuilder<Flow> builder = new FlowBuilder<>("flow");
JobRepository jobRepository = new JobRepositorySupport();
JobExecution execution = jobRepository.createJobExecution("foo", new JobParameters());

builder.from(createCompleteStep("stepA"))
.end()
.start(new JobFlowExecutor(jobRepository, new SimpleStepHandler(jobRepository), execution));

Iterator<StepExecution> stepExecutions = execution.getStepExecutions().iterator();
assertEquals(stepExecutions.next().getStepName(), "stepA");
assertFalse(stepExecutions.hasNext());
}

@Test
Expand All @@ -66,7 +121,7 @@ void testTransitionOrdering() throws Exception {
@Override
public void execute(StepExecution stepExecution)
throws JobInterruptedException, UnexpectedJobExecutionException {
stepExecution.setExitStatus(new ExitStatus("FAILED"));
stepExecution.setExitStatus(ExitStatus.FAILED);
}
};

Expand Down Expand Up @@ -94,10 +149,19 @@ public void execute(StepExecution stepExecution)
.start(new JobFlowExecutor(jobRepository, new SimpleStepHandler(jobRepository), execution));

Iterator<StepExecution> stepExecutions = execution.getStepExecutions().iterator();
StepExecution stepExecutionA = stepExecutions.next();
assertEquals(stepExecutionA.getStepName(), "stepA");
StepExecution stepExecutionC = stepExecutions.next();
assertEquals(stepExecutionC.getStepName(), "stepC");
assertEquals(stepExecutions.next().getStepName(), "stepA");
assertEquals(stepExecutions.next().getStepName(), "stepC");
assertFalse(stepExecutions.hasNext());
}

private static StepSupport createCompleteStep(String name) {
return new StepSupport(name) {
@Override
public void execute(StepExecution stepExecution)
throws JobInterruptedException, UnexpectedJobExecutionException {
stepExecution.upgradeStatus(BatchStatus.COMPLETED);
stepExecution.setExitStatus(ExitStatus.COMPLETED);
}
};
}
}