Skip to content

Commit e64383d

Browse files
committed
Fix step execution context is not persisted and restored
1. Step execution context is not persisted in `SimpleStepExecutionSplitter::split` 2. Step execution context is not restored in `SimpleJobRepository::getStepExecution` Closes GH-5138 Signed-off-by: Yanming Zhou <[email protected]>
1 parent 088487b commit e64383d

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/partition/support/SimpleStepExecutionSplitter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*
4646
* @author Dave Syer
4747
* @author Mahmoud Ben Hassine
48+
* @author Yanming Zhou
4849
* @since 2.0
4950
*/
5051
public class SimpleStepExecutionSplitter implements StepExecutionSplitter {
@@ -138,13 +139,15 @@ public Set<StepExecution> split(StepExecution stepExecution, int gridSize) throw
138139
if (lastStepExecution == null) { // fresh start
139140
StepExecution currentStepExecution = jobRepository.createStepExecution(stepName, jobExecution);
140141
currentStepExecution.setExecutionContext(context.getValue());
142+
jobRepository.updateExecutionContext(currentStepExecution);
141143
set.add(currentStepExecution);
142144
}
143145
else { // restart
144146
if (lastStepExecution.getStatus() != BatchStatus.COMPLETED
145147
&& shouldStart(allowStartIfComplete, stepExecution, lastStepExecution)) {
146148
StepExecution currentStepExecution = jobRepository.createStepExecution(stepName, jobExecution);
147149
currentStepExecution.setExecutionContext(lastStepExecution.getExecutionContext());
150+
jobRepository.updateExecutionContext(currentStepExecution);
148151
set.add(currentStepExecution);
149152
}
150153
}

spring-batch-core/src/main/java/org/springframework/batch/core/repository/explore/support/SimpleJobExplorer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* @author Mahmoud Ben Hassine
4545
* @author Parikshit Dutta
4646
* @author Glenn Renfro
47+
* @author Yanming Zhou
4748
* @see JobExplorer
4849
* @see JobInstanceDao
4950
* @see JobExecutionDao
@@ -287,7 +288,7 @@ public long getStepExecutionCount(JobInstance jobInstance, String stepName) thro
287288
return stepExecutionDao.countStepExecutions(jobInstance, stepName);
288289
}
289290

290-
private void getStepExecutionDependencies(StepExecution stepExecution) {
291+
protected void getStepExecutionDependencies(StepExecution stepExecution) {
291292
if (stepExecution != null) {
292293
stepExecution.setExecutionContext(ecDao.getExecutionContext(stepExecution));
293294
}

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
* @author Baris Cubukcuoglu
5353
* @author Parikshit Dutta
5454
* @author Mark John Moreno
55+
* @author Yanming Zhou
5556
* @see JobRepository
5657
* @see JobInstanceDao
5758
* @see JobExecutionDao
@@ -82,7 +83,9 @@ public List<JobInstance> findJobInstances(String jobName) {
8283
@Nullable
8384
@Override
8485
public StepExecution getStepExecution(long executionId) {
85-
return this.stepExecutionDao.getStepExecution(executionId);
86+
StepExecution stepExecution = this.stepExecutionDao.getStepExecution(executionId);
87+
getStepExecutionDependencies(stepExecution);
88+
return stepExecution;
8689
}
8790

8891
/**

spring-batch-core/src/test/java/org/springframework/batch/core/partition/PartitionStepTests.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
package org.springframework.batch.core.partition;
1717

1818
import java.time.LocalDateTime;
19-
import java.util.Arrays;
20-
import java.util.Collection;
21-
import java.util.Set;
19+
import java.util.*;
2220
import java.util.concurrent.atomic.AtomicBoolean;
2321

2422
import org.junit.jupiter.api.BeforeEach;
@@ -41,10 +39,12 @@
4139
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
4240

4341
import static org.junit.jupiter.api.Assertions.assertEquals;
42+
import static org.junit.jupiter.api.Assertions.assertNotNull;
4443

4544
/**
4645
* @author Dave Syer
4746
* @author Mahmoud Ben Hassine
47+
* @author Yanming Zhou
4848
*
4949
*/
5050
class PartitionStepTests {
@@ -71,12 +71,24 @@ void setUp() throws Exception {
7171
@Test
7272
void testVanillaStepExecution() throws Exception {
7373
SimpleStepExecutionSplitter stepExecutionSplitter = new SimpleStepExecutionSplitter(jobRepository,
74-
step.getName(), new SimplePartitioner());
74+
step.getName(), gridSize -> {
75+
Map<String, ExecutionContext> map = new HashMap<>(gridSize);
76+
for (int i = 0; i < gridSize; i++) {
77+
ExecutionContext context = new ExecutionContext();
78+
context.putString("foo", "foo" + i);
79+
map.put("partition" + i, context);
80+
}
81+
return map;
82+
});
7583
stepExecutionSplitter.setAllowStartIfComplete(true);
7684
step.setStepExecutionSplitter(stepExecutionSplitter);
7785
step.setPartitionHandler((stepSplitter, stepExecution) -> {
7886
Set<StepExecution> executions = stepSplitter.split(stepExecution, 2);
7987
for (StepExecution execution : executions) {
88+
// Query from repository to ensure it's persisted
89+
ExecutionContext context = jobRepository.getStepExecution(execution.getId()).getExecutionContext();
90+
assertNotNull(context.getString("foo"));
91+
8092
execution.setStatus(BatchStatus.COMPLETED);
8193
execution.setExitStatus(ExitStatus.COMPLETED);
8294
jobRepository.update(execution);
@@ -144,7 +156,9 @@ void testRestartStepExecution() throws Exception {
144156
else {
145157
for (StepExecution execution : executions) {
146158
// On restart the execution context should have been restored
147-
assertEquals(execution.getStepName(), execution.getExecutionContext().getString("foo"));
159+
// Query from repository to ensure it's persisted
160+
ExecutionContext context = jobRepository.getStepExecution(execution.getId()).getExecutionContext();
161+
assertEquals(execution.getStepName(), context.getString("foo"));
148162
}
149163
}
150164
for (StepExecution execution : executions) {

0 commit comments

Comments
 (0)