Skip to content
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

Not unique steps names in a FlowJob should raise an exception or at least log a warning #3757

Open
FBibonne opened this issue Aug 5, 2020 · 5 comments

Comments

@FBibonne
Copy link

FBibonne commented Aug 5, 2020

When running a job with conditional flow, if steps in this job have the same name, the job doesn't behavior as expected (infinite loop on one step ...) because transitions in the job rely on step names. So in order to avoid such behavior, steps names in a flow job should be unique : spring batch should raise an exception or at least should log a warning.
Furthermore, in xml config, as step names are defined as ids of <step>, they must be unique : it is an xsd constraint.

Here an example :

// Job definition :
public Job flowjob() {
	return jobBuilderFactory.get("flowJob")
			.start(step1())
			.on("ODD").to(step2())
			.from(step1()).on("EVEN").to(step3()
			.from(step3()).next(step4())
			.end()
			.build();
}

// step1 definition with the name of step1 :
private Step step1() {
	return stepBuilderFactory.get("step1").tasklet(new Tasklet() {
		@Override
		public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
			String exitStatus = (System.currentTimeMillis() % 2 == 0) ? "EVEN" : "ODD";
			contribution.setExitStatus(new ExitStatus(exitStatus));
			return RepeatStatus.FINISHED;
		}
	})
        .build();
}

//step2 definition with name of **step1** :
private Step step2() {
	return stepBuilderFactory.get("step1") /*Big mistake !*/
        .tasklet((contrib, chunk)->RepeatStatus.FINISHED)
        .build();
}

// and so on : step3 and step4 are defined as step2 with always the same name (step1)

When running the job, spring batch loops infinitely on step3.

@FBibonne FBibonne added the status: waiting-for-triage Issues that we did not analyse yet label Aug 5, 2020
@FBibonne
Copy link
Author

FBibonne commented Aug 5, 2020

Step names are used as identifiers (keys in maps in spring batch internal code) so they should be unique and the piece of code above is a non sense.
But as a new spring batch developer I remember I did the mistake and it took a lot of time to correct it. I saw recently the same mistake by an other new spring btach developer. So I submit this issue mainly thinking about new spring batch developers

@fmbenhassine
Copy link
Contributor

fmbenhassine commented Nov 21, 2023

Thank you for raising this issue. That's a valid concern. Step names must be unique within a flow definition and indeed, this is not enforced currently (as of v5.0.3).

The samples in documentation were updated in dde5455 (with a warning about the importance of unique step names in flow definitions), but it is better to check this programmatically and raise and exception or log a warning when this constraint is violated. Adding name-based equals and hashCode to AbstractStep might improve things, but probably not much after this commit: edc197a. That fix should probably be reviewed/reverted and the original issue should be fixed differently (like detecting if the step is a proxy and get its name from the target object).

This will be a breaking change, so I will plan it for 5.2.

@fmbenhassine fmbenhassine added this to the 5.2.0 milestone Nov 21, 2023
FBibonne pushed a commit to FBibonne/spring-batch that referenced this issue Feb 5, 2024
the names of different steps in a job must be different
@FBibonne
Copy link
Author

FBibonne commented Feb 5, 2024

I created the PR to indicate work in progress but it is still in draft because 5 tests still fail

@FBibonne
Copy link
Author

FBibonne commented Feb 6, 2024

PR #4544 is ready for review

@fmbenhassine fmbenhassine modified the milestones: 5.2.0, 6.0.0 Nov 20, 2024
FBibonne added a commit to FBibonne/spring-batch that referenced this issue Feb 2, 2025
FBibonne pushed a commit to FBibonne/spring-batch that referenced this issue Feb 2, 2025
the names of different steps in a job must be different

Signed-off-by: Fabrice Bibonne <[email protected]>
FBibonne pushed a commit to FBibonne/spring-batch that referenced this issue Feb 3, 2025
the names of different steps in a job must be different

Signed-off-by: Fabrice Bibonne <[email protected]>
@FBibonne
Copy link
Author

FBibonne commented Feb 4, 2025

New PR to solve the issue : #4756

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants