Repository navigation
Description
In a remote chunking manager step (a step whose item writer is a ChunkMessageChannelItemWriter), the write count of the step execution is twice the number of items written. This is the case for a manager step based on ChunkOrientedStep as well as for the TaskletStep created by RemoteChunkingManagerStepBuilder (see "Affected versions" below).
The write count is incremented twice for the same items:
- The manager step increments it as soon as
ChunkMessageChannelItemWriter#writereturns.ChunkOrientedStepdoes it by the chunk size (contribution.incrementWriteCount(chunk.size())), andFaultTolerantChunkProcessor, used by theTaskletStepcreated byRemoteChunkingManagerStepBuilder, does it by the number of outputs. At that point the chunk has only been sent to the workers. - In
ChunkMessageChannelItemWriter#afterStep, the contributions returned by the workers (which carry their own write counts) are applied withstepExecution.apply(contribution), which adds their write counts on top.
Reproduction
A manager step built with RemoteChunkingManagerBuilder, with a worker simulated by a ChunkProcessorChunkRequestHandler that writes all items:
ChunkOrientedStep<String, String> step = new RemoteChunkingManagerBuilder("managerStep", jobRepository)
.inputChannel(inputChannel)
.outputChannel(outputChannel)
.<String, String>chunk(2)
.reader(new ListItemReader<>(items))
.transactionManager(transactionManager)
.build();
step.execute(stepExecution);Results on main (all runs COMPLETED, all items written exactly once by the worker):
| items | chunk size | readCount |
expected writeCount |
actual writeCount |
|---|---|---|---|---|
| 5 | 2 | 5 | 5 | 10 |
| 9 | 2 | 9 | 9 | 18 |
| 9 | 3 | 9 | 9 | 18 |
The actual write count was exactly twice the item count for every item count from 1 to 12 and for chunk sizes 2 and 3. The result is the same whether the worker increments the write count by hand or uses a DefaultChunkProcessor with an ItemWriter. The worker contributions sum to the number of items (e.g. 2+2+2+2+1 for 9 items with a chunk size of 2), so the extra count comes from the manager side.
Affected versions
Reproduced on main (6.1.0-SNAPSHOT) and on 6.0.x (6.0.6-SNAPSHOT).
RemoteChunkingManagerBuilder is new in 6.1, so on 6.0.x the same worker and the same item counts were run against two other manager steps, both giving exactly twice the item count (e.g. 10 for 5 items and 18 for 9 items with a chunk size of 2):
- a
TaskletStepcreated withRemoteChunkingManagerStepBuilder - a
ChunkOrientedStepwired by hand with aChunkMessageChannelItemWriteras the item writer
Versions before 6.0 have not been checked.
Possible fix
ChunkTaskExecutorItemWriter already handles the same situation: it resets the write count in afterStep before applying the contributions of the tasks. ChunkMessageChannelItemWriter#afterStep could do the same, so that the items are not counted as written on the manager side.
Note that the write count of a failed or partially processed chunk must reflect what the workers actually wrote, so the fix should keep the worker contributions as the source of truth.
Activity
Refine contribution #5631
Reset manager write counts before applying worker contributions in Ch…