From 060fb2dfe472eb64481d2b0a3b78a8c09ce1280d Mon Sep 17 00:00:00 2001 From: wenyh <44251917+wenyh1@users.noreply.github.com> Date: Thu, 18 May 2023 15:58:11 +0800 Subject: [PATCH] [inner-2227] prevents 'connection-pool-evictor-thread' thread from exiting unexpectedly --- .../dble/backend/pool/ConnectionPool.java | 4 ++++ .../dble/backend/pool/EvictionTimer.java | 17 +++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/actiontech/dble/backend/pool/ConnectionPool.java b/src/main/java/com/actiontech/dble/backend/pool/ConnectionPool.java index 3677dafda..2283416b0 100644 --- a/src/main/java/com/actiontech/dble/backend/pool/ConnectionPool.java +++ b/src/main/java/com/actiontech/dble/backend/pool/ConnectionPool.java @@ -437,6 +437,8 @@ public class ConnectionPool extends PoolBase implements MySQLConnectionListener // Try to maintain minimum connections fillPool(); + } catch (Throwable t) { + LOGGER.warn("Evictor.run happen Throwable: ", t); } finally { // Restore the previous CCL Thread.currentThread().setContextClassLoader(savedClassLoader); @@ -450,6 +452,7 @@ public class ConnectionPool extends PoolBase implements MySQLConnectionListener */ void setScheduledFuture(final ScheduledFuture scheduledFuture) { this.scheduledFuture = scheduledFuture; + EvictionTimer.getAliveEvictor().add(scheduledFuture); } /** @@ -457,6 +460,7 @@ public class ConnectionPool extends PoolBase implements MySQLConnectionListener */ void cancel() { scheduledFuture.cancel(false); + EvictionTimer.getAliveEvictor().remove(scheduledFuture); } } } diff --git a/src/main/java/com/actiontech/dble/backend/pool/EvictionTimer.java b/src/main/java/com/actiontech/dble/backend/pool/EvictionTimer.java index 96e629e78..3275efc77 100644 --- a/src/main/java/com/actiontech/dble/backend/pool/EvictionTimer.java +++ b/src/main/java/com/actiontech/dble/backend/pool/EvictionTimer.java @@ -1,19 +1,20 @@ package com.actiontech.dble.backend.pool; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.security.AccessController; import java.security.PrivilegedAction; -import java.util.concurrent.ScheduledFuture; -import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; final class EvictionTimer { + private static final Logger LOGGER = LoggerFactory.getLogger(EvictionTimer.class); /** * Executor instance */ private static ScheduledThreadPoolExecutor executor; //@GuardedBy("EvictionTimer.class") - + private static ConcurrentLinkedQueue aliveEvictor = new ConcurrentLinkedQueue(); /** * Prevents instantiation */ @@ -21,6 +22,9 @@ final class EvictionTimer { // Hide the default constructor } + public static ConcurrentLinkedQueue getAliveEvictor() { + return aliveEvictor; + } /** * @since 2.4.3 @@ -68,8 +72,9 @@ final class EvictionTimer { if (evictor != null) { evictor.cancel(); } - if (executor != null && executor.getQueue().isEmpty()) { + if (executor != null && aliveEvictor.isEmpty()) { executor.shutdown(); + LOGGER.info("EvictionTimer executor shutdown"); try { executor.awaitTermination(timeout, unit); } catch (final InterruptedException e) {