From 8c57759eaa09ff8ef41aba9e16cdfcbb528c1379 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Tue, 28 Jan 2020 20:14:16 +0800 Subject: [PATCH] PoolUpdater can set Schedule Period now --- .../pool/ha/HighAvailableDataSource.java | 11 ++-- .../druid/pool/ha/node/PoolUpdater.java | 54 +++++++++++++------ .../druid/pool/ha/node/PoolUpdaterTest.java | 1 + 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java b/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java index 2adfedfcb9..851a929060 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java @@ -96,6 +96,7 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc private DataSourceSelector selector; private String dataSourceFile = DEFAULT_DATA_SOURCE_FILE; private String propertyPrefix = ""; + private int poolUpdatePeriodSeconds = PoolUpdater.DEFAULT_PERIOD; private volatile boolean inited = false; @@ -110,6 +111,8 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc if (inited) { return; } + poolUpdater.setPeriodSeconds(poolUpdatePeriodSeconds); + poolUpdater.init(); if (dataSourceMap == null || dataSourceMap.isEmpty()) { createNodeMap(); } @@ -300,12 +303,12 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc // Getters & Setters - public PoolUpdater getPoolUpdater() { - return poolUpdater; + public int getPoolUpdatePeriodSeconds() { + return poolUpdatePeriodSeconds; } - public void setPoolUpdater(PoolUpdater poolUpdater) { - this.poolUpdater = poolUpdater; + public void setPoolUpdatePeriodSeconds(int poolUpdatePeriodSeconds) { + this.poolUpdatePeriodSeconds = poolUpdatePeriodSeconds; } public NodeListener getNodeListener() { diff --git a/src/main/java/com/alibaba/druid/pool/ha/node/PoolUpdater.java b/src/main/java/com/alibaba/druid/pool/ha/node/PoolUpdater.java index 7977bac637..8dbf2df5f1 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/node/PoolUpdater.java +++ b/src/main/java/com/alibaba/druid/pool/ha/node/PoolUpdater.java @@ -40,29 +40,17 @@ import java.util.concurrent.locks.ReentrantLock; * @author DigitalSonic */ public class PoolUpdater implements Observer { + public final static int DEFAULT_PERIOD = 60; private final static Log LOG = LogFactory.getLog(PoolUpdater.class); - private Set nodesToDel = new CopyOnWriteArraySet(); private HighAvailableDataSource highAvailableDataSource; private Lock lock = new ReentrantLock(); private ScheduledExecutorService executor; + private int periodSeconds = DEFAULT_PERIOD; + private boolean inited = false; - /** - * Create a ScheduledExecutorService to remove unused DataSources. - */ public PoolUpdater() { - executor = Executors.newScheduledThreadPool(1); - executor.scheduleAtFixedRate(new Runnable() { - @Override - public void run() { - try { - removeDataSources(); - } catch (Exception e) { - LOG.error("Exception occurred while removing DataSources.", e); - } - } - }, 60, 60, TimeUnit.SECONDS); } /** @@ -73,6 +61,34 @@ public class PoolUpdater implements Observer { setHighAvailableDataSource(highAvailableDataSource); } + /** + * Create a ScheduledExecutorService to remove unused DataSources. + */ + public void init() { + if (inited) { + return; + } + synchronized (this) { + if (inited) { + return; + } + if (periodSeconds <= 0) { + periodSeconds = DEFAULT_PERIOD; + } + executor = Executors.newScheduledThreadPool(1); + executor.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + try { + removeDataSources(); + } catch (Exception e) { + LOG.error("Exception occurred while removing DataSources.", e); + } + } + }, periodSeconds, periodSeconds, TimeUnit.SECONDS); + } + } + public void destroy() { if (executor == null || executor.isShutdown()) { return; @@ -229,4 +245,12 @@ public class PoolUpdater implements Observer { public Set getNodesToDel() { return nodesToDel; } + + public int getPeriodSeconds() { + return periodSeconds; + } + + public void setPeriodSeconds(int periodSeconds) { + this.periodSeconds = periodSeconds; + } } diff --git a/src/test/java/com/alibaba/druid/pool/ha/node/PoolUpdaterTest.java b/src/test/java/com/alibaba/druid/pool/ha/node/PoolUpdaterTest.java index b41e021923..700abc5c5c 100644 --- a/src/test/java/com/alibaba/druid/pool/ha/node/PoolUpdaterTest.java +++ b/src/test/java/com/alibaba/druid/pool/ha/node/PoolUpdaterTest.java @@ -24,6 +24,7 @@ public class PoolUpdaterTest { haDataSource.setDataSourceMap(map); updater = new PoolUpdater(haDataSource); + updater.init(); } @After