fix batch2 npe when no results returned (#5219)

* fix npe error

* spotless

* spotless
This commit is contained in:
Ken Stevens 2023-08-19 20:37:16 -04:00 committed by GitHub
parent ab2a86b0d5
commit b9155721a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 29 deletions

View File

@ -0,0 +1,4 @@
---
type: fix
issue: 5219
title: "Reindex batch job threw an exception when no results matched the reindex request. This has been corrected."

View File

@ -90,15 +90,15 @@ public class ResourceIdListStep<PT extends PartitionedJobParameters, IT extends
if (nextChunk.isEmpty()) {
ourLog.info("No data returned");
}
} else {
ourLog.debug("Found {} IDs from {} to {}", nextChunk.size(), start, nextChunk.getLastDate());
final Set<TypedPidJson> idBuffer = nextChunk.getTypedResourcePids().stream()
.map(TypedPidJson::new)
.collect(Collectors.toCollection(LinkedHashSet::new));
final UnmodifiableIterator<List<TypedPidJson>> partition = Iterators.partition(idBuffer.iterator(), maxBatchId);
final UnmodifiableIterator<List<TypedPidJson>> partition =
Iterators.partition(idBuffer.iterator(), maxBatchId);
while (partition.hasNext()) {
final List<TypedPidJson> submissionIds = partition.next();
@ -109,6 +109,7 @@ public class ResourceIdListStep<PT extends PartitionedJobParameters, IT extends
}
ourLog.info("Submitted {} chunks with {} resource IDs", chunkCount, totalIdsFound);
}
return RunOutcome.SUCCESS;
}

View File

@ -56,18 +56,17 @@ class ResourceIdListStepTest {
}
@ParameterizedTest
@ValueSource(ints = {1, 100, 500, 501, 2345, 10500})
@ValueSource(ints = {0, 1, 100, 500, 501, 2345, 10500})
void testResourceIdListBatchSizeLimit(int theListSize) {
List<TypedResourcePid> idList = generateIdList(theListSize);
when(myStepExecutionDetails.getData()).thenReturn(myData);
when(myParameters.getBatchSize()).thenReturn(theListSize);
when(myStepExecutionDetails.getParameters()).thenReturn(myParameters);
HomogeneousResourcePidList homogeneousResourcePidList = mock(HomogeneousResourcePidList.class);
if (theListSize > 0) {
when(homogeneousResourcePidList.getTypedResourcePids()).thenReturn(idList);
when(homogeneousResourcePidList.getLastDate()).thenReturn(new Date());
when(myIdChunkProducer.fetchResourceIdsPage(any(), any(), any(), any(), any()))
.thenReturn(homogeneousResourcePidList);
when(homogeneousResourcePidList.isEmpty()).thenReturn(false);
// Ensure none of the work chunks exceed MAX_BATCH_OF_IDS in size:
doAnswer(i -> {
ResourceIdListWorkChunkJson list = i.getArgument(0);
@ -75,6 +74,12 @@ class ResourceIdListStepTest {
"Id batch size should never exceed " + ResourceIdListStep.MAX_BATCH_OF_IDS);
return null;
}).when(myDataSink).accept(any(ResourceIdListWorkChunkJson.class));
} else {
when(homogeneousResourcePidList.isEmpty()).thenReturn(true);
}
when(myIdChunkProducer.fetchResourceIdsPage(any(), any(), any(), any(), any()))
.thenReturn(homogeneousResourcePidList);
final RunOutcome run = myResourceIdListStep.run(myStepExecutionDetails, myDataSink);
assertNotEquals(null, run);
@ -93,8 +98,10 @@ class ResourceIdListStepTest {
// The very last chunk should be whatever is left over (if there is a remainder):
int expectedLastBatchSize = theListSize % ResourceIdListStep.MAX_BATCH_OF_IDS;
expectedLastBatchSize = (expectedLastBatchSize == 0) ? ResourceIdListStep.MAX_BATCH_OF_IDS : expectedLastBatchSize;
if (!allDataChunks.isEmpty()) {
assertEquals(expectedLastBatchSize, allDataChunks.get(allDataChunks.size() - 1).size());
}
}
private List<TypedResourcePid> generateIdList(int theListSize) {
List<TypedResourcePid> idList = new ArrayList<>();