Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update OptaplannerKieServerExtension.java #1875

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;

import org.kie.server.api.KieServerConstants;
import org.kie.server.api.model.Message;
Expand Down Expand Up @@ -59,7 +56,7 @@ public class OptaplannerKieServerExtension
// will use a standard java thread pool for the job.
// If necessary, we will need to look for alternatives
// in the future.
private ExecutorService threadPool = null;
private ThreadPoolExecutor threadPool = null;

private final List<Object> services = new ArrayList<>();
private boolean initialized = false;
Expand All @@ -86,18 +83,33 @@ public void init(KieServerImpl kieServer, KieServerRegistry registry) {
// The following thread pool will have a max thread count equal to the number of cores on the machine minus 2,
// leaving a few cores unoccupied to handle REST/JMS requests and run the OS.
// If new jobs are submitted and all threads are busy, the default reject policy will kick in.
int availableProcessorCount = Runtime.getRuntime().availableProcessors();
int resolvedActiveThreadCount = Math.max(1, availableProcessorCount - 2);
int queueSize = Integer.parseInt(System.getProperty(
KieServerConstants.KIE_OPTAPLANNER_THREAD_POOL_QUEUE_SIZE, String.valueOf(resolvedActiveThreadCount)));
logger.info("Creating a ThreadPoolExecutor with corePoolSize = " + resolvedActiveThreadCount + ","
+ " maximumPoolSize = " + resolvedActiveThreadCount + ", queueSize = " + queueSize);
//int availableProcessorCount = Runtime.getRuntime().availableProcessors();
//int resolvedActiveThreadCount = Math.max(1, availableProcessorCount - 2);

String corePoolSize = System.getProperty("corePoolSize").isEmpty() ? System.getProperty("corePoolSize") : "2";
String maxPoolSize = System.getProperty("maxPoolSize").isEmpty() ? System.getProperty("maxPoolSize") : "10";
String keepAliveTimeSeconds = System.getProperty("keepAliveTimeSeconds").isEmpty() ? System.getProperty("keepAliveTimeSeconds") : "10";

this.threadPool = new ThreadPoolExecutor(
resolvedActiveThreadCount,
resolvedActiveThreadCount,
10, // thread keep alive time
Integer.parseInt(corePoolSize),
Integer.parseInt(maxPoolSize),
Integer.parseInt(keepAliveTimeSeconds), // thread keep alive time
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(queueSize)); // queue with a size
new LinkedBlockingQueue<>()); // queue with a size

StringBuilder s = new StringBuilder();
s.append("poolSize = ")
.append(this.threadPool.getPoolSize())
.append(", corePoolSize = ")
.append(this.threadPool.getCorePoolSize())
.append(", queueSize = ")
.append(this.threadPool.getQueue().size())
.append(", queueRemainingCapacity = ")
.append(this.threadPool.getQueue().remainingCapacity())
.append(", maximumPoolSize = ")
.append(this.threadPool.getMaximumPoolSize());

logger.info(s.toString());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too verbose to output on info logging. Let's make this debug logging.

Copy link
Collaborator

@ge0ffrey ge0ffrey Sep 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use StringBuilder here, it's equally fast as just String concatenation because there is no loop. (faster even, see shipilev's blog).
Furthermore, better to use this to avoid string concatenation if the logging level > debug.

     logger.debug("OptaPlanner extension created with corePoolSize ({}), queueSize ({}), ...", corePoolSize, queueSize, ...)

this.solverServiceBase = new SolverServiceBase(registry, threadPool);

this.optaplannerCommandService = new OptaplannerCommandServiceImpl(registry, solverServiceBase);
Expand Down