From 3104292f68816abb8206f3db5f27df7d949b37c2 Mon Sep 17 00:00:00 2001 From: Brett Wooldridge Date: Fri, 1 Jul 2022 02:52:08 +0900 Subject: [PATCH] Minor code cleanups. --- .../com/zaxxer/hikari/pool/HikariPool.java | 29 +++++++------------ .../java/com/zaxxer/hikari/pool/PoolBase.java | 2 +- .../com/zaxxer/hikari/util/UtilityElf.java | 24 +++++++-------- .../hikari/pool/HouseKeeperCleanupTest.java | 2 +- 4 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/zaxxer/hikari/pool/HikariPool.java b/src/main/java/com/zaxxer/hikari/pool/HikariPool.java index c735f594c..6cfefcdbe 100644 --- a/src/main/java/com/zaxxer/hikari/pool/HikariPool.java +++ b/src/main/java/com/zaxxer/hikari/pool/HikariPool.java @@ -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); @@ -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()} @@ -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 { @@ -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 } @@ -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; diff --git a/src/main/java/com/zaxxer/hikari/pool/PoolBase.java b/src/main/java/com/zaxxer/hikari/pool/PoolBase.java index e87251d44..a200c6a3f 100644 --- a/src/main/java/com/zaxxer/hikari/pool/PoolBase.java +++ b/src/main/java/com/zaxxer/hikari/pool/PoolBase.java @@ -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); diff --git a/src/main/java/com/zaxxer/hikari/util/UtilityElf.java b/src/main/java/com/zaxxer/hikari/util/UtilityElf.java index 9dbe6a973..225bd66da 100644 --- a/src/main/java/com/zaxxer/hikari/util/UtilityElf.java +++ b/src/main/java/com/zaxxer/hikari/util/UtilityElf.java @@ -118,14 +118,7 @@ public static T createInstance(final String className, final Class 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(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 static ThreadPoolExecutor createThreadPoolExecutor(final int queueSize, f public static ThreadPoolExecutor createThreadPoolExecutor(final BlockingQueue 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 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 diff --git a/src/test/java/com/zaxxer/hikari/pool/HouseKeeperCleanupTest.java b/src/test/java/com/zaxxer/hikari/pool/HouseKeeperCleanupTest.java index 670143d1e..0d717cbb2 100644 --- a/src/test/java/com/zaxxer/hikari/pool/HouseKeeperCleanupTest.java +++ b/src/test/java/com/zaxxer/hikari/pool/HouseKeeperCleanupTest.java @@ -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);