Minor code cleanups.

pull/1952/head
Brett Wooldridge 3 years ago
parent 979c0ec64e
commit 3104292f68

@ -619,7 +619,7 @@ public final class HikariPool extends PoolBase implements HikariPoolMXBean, IBag
private ScheduledExecutorService initializeHouseKeepingExecutorService()
{
if (config.getScheduledExecutor() == null) {
final var threadFactory = Optional.ofNullable(config.getThreadFactory()).orElseGet(() -> new DefaultThreadFactory(poolName + " housekeeper", true));
final var threadFactory = Optional.ofNullable(config.getThreadFactory()).orElseGet(() -> new DefaultThreadFactory(poolName + " housekeeper"));
final var executor = new ScheduledThreadPoolExecutor(1, threadFactory, new ThreadPoolExecutor.DiscardPolicy());
executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
executor.setRemoveOnCancelPolicy(true);
@ -665,9 +665,9 @@ public final class HikariPool extends PoolBase implements HikariPoolMXBean, IBag
* timeout occurred when trying to acquire a Connection from the pool. If there was an underlying cause for the
* timeout, e.g. a SQLException thrown by the driver while trying to create a new Connection, then use the
* SQL State from that exception as our own and additionally set that exception as the "next" SQLException inside
* of our exception.
* our exception.
*
* As a side-effect, log the timeout failure at DEBUG, and record the timeout failure in the metrics tracker.
* As a side effect, log the timeout failure at DEBUG, and record the timeout failure in the metrics tracker.
*
* @param startTime the start time (timestamp) of the acquisition attempt
* @return a SQLException to be thrown from {@link #getConnection()}
@ -758,7 +758,7 @@ public final class HikariPool extends PoolBase implements HikariPoolMXBean, IBag
}
/**
* The house keeping task to retire and maintain minimum idle connections.
* The housekeeping task to retire and maintain minimum idle connections.
*/
private final class HouseKeeper implements Runnable
{
@ -797,22 +797,20 @@ public final class HikariPool extends PoolBase implements HikariPoolMXBean, IBag
previous = now;
var afterPrefix = "Pool ";
if (idleTimeout > 0L && config.getMinimumIdle() < config.getMaximumPoolSize()) {
logPoolState("Before cleanup ");
afterPrefix = "After cleanup ";
final var notInUse = connectionBag.values(STATE_NOT_IN_USE);
var toRemove = notInUse.size() - config.getMinimumIdle();
var maxToRemove = notInUse.size() - config.getMinimumIdle();
for (PoolEntry entry : notInUse) {
if (toRemove > 0 && elapsedMillis(entry.lastAccessed, now) > idleTimeout && connectionBag.reserve(entry)) {
if (maxToRemove > 0 && elapsedMillis(entry.lastAccessed, now) > idleTimeout && connectionBag.reserve(entry)) {
closeConnection(entry, "(connection has passed idleTimeout)");
toRemove--;
maxToRemove--;
}
}
logPoolState("After cleanup ");
}
logPoolState(afterPrefix);
else
logPoolState("Pool ");
fillPool(true); // Try to maintain minimum connections
}
@ -822,13 +820,6 @@ public final class HikariPool extends PoolBase implements HikariPoolMXBean, IBag
}
}
private static class CustomDiscardPolicy implements RejectedExecutionHandler
{
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
}
}
private final class MaxLifetimeTask implements Runnable
{
private final PoolEntry poolEntry;

@ -600,7 +600,7 @@ abstract class PoolBase
}
else {
ThreadFactory threadFactory = config.getThreadFactory();
threadFactory = threadFactory != null ? threadFactory : new DefaultThreadFactory(poolName + " network timeout executor", true);
threadFactory = threadFactory != null ? threadFactory : new DefaultThreadFactory(poolName + " network timeout executor");
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool(threadFactory);
executor.setKeepAliveTime(15, SECONDS);
executor.allowCoreThreadTimeOut(true);

@ -118,14 +118,7 @@ public final class UtilityElf
*/
public static ThreadPoolExecutor createThreadPoolExecutor(final int queueSize, final String threadName, ThreadFactory threadFactory, final RejectedExecutionHandler policy)
{
if (threadFactory == null) {
threadFactory = new DefaultThreadFactory(threadName, true);
}
var queue = new LinkedBlockingQueue<Runnable>(queueSize);
var executor = new ThreadPoolExecutor(1 /*core*/, 1 /*max*/, 5 /*keepalive*/, SECONDS, queue, threadFactory, policy);
executor.allowCoreThreadTimeOut(true);
return executor;
return createThreadPoolExecutor(new LinkedBlockingQueue<>(queueSize), threadName, threadFactory, policy);
}
/**
@ -140,7 +133,7 @@ public final class UtilityElf
public static ThreadPoolExecutor createThreadPoolExecutor(final BlockingQueue<Runnable> queue, final String threadName, ThreadFactory threadFactory, final RejectedExecutionHandler policy)
{
if (threadFactory == null) {
threadFactory = new DefaultThreadFactory(threadName, true);
threadFactory = new DefaultThreadFactory(threadName);
}
var executor = new ThreadPoolExecutor(1 /*core*/, 1 /*max*/, 5 /*keepalive*/, SECONDS, queue, threadFactory, policy);
@ -186,14 +179,21 @@ public final class UtilityElf
return -1;
}
public static final class DefaultThreadFactory implements ThreadFactory {
public static class CustomDiscardPolicy implements RejectedExecutionHandler
{
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
}
}
public static final class DefaultThreadFactory implements ThreadFactory
{
private final String threadName;
private final boolean daemon;
public DefaultThreadFactory(String threadName, boolean daemon) {
public DefaultThreadFactory(String threadName) {
this.threadName = threadName;
this.daemon = daemon;
this.daemon = true;
}
@Override

@ -43,7 +43,7 @@ public class HouseKeeperCleanupTest
@Before
public void before() throws Exception
{
ThreadFactory threadFactory = new UtilityElf.DefaultThreadFactory("global housekeeper", true);
ThreadFactory threadFactory = new UtilityElf.DefaultThreadFactory("global housekeeper");
executor = new ScheduledThreadPoolExecutor(1, threadFactory, new ThreadPoolExecutor.DiscardPolicy());
executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);

Loading…
Cancel
Save