BATCH-2401: fix CPU intensive loop while polling partitions completion

Before this commit, the JsrPartitionHandler was polling partitions
completion continuously. This causes a high CPU usage.

This commit makes the polling thread sleep for a configurable amount
of time in order to decrease CPU usage during the polling period.

Resolves BATCH-2401
This commit is contained in:
Mahmoud Ben Hassine
2018-02-12 14:39:18 +01:00
committed by Michael Minella
parent b0ffe55113
commit e11b838452
2 changed files with 43 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@ import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
import org.springframework.batch.core.step.JobRepositorySupport;
import org.springframework.batch.core.step.StepSupport;
import org.springframework.util.StopWatch;
import javax.batch.api.BatchProperty;
import javax.batch.api.partition.PartitionAnalyzer;
@@ -46,6 +47,7 @@ import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class JsrPartitionHandlerTests extends AbstractJsrTestCase {
@@ -114,6 +116,14 @@ public class JsrPartitionHandlerTests extends AbstractJsrTestCase {
handler.setJobRepository(repository);
handler.afterPropertiesSet();
handler.setPollingInterval(-1);
try {
handler.afterPropertiesSet();
fail("Polling interval was not checked for");
} catch(IllegalArgumentException iae) {
assertEquals("The polling interval must be positive", iae.getMessage());
}
}
@Test
@@ -128,6 +138,23 @@ public class JsrPartitionHandlerTests extends AbstractJsrTestCase {
assertEquals(3, count);
}
@Test
public void testPollingPartitionsCompletion() throws Exception {
handler.setThreads(3);
handler.setPartitions(3);
handler.setPollingInterval(1000);
handler.afterPropertiesSet();
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Collection<StepExecution> executions = handler.handle(stepSplitter, stepExecution);
stopWatch.stop();
assertEquals(3, executions.size());
assertEquals(3, count);
assertTrue(stopWatch.getLastTaskTimeMillis() >= 1000);
}
@Test
public void testMapperProvidesPartitions() throws Exception {
handler.setPartitionMapper(new PartitionMapper() {