Skip to content

Commit 0826db6

Browse files
author
dsyer
committed
IN PROGRESS - issue BATCH-677: Partition SPI.
Javadocs
1 parent f6487f6 commit 0826db6

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
/**
1010
* Strategy interface for generating input contexts for a partitioned step
11-
* execution.
11+
* execution independent from the fabric they are going to run on.
1212
*
1313
* @author Dave Syer
1414
*
@@ -33,8 +33,8 @@ public interface StepExecutionSplitter {
3333
*
3434
* On a restart clients of the {@link StepExecutionSplitter} should expect
3535
* it to reconstitute the state of the last failed execution and only return
36-
* those executions that need to be restarted. Thus the grid size hint
37-
* should be ignored on a restart.
36+
* those executions that need to be restarted. Thus the grid size hint will
37+
* be ignored on a restart.
3838
*
3939
* @param stepExecution the {@link StepExecution} to be partitioned.
4040
* @param gridSize a hint for the splitter if the size of the grid is known

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@
44

55
import org.springframework.batch.item.ExecutionContext;
66

7+
/**
8+
* Central strategy interface for creating input parameters for a partitioned
9+
* step in the form of {@link ExecutionContext} instances. The usual aim is to
10+
* create a set of distinct input values, e.g. a set of non-overlapping primary
11+
* key ranges, or a set of unique filenames.
12+
*
13+
* @author Dave Syer
14+
*
15+
*/
716
public interface Partitioner {
817

18+
/**
19+
* Create a set of distinct {@link ExecutionContext} instances together with
20+
* a unique identifier for each one. The identifiers should be short,
21+
* mnemonic values, and only have to be unique within the return value (e.g.
22+
* use an incrementer).
23+
*
24+
* @param gridSize the size of the map to return
25+
* @return a map from identifier to input parameters
26+
*/
927
Map<String, ExecutionContext> partition(int gridSize);
10-
28+
1129
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55

66
import org.springframework.batch.item.ExecutionContext;
77

8+
/**
9+
* Simplest possible implementation of {@link Partitioner}. Just creates a set
10+
* of empty {@link ExecutionContext} instances, and labels them as
11+
* <code>{partition0, partition1, ..., partitionN}</code>, where <code>N</code> is the grid
12+
* size.
13+
*
14+
* @author Dave Syer
15+
*
16+
*/
817
public class SimplePartitioner implements Partitioner {
918

1019
private static final String PARTITION_KEY = "partition";

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@
1414
import org.springframework.batch.core.repository.JobRepository;
1515
import org.springframework.batch.item.ExecutionContext;
1616

17+
/**
18+
* Generic implementation of {@link StepExecutionSplitter} that delegates to a
19+
* {@link Partitioner} to generate {@link ExecutionContext} instances. Takes
20+
* care of restartability and identifying the step executions from previous runs
21+
* of the same job. The generated {@link StepExecution} instances have names
22+
* that identify them uniquely in the partition. The name is constructed from a
23+
* base (name of the target step) plus a suffix taken from the
24+
* {@link Partitioner} identifiers, separated by a colon, e.g.
25+
* <code>{step1:partition0, step1:partition1, ...}</code>.
26+
*
27+
* @author Dave Syer
28+
*
29+
*/
1730
public class SimpleStepExecutionSplitter implements StepExecutionSplitter {
1831

1932
private static final String STEP_NAME_SEPARATOR = ":";
@@ -30,6 +43,15 @@ public SimpleStepExecutionSplitter(JobRepository jobRepository, Step step) {
3043
this(jobRepository, step, new SimplePartitioner());
3144
}
3245

46+
/**
47+
* Construct a {@link SimpleStepExecutionSplitter} from its mandatory
48+
* properties.
49+
*
50+
* @param jobRepository the {@link JobRepository}
51+
* @param step the target step (a local version of it)
52+
* @param partitioner a {@link Partitioner} to use for generating input
53+
* parameters
54+
*/
3355
public SimpleStepExecutionSplitter(JobRepository jobRepository, Step step, Partitioner partitioner) {
3456
this.jobRepository = jobRepository;
3557
this.step = step;
@@ -95,7 +117,7 @@ private boolean getStartable(StepExecution stepExecution, ExecutionContext conte
95117

96118
boolean isRestart = (lastStepExecution != null && lastStepExecution.getStatus() != BatchStatus.COMPLETED) ? true
97119
: false;
98-
120+
99121
if (isRestart) {
100122
stepExecution.setExecutionContext(lastStepExecution.getExecutionContext());
101123
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,30 @@
44

55
import org.springframework.batch.core.BatchStatus;
66
import org.springframework.batch.core.StepExecution;
7+
import org.springframework.batch.repeat.ExitStatus;
78
import org.springframework.util.Assert;
89

10+
/**
11+
* Convenience class for aggregating a set of {@link StepExecution} instances
12+
* into a single result.
13+
*
14+
* @author Dave Syer
15+
*
16+
*/
917
public class StepExecutionAggregator {
1018

19+
/**
20+
* Take the inputs and aggregate certain fields, putting the aggregates into
21+
* the result. The aggregated fields are
22+
* <ul>
23+
* <li>status - choosing the highest value using {@link BatchStatus#max(BatchStatus, BatchStatus)}</li>
24+
* <li>exitStatus - using {@link ExitStatus#and(ExitStatus)}</li>
25+
* <li>commitCount, rollbackCount, etc. - by arithmetic sum</li>
26+
* </ul>
27+
*
28+
* @param result the result to overwrite
29+
* @param executions the inputs
30+
*/
1131
public void aggregate(StepExecution result, Collection<StepExecution> executions) {
1232
Assert.notNull(result, "To aggregate into a result it must be non-null.");
1333
if (executions == null || executions.isEmpty()) {

0 commit comments

Comments
 (0)