From 007c5acb71df3250a1ba0f6307aa741c7cc3133a Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Tue, 21 May 2019 15:04:11 +0800 Subject: [PATCH 01/26] optimize the random logic in HA --- .../ha/selector/RandomDataSourceSelector.java | 54 +++++++++++++------ .../StickyRandomDataSourceSelector.java | 10 +++- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelector.java b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelector.java index e8f5b9fddd..693b3351c5 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelector.java +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelector.java @@ -86,21 +86,10 @@ public class RandomDataSourceSelector implements DataSourceSelector { return null; } - Collection targetDataSourceSet; - if (blacklist == null || blacklist.isEmpty() || blacklist.size() >= dataSourceMap.size()) { - targetDataSourceSet = dataSourceMap.values(); - } else { - targetDataSourceSet = new HashSet(dataSourceMap.values()); - for (DataSource b : blacklist) { - targetDataSourceSet.remove(b); - } - } - - DataSource[] dataSources = targetDataSourceSet.toArray(new DataSource[] {}); - if (dataSources != null && dataSources.length > 0) { - return dataSources[random.nextInt(targetDataSourceSet.size())]; - } - return null; + Collection targetDataSourceSet = removeBlackList(dataSourceMap); + removeBusyDataSource(targetDataSourceSet); + DataSource dataSource = getRandomDataSource(targetDataSourceSet); + return dataSource; } @Override @@ -182,6 +171,41 @@ public class RandomDataSourceSelector implements DataSourceSelector { new Thread(recoverThread, "RandomDataSourceSelector-recover-thread").start(); } + private Collection removeBlackList(Map dataSourceMap) { + Collection dataSourceSet; + if (blacklist == null || blacklist.isEmpty() || blacklist.size() >= dataSourceMap.size()) { + dataSourceSet = dataSourceMap.values(); + } else { + dataSourceSet = new HashSet(dataSourceMap.values()); + for (DataSource b : blacklist) { + dataSourceSet.remove(b); + } + } + return dataSourceSet; + } + + private void removeBusyDataSource(Collection dataSourceSet) { + Collection busyDataSourceSet = new HashSet(); + for (DataSource ds : dataSourceSet) { + if (ds instanceof DruidDataSource && ((DruidDataSource) ds).getPoolingCount() <= 0) { + busyDataSourceSet.add(ds); + } + } + if (!busyDataSourceSet.isEmpty() && busyDataSourceSet.size() < dataSourceSet.size()) { + for (DataSource ds : busyDataSourceSet) { + dataSourceSet.remove(ds); + } + } + } + + private DataSource getRandomDataSource(Collection dataSourceSet) { + DataSource[] dataSources = dataSourceSet.toArray(new DataSource[] {}); + if (dataSources != null && dataSources.length > 0) { + return dataSources[random.nextInt(dataSourceSet.size())]; + } + return null; + } + public HighAvailableDataSource getHighAvailableDataSource() { return highAvailableDataSource; } diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/StickyRandomDataSourceSelector.java b/src/main/java/com/alibaba/druid/pool/ha/selector/StickyRandomDataSourceSelector.java index a06b8de271..869a4b1404 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/selector/StickyRandomDataSourceSelector.java +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/StickyRandomDataSourceSelector.java @@ -48,7 +48,7 @@ public class StickyRandomDataSourceSelector extends RandomDataSourceSelector { @Override public DataSource get() { StickyDataSourceHolder holder = holders.get(); - if (holder != null && isValid(holder) && !isExpired(holder)) { + if (holder != null && isAvailable(holder)) { LOG.debug("Return the sticky DataSource " + holder.getDataSource().toString() + " directly."); return holder.getDataSource(); } @@ -60,6 +60,14 @@ public class StickyRandomDataSourceSelector extends RandomDataSourceSelector { return dataSource; } + private boolean isAvailable(StickyDataSourceHolder holder) { + boolean flag = isValid(holder) && !isExpired(holder); + if (flag && holder.getDataSource() instanceof DruidDataSource) { + flag = ((DruidDataSource) holder.getDataSource()).getPoolingCount() > 0; + } + return flag; + } + private boolean isValid(StickyDataSourceHolder holder) { boolean flag = holder.isValid() && !getBlacklist().contains(holder.getDataSource()); if (!(holder.getDataSource() instanceof DruidDataSource) || !flag) { From bc29864be67d756f82a0666ac364388805673685 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Sat, 12 Oct 2019 16:16:42 +0800 Subject: [PATCH 02/26] close DataSource if creation failed close the DataSource if creation failed, otherwise the create-thread will try to create Connection and log ERROR in an endless loop --- .../com/alibaba/druid/pool/ha/DataSourceCreator.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java b/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java index e8109d1120..aad52ed234 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java +++ b/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java @@ -66,11 +66,19 @@ public class DataSourceCreator { String username = properties.getProperty(n + ".username"); String password = properties.getProperty(n + ".password"); LOG.info("Creating " + n + " with url[" + url + "] and username[" + username + "]."); + DruidDataSource dataSource = null; try { - DruidDataSource dataSource = create(n, url, username, password, haDataSource); + dataSource = create(n, url, username, password, haDataSource); map.put(n, dataSource); } catch(Exception e) { LOG.error("Can NOT create DruidDataSource for " + n, e); + if (dataSource != null) { + try { + dataSource.close(); + } catch (Exception ex) { + LOG.error("Exception occurred while closing the FAILURE DataSource.", ex); + } + } } } LOG.info(map.size() + " DruidDataSource(s) created. "); @@ -172,7 +180,7 @@ public class DataSourceCreator { try { is.close(); } catch (Exception e) { - // ignore + LOG.debug("Can not close Inputstream.", e); } } } From 89c36661cb8fe453792a359c23158d29e86b8a75 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Sun, 13 Oct 2019 16:22:21 +0800 Subject: [PATCH 03/26] Optimize the Validation logic of RandomDataSource treating normal SQL execution in the pool as validation, so we can skip some validation operations --- .../RandomDataSourceValidateFilter.java | 53 +++++++++++++++++ .../RandomDataSourceValidateThread.java | 57 +++++++++++++++---- .../META-INF/druid-filter.properties | 3 +- 3 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateFilter.java diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateFilter.java b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateFilter.java new file mode 100644 index 0000000000..f0b1541ec4 --- /dev/null +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateFilter.java @@ -0,0 +1,53 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.pool.ha.selector; + +import com.alibaba.druid.filter.FilterEventAdapter; +import com.alibaba.druid.proxy.jdbc.ConnectionProxy; +import com.alibaba.druid.proxy.jdbc.DataSourceProxy; +import com.alibaba.druid.proxy.jdbc.ResultSetProxy; +import com.alibaba.druid.proxy.jdbc.StatementProxy; + +/** + * A Druid Filter that records the last success execute time. + * + * @author DigitalSonic + */ +public class RandomDataSourceValidateFilter extends FilterEventAdapter { + protected void statementExecuteUpdateAfter(StatementProxy statement, String sql, int updateCount) { + recordTime(statement); + } + + protected void statementExecuteQueryAfter(StatementProxy statement, String sql, ResultSetProxy resultSet) { + recordTime(statement); + } + + protected void statementExecuteAfter(StatementProxy statement, String sql, boolean result) { + recordTime(statement); + } + + protected void statementExecuteBatchAfter(StatementProxy statement, int[] result) { + recordTime(statement); + } + + private void recordTime(StatementProxy statement) { + ConnectionProxy conn = statement.getConnectionProxy(); + if (conn != null) { + DataSourceProxy dataSource = conn.getDirectDataSource(); + RandomDataSourceValidateThread.logSuccessTime(dataSource); + } + } +} diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java index 2cc268f468..5a64b5c856 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java @@ -17,7 +17,6 @@ package com.alibaba.druid.pool.ha.selector; import java.sql.Connection; import java.sql.Driver; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -30,9 +29,11 @@ import java.util.concurrent.Executors; import javax.sql.DataSource; import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.proxy.jdbc.DataSourceProxy; import com.alibaba.druid.support.logging.Log; import com.alibaba.druid.support.logging.LogFactory; import com.alibaba.druid.util.JdbcUtils; +import com.alibaba.druid.util.StringUtils; /** * A Thread trying to test all DataSource provided by HADataSource. @@ -42,12 +43,24 @@ import com.alibaba.druid.util.JdbcUtils; */ public class RandomDataSourceValidateThread implements Runnable { private final static Log LOG = LogFactory.getLog(RandomDataSourceValidateThread.class); - private int checkingIntervalSeconds = 15; + private final static Map SUCCESS_TIMES = new ConcurrentHashMap(); + + private int checkingIntervalSeconds = 10; // This value should NOT be too small. private int validationSleepSeconds = 0; private int blacklistThreshold = 3; private RandomDataSourceSelector selector; private ExecutorService checkExecutor = Executors.newFixedThreadPool(5); private Map errorCounts = new ConcurrentHashMap(); + private Map lastCheckTimes = new ConcurrentHashMap(); + + /** + * Provide a static method to record the last success time of a DataSource + */ + public static void logSuccessTime(DataSourceProxy dataSource) { + if (dataSource != null && !StringUtils.isEmpty(dataSource.getName())) { + SUCCESS_TIMES.put(dataSource.getName(), System.currentTimeMillis()); + } + } public RandomDataSourceValidateThread(RandomDataSourceSelector selector) { this.selector = selector; @@ -88,12 +101,13 @@ public class RandomDataSourceValidateThread implements Runnable { private void maintainBlacklist() { Map dataSourceMap = selector.getDataSourceMap(); for (Map.Entry e : errorCounts.entrySet()) { + DataSource dataSource = dataSourceMap.get(e.getKey()); if (e.getValue() <= 0) { - selector.removeBlacklist(dataSourceMap.get(e.getKey())); + selector.removeBlacklist(dataSource); } else if (e.getValue() >= blacklistThreshold - && !selector.containInBlacklist(dataSourceMap.get(e.getKey()))) { + && !selector.containInBlacklist(dataSource)) { LOG.warn("Adding " + e.getKey() + " to blacklist."); - selector.addBlacklist(dataSourceMap.get(e.getKey())); + selector.addBlacklist(dataSource); } } } @@ -115,17 +129,26 @@ public class RandomDataSourceValidateThread implements Runnable { tasks.add(new Callable() { @Override public Boolean call() { - boolean flag = check(e.getKey(), (DruidDataSource) e.getValue()); + String key = e.getKey(); + DruidDataSource dataSource = (DruidDataSource) e.getValue(); + + if (isSkipChecking(key, dataSource)) { + return true; + } + + boolean flag = check(key, dataSource); if (flag) { - errorCounts.put(e.getKey(), 0); + logSuccessTime(dataSource); + errorCounts.put(key, 0); } else { - if (!errorCounts.containsKey(e.getKey())) { - errorCounts.put(e.getKey(), 0); + if (!errorCounts.containsKey(key)) { + errorCounts.put(key, 0); } - int count = errorCounts.get(e.getKey()); - errorCounts.put(e.getKey(), count + 1); + int count = errorCounts.get(key); + errorCounts.put(key, count + 1); } + lastCheckTimes.put(dataSource.getName(), System.currentTimeMillis()); return flag; } @@ -138,6 +161,16 @@ public class RandomDataSourceValidateThread implements Runnable { } } + private boolean isSkipChecking(String key, DruidDataSource dataSource) { + Long lastSuccessTime = SUCCESS_TIMES.get(dataSource.getName()); + Long lastCheckTime = lastCheckTimes.get(dataSource.getName()); + + return lastSuccessTime != null && lastCheckTime != null + && (System.currentTimeMillis() - lastSuccessTime) <= checkingIntervalSeconds * 1000 + && (System.currentTimeMillis() - lastCheckTime) <= 5 * checkingIntervalSeconds * 1000 + && (!errorCounts.containsKey(key) || errorCounts.get(key) < 1); + } + private boolean check(String name, DruidDataSource dataSource) { boolean result = true; Driver driver = dataSource.getRawDriver(); @@ -158,7 +191,7 @@ public class RandomDataSourceValidateThread implements Runnable { conn = driver.connect(url, info); sleepBeforeValidation(); dataSource.validateConnection(conn); - } catch (SQLException e) { + } catch (Exception e) { LOG.warn("Validation FAILED for " + name + " with url [" + url + "] and username [" + info.getProperty("user") + "]. Exception: " + e.getMessage()); result = false; diff --git a/src/main/resources/META-INF/druid-filter.properties b/src/main/resources/META-INF/druid-filter.properties index 5f8cbac322..ca9f2537e1 100644 --- a/src/main/resources/META-INF/druid-filter.properties +++ b/src/main/resources/META-INF/druid-filter.properties @@ -9,4 +9,5 @@ druid.filters.slf4j=com.alibaba.druid.filter.logging.Slf4jLogFilter druid.filters.commonlogging=com.alibaba.druid.filter.logging.CommonsLogFilter druid.filters.commonLogging=com.alibaba.druid.filter.logging.CommonsLogFilter druid.filters.wall=com.alibaba.druid.wall.WallFilter -druid.filters.config=com.alibaba.druid.filter.config.ConfigFilter \ No newline at end of file +druid.filters.config=com.alibaba.druid.filter.config.ConfigFilter +druid.filters.haRandomValidator=com.alibaba.druid.pool.ha.selector.RandomDataSourceValidateFilter \ No newline at end of file From b6e6812d9d43d2f9fd9d401f7aa927be6e9729aa Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Tue, 5 Nov 2019 18:00:00 +0800 Subject: [PATCH 04/26] Update RandomDataSourceValidateThread.java add some logs --- .../selector/RandomDataSourceValidateThread.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java index 5a64b5c856..64b55ce50c 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java @@ -58,7 +58,10 @@ public class RandomDataSourceValidateThread implements Runnable { */ public static void logSuccessTime(DataSourceProxy dataSource) { if (dataSource != null && !StringUtils.isEmpty(dataSource.getName())) { - SUCCESS_TIMES.put(dataSource.getName(), System.currentTimeMillis()); + String name = dataSource.getName(); + long time = System.currentTimeMillis(); + LOG.debug("Log successTime [" + time + "] for " + name); + SUCCESS_TIMES.put(name, time); } } @@ -115,7 +118,7 @@ public class RandomDataSourceValidateThread implements Runnable { private void checkAllDataSources() { Map dataSourceMap = selector.getDataSourceMap(); List> tasks = new ArrayList>(); - + LOG.debug("Checking all DataSource."); for (final Map.Entry e : dataSourceMap.entrySet()) { if (!(e.getValue() instanceof DruidDataSource)) { continue; @@ -136,6 +139,7 @@ public class RandomDataSourceValidateThread implements Runnable { return true; } + LOG.debug("Start checking " + key + "."); boolean flag = check(key, dataSource); if (flag) { @@ -164,10 +168,12 @@ public class RandomDataSourceValidateThread implements Runnable { private boolean isSkipChecking(String key, DruidDataSource dataSource) { Long lastSuccessTime = SUCCESS_TIMES.get(dataSource.getName()); Long lastCheckTime = lastCheckTimes.get(dataSource.getName()); - + long currentTime = System.currentTimeMillis(); + LOG.debug("Connection=" + key + ", lastSuccessTime=" + lastSuccessTime + + ", lastCheckTime=" + lastCheckTime + ", currentTime=" + currentTime); return lastSuccessTime != null && lastCheckTime != null - && (System.currentTimeMillis() - lastSuccessTime) <= checkingIntervalSeconds * 1000 - && (System.currentTimeMillis() - lastCheckTime) <= 5 * checkingIntervalSeconds * 1000 + && (currentTime - lastSuccessTime) <= checkingIntervalSeconds * 1000 + && (currentTime - lastCheckTime) <= 5 * checkingIntervalSeconds * 1000 && (!errorCounts.containsKey(key) || errorCounts.get(key) < 1); } From ea16b58cc000c114607b1fceca3d693d9dae6798 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Tue, 5 Nov 2019 18:00:48 +0800 Subject: [PATCH 05/26] Update RandomDataSourceSelector.java if threads are running, interrupt them first, and then start new threads --- .../pool/ha/selector/RandomDataSourceSelector.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelector.java b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelector.java index 693b3351c5..fdf60993b4 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelector.java +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelector.java @@ -50,6 +50,8 @@ public class RandomDataSourceSelector implements DataSourceSelector { private HighAvailableDataSource highAvailableDataSource; private RandomDataSourceValidateThread validateThread; private RandomDataSourceRecoverThread recoverThread; + private Thread runningValidateThread; + private Thread runningRecoverThread; private int checkingIntervalSeconds = 15; private int recoveryIntervalSeconds = 120; @@ -161,14 +163,22 @@ public class RandomDataSourceSelector implements DataSourceSelector { validateThread.setValidationSleepSeconds(validationSleepSeconds); validateThread.setBlacklistThreshold(blacklistThreshold); } - new Thread(validateThread, "RandomDataSourceSelector-validate-thread").start(); + if (runningValidateThread != null) { + runningValidateThread.interrupt(); + } + runningValidateThread = new Thread(validateThread, "RandomDataSourceSelector-validate-thread"); + runningValidateThread.start(); if (recoverThread == null) { recoverThread = new RandomDataSourceRecoverThread(this); recoverThread.setSleepSeconds(recoveryIntervalSeconds); recoverThread.setValidationSleepSeconds(validationSleepSeconds); } - new Thread(recoverThread, "RandomDataSourceSelector-recover-thread").start(); + if (runningRecoverThread != null) { + runningRecoverThread.interrupt(); + } + runningRecoverThread = new Thread(recoverThread, "RandomDataSourceSelector-recover-thread"); + runningRecoverThread.start(); } private Collection removeBlackList(Map dataSourceMap) { From 4f45e7f8dcb6520fe23379f39b794afa54a81f76 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Tue, 5 Nov 2019 18:01:08 +0800 Subject: [PATCH 06/26] add Testcases --- .../ha/CounterValidConnectionChecker.java | 45 ++++++ ...andomDataSourceSelectorWithFilterTest.java | 137 ++++++++++++++++++ ...mDataSourceSelectorWithValidationTest.java | 23 ++- 3 files changed, 197 insertions(+), 8 deletions(-) create mode 100644 src/test/java/com/alibaba/druid/pool/ha/CounterValidConnectionChecker.java create mode 100644 src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithFilterTest.java diff --git a/src/test/java/com/alibaba/druid/pool/ha/CounterValidConnectionChecker.java b/src/test/java/com/alibaba/druid/pool/ha/CounterValidConnectionChecker.java new file mode 100644 index 0000000000..c40798ee8f --- /dev/null +++ b/src/test/java/com/alibaba/druid/pool/ha/CounterValidConnectionChecker.java @@ -0,0 +1,45 @@ +package com.alibaba.druid.pool.ha; + +import com.alibaba.druid.pool.ValidConnectionChecker; + +import java.sql.Connection; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; + +public class CounterValidConnectionChecker implements ValidConnectionChecker { + private int countBeforeFailure = 100; + private Map counterMap = new ConcurrentHashMap(); + + @Override + public boolean isValidConnection(Connection c, String query, int validationQueryTimeout) throws Exception { + String url = c.getMetaData().getURL(); + if (!counterMap.containsKey(url)) { + counterMap.put(url, 0); + } + Integer counter = counterMap.get(url); + counterMap.put(url, ++counter); + if (counter >= countBeforeFailure) { + return false; + } + + return true; + } + + @Override + public void configFromProperties(Properties properties) { + + } + + public int getCountValue(String url) { + return counterMap.containsKey(url) ? counterMap.get(url) : 0; + } + + public int getCountBeforeFailure() { + return countBeforeFailure; + } + + public void setCountBeforeFailure(int countBeforeFailure) { + this.countBeforeFailure = countBeforeFailure; + } +} diff --git a/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithFilterTest.java b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithFilterTest.java new file mode 100644 index 0000000000..7b42b8337a --- /dev/null +++ b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithFilterTest.java @@ -0,0 +1,137 @@ +package com.alibaba.druid.pool.ha.selector; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.pool.ha.CounterValidConnectionChecker; +import com.alibaba.druid.pool.ha.HighAvailableDataSource; +import com.alibaba.druid.support.logging.Log; +import com.alibaba.druid.support.logging.LogFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class RandomDataSourceSelectorWithFilterTest { + private final static Log LOG = LogFactory.getLog(RandomDataSourceSelectorWithFilterTest.class); + private HighAvailableDataSource highAvailableDataSource; + private CounterValidConnectionChecker checker; + + @Before + public void setUp() throws Exception { + LOG.info("setUp()"); + checker = new CounterValidConnectionChecker(); + highAvailableDataSource = new HighAvailableDataSource(); + String file = "/com/alibaba/druid/pool/ha/ha-datasource.properties"; + highAvailableDataSource.setDataSourceFile(file); + highAvailableDataSource.setFilters("haRandomValidator"); + highAvailableDataSource.init(); + initChecker(); + } + + @After + public void tearDown() { + LOG.info("tearDown()"); + highAvailableDataSource.destroy(); + highAvailableDataSource = null; + checker = null; + } + + @Test + public void testSkipValidation() throws Exception { + RandomDataSourceSelector selector = ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector()); + RandomDataSourceValidateThread thread = new RandomDataSourceValidateThread(selector); + thread.setCheckingIntervalSeconds(2); + selector.setValidateThread(thread); + selector.init(); + + Connection conn = highAvailableDataSource.getConnection(); + createTable(conn); + String url = conn.getMetaData().getURL(); + Thread.sleep(5000); + int initValue = checker.getCountValue(url); + + executeSQL(conn, "select * from foo"); + Thread.sleep(1000); + executeSQL(conn, "select * from foo"); + Thread.sleep(1000); + assertEquals(initValue, checker.getCountValue(url)); + + Thread.sleep(3000); + int value = checker.getCountValue(url); + LOG.info("URL: " + url + " Value: " + value + " Init: " + initValue); + assertEquals(initValue + 1, value); + + Thread.sleep(2000); + value = checker.getCountValue(url); + LOG.info("URL: " + url + " Value: " + value + " Init: " + initValue); + assertEquals(initValue + 2, value); + + conn.close(); + } + + @Test + public void testForceValidation() throws Exception { + RandomDataSourceSelector selector = ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector()); + RandomDataSourceValidateThread thread = new RandomDataSourceValidateThread(selector); + thread.setCheckingIntervalSeconds(2); + selector.setValidateThread(thread); + selector.init(); + + Connection conn = highAvailableDataSource.getConnection(); + createTable(conn); + String url = conn.getMetaData().getURL(); + Thread.sleep(2000); + LOG.info("Start execute SQL"); + int initValue = 0; + int value = 0; + for (int i = 0; i < 10; i++) { + executeSQL(conn, "select * from foo"); + if (initValue == 0) { + initValue = checker.getCountValue(url); + } + Thread.sleep(600); + value = checker.getCountValue(url); + LOG.info("URL: " + url + " Value: " + value + " Init: " + initValue); + assertEquals(initValue, value); + } + LOG.info("URL: " + url + " Value: " + value + " Init: " + initValue); + Thread.sleep(5000); + value = checker.getCountValue(url); + assertTrue(value > initValue + 1); + } + + private void createTable(Connection connection) { + try { + executeSQL(connection, "create table foo (bar varchar(1))"); + } catch(SQLException e) { + try { + executeSQL(connection, "drop table foo"); + executeSQL(connection, "create table foo (bar varchar(1))"); + } catch (SQLException ex) { + // ignore + } + } + } + + private void executeSQL(Connection connection, String sql) throws SQLException { + Statement stmt = connection.createStatement(); + stmt.execute(sql); + LOG.info("Executing " + sql); + stmt.close(); + } + + private void initChecker() { + Map map = highAvailableDataSource.getDataSourceMap(); + for (DataSource ds : map.values()) { + DruidDataSource dds = (DruidDataSource) ds; + dds.setValidConnectionChecker(checker); + } + } +} diff --git a/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java index 50c284818f..2e919b20bb 100644 --- a/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java +++ b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java @@ -30,10 +30,7 @@ public class RandomDataSourceSelectorWithValidationTest { @Test public void testOneDataSourceFailAndRecover() throws Exception { - RandomDataSourceSelector selector = ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector()); - selector.getValidateThread().setCheckingIntervalSeconds(3); - selector.getRecoverThread().setSleepSeconds(3); - selector.init(); + RandomDataSourceSelector selector = initSelector(); DruidDataSource dataSource = (DruidDataSource) highAvailableDataSource.getDataSourceMap().get("foo"); dataSource.setValidationQuery("select xxx from yyy"); @@ -57,10 +54,8 @@ public class RandomDataSourceSelectorWithValidationTest { @Test public void testAllDataSourceFail() throws Exception { - RandomDataSourceSelector selector = (RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector(); - selector.setCheckingIntervalSeconds(3); - selector.setRecoveryIntervalSeconds(3); - selector.init(); + initSelector(); + RandomDataSourceSelector selector = ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector()); DruidDataSource foo = (DruidDataSource) highAvailableDataSource.getDataSourceMap().get("foo"); DruidDataSource bar = (DruidDataSource) highAvailableDataSource.getDataSourceMap().get("bar"); @@ -83,4 +78,16 @@ public class RandomDataSourceSelectorWithValidationTest { assertTrue(count[0] > 0); assertTrue(count[1] > 0); } + + private RandomDataSourceSelector initSelector() { + RandomDataSourceSelector selector = ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector()); + RandomDataSourceValidateThread validateThread = new RandomDataSourceValidateThread(selector); + RandomDataSourceRecoverThread recoverThread = new RandomDataSourceRecoverThread(selector); + validateThread.setCheckingIntervalSeconds(3); + recoverThread.setSleepSeconds(3); + selector.setValidateThread(validateThread); + selector.setRecoverThread(recoverThread); + selector.init(); + return selector; + } } From 781fd69ce6ce1e784ba8ac20ab86a9a2f1dea75f Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Mon, 20 Jan 2020 17:29:05 +0800 Subject: [PATCH 07/26] extract some Properties related method into Utils --- .../druid/pool/ha/DataSourceCreator.java | 69 +----------- .../druid/pool/ha/PropertiesUtils.java | 101 ++++++++++++++++++ 2 files changed, 106 insertions(+), 64 deletions(-) create mode 100644 src/main/java/com/alibaba/druid/pool/ha/PropertiesUtils.java diff --git a/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java b/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java index aad52ed234..bfcaa1f73a 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java +++ b/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java @@ -15,16 +15,10 @@ */ package com.alibaba.druid.pool.ha; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import javax.sql.DataSource; @@ -41,19 +35,19 @@ import com.alibaba.druid.support.logging.LogFactory; public class DataSourceCreator { private final static Log LOG = LogFactory.getLog(DataSourceCreator.class); - private Properties properties = new Properties(); - private List nameList = new ArrayList(); + private Properties properties; + private List nameList; public DataSourceCreator(String file) { this(file, ""); } public DataSourceCreator(String file, String propertyPrefix) { - loadProperties(file); - loadNameList(propertyPrefix); + this.properties = PropertiesUtils.loadProperties(file); + this.nameList = PropertiesUtils.loadNameList(this.properties, propertyPrefix); } - public Map createMap(HighAvailableDataSource haDataSource) throws SQLException { + public Map createMap(HighAvailableDataSource haDataSource) { Map map = new ConcurrentHashMap(); if (nameList == null || nameList.isEmpty()) { @@ -136,59 +130,6 @@ public class DataSourceCreator { return dataSource; } - private void loadNameList(String propertyPrefix) { - Set names = new HashSet(); - for (String n : properties.stringPropertyNames()) { - if (propertyPrefix != null && !propertyPrefix.isEmpty() - && !n.startsWith(propertyPrefix)) { - continue; - } - if (n.endsWith(".url")) { - names.add(n.split("\\.url")[0]); - } - } - if (!names.isEmpty()) { - nameList.addAll(names); - } - } - - private void loadProperties(String file) { - Properties properties = new Properties(); - if (file == null) { - return; - } - InputStream is = null; - try { - LOG.debug("Trying to load " + file + " from FileSystem."); - is = new FileInputStream(file); - } catch(FileNotFoundException e) { - LOG.debug("Trying to load " + file + " from Classpath."); - try { - is = DataSourceCreator.class.getResourceAsStream(file); - } catch (Exception ex) { - LOG.warn("Can not load resource " + file, ex); - } - } - if (is != null) { - try { - properties.load(is); - this.properties = properties; - } catch(Exception e) { - LOG.error("Exception occurred while loading " + file, e); - } finally { - if (is != null) { - try { - is.close(); - } catch (Exception e) { - LOG.debug("Can not close Inputstream.", e); - } - } - } - } else { - LOG.warn("File " + file + " can't be loaded!"); - } - } - public List getNameList() { return nameList; } diff --git a/src/main/java/com/alibaba/druid/pool/ha/PropertiesUtils.java b/src/main/java/com/alibaba/druid/pool/ha/PropertiesUtils.java new file mode 100644 index 0000000000..337a589427 --- /dev/null +++ b/src/main/java/com/alibaba/druid/pool/ha/PropertiesUtils.java @@ -0,0 +1,101 @@ +/* + * Copyright 1999-2020 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.pool.ha; + +import com.alibaba.druid.support.logging.Log; +import com.alibaba.druid.support.logging.LogFactory; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Properties; +import java.util.Set; + +/** + * Utilities for Properties. + * + * @author DigitalSonic + */ +public class PropertiesUtils { + private final static Log LOG = LogFactory.getLog(PropertiesUtils.class); + + /** + * Load properties from the given file into Properties. + */ + public static Properties loadProperties(String file) { + Properties properties = new Properties(); + if (file == null) { + return properties; + } + + InputStream is = null; + try { + LOG.debug("Trying to load " + file + " from FileSystem."); + is = new FileInputStream(file); + } catch(FileNotFoundException e) { + LOG.debug("Trying to load " + file + " from Classpath."); + try { + is = PropertiesUtils.class.getResourceAsStream(file); + } catch (Exception ex) { + LOG.warn("Can not load resource " + file, ex); + } + } + if (is != null) { + try { + properties.load(is); + } catch(Exception e) { + LOG.error("Exception occurred while loading " + file, e); + } finally { + if (is != null) { + try { + is.close(); + } catch (Exception e) { + LOG.debug("Can not close Inputstream.", e); + } + } + } + } else { + LOG.warn("File " + file + " can't be loaded!"); + } + + return properties; + } + + /** + * Pick the name of a JDBC url. Such as xxx.url, xxx is the name. + */ + public static List loadNameList(Properties properties, String propertyPrefix) { + List nameList = new ArrayList(); + + Set names = new HashSet(); + for (String n : properties.stringPropertyNames()) { + if (propertyPrefix != null && !propertyPrefix.isEmpty() + && !n.startsWith(propertyPrefix)) { + continue; + } + if (n.endsWith(".url")) { + names.add(n.split("\\.url")[0]); + } + } + if (!names.isEmpty()) { + nameList.addAll(names); + } + return nameList; + } +} From 75a094c41e384941a5e3646ddaefc544b6b00df6 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Tue, 21 Jan 2020 14:58:19 +0800 Subject: [PATCH 08/26] add a blacklist in HighAvailableDataSource Filter the DataSourceMap with the blacklist before returning --- .../druid/pool/ha/DataSourceCreator.java | 2 +- .../pool/ha/HighAvailableDataSource.java | 31 ++++++++++++++++++- .../ha/selector/NamedDataSourceSelector.java | 2 +- .../ha/selector/RandomDataSourceSelector.java | 9 +++++- .../RandomDataSourceValidateThread.java | 2 +- ...andomDataSourceSelectorWithFilterTest.java | 2 +- ...mDataSourceSelectorWithValidationTest.java | 6 ++-- 7 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java b/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java index bfcaa1f73a..90a4206fa6 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java +++ b/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java @@ -79,7 +79,7 @@ public class DataSourceCreator { return map; } - protected DruidDataSource create(String name, String url, String username, String password, + public static DruidDataSource create(String name, String url, String username, String password, HighAvailableDataSource haDataSource) throws SQLException { DruidDataSource dataSource = new DruidDataSource(); 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 380501e030..8b443a4ce7 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java @@ -23,8 +23,10 @@ import java.sql.SQLFeatureNotSupportedException; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CopyOnWriteArraySet; import java.util.logging.Logger; import javax.sql.DataSource; @@ -87,13 +89,14 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc // Properties copied from DruidAbstractDataSource END private Map dataSourceMap = new ConcurrentHashMap(); + private Set blacklist = new CopyOnWriteArraySet(); private DataSourceSelector selector; private String dataSourceFile = DEFAULT_DATA_SOURCE_FILE; private String propertyPrefix = ""; private volatile boolean inited = false; - public void init() throws SQLException { + public void init() { if (inited) { return; } @@ -114,6 +117,10 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc } } + public void close() { + destroy(); + } + public void destroy() { if (dataSourceMap == null || dataSourceMap.isEmpty()) { return; @@ -166,6 +173,28 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc return dataSourceMap; } + public Map getAvailableDataSourceMap() { + Map map = new ConcurrentHashMap(this.dataSourceMap); + for (String n : blacklist) { + if (map.containsKey(n)) { + map.remove(n); + } + } + return map; + } + + public void addBlackList(String name) { + if (dataSourceMap.containsKey(name)) { + blacklist.add(name); + } else { + LOG.info("Key " + name + " is NOT existed, ignore it."); + } + } + + public void removeBlackList(String name) { + blacklist.remove(name); + } + public void setSelector(String name) { DataSourceSelector selector = DataSourceSelectorFactory.getSelector(name, this); if (selector != null) { diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/NamedDataSourceSelector.java b/src/main/java/com/alibaba/druid/pool/ha/selector/NamedDataSourceSelector.java index 9c485c2118..6ce54f801c 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/selector/NamedDataSourceSelector.java +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/NamedDataSourceSelector.java @@ -51,7 +51,7 @@ public class NamedDataSourceSelector implements DataSourceSelector { return null; } - Map dataSourceMap = highAvailableDataSource.getDataSourceMap(); + Map dataSourceMap = highAvailableDataSource.getAvailableDataSourceMap(); if (dataSourceMap == null || dataSourceMap.isEmpty()) { return null; } diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelector.java b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelector.java index fdf60993b4..c213acdd81 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelector.java +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelector.java @@ -99,13 +99,20 @@ public class RandomDataSourceSelector implements DataSourceSelector { // do nothing } - public Map getDataSourceMap() { + public Map getFullDataSourceMap() { if (highAvailableDataSource != null) { return highAvailableDataSource.getDataSourceMap(); } return new HashMap(); } + public Map getDataSourceMap() { + if (highAvailableDataSource != null) { + return highAvailableDataSource.getAvailableDataSourceMap(); + } + return new HashMap(); + } + public List getBlacklist() { return blacklist; } diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java index 64b55ce50c..088b7a24a5 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java @@ -116,7 +116,7 @@ public class RandomDataSourceValidateThread implements Runnable { } private void checkAllDataSources() { - Map dataSourceMap = selector.getDataSourceMap(); + Map dataSourceMap = selector.getFullDataSourceMap(); List> tasks = new ArrayList>(); LOG.debug("Checking all DataSource."); for (final Map.Entry e : dataSourceMap.entrySet()) { diff --git a/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithFilterTest.java b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithFilterTest.java index 7b42b8337a..0b05b61d9c 100644 --- a/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithFilterTest.java +++ b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithFilterTest.java @@ -128,7 +128,7 @@ public class RandomDataSourceSelectorWithFilterTest { } private void initChecker() { - Map map = highAvailableDataSource.getDataSourceMap(); + Map map = highAvailableDataSource.getAvailableDataSourceMap(); for (DataSource ds : map.values()) { DruidDataSource dds = (DruidDataSource) ds; dds.setValidConnectionChecker(checker); diff --git a/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java index 2e919b20bb..4059600723 100644 --- a/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java +++ b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java @@ -32,7 +32,7 @@ public class RandomDataSourceSelectorWithValidationTest { public void testOneDataSourceFailAndRecover() throws Exception { RandomDataSourceSelector selector = initSelector(); - DruidDataSource dataSource = (DruidDataSource) highAvailableDataSource.getDataSourceMap().get("foo"); + DruidDataSource dataSource = (DruidDataSource) highAvailableDataSource.getAvailableDataSourceMap().get("foo"); dataSource.setValidationQuery("select xxx from yyy"); Thread.sleep(10 * 1000); assertTrue(dataSource.isTestOnReturn()); @@ -57,8 +57,8 @@ public class RandomDataSourceSelectorWithValidationTest { initSelector(); RandomDataSourceSelector selector = ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector()); - DruidDataSource foo = (DruidDataSource) highAvailableDataSource.getDataSourceMap().get("foo"); - DruidDataSource bar = (DruidDataSource) highAvailableDataSource.getDataSourceMap().get("bar"); + DruidDataSource foo = (DruidDataSource) highAvailableDataSource.getAvailableDataSourceMap().get("foo"); + DruidDataSource bar = (DruidDataSource) highAvailableDataSource.getAvailableDataSourceMap().get("bar"); foo.setValidationQuery("select xxx from yyy"); bar.setValidationQuery("select xxx from yyy"); From 830640a11d1a1e1caa597365eddc23e346ccaa22 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Tue, 21 Jan 2020 15:40:46 +0800 Subject: [PATCH 09/26] add a blacklist in HighAvailableDataSource --- .../druid/pool/ha/selector/RandomDataSourceValidateThread.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java index 088b7a24a5..89d35e888e 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceValidateThread.java @@ -102,7 +102,7 @@ public class RandomDataSourceValidateThread implements Runnable { } private void maintainBlacklist() { - Map dataSourceMap = selector.getDataSourceMap(); + Map dataSourceMap = selector.getFullDataSourceMap(); for (Map.Entry e : errorCounts.entrySet()) { DataSource dataSource = dataSourceMap.get(e.getKey()); if (e.getValue() <= 0) { From dd2b46941436683af6724e9fa6566823af224a7b Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Wed, 22 Jan 2020 14:37:32 +0800 Subject: [PATCH 10/26] add a isInBlackList() method --- .../com/alibaba/druid/pool/ha/HighAvailableDataSource.java | 4 ++++ 1 file changed, 4 insertions(+) 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 8b443a4ce7..4cb1a710a6 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java @@ -195,6 +195,10 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc blacklist.remove(name); } + public boolean isInBlackList(String name) { + return blacklist.contains(name); + } + public void setSelector(String name) { DataSourceSelector selector = DataSourceSelectorFactory.getSelector(name, this); if (selector != null) { From 82adaad442d7b7670218c92e88e5604e12b42984 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Wed, 22 Jan 2020 16:48:16 +0800 Subject: [PATCH 11/26] add NodeListener mechanism --- .../druid/pool/ha/node/FileNodeListener.java | 120 +++++++++++ .../alibaba/druid/pool/ha/node/NodeEvent.java | 118 ++++++++++ .../druid/pool/ha/node/NodeEventType.java | 25 +++ .../druid/pool/ha/node/NodeListener.java | 93 ++++++++ .../druid/pool/ha/node/PoolUpdater.java | 201 ++++++++++++++++++ .../pool/ha/node/FileNodeListenerTest.java | 71 +++++++ .../druid/pool/ha/node/NodeEventTest.java | 42 ++++ .../druid/pool/ha/node/PoolUpdaterTest.java | 148 +++++++++++++ .../ha/ha-with-prefix-datasource.properties | 4 + 9 files changed, 822 insertions(+) create mode 100644 src/main/java/com/alibaba/druid/pool/ha/node/FileNodeListener.java create mode 100644 src/main/java/com/alibaba/druid/pool/ha/node/NodeEvent.java create mode 100644 src/main/java/com/alibaba/druid/pool/ha/node/NodeEventType.java create mode 100644 src/main/java/com/alibaba/druid/pool/ha/node/NodeListener.java create mode 100644 src/main/java/com/alibaba/druid/pool/ha/node/PoolUpdater.java create mode 100644 src/test/java/com/alibaba/druid/pool/ha/node/FileNodeListenerTest.java create mode 100644 src/test/java/com/alibaba/druid/pool/ha/node/NodeEventTest.java create mode 100644 src/test/java/com/alibaba/druid/pool/ha/node/PoolUpdaterTest.java diff --git a/src/main/java/com/alibaba/druid/pool/ha/node/FileNodeListener.java b/src/main/java/com/alibaba/druid/pool/ha/node/FileNodeListener.java new file mode 100644 index 0000000000..6ed37cd560 --- /dev/null +++ b/src/main/java/com/alibaba/druid/pool/ha/node/FileNodeListener.java @@ -0,0 +1,120 @@ +/* + * Copyright 1999-2020 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.pool.ha.node; + +import com.alibaba.druid.pool.ha.PropertiesUtils; +import com.alibaba.druid.support.logging.Log; +import com.alibaba.druid.support.logging.LogFactory; + +import java.util.List; +import java.util.Properties; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * A NodeList that monitors the change of a file. + * + * @author DigitalSonic + */ +public class FileNodeListener extends NodeListener { + private final static Log LOG = LogFactory.getLog(FileNodeListener.class); + + private String file = null; + private String prefix = ""; + private int intervalSeconds = 60; + private ScheduledExecutorService executor; + + /** + * Start a Scheduler to check the specified file. + * + * @see #setIntervalSeconds(int) + * @see #update() + */ + @Override + public void init() { + super.init(); + if (intervalSeconds <= 0) { + intervalSeconds = 60; + } + executor = Executors.newScheduledThreadPool(1); + executor.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + try { + update(); + } catch (Exception e) { + LOG.error("Can NOT update the node list.", e); + } + } + }, 0, intervalSeconds, TimeUnit.SECONDS); + } + + /** + * Load the properties file and diff with the stored Properties. + * + * @return A List of the modification + */ + @Override + public List refresh() { + Properties originalProperties = PropertiesUtils.loadProperties(file); + List nameList = PropertiesUtils.loadNameList(originalProperties, prefix); + Properties properties = new Properties(); + for (String n : nameList) { + String url = originalProperties.getProperty(n + ".url"); + String username = originalProperties.getProperty(n + ".username"); + String password = originalProperties.getProperty(n + ".password"); + if (url == null || url.isEmpty() + || username == null || username.isEmpty() + || password == null || password.isEmpty()) { + continue; + } + properties.setProperty(n + ".url", url); + properties.setProperty(n + ".username", username); + properties.setProperty(n + ".password", password); + } + + List events = NodeEvent.getEventListFromProperties(getProperties(), properties); + if (events != null && !events.isEmpty()) { + setProperties(properties); + } + return events; + } + + public int getIntervalSeconds() { + return intervalSeconds; + } + + public void setIntervalSeconds(int intervalSeconds) { + this.intervalSeconds = intervalSeconds; + } + + public String getFile() { + return file; + } + + public void setFile(String file) { + this.file = file; + } + + public String getPrefix() { + return prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } +} diff --git a/src/main/java/com/alibaba/druid/pool/ha/node/NodeEvent.java b/src/main/java/com/alibaba/druid/pool/ha/node/NodeEvent.java new file mode 100644 index 0000000000..eaac382bf1 --- /dev/null +++ b/src/main/java/com/alibaba/druid/pool/ha/node/NodeEvent.java @@ -0,0 +1,118 @@ +/* + * Copyright 1999-2020 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.pool.ha.node; + +import com.alibaba.druid.pool.ha.PropertiesUtils; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +/** + * Define the detail of a node update event. + * + * @author DigitalSonic + */ +public class NodeEvent { + private NodeEventType type; + private String nodeName; + private String url; + private String username; + private String password; + + /** + * Diff the given two Properties. + * + * @return A List of AddEvent and DelEvent + */ + public static List getEventListFromProperties(Properties previous, Properties next) { + List prevNames = PropertiesUtils.loadNameList(previous, ""); + List nextNames = PropertiesUtils.loadNameList(next, ""); + + List namesToAdd = new ArrayList(); + List namesToDel = new ArrayList(); + for (String n : prevNames) { + if (n != null && !n.trim().isEmpty() && !nextNames.contains(n)) { + namesToDel.add(n); + } + } + for (String n : nextNames) { + if (n != null && !n.trim().isEmpty() && !prevNames.contains(n)) { + namesToAdd.add(n); + } + } + + List list = new ArrayList(); + list.addAll(generateEvents(next, namesToAdd, NodeEventType.ADD)); + list.addAll(generateEvents(previous, namesToDel, NodeEventType.DELETE)); + + return list; + } + + private static List generateEvents(Properties properties, List names, NodeEventType type) { + List list = new ArrayList(); + for (String n : names) { + NodeEvent event = new NodeEvent(); + event.setType(type); + event.setNodeName(n); + event.setUrl(properties.getProperty(n + ".url")); + event.setUsername(properties.getProperty(n + ".username")); + event.setPassword(properties.getProperty(n + ".password")); + list.add(event); + } + return list; + } + + public NodeEventType getType() { + return type; + } + + public void setType(NodeEventType type) { + this.type = type; + } + + public String getNodeName() { + return nodeName; + } + + public void setNodeName(String nodeName) { + this.nodeName = nodeName; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/src/main/java/com/alibaba/druid/pool/ha/node/NodeEventType.java b/src/main/java/com/alibaba/druid/pool/ha/node/NodeEventType.java new file mode 100644 index 0000000000..078d1f45a6 --- /dev/null +++ b/src/main/java/com/alibaba/druid/pool/ha/node/NodeEventType.java @@ -0,0 +1,25 @@ +/* + * Copyright 1999-2020 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.pool.ha.node; + +/** + * Enum that specifies the type of a NodeEvent. + * + * @author DigitalSonic + */ +public enum NodeEventType { + ADD, DELETE +} diff --git a/src/main/java/com/alibaba/druid/pool/ha/node/NodeListener.java b/src/main/java/com/alibaba/druid/pool/ha/node/NodeListener.java new file mode 100644 index 0000000000..4f61bef40b --- /dev/null +++ b/src/main/java/com/alibaba/druid/pool/ha/node/NodeListener.java @@ -0,0 +1,93 @@ +/* + * Copyright 1999-2020 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.pool.ha.node; + +import java.util.Date; +import java.util.List; +import java.util.Observable; +import java.util.Observer; +import java.util.Properties; + +/** + * This abstract class defines a listener to monitor the change of DataSource nodes. + * + * @author DigitalSonic + * @see Observable + */ +public abstract class NodeListener extends Observable { + private Properties properties = new Properties(); + private Date lastUpdateTime = null; + private Observer observer = null; + + /** + * The method implements the detail update logic. + */ + public abstract List refresh(); + + /** + * Add the given PoolUpdater as the Observer. + * + * @see #setObserver(Observer) + */ + public void init() { + if (observer == null) { + throw new RuntimeException("No Observer(such as PoolUpdater) specified."); + } + this.addObserver(observer); + } + + /** + * Fire the refresh() method and notify the Observer. + * + * @see #refresh() + */ + public void update() { + List events = refresh(); + if (events != null && !events.isEmpty()) { + this.lastUpdateTime = new Date(); + NodeEvent[] arr = new NodeEvent[events.size()]; + for (int i = 0; i < events.size(); i++) { + arr[i] = events.get(i); + } + this.setChanged(); + this.notifyObservers(arr); + } + } + + public Observer getObserver() { + return observer; + } + + public void setObserver(Observer observer) { + this.observer = observer; + } + + public Date getLastUpdateTime() { + return lastUpdateTime; + } + + public void setLastUpdateTime(Date lastUpdateTime) { + this.lastUpdateTime = lastUpdateTime; + } + + public Properties getProperties() { + return properties; + } + + public void setProperties(Properties properties) { + this.properties = properties; + } +} 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 new file mode 100644 index 0000000000..dde3946929 --- /dev/null +++ b/src/main/java/com/alibaba/druid/pool/ha/node/PoolUpdater.java @@ -0,0 +1,201 @@ +/* + * Copyright 1999-2020 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.pool.ha.node; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.pool.ha.DataSourceCreator; +import com.alibaba.druid.pool.ha.HighAvailableDataSource; +import com.alibaba.druid.support.logging.Log; +import com.alibaba.druid.support.logging.LogFactory; + +import javax.sql.DataSource; +import java.util.HashSet; +import java.util.Map; +import java.util.Observable; +import java.util.Observer; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArraySet; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +/** + * Update the DataSource Connection Pool when notified. + * + * @author DigitalSonic + */ +public class PoolUpdater implements Observer { + 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; + + 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); + } + + public PoolUpdater(HighAvailableDataSource highAvailableDataSource) { + this(); + setHighAvailableDataSource(highAvailableDataSource); + } + + @Override + public void update(Observable o, Object arg) { + if (!(o instanceof NodeListener)) { + return; + } + if (arg == null || !(arg instanceof NodeEvent[])) { + return; + } + NodeEvent[] events = (NodeEvent[]) arg; + + if (events.length <= 0) { + return; + } + + try { + LOG.info("Waiting for Lock to start processing NodeEvents."); + lock.lock(); + LOG.info("Start processing the NodeEvent[" + events.length + "]."); + for (NodeEvent e : events) { + if (e.getType() == NodeEventType.ADD) { + addNode(e); + } else if (e.getType() == NodeEventType.DELETE) { + deleteNode(e); + } + } + } catch (Exception e) { + LOG.error("Exception occurred while updating Pool.", e); + } finally { + lock.unlock(); + } + } + + public void removeDataSources() { + if (nodesToDel == null || nodesToDel.isEmpty()) { + return; + } + try { + lock.lock(); + Map map = highAvailableDataSource.getDataSourceMap(); + Set copySet = new HashSet(nodesToDel); + for (String nodeName : copySet) { + LOG.info("Start removing Node " + nodeName + "."); + if (!map.containsKey(nodeName)) { + LOG.info("Node " + nodeName + " is NOT existed in the map."); + cancelBlacklistNode(nodeName); + continue; + } + DataSource ds = map.get(nodeName); + if (ds instanceof DruidDataSource) { + DruidDataSource dds = (DruidDataSource) ds; + int activeCount = dds.getActiveCount(); // CAUTION, activeCount MAYBE changed! + if (activeCount > 0) { + LOG.warn("Node " + nodeName + " is still running [activeCount=" + activeCount + + "], try next time."); + continue; + } else { + LOG.info("Close Node " + nodeName + " and remove it."); + try { + dds.close(); + } catch (Exception e) { + LOG.error("Exception occurred while closing Node " + nodeName + + ", just remove it.", e); + } + } + } + map.remove(nodeName); // Remove the node directly if it is NOT a DruidDataSource. + cancelBlacklistNode(nodeName); + } + } catch (Exception e) { + LOG.error("Exception occurred while removing DataSources.", e); + } finally { + lock.unlock(); + } + } + + protected void addNode(NodeEvent event) { + String nodeName = event.getNodeName(); + String url = event.getUrl(); + String username = event.getUsername(); + String password = event.getPassword(); + + Map map = highAvailableDataSource.getDataSourceMap(); + if (nodeName == null || nodeName.isEmpty()) { + return; + } + LOG.info("Adding Node " + nodeName + "[url: " + url + ", username: " + username + "]."); + if (map.containsKey(nodeName)) { + cancelBlacklistNode(nodeName); + return; + } + try { + DruidDataSource dataSource = DataSourceCreator.create(nodeName, url, username, + password, this.highAvailableDataSource); + map.put(nodeName, dataSource); + } catch (Exception e) { + LOG.error("Can NOT create DataSource " + nodeName, e); + } + } + + protected void deleteNode(NodeEvent event) { + String nodeName = event.getNodeName(); + Map map = highAvailableDataSource.getDataSourceMap(); + if (nodeName == null || nodeName.isEmpty() || !map.containsKey(nodeName)) { + return; + } + blacklistNode(nodeName); + } + + private void cancelBlacklistNode(String nodeName) { + LOG.info("Cancel the deletion of Node " + nodeName + "."); + nodesToDel.remove(nodeName); + highAvailableDataSource.removeBlackList(nodeName); + } + + private void blacklistNode(String nodeName) { + LOG.info("Deleting Node " + nodeName + ", just add into blacklist."); + nodesToDel.add(nodeName); + highAvailableDataSource.addBlackList(nodeName); + } + + public HighAvailableDataSource getHighAvailableDataSource() { + return highAvailableDataSource; + } + + public void setHighAvailableDataSource(HighAvailableDataSource highAvailableDataSource) { + this.highAvailableDataSource = highAvailableDataSource; + } + + public Set getNodesToDel() { + return nodesToDel; + } +} diff --git a/src/test/java/com/alibaba/druid/pool/ha/node/FileNodeListenerTest.java b/src/test/java/com/alibaba/druid/pool/ha/node/FileNodeListenerTest.java new file mode 100644 index 0000000000..cc9decf9dd --- /dev/null +++ b/src/test/java/com/alibaba/druid/pool/ha/node/FileNodeListenerTest.java @@ -0,0 +1,71 @@ +package com.alibaba.druid.pool.ha.node; + +import org.junit.Test; +import sun.java2d.SurfaceDataProxy; + +import java.util.List; +import java.util.Observable; +import java.util.Observer; +import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.*; + +public class FileNodeListenerTest { + @Test + public void testUpdate() throws InterruptedException { + final CountDownLatch cdl = new CountDownLatch(1); + String file = "/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties"; + FileNodeListener listener = new FileNodeListener(); + listener.setFile(file); + listener.setPrefix("prefix1"); + listener.setObserver(new Observer() { + @Override + public void update(Observable o, Object arg) { + cdl.countDown(); + assertTrue(o instanceof FileNodeListener); + assertTrue(arg instanceof NodeEvent[]); + NodeEvent[] events = (NodeEvent[]) arg; + assertEquals(1, events.length); + assertEquals(NodeEventType.ADD, events[0].getType()); + assertEquals("prefix1.foo", events[0].getNodeName()); + assertEquals("jdbc:derby:memory:foo1;create=true", events[0].getUrl()); + } + }); + listener.init(); + listener.update(); + assertTrue(cdl.await(100, TimeUnit.MILLISECONDS)); + } + + @Test + public void testRefresh_emptyPropertiesMatch() { + String file = "/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties"; + FileNodeListener listener = new FileNodeListener(); + listener.setFile(file); + listener.setPrefix("prefix3"); + List list = listener.refresh(); + + assertTrue(listener.getProperties().isEmpty()); + assertTrue(list.isEmpty()); + } + + @Test + public void testRefresh() { + String file = "/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties"; + FileNodeListener listener = new FileNodeListener(); + listener.setFile(file); + listener.setPrefix("prefix1"); + List list = listener.refresh(); + + Properties properties = listener.getProperties(); + assertEquals(3, properties.size()); + assertEquals("jdbc:derby:memory:foo1;create=true", properties.getProperty("prefix1.foo.url")); + + assertEquals(1, list.size()); + NodeEvent event = list.get(0); + assertEquals(NodeEventType.ADD, event.getType()); + assertEquals("prefix1.foo", event.getNodeName()); + assertEquals("jdbc:derby:memory:foo1;create=true", event.getUrl()); + } +} \ No newline at end of file diff --git a/src/test/java/com/alibaba/druid/pool/ha/node/NodeEventTest.java b/src/test/java/com/alibaba/druid/pool/ha/node/NodeEventTest.java new file mode 100644 index 0000000000..cf61193049 --- /dev/null +++ b/src/test/java/com/alibaba/druid/pool/ha/node/NodeEventTest.java @@ -0,0 +1,42 @@ +package com.alibaba.druid.pool.ha.node; + +import org.junit.Test; + +import java.util.List; +import java.util.Properties; + +import static org.junit.Assert.*; + +public class NodeEventTest { + + @Test + public void testGetEventListFromProperties_emptyProperties() { + Properties p1 = new Properties(); + Properties p2 = new Properties(); + + List list = NodeEvent.getEventListFromProperties(p1, p2); + assertTrue(list.isEmpty()); + + p1.setProperty("foo.url", "foo_url"); + p1.setProperty("foo.username", "foo_username"); + p1.setProperty("foo.password", "foo_password"); + + list = NodeEvent.getEventListFromProperties(p1, p2); + assertEquals(1, list.size()); + NodeEvent event = list.get(0); + assertEquals(NodeEventType.DELETE, event.getType()); + assertEquals("foo", event.getNodeName()); + assertEquals("foo_url", event.getUrl()); + assertEquals("foo_username", event.getUsername()); + assertEquals("foo_password", event.getPassword()); + + list = NodeEvent.getEventListFromProperties(p2, p1); + assertEquals(1, list.size()); + event = list.get(0); + assertEquals(NodeEventType.ADD, event.getType()); + assertEquals("foo", event.getNodeName()); + assertEquals("foo_url", event.getUrl()); + assertEquals("foo_username", event.getUsername()); + assertEquals("foo_password", event.getPassword()); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000000..82022e38c4 --- /dev/null +++ b/src/test/java/com/alibaba/druid/pool/ha/node/PoolUpdaterTest.java @@ -0,0 +1,148 @@ +package com.alibaba.druid.pool.ha.node; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.pool.ha.HighAvailableDataSource; +import com.alibaba.druid.pool.ha.MockDataSource; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import javax.sql.DataSource; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import static org.junit.Assert.*; + +public class PoolUpdaterTest { + private HighAvailableDataSource haDataSource; + private PoolUpdater updater; + + @Before + public void setUp() throws Exception { + haDataSource = new HighAvailableDataSource(); + Map map = new HashMap(); + haDataSource.setDataSourceMap(map); + + updater = new PoolUpdater(haDataSource); + } + + @After + public void tearDown() throws Exception { + haDataSource = null; + updater = null; + } + + @Test + public void testUpdate() { + haDataSource.getDataSourceMap().put("foo", new MockDataSource("foo")); + + NodeEvent event = new NodeEvent(); + event.setNodeName("foo"); + event.setType(NodeEventType.DELETE); + + updater.update(new FileNodeListener(), new NodeEvent[] { event }); + validateDeleteNode(); + + event = new NodeEvent(); + event.setNodeName("foo"); + event.setType(NodeEventType.ADD); + updater.update(new FileNodeListener(), new NodeEvent[] { event }); + assertFalse(updater.getNodesToDel().contains("foo")); + assertFalse(haDataSource.isInBlackList("foo")); + } + + @Test + public void testRemoveDataSources() { + String url = "jdbc:derby:memory:foo;create=true"; + String name = "foo"; + addNode(url, name); + DruidDataSource ds = (DruidDataSource) haDataSource.getDataSourceMap().get(name); + + updater.getNodesToDel().add(name); + haDataSource.addBlackList(name); + + updater.removeDataSources(); + assertTrue(haDataSource.getDataSourceMap().isEmpty()); + assertFalse(haDataSource.isInBlackList(name)); + assertTrue(ds.isClosed()); + } + + @Test + public void testRemoveDataSources_nodeNotExisted() { + DataSource ds = new MockDataSource("foo"); + haDataSource.getDataSourceMap().put("foo", ds); + updater.getNodesToDel().add("bar"); + updater.removeDataSources(); + assertFalse(updater.getNodesToDel().contains("bar")); + assertEquals(1, haDataSource.getDataSourceMap().size()); + } + + @Test + public void testRemoveDataSources_notDruid() { + DataSource ds = new MockDataSource("foo"); + haDataSource.getDataSourceMap().put("foo", ds); + updater.getNodesToDel().add("foo"); + updater.removeDataSources(); + assertFalse(updater.getNodesToDel().contains("foo")); + assertTrue(haDataSource.getDataSourceMap().isEmpty()); + } + + @Test + public void testAddNode() { + String url = "jdbc:derby:memory:foo;create=true"; + String name = "foo"; + addNode(url, name); + String targetUrl = ((DruidDataSource) haDataSource.getDataSourceMap().get(name)).getUrl(); + assertEquals(1, haDataSource.getDataSourceMap().size()); + assertEquals(url, targetUrl); + } + + @Test + public void testAddNode_alreadyExisted() { + DataSource ds = new MockDataSource("foo"); + haDataSource.getDataSourceMap().put("foo", ds); + updater.getNodesToDel().add("foo"); + + addNode("foo_url", "foo"); + + assertFalse(updater.getNodesToDel().contains("foo")); + assertFalse(haDataSource.isInBlackList("foo")); + assertEquals(ds, haDataSource.getDataSourceMap().get("foo")); + } + + @Test + public void testDeleteNode_notExisted() { + NodeEvent event = new NodeEvent(); + event.setNodeName("foo"); + event.setType(NodeEventType.DELETE); + updater.deleteNode(event); + assertTrue(updater.getNodesToDel().isEmpty()); + assertFalse(haDataSource.isInBlackList("foo")); + } + + @Test + public void testDeleteNode() { + haDataSource.getDataSourceMap().put("foo", new MockDataSource("foo")); + + NodeEvent event = new NodeEvent(); + event.setNodeName("foo"); + event.setType(NodeEventType.DELETE); + updater.deleteNode(event); + validateDeleteNode(); + } + + private void validateDeleteNode() { + assertEquals(1, updater.getNodesToDel().size()); + assertTrue(updater.getNodesToDel().contains("foo")); + assertTrue(haDataSource.isInBlackList("foo")); + } + + private void addNode(String url, String name) { + NodeEvent event = new NodeEvent(); + event.setNodeName(name); + event.setUrl(url); + event.setType(NodeEventType.ADD); + updater.addNode(event); + } +} \ No newline at end of file diff --git a/src/test/resources/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties b/src/test/resources/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties index b008c63d9f..ae431a0e65 100644 --- a/src/test/resources/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties +++ b/src/test/resources/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties @@ -1,4 +1,8 @@ prefix1.foo.url=jdbc:derby:memory:foo1;create=true +prefix1.foo.username=foo +prefix1.foo.password=foo_password prefix2.foo.url=jdbc:derby:memory:foo2;create=true +prefix2.foo.username=bar +prefix2.foo.password=bar_password foo.url=jdbc:derby:memory:foo3;create=true bar=jdbc:derby:memory:bar;create=true From e03511cdaaf2fde085e044bf8e1e38b169dad753 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Wed, 22 Jan 2020 16:51:16 +0800 Subject: [PATCH 12/26] rename the NodeEventType to NodeEventTypeEnum --- .../com/alibaba/druid/pool/ha/node/NodeEvent.java | 12 ++++++------ .../{NodeEventType.java => NodeEventTypeEnum.java} | 2 +- .../com/alibaba/druid/pool/ha/node/PoolUpdater.java | 4 ++-- .../druid/pool/ha/node/FileNodeListenerTest.java | 5 ++--- .../alibaba/druid/pool/ha/node/NodeEventTest.java | 4 ++-- .../alibaba/druid/pool/ha/node/PoolUpdaterTest.java | 11 +++++------ 6 files changed, 18 insertions(+), 20 deletions(-) rename src/main/java/com/alibaba/druid/pool/ha/node/{NodeEventType.java => NodeEventTypeEnum.java} (95%) diff --git a/src/main/java/com/alibaba/druid/pool/ha/node/NodeEvent.java b/src/main/java/com/alibaba/druid/pool/ha/node/NodeEvent.java index eaac382bf1..78edb08e65 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/node/NodeEvent.java +++ b/src/main/java/com/alibaba/druid/pool/ha/node/NodeEvent.java @@ -27,7 +27,7 @@ import java.util.Properties; * @author DigitalSonic */ public class NodeEvent { - private NodeEventType type; + private NodeEventTypeEnum type; private String nodeName; private String url; private String username; @@ -56,13 +56,13 @@ public class NodeEvent { } List list = new ArrayList(); - list.addAll(generateEvents(next, namesToAdd, NodeEventType.ADD)); - list.addAll(generateEvents(previous, namesToDel, NodeEventType.DELETE)); + list.addAll(generateEvents(next, namesToAdd, NodeEventTypeEnum.ADD)); + list.addAll(generateEvents(previous, namesToDel, NodeEventTypeEnum.DELETE)); return list; } - private static List generateEvents(Properties properties, List names, NodeEventType type) { + private static List generateEvents(Properties properties, List names, NodeEventTypeEnum type) { List list = new ArrayList(); for (String n : names) { NodeEvent event = new NodeEvent(); @@ -76,11 +76,11 @@ public class NodeEvent { return list; } - public NodeEventType getType() { + public NodeEventTypeEnum getType() { return type; } - public void setType(NodeEventType type) { + public void setType(NodeEventTypeEnum type) { this.type = type; } diff --git a/src/main/java/com/alibaba/druid/pool/ha/node/NodeEventType.java b/src/main/java/com/alibaba/druid/pool/ha/node/NodeEventTypeEnum.java similarity index 95% rename from src/main/java/com/alibaba/druid/pool/ha/node/NodeEventType.java rename to src/main/java/com/alibaba/druid/pool/ha/node/NodeEventTypeEnum.java index 078d1f45a6..a8bab281e3 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/node/NodeEventType.java +++ b/src/main/java/com/alibaba/druid/pool/ha/node/NodeEventTypeEnum.java @@ -20,6 +20,6 @@ package com.alibaba.druid.pool.ha.node; * * @author DigitalSonic */ -public enum NodeEventType { +public enum NodeEventTypeEnum { ADD, DELETE } 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 dde3946929..80c79670ea 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 @@ -86,9 +86,9 @@ public class PoolUpdater implements Observer { lock.lock(); LOG.info("Start processing the NodeEvent[" + events.length + "]."); for (NodeEvent e : events) { - if (e.getType() == NodeEventType.ADD) { + if (e.getType() == NodeEventTypeEnum.ADD) { addNode(e); - } else if (e.getType() == NodeEventType.DELETE) { + } else if (e.getType() == NodeEventTypeEnum.DELETE) { deleteNode(e); } } diff --git a/src/test/java/com/alibaba/druid/pool/ha/node/FileNodeListenerTest.java b/src/test/java/com/alibaba/druid/pool/ha/node/FileNodeListenerTest.java index cc9decf9dd..5a9c7e6a9d 100644 --- a/src/test/java/com/alibaba/druid/pool/ha/node/FileNodeListenerTest.java +++ b/src/test/java/com/alibaba/druid/pool/ha/node/FileNodeListenerTest.java @@ -1,7 +1,6 @@ package com.alibaba.druid.pool.ha.node; import org.junit.Test; -import sun.java2d.SurfaceDataProxy; import java.util.List; import java.util.Observable; @@ -28,7 +27,7 @@ public class FileNodeListenerTest { assertTrue(arg instanceof NodeEvent[]); NodeEvent[] events = (NodeEvent[]) arg; assertEquals(1, events.length); - assertEquals(NodeEventType.ADD, events[0].getType()); + assertEquals(NodeEventTypeEnum.ADD, events[0].getType()); assertEquals("prefix1.foo", events[0].getNodeName()); assertEquals("jdbc:derby:memory:foo1;create=true", events[0].getUrl()); } @@ -64,7 +63,7 @@ public class FileNodeListenerTest { assertEquals(1, list.size()); NodeEvent event = list.get(0); - assertEquals(NodeEventType.ADD, event.getType()); + assertEquals(NodeEventTypeEnum.ADD, event.getType()); assertEquals("prefix1.foo", event.getNodeName()); assertEquals("jdbc:derby:memory:foo1;create=true", event.getUrl()); } diff --git a/src/test/java/com/alibaba/druid/pool/ha/node/NodeEventTest.java b/src/test/java/com/alibaba/druid/pool/ha/node/NodeEventTest.java index cf61193049..1ad4bf03aa 100644 --- a/src/test/java/com/alibaba/druid/pool/ha/node/NodeEventTest.java +++ b/src/test/java/com/alibaba/druid/pool/ha/node/NodeEventTest.java @@ -24,7 +24,7 @@ public class NodeEventTest { list = NodeEvent.getEventListFromProperties(p1, p2); assertEquals(1, list.size()); NodeEvent event = list.get(0); - assertEquals(NodeEventType.DELETE, event.getType()); + assertEquals(NodeEventTypeEnum.DELETE, event.getType()); assertEquals("foo", event.getNodeName()); assertEquals("foo_url", event.getUrl()); assertEquals("foo_username", event.getUsername()); @@ -33,7 +33,7 @@ public class NodeEventTest { list = NodeEvent.getEventListFromProperties(p2, p1); assertEquals(1, list.size()); event = list.get(0); - assertEquals(NodeEventType.ADD, event.getType()); + assertEquals(NodeEventTypeEnum.ADD, event.getType()); assertEquals("foo", event.getNodeName()); assertEquals("foo_url", event.getUrl()); assertEquals("foo_username", event.getUsername()); 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 82022e38c4..b41e021923 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 @@ -10,7 +10,6 @@ import org.junit.Test; import javax.sql.DataSource; import java.util.HashMap; import java.util.Map; -import java.util.Set; import static org.junit.Assert.*; @@ -39,14 +38,14 @@ public class PoolUpdaterTest { NodeEvent event = new NodeEvent(); event.setNodeName("foo"); - event.setType(NodeEventType.DELETE); + event.setType(NodeEventTypeEnum.DELETE); updater.update(new FileNodeListener(), new NodeEvent[] { event }); validateDeleteNode(); event = new NodeEvent(); event.setNodeName("foo"); - event.setType(NodeEventType.ADD); + event.setType(NodeEventTypeEnum.ADD); updater.update(new FileNodeListener(), new NodeEvent[] { event }); assertFalse(updater.getNodesToDel().contains("foo")); assertFalse(haDataSource.isInBlackList("foo")); @@ -115,7 +114,7 @@ public class PoolUpdaterTest { public void testDeleteNode_notExisted() { NodeEvent event = new NodeEvent(); event.setNodeName("foo"); - event.setType(NodeEventType.DELETE); + event.setType(NodeEventTypeEnum.DELETE); updater.deleteNode(event); assertTrue(updater.getNodesToDel().isEmpty()); assertFalse(haDataSource.isInBlackList("foo")); @@ -127,7 +126,7 @@ public class PoolUpdaterTest { NodeEvent event = new NodeEvent(); event.setNodeName("foo"); - event.setType(NodeEventType.DELETE); + event.setType(NodeEventTypeEnum.DELETE); updater.deleteNode(event); validateDeleteNode(); } @@ -142,7 +141,7 @@ public class PoolUpdaterTest { NodeEvent event = new NodeEvent(); event.setNodeName(name); event.setUrl(url); - event.setType(NodeEventType.ADD); + event.setType(NodeEventTypeEnum.ADD); updater.addNode(event); } } \ No newline at end of file From cff870e80ed1a78b1dbf13fb40d2172499b4a96c Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Wed, 22 Jan 2020 18:46:20 +0800 Subject: [PATCH 13/26] Refactor Pool creation logic 1. Refactor the create logic to use NodeListener instead of DataSourceCreator.createMap 2. Add destroy methods to NodeListener and PoolUpdater 3. Add a lock to prevent Node update concurrently --- .../druid/pool/ha/DataSourceCreator.java | 57 ------- .../pool/ha/HighAvailableDataSource.java | 158 +++++++++++------- .../druid/pool/ha/node/FileNodeListener.java | 34 +++- .../druid/pool/ha/node/NodeListener.java | 37 +++- .../druid/pool/ha/node/PoolUpdater.java | 35 +++- .../druid/pool/ha/DataSourceCreatorTest.java | 57 ------- .../BaseRandomDataSourceSelectorTest.java | 1 + .../selector/NamedDataSourceSelectorTest.java | 1 + ...mDataSourceSelectorWithValidationTest.java | 1 + 9 files changed, 193 insertions(+), 188 deletions(-) delete mode 100644 src/test/java/com/alibaba/druid/pool/ha/DataSourceCreatorTest.java diff --git a/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java b/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java index 90a4206fa6..ea9b9b3203 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java +++ b/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java @@ -16,16 +16,8 @@ package com.alibaba.druid.pool.ha; import java.sql.SQLException; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.concurrent.ConcurrentHashMap; - -import javax.sql.DataSource; import com.alibaba.druid.pool.DruidDataSource; -import com.alibaba.druid.support.logging.Log; -import com.alibaba.druid.support.logging.LogFactory; /** * An utility class to create DruidDataSource dynamically. @@ -33,51 +25,6 @@ import com.alibaba.druid.support.logging.LogFactory; * @author DigitalSonic */ public class DataSourceCreator { - private final static Log LOG = LogFactory.getLog(DataSourceCreator.class); - - private Properties properties; - private List nameList; - - public DataSourceCreator(String file) { - this(file, ""); - } - - public DataSourceCreator(String file, String propertyPrefix) { - this.properties = PropertiesUtils.loadProperties(file); - this.nameList = PropertiesUtils.loadNameList(this.properties, propertyPrefix); - } - - public Map createMap(HighAvailableDataSource haDataSource) { - Map map = new ConcurrentHashMap(); - - if (nameList == null || nameList.isEmpty()) { - LOG.error("No DataSource will be created!"); - return map; - } - - for (String n : nameList) { - String url = properties.getProperty(n + ".url"); - String username = properties.getProperty(n + ".username"); - String password = properties.getProperty(n + ".password"); - LOG.info("Creating " + n + " with url[" + url + "] and username[" + username + "]."); - DruidDataSource dataSource = null; - try { - dataSource = create(n, url, username, password, haDataSource); - map.put(n, dataSource); - } catch(Exception e) { - LOG.error("Can NOT create DruidDataSource for " + n, e); - if (dataSource != null) { - try { - dataSource.close(); - } catch (Exception ex) { - LOG.error("Exception occurred while closing the FAILURE DataSource.", ex); - } - } - } - } - LOG.info(map.size() + " DruidDataSource(s) created. "); - return map; - } public static DruidDataSource create(String name, String url, String username, String password, HighAvailableDataSource haDataSource) throws SQLException { @@ -129,8 +76,4 @@ public class DataSourceCreator { return dataSource; } - - public List getNameList() { - return nameList; - } } 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 4cb1a710a6..2adfedfcb9 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java @@ -35,6 +35,9 @@ import com.alibaba.druid.filter.Filter; import com.alibaba.druid.pool.DruidAbstractDataSource; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.WrapperAdapter; +import com.alibaba.druid.pool.ha.node.FileNodeListener; +import com.alibaba.druid.pool.ha.node.NodeListener; +import com.alibaba.druid.pool.ha.node.PoolUpdater; import com.alibaba.druid.pool.ha.selector.DataSourceSelector; import com.alibaba.druid.pool.ha.selector.DataSourceSelectorEnum; import com.alibaba.druid.pool.ha.selector.DataSourceSelectorFactory; @@ -96,6 +99,9 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc private volatile boolean inited = false; + private PoolUpdater poolUpdater = new PoolUpdater(this); + private NodeListener nodeListener; + public void init() { if (inited) { return; @@ -105,7 +111,7 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc return; } if (dataSourceMap == null || dataSourceMap.isEmpty()) { - dataSourceMap = new DataSourceCreator(dataSourceFile, propertyPrefix).createMap(this); + createNodeMap(); } if (selector == null) { setSelector(DataSourceSelectorEnum.RANDOM.getName()); @@ -122,6 +128,12 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc } public void destroy() { + if (nodeListener != null) { + nodeListener.destroy(); + } + if (poolUpdater != null) { + poolUpdater.destroy(); + } if (dataSourceMap == null || dataSourceMap.isEmpty()) { return; } @@ -211,29 +223,11 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc return selector == null ? null : selector.getName(); } - public DataSourceSelector getDataSourceSelector() { - return this.selector; - } - - public void setDataSourceSelector(DataSourceSelector dataSourceSelector) { - this.selector = dataSourceSelector; - } - @Override public Connection getConnection(String username, String password) throws SQLException { throw new UnsupportedOperationException("Not supported by HighAvailableDataSource."); } - @Override - public PrintWriter getLogWriter() throws SQLException { - return logWriter; - } - - @Override - public void setLogWriter(PrintWriter out) throws SQLException { - this.logWriter = out; - } - @Override public void setLoginTimeout(int seconds) { DriverManager.setLoginTimeout(seconds); @@ -248,16 +242,34 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc throw new SQLFeatureNotSupportedException(); } - public String getDriverClassName() { - return driverClassName; - } + public void setConnectionProperties(String connectionProperties) { + this.connectionProperties = connectionProperties; - public void setDriverClassName(String driverClassName) { - this.driverClassName = driverClassName; - } + // COPIED FROM DruidAbstractDataSource.setConnectionProperties() - public Properties getConnectProperties() { - return connectProperties; + if (connectionProperties == null || connectionProperties.trim().length() == 0) { + setConnectProperties(null); + return; + } + + String[] entries = connectionProperties.split(";"); + Properties properties = new Properties(); + for (int i = 0; i < entries.length; i++) { + String entry = entries[i]; + if (entry.length() > 0) { + int index = entry.indexOf('='); + if (index > 0) { + String name = entry.substring(0, index); + String value = entry.substring(index + 1); + properties.setProperty(name, value); + } else { + // no value is empty string which is how java.util.Properties works + properties.setProperty(entry, ""); + } + } + } + + setConnectProperties(properties); } public void setConnectProperties(Properties connectProperties) { @@ -272,6 +284,68 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc } } + private void createNodeMap() { + if (nodeListener == null) { + // Compatiable with the old version. + // Create a FileNodeListener to watch the dataSourceFile. + FileNodeListener listener = new FileNodeListener(); + listener.setFile(dataSourceFile); + listener.setPrefix(propertyPrefix); + listener.setObserver(poolUpdater); + listener.init(); + nodeListener = listener; + } + nodeListener.update(); // Do update in the current Thread at the startup + } + + // Getters & Setters + + public PoolUpdater getPoolUpdater() { + return poolUpdater; + } + + public void setPoolUpdater(PoolUpdater poolUpdater) { + this.poolUpdater = poolUpdater; + } + + public NodeListener getNodeListener() { + return nodeListener; + } + + public void setNodeListener(NodeListener nodeListener) { + this.nodeListener = nodeListener; + } + + public DataSourceSelector getDataSourceSelector() { + return this.selector; + } + + public void setDataSourceSelector(DataSourceSelector dataSourceSelector) { + this.selector = dataSourceSelector; + } + + @Override + public PrintWriter getLogWriter() throws SQLException { + return logWriter; + } + + @Override + public void setLogWriter(PrintWriter out) throws SQLException { + this.logWriter = out; + } + + public String getDriverClassName() { + return driverClassName; + } + + public void setDriverClassName(String driverClassName) { + this.driverClassName = driverClassName; + } + + public Properties getConnectProperties() { + return connectProperties; + } + public int getInitialSize() { return initialSize; } @@ -452,36 +526,6 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc return connectionProperties; } - public void setConnectionProperties(String connectionProperties) { - this.connectionProperties = connectionProperties; - - // COPIED FROM DruidAbstractDataSource.setConnectionProperties() - - if (connectionProperties == null || connectionProperties.trim().length() == 0) { - setConnectProperties(null); - return; - } - - String[] entries = connectionProperties.split(";"); - Properties properties = new Properties(); - for (int i = 0; i < entries.length; i++) { - String entry = entries[i]; - if (entry.length() > 0) { - int index = entry.indexOf('='); - if (index > 0) { - String name = entry.substring(0, index); - String value = entry.substring(index + 1); - properties.setProperty(name, value); - } else { - // no value is empty string which is how java.util.Properties works - properties.setProperty(entry, ""); - } - } - } - - setConnectProperties(properties); - } - public String getFilters() { return filters; } diff --git a/src/main/java/com/alibaba/druid/pool/ha/node/FileNodeListener.java b/src/main/java/com/alibaba/druid/pool/ha/node/FileNodeListener.java index 6ed37cd560..6005135d49 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/node/FileNodeListener.java +++ b/src/main/java/com/alibaba/druid/pool/ha/node/FileNodeListener.java @@ -60,7 +60,7 @@ public class FileNodeListener extends NodeListener { LOG.error("Can NOT update the node list.", e); } } - }, 0, intervalSeconds, TimeUnit.SECONDS); + }, intervalSeconds, intervalSeconds, TimeUnit.SECONDS); } /** @@ -77,14 +77,22 @@ public class FileNodeListener extends NodeListener { String url = originalProperties.getProperty(n + ".url"); String username = originalProperties.getProperty(n + ".username"); String password = originalProperties.getProperty(n + ".password"); - if (url == null || url.isEmpty() - || username == null || username.isEmpty() - || password == null || password.isEmpty()) { + if (url == null || url.isEmpty()) { + LOG.warn(n + ".url is EMPTY! IGNORE!"); continue; + } else { + properties.setProperty(n + ".url", url); + } + if (username == null || username.isEmpty()) { + LOG.warn(n + ".username is EMPTY. Maybe you should check the config."); + } else { + properties.setProperty(n + ".username", username); + } + if (password == null || password.isEmpty()) { + LOG.warn(n + ".password is EMPTY. Maybe you should check the config."); + } else { + properties.setProperty(n + ".password", password); } - properties.setProperty(n + ".url", url); - properties.setProperty(n + ".username", username); - properties.setProperty(n + ".password", password); } List events = NodeEvent.getEventListFromProperties(getProperties(), properties); @@ -94,6 +102,18 @@ public class FileNodeListener extends NodeListener { return events; } + @Override + public void destroy() { + if (executor == null || executor.isShutdown()) { + return; + } + try { + executor.shutdown(); + } catch (Exception e) { + LOG.error("Can NOT shutdown the ScheduledExecutorService.", e); + } + } + public int getIntervalSeconds() { return intervalSeconds; } diff --git a/src/main/java/com/alibaba/druid/pool/ha/node/NodeListener.java b/src/main/java/com/alibaba/druid/pool/ha/node/NodeListener.java index 4f61bef40b..f91b90c056 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/node/NodeListener.java +++ b/src/main/java/com/alibaba/druid/pool/ha/node/NodeListener.java @@ -15,11 +15,16 @@ */ package com.alibaba.druid.pool.ha.node; +import com.alibaba.druid.support.logging.Log; +import com.alibaba.druid.support.logging.LogFactory; + import java.util.Date; import java.util.List; import java.util.Observable; import java.util.Observer; import java.util.Properties; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; /** * This abstract class defines a listener to monitor the change of DataSource nodes. @@ -28,15 +33,23 @@ import java.util.Properties; * @see Observable */ public abstract class NodeListener extends Observable { + private final static Log LOG = LogFactory.getLog(NodeListener.class); + private Properties properties = new Properties(); private Date lastUpdateTime = null; private Observer observer = null; + private Lock lock = new ReentrantLock(); /** * The method implements the detail update logic. */ public abstract List refresh(); + /** + * Do some cleanup. + */ + public abstract void destroy(); + /** * Add the given PoolUpdater as the Observer. * @@ -55,15 +68,23 @@ public abstract class NodeListener extends Observable { * @see #refresh() */ public void update() { - List events = refresh(); - if (events != null && !events.isEmpty()) { - this.lastUpdateTime = new Date(); - NodeEvent[] arr = new NodeEvent[events.size()]; - for (int i = 0; i < events.size(); i++) { - arr[i] = events.get(i); + if (!lock.tryLock()) { + LOG.info("Can not acquire the lock, skip this time."); + return; + } + try { + List events = refresh(); + if (events != null && !events.isEmpty()) { + this.lastUpdateTime = new Date(); + NodeEvent[] arr = new NodeEvent[events.size()]; + for (int i = 0; i < events.size(); i++) { + arr[i] = events.get(i); + } + this.setChanged(); + this.notifyObservers(arr); } - this.setChanged(); - this.notifyObservers(arr); + } finally { + lock.unlock(); } } 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 80c79670ea..7977bac637 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 @@ -48,6 +48,9 @@ public class PoolUpdater implements Observer { private Lock lock = new ReentrantLock(); private ScheduledExecutorService executor; + /** + * Create a ScheduledExecutorService to remove unused DataSources. + */ public PoolUpdater() { executor = Executors.newScheduledThreadPool(1); executor.scheduleAtFixedRate(new Runnable() { @@ -62,11 +65,28 @@ public class PoolUpdater implements Observer { }, 60, 60, TimeUnit.SECONDS); } + /** + * @see #PoolUpdater() + */ public PoolUpdater(HighAvailableDataSource highAvailableDataSource) { this(); setHighAvailableDataSource(highAvailableDataSource); } + public void destroy() { + if (executor == null || executor.isShutdown()) { + return; + } + try { + executor.shutdown(); + } catch (Exception e) { + LOG.error("Can NOT shutdown the ScheduledExecutorService.", e); + } + } + + /** + * Process the given NodeEvent[]. Maybe add / delete some nodes. + */ @Override public void update(Observable o, Object arg) { if (!(o instanceof NodeListener)) { @@ -99,6 +119,9 @@ public class PoolUpdater implements Observer { } } + /** + * Remove unused DataSources. + */ public void removeDataSources() { if (nodesToDel == null || nodesToDel.isEmpty()) { return; @@ -157,12 +180,20 @@ public class PoolUpdater implements Observer { cancelBlacklistNode(nodeName); return; } + DruidDataSource dataSource = null; try { - DruidDataSource dataSource = DataSourceCreator.create(nodeName, url, username, + dataSource = DataSourceCreator.create(nodeName, url, username, password, this.highAvailableDataSource); map.put(nodeName, dataSource); } catch (Exception e) { - LOG.error("Can NOT create DataSource " + nodeName, e); + LOG.error("Can NOT create DataSource " + nodeName + ". IGNORE IT.", e); + if (dataSource != null) { + try { + dataSource.close(); + } catch (Exception ex) { + LOG.error("Exception occurred while closing the FAILURE DataSource.", ex); + } + } } } diff --git a/src/test/java/com/alibaba/druid/pool/ha/DataSourceCreatorTest.java b/src/test/java/com/alibaba/druid/pool/ha/DataSourceCreatorTest.java deleted file mode 100644 index 76a4dfc08d..0000000000 --- a/src/test/java/com/alibaba/druid/pool/ha/DataSourceCreatorTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.alibaba.druid.pool.ha; - -import org.junit.Test; - -import javax.sql.DataSource; -import java.util.List; -import java.util.Map; - -import static org.junit.Assert.*; - -public class DataSourceCreatorTest { - @Test - public void testNameList() { - String file = "/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties"; - DataSourceCreator creator = new DataSourceCreator(file, "prefix1"); - List list = creator.getNameList(); - assertFalse(list.isEmpty()); - assertEquals(1, list.size()); - assertEquals("prefix1.foo", list.get(0)); - - creator = new DataSourceCreator(file); - list = creator.getNameList(); - assertEquals(3, list.size()); - for (String k : list) { - assertNotEquals("bar", k); - } - } - - @Test - public void createMap() throws Exception { - HighAvailableDataSource highAvailableDataSource = new HighAvailableDataSource(); - String file = "/com/alibaba/druid/pool/ha/ha-datasource.properties"; - DataSourceCreator creator = new DataSourceCreator(file); - Map map = creator.createMap(highAvailableDataSource); - assertEquals(2, map.size()); - assertNotNull(map.get("foo")); - assertNotNull(map.get("bar")); - } - - @Test - public void createMapWithPrefix() throws Exception { - HighAvailableDataSource highAvailableDataSource = new HighAvailableDataSource(); - String file = "/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties"; - DataSourceCreator creator = new DataSourceCreator(file, "prefix1"); - Map map = creator.createMap(highAvailableDataSource); - assertEquals(1, map.size()); - assertNotNull(map.get("prefix1.foo")); - assertNull(map.get("prefix2.foo")); - - creator = new DataSourceCreator(file, "prefix2"); - map = creator.createMap(highAvailableDataSource); - assertEquals(1, map.size()); - assertNull(map.get("prefix1.foo")); - assertNotNull(map.get("prefix2.foo")); - } - -} \ No newline at end of file diff --git a/src/test/java/com/alibaba/druid/pool/ha/selector/BaseRandomDataSourceSelectorTest.java b/src/test/java/com/alibaba/druid/pool/ha/selector/BaseRandomDataSourceSelectorTest.java index 78a6ae9e38..4b5cddbf3d 100644 --- a/src/test/java/com/alibaba/druid/pool/ha/selector/BaseRandomDataSourceSelectorTest.java +++ b/src/test/java/com/alibaba/druid/pool/ha/selector/BaseRandomDataSourceSelectorTest.java @@ -26,6 +26,7 @@ public class BaseRandomDataSourceSelectorTest { @After public void tearDown() { dataSourceMap = null; + dataSource.destroy(); dataSource = null; } } diff --git a/src/test/java/com/alibaba/druid/pool/ha/selector/NamedDataSourceSelectorTest.java b/src/test/java/com/alibaba/druid/pool/ha/selector/NamedDataSourceSelectorTest.java index 41e552de1a..6052ad4098 100644 --- a/src/test/java/com/alibaba/druid/pool/ha/selector/NamedDataSourceSelectorTest.java +++ b/src/test/java/com/alibaba/druid/pool/ha/selector/NamedDataSourceSelectorTest.java @@ -29,6 +29,7 @@ public class NamedDataSourceSelectorTest { @After public void tearDown() { dataSourceMap = null; + dataSource.destroy(); dataSource = null; } diff --git a/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java index 4059600723..9367973879 100644 --- a/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java +++ b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorWithValidationTest.java @@ -25,6 +25,7 @@ public class RandomDataSourceSelectorWithValidationTest { @After public void tearDown() { + highAvailableDataSource.destroy(); highAvailableDataSource = null; } From 8c57759eaa09ff8ef41aba9e16cdfcbb528c1379 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Tue, 28 Jan 2020 20:14:16 +0800 Subject: [PATCH 14/26] 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 From 6b884941fb558d41c9167682f41aa616b1eede89 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Mon, 3 Feb 2020 16:48:22 +0800 Subject: [PATCH 15/26] Some small refactor --- .../druid/pool/ha/PropertiesUtils.java | 14 +++++++++ .../druid/pool/ha/node/FileNodeListener.java | 11 ++++++- .../alibaba/druid/pool/ha/node/NodeEvent.java | 4 +-- .../druid/pool/ha/node/NodeListener.java | 31 +++++++++---------- .../druid/pool/ha/node/NodeEventTest.java | 6 ++-- 5 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/alibaba/druid/pool/ha/PropertiesUtils.java b/src/main/java/com/alibaba/druid/pool/ha/PropertiesUtils.java index 337a589427..06ad20928e 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/PropertiesUtils.java +++ b/src/main/java/com/alibaba/druid/pool/ha/PropertiesUtils.java @@ -17,6 +17,7 @@ package com.alibaba.druid.pool.ha; import com.alibaba.druid.support.logging.Log; import com.alibaba.druid.support.logging.LogFactory; +import com.alibaba.druid.util.StringUtils; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -98,4 +99,17 @@ public class PropertiesUtils { } return nameList; } + + public static Properties filterPrefix(Properties properties, String prefix) { + if (properties == null || prefix == null || prefix.isEmpty()) { + return properties; + } + Properties result = new Properties(); + for (String n : properties.stringPropertyNames()) { + if (n.startsWith(prefix)) { + result.setProperty(n, properties.getProperty(n)); + } + } + return result; + } } diff --git a/src/main/java/com/alibaba/druid/pool/ha/node/FileNodeListener.java b/src/main/java/com/alibaba/druid/pool/ha/node/FileNodeListener.java index 6005135d49..2bc1b9cef2 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/node/FileNodeListener.java +++ b/src/main/java/com/alibaba/druid/pool/ha/node/FileNodeListener.java @@ -24,6 +24,8 @@ import java.util.Properties; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; /** * A NodeList that monitors the change of a file. @@ -33,6 +35,7 @@ import java.util.concurrent.TimeUnit; public class FileNodeListener extends NodeListener { private final static Log LOG = LogFactory.getLog(FileNodeListener.class); + private Lock lock = new ReentrantLock(); private String file = null; private String prefix = ""; private int intervalSeconds = 60; @@ -54,10 +57,16 @@ public class FileNodeListener extends NodeListener { executor.scheduleAtFixedRate(new Runnable() { @Override public void run() { + if (!lock.tryLock()) { + LOG.info("Can not acquire the lock, skip this time."); + return; + } try { update(); } catch (Exception e) { LOG.error("Can NOT update the node list.", e); + } finally { + lock.unlock(); } } }, intervalSeconds, intervalSeconds, TimeUnit.SECONDS); @@ -95,7 +104,7 @@ public class FileNodeListener extends NodeListener { } } - List events = NodeEvent.getEventListFromProperties(getProperties(), properties); + List events = NodeEvent.getEventsByDiffProperties(getProperties(), properties); if (events != null && !events.isEmpty()) { setProperties(properties); } diff --git a/src/main/java/com/alibaba/druid/pool/ha/node/NodeEvent.java b/src/main/java/com/alibaba/druid/pool/ha/node/NodeEvent.java index 78edb08e65..51f7b3293f 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/node/NodeEvent.java +++ b/src/main/java/com/alibaba/druid/pool/ha/node/NodeEvent.java @@ -38,7 +38,7 @@ public class NodeEvent { * * @return A List of AddEvent and DelEvent */ - public static List getEventListFromProperties(Properties previous, Properties next) { + public static List getEventsByDiffProperties(Properties previous, Properties next) { List prevNames = PropertiesUtils.loadNameList(previous, ""); List nextNames = PropertiesUtils.loadNameList(next, ""); @@ -62,7 +62,7 @@ public class NodeEvent { return list; } - private static List generateEvents(Properties properties, List names, NodeEventTypeEnum type) { + public static List generateEvents(Properties properties, List names, NodeEventTypeEnum type) { List list = new ArrayList(); for (String n : names) { NodeEvent event = new NodeEvent(); diff --git a/src/main/java/com/alibaba/druid/pool/ha/node/NodeListener.java b/src/main/java/com/alibaba/druid/pool/ha/node/NodeListener.java index f91b90c056..f9250ac7e3 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/node/NodeListener.java +++ b/src/main/java/com/alibaba/druid/pool/ha/node/NodeListener.java @@ -66,25 +66,24 @@ public abstract class NodeListener extends Observable { * Fire the refresh() method and notify the Observer. * * @see #refresh() + * @see #update(List) */ public void update() { - if (!lock.tryLock()) { - LOG.info("Can not acquire the lock, skip this time."); - return; - } - try { - List events = refresh(); - if (events != null && !events.isEmpty()) { - this.lastUpdateTime = new Date(); - NodeEvent[] arr = new NodeEvent[events.size()]; - for (int i = 0; i < events.size(); i++) { - arr[i] = events.get(i); - } - this.setChanged(); - this.notifyObservers(arr); + update(refresh()); + } + + /** + * Notify the Observer. + */ + public void update(List events) { + if (events != null && !events.isEmpty()) { + this.lastUpdateTime = new Date(); + NodeEvent[] arr = new NodeEvent[events.size()]; + for (int i = 0; i < events.size(); i++) { + arr[i] = events.get(i); } - } finally { - lock.unlock(); + this.setChanged(); + this.notifyObservers(arr); } } diff --git a/src/test/java/com/alibaba/druid/pool/ha/node/NodeEventTest.java b/src/test/java/com/alibaba/druid/pool/ha/node/NodeEventTest.java index 1ad4bf03aa..f24ed7f535 100644 --- a/src/test/java/com/alibaba/druid/pool/ha/node/NodeEventTest.java +++ b/src/test/java/com/alibaba/druid/pool/ha/node/NodeEventTest.java @@ -14,14 +14,14 @@ public class NodeEventTest { Properties p1 = new Properties(); Properties p2 = new Properties(); - List list = NodeEvent.getEventListFromProperties(p1, p2); + List list = NodeEvent.getEventsByDiffProperties(p1, p2); assertTrue(list.isEmpty()); p1.setProperty("foo.url", "foo_url"); p1.setProperty("foo.username", "foo_username"); p1.setProperty("foo.password", "foo_password"); - list = NodeEvent.getEventListFromProperties(p1, p2); + list = NodeEvent.getEventsByDiffProperties(p1, p2); assertEquals(1, list.size()); NodeEvent event = list.get(0); assertEquals(NodeEventTypeEnum.DELETE, event.getType()); @@ -30,7 +30,7 @@ public class NodeEventTest { assertEquals("foo_username", event.getUsername()); assertEquals("foo_password", event.getPassword()); - list = NodeEvent.getEventListFromProperties(p2, p1); + list = NodeEvent.getEventsByDiffProperties(p2, p1); assertEquals(1, list.size()); event = list.get(0); assertEquals(NodeEventTypeEnum.ADD, event.getType()); From 7f43c62cb60855bd2184eb2472b4212fe4e16653 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Tue, 4 Feb 2020 17:01:39 +0800 Subject: [PATCH 16/26] add a NodeRegister to register Node to ZK --- pom.xml | 13 +- .../druid/pool/ha/node/FileNodeListener.java | 3 + .../druid/pool/ha/node/ZookeeperNodeInfo.java | 74 +++++++++ .../pool/ha/node/ZookeeperNodeRegister.java | 156 ++++++++++++++++++ .../ha/node/ZookeeperNodeRegisterTest.java | 103 ++++++++++++ 5 files changed, 348 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/alibaba/druid/pool/ha/node/ZookeeperNodeInfo.java create mode 100644 src/main/java/com/alibaba/druid/pool/ha/node/ZookeeperNodeRegister.java create mode 100644 src/test/java/com/alibaba/druid/pool/ha/node/ZookeeperNodeRegisterTest.java diff --git a/pom.xml b/pom.xml index 4d4db226ae..def015ee6d 100644 --- a/pom.xml +++ b/pom.xml @@ -527,7 +527,12 @@ 2.5.3 provided - + + org.apache.curator + curator-recipes + 4.2.0 + provided +