Skip to content

Commit a8961a6

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 <zhouyanming@gmail.com>
1 parent 088487b commit a8961a6

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
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: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.time.LocalDateTime;
1919
import java.util.Arrays;
2020
import java.util.Collection;
21+
import java.util.HashMap;
22+
import java.util.Map;
2123
import java.util.Set;
2224
import java.util.concurrent.atomic.AtomicBoolean;
2325

@@ -41,10 +43,12 @@
4143
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
4244

4345
import static org.junit.jupiter.api.Assertions.assertEquals;
46+
import static org.junit.jupiter.api.Assertions.assertNotNull;
4447

4548
/**
4649
* @author Dave Syer
4750
* @author Mahmoud Ben Hassine
51+
* @author Yanming Zhou
4852
*
4953
*/
5054
class PartitionStepTests {
@@ -71,12 +75,24 @@ void setUp() throws Exception {
7175
@Test
7276
void testVanillaStepExecution() throws Exception {
7377
SimpleStepExecutionSplitter stepExecutionSplitter = new SimpleStepExecutionSplitter(jobRepository,
74-
step.getName(), new SimplePartitioner());
78+
step.getName(), gridSize -> {
79+
Map<String, ExecutionContext> map = new HashMap<>(gridSize);
80+
for (int i = 0; i < gridSize; i++) {
81+
ExecutionContext context = new ExecutionContext();
82+
context.putString("foo", "foo" + i);
83+
map.put("partition" + i, context);
84+
}
85+
return map;
86+
});
7587
stepExecutionSplitter.setAllowStartIfComplete(true);
7688
step.setStepExecutionSplitter(stepExecutionSplitter);
7789
step.setPartitionHandler((stepSplitter, stepExecution) -> {
7890
Set<StepExecution> executions = stepSplitter.split(stepExecution, 2);
7991
for (StepExecution execution : executions) {
92+
// Query from repository to ensure it's persisted
93+
ExecutionContext context = jobRepository.getStepExecution(execution.getId()).getExecutionContext();
94+
assertNotNull(context.getString("foo"));
95+
8096
execution.setStatus(BatchStatus.COMPLETED);
8197
execution.setExitStatus(ExitStatus.COMPLETED);
8298
jobRepository.update(execution);
@@ -144,7 +160,9 @@ void testRestartStepExecution() throws Exception {
144160
else {
145161
for (StepExecution execution : executions) {
146162
// On restart the execution context should have been restored
147-
assertEquals(execution.getStepName(), execution.getExecutionContext().getString("foo"));
163+
// Query from repository to ensure it's persisted
164+
ExecutionContext context = jobRepository.getStepExecution(execution.getId()).getExecutionContext();
165+
assertEquals(execution.getStepName(), context.getString("foo"));
148166
}
149167
}
150168
for (StepExecution execution : executions) {

spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* @author Robert Kasanicky
4949
* @author Dimitrios Liapis
5050
* @author Mahmoud Ben Hassine
51+
* @author Yanming Zhou
5152
*/
5253
// TODO rename to JdbcJobRepositoryIntegrationTests and update to new domain model
5354
// TODO should add a mongodb similar test suite
@@ -171,10 +172,15 @@ void testSaveExecutionContext() throws Exception {
171172
Step step = new StepSupport("step1");
172173
StepExecution stepExec = jobRepository.createStepExecution(step.getName(), jobExec);
173174
stepExec.setExecutionContext(ctx);
175+
jobRepository.updateExecutionContext(stepExec);
174176

175177
StepExecution retrievedStepExec = jobRepository.getLastStepExecution(jobExec.getJobInstance(), step.getName());
176178
assertEquals(stepExec, retrievedStepExec);
177179
assertEquals(ctx, retrievedStepExec.getExecutionContext());
180+
181+
retrievedStepExec = jobRepository.getStepExecution(stepExec.getId());
182+
assertEquals(stepExec, retrievedStepExec);
183+
assertEquals(ctx, retrievedStepExec.getExecutionContext());
178184
}
179185

180186
/*

0 commit comments

Comments
 (0)