Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Batch Step does not execute

I'm trying to fix a problem in Spring Batch that has been plaguing our system recently. We have a job that, for the most part works just fine. It's a multi-step job that downloads and processes data.

The problem is that sometimes the job will bomb out. Maybe the server we're trying to connect to throws an error or we shut down the server in the middle of the job. At this point the next time our quartz scheduler tries to run the job it doesn't seem to do anything. Below is an abridged version of this job definition:

<batch:job id="job.download-stuff" restartable="true">
<batch:validator ref="downloadValidator"/>
<batch:step id="job.download-stuff.download">
    <batch:tasklet ref="salesChannelOrderDownloader" transaction-manager="transactionManager">
        <batch:transaction-attributes isolation="READ_UNCOMMITTED" propagation="NOT_SUPPORTED"/>
        <batch:listeners>
            <batch:listener ref="downloadListener"/>
            <batch:listener ref="loggingContextStepListener" />
        </batch:listeners>
    </batch:tasklet>
    <batch:next on="CONTINUE" to="job.download-stuff.process-stuff.step" />
    <batch:end on="*" />
</batch:step>
<batch:step id="job.download-stuff.process-stuff.step">
    ...
</batch:step>
<batch:listeners>
    <batch:listener ref="loggingContextJobListener"/>
</batch:listeners>

Once it gets into this state, the downloadValidator runs, but it never makes it into the first step download-stuff.download. I set a breakpoint in the tasklet and it never makes it inside.

If I clear out all of the spring batch tables, which are stored in our mysql database, and restart the server it will begin working again, but I'd rather understand what prevents it from operating correctly at this point rather than employ scorched earth tactics to get the job running.

I'm a novice at Spring Batch, to put it mildly, so forgive me if I am omitting important details. I've set breakpoints and turned on logging to learn what I can.

What I have observed so far from going through the database is that entries appear to no longer be written to the BATCH_STEP_EXECUTION and BATCH_JOB_EXECUTION tables.

There are no BATCH_JOB_EXECUTION entries for the job that are not in COMPLETED status and no BATCH_STEP_EXECUTION entries that are not in COMPLETED

You'll see that there is a batch:validator defined, I've confirmed that spring batch calls that validator and that it makes it through successfully (set a breakpoint and stepped through). The first step does not get executed.

Neither the loggingContextJobListener nor the loggingContextStepListener seem to fire either. What could be causing this?

UPDATE I took a closer look at the downloadListener added as a batch:listener. Here's the source code of afterStep:

@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public ExitStatus afterStep(StepExecution stepExecution) {
    long runSeconds = TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - nanoStart);

    // If Success - we're good
    if (stepExecution.getStatus() == BatchStatus.COMPLETED) {
        Long endTs = stepExecution.getExecutionContext().getLong("toTime");
        Date toTime = new Date(endTs);
        handleSuccess(toTime, stepExecution.getWriteCount());
        return null;
    }

    // Otherwise - record errors
    List<Throwable> failures = stepExecution.getFailureExceptions();
    handleError(failures);
    return ExitStatus.FAILED;
}

I confirmed that the return ExitStatus.FAILED line executes and that the exception that was thrown is logged in the failureExceptions. It seems like once that happens the BATCH_JOB_EXECUTION entry is in COMPLETED status (and exit_code) and the STEP_EXECUTION is failed.

At this point, the entries in the BATCH_JOB_EXECUTION_PARAMS table remain. I actually tried modifying the values of their KEY_NAME and value columns, but this still didn't allow the job to run. As long as there are parameters tied to a JOB_EXECUTION_ID, another job belonging to the same BATCH_JOB_INSTANCE cannot run.

Once I remove the entries in BATCH_JOB_EXECUTION_PARAMS for that specific JOB_EXECUTION_ID, another BATCH_JOB_EXECUTION can run, even though all the BATCH_JOB_EXECUTION entries are in a completed state.

So I guess I have two questions- is that the correct behavior? And if so, what is preventing the BATCH_JOB_EXECUTION_PARAMS from being removed and how do I remove them?

like image 676
IcedDante Avatar asked May 05 '16 15:05

IcedDante


2 Answers

Had the same issue, during test/debug process I kept job name and parameters same, make sure you are changing job name or job parameters to get different JobExecution

like image 120
Georgy Gobozov Avatar answered Oct 14 '22 05:10

Georgy Gobozov


The JobParametersValidator, in your case the downloadValidator bean runs before the job kicks off.

What's happening in your case is the parameters you're passing the job are the same as that "blown up" JobInstance. However, because that job failed in dramatic fashion, it probably wasn't put into a failed status.

You can either run the job with different parameters (to get a new job instance) or try updating the status of the former step/job to FAILED in BATCH_STEP_EXECUTION or BATCH_JOB_EXECUTION before restarting.

UPDATE (new info added to question) You must be carefull of your job flow here. Yes your step failed, but your context file indicates that the job should END (complete) on anything other than CONTINUE.

<batch:next on="CONTINUE" to="job.download-stuff.process-stuff.step" />
<batch:end on="*" />

First, be very careful of ending on *. In your scenario, it is causing you to finish your job (with a "success") for an ExitCode of FAILED. Also, the default ExitCode for a successful step is COMPLETED, not CONTINUE, so be careful there.

<!-- nothing to me indicates you'd get CONTINUE here, so I changed it -->
<batch:next on="COMPLETED" to="job.download-stuff.process-stuff.step" />

<!-- if you ever have reason to stop here -->
<batch:end on="END" /> 

<!-- always fail on anything unexpected -->
<batch:fail on="*" />
like image 41
Dean Clark Avatar answered Oct 14 '22 03:10

Dean Clark