Skip to content

Commit

Permalink
Minor code cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
brettwooldridge committed Jun 30, 2022
1 parent 979c0ec commit 3104292
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
29 changes: 10 additions & 19 deletions src/main/java/com/zaxxer/hikari/pool/HikariPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ private boolean softEvictConnection(final PoolEntry poolEntry, final String reas
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);
Expand Down Expand Up @@ -665,9 +665,9 @@ protected void update() {
* 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()}
Expand Down Expand Up @@ -758,7 +758,7 @@ private synchronized boolean shouldContinueCreating() {
}

/**
* 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
{
Expand Down Expand Up @@ -797,22 +797,20 @@ else if (now > plusMillis(previous, (3 * housekeepingPeriodMs) / 2)) {

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
}
Expand All @@ -822,13 +820,6 @@ else if (now > plusMillis(previous, (3 * housekeepingPeriodMs) / 2)) {
}
}

private static class CustomDiscardPolicy implements RejectedExecutionHandler
{
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
}
}

private final class MaxLifetimeTask implements Runnable
{
private final PoolEntry poolEntry;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/zaxxer/hikari/pool/PoolBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ private void createNetworkTimeoutExecutor(final DataSource dataSource, final Str
}
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);
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/zaxxer/hikari/util/UtilityElf.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,7 @@ public static <T> T createInstance(final String className, final Class<T> clazz,
*/
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);
}

/**
Expand All @@ -140,7 +133,7 @@ public static ThreadPoolExecutor createThreadPoolExecutor(final int queueSize, f
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);
Expand Down Expand Up @@ -186,14 +179,21 @@ public static int getTransactionIsolation(final String transactionIsolationName)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3104292

Please sign in to comment.