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

Duplicated job execution for single job instance. #3788

Closed
Ceglee opened this issue Oct 7, 2020 · 6 comments
Closed

Duplicated job execution for single job instance. #3788

Ceglee opened this issue Oct 7, 2020 · 6 comments

Comments

@Ceglee
Copy link

Ceglee commented Oct 7, 2020

Bug description
There is a chance, to have more than one actively running job executions, for a one job instance, which has restartable option set to false (preventRestart() being called).

Environment
4.2.1.RELEASE, Java 8u261, Oracle, 4 application instances running at the same time.

Steps to reproduce
Just create a job with single step, prevent it from restart and try to run it, in more than one java process at the same time. At some point you should be able to reproduce it.

Expected behavior
It should create only one job execution for each job instance.

Minimal Complete Reproducible example
You just have to configure any batch job, just be sure that you are using the same database for all processes.

Cause of the problem
Problem lies in SimpleJobRepository class in createJobExecution method. You are not checking there, whether job is restartable or not. You are only doing it at level of SimpleJobLauncher, which is calling createJobExecution, but this is not enough. You are setting proxy transaction only over the SimleJobRepository, so state in run method from SimpleJobLauncher might change.
Please take a look on following image. Assuming that processes (or even threads) will do it in following order and the 1 process will not yet start its job execution, job instance will end up with having two job executions.

image

Btw. in your documentation (https://docs.spring.io/spring-batch/docs/current/reference/html/schema-appendix.html) you are missing unique constraint in BATCH_JOB_INSTANCE definition.

@Ceglee Ceglee added status: waiting-for-triage Issues that we did not analyse yet type: bug labels Oct 7, 2020
@Ceglee
Copy link
Author

Ceglee commented Oct 7, 2020

I think, in method createJobExecution changing this condition:
if (execution.isRunning() || execution.isStopping()) {...}
to something like this:
if (execution.isStarting() || execution.isRunning() || execution.isStopping()) {...}
might help.

@cormoran
Copy link

I also encountered this problem with restartable job. (createJobExecution could be called from different process before starting the execution.)
This behavior seems introduced from #659.

Adding the guard condition execution.isStarting() in createJobExecution sounds good to me.
The implementation may be like as follows, considering the usage of startTime?

public boolean isStarting() {
	return startTime == null && status == BatchStatus.STARTING;
}

@baalaad
Copy link

baalaad commented Jul 22, 2021

Any update on this issue. We still have similar issue. Also raised new issue with more details - #3966

@lcmarvin
Copy link
Contributor

This is related to #1483 , and can also be fixed by PR #4113

@fmbenhassine fmbenhassine modified the milestones: 5.0.0, 5.0.0-RC1 Oct 12, 2022
@fmbenhassine fmbenhassine modified the milestones: 5.0.0-RC1, 5.0.0-RC2 Oct 20, 2022
@fmbenhassine fmbenhassine modified the milestones: 5.0.0-RC2, 5.0.0 Nov 8, 2022
@fmbenhassine
Copy link
Contributor

@Ceglee Thank you for reporting this issue! And thanks to @cormoran and @lcmarvin for the follow up and the PRs 👍

#1483 and #4113 have now been resolved, can you please give a try with the latest 5.0.0 snapshots and share your feedback? Thank you upfront!

@fmbenhassine fmbenhassine added the status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter label Nov 18, 2022
@fmbenhassine
Copy link
Contributor

The fixes have been released in v5.0.0.

If you still have the issue, please add a comment here and we will re-open the issue for further investigation.

@fmbenhassine fmbenhassine removed the status: waiting-for-reporter Issues for which we are waiting for feedback from the reporter label Nov 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants