From 0d78a27d48a701b705efa9e9b0c6c802c4639eee Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Thu, 12 Jul 2018 00:00:24 +0800 Subject: [PATCH 1/4] Fix validation bug fix the following situation: 1. rawURL can not meet the need to create Connection 2. username and password are not given in Properties if testOnBorrow or testOnReturn is set to true, the ValidationThread will be disabled. --- .../druid/pool/ha/DataSourceCreator.java | 4 ++-- .../ha/selector/RandomDataSourceSelector.java | 16 +++++++++---- .../RandomDataSourceValidateThread.java | 24 +++++++++++-------- 3 files changed, 28 insertions(+), 16 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 d5fc9dd5b7..5d2d4906a4 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java +++ b/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java @@ -122,8 +122,8 @@ public class DataSourceCreator { private void loadNameList() { Set names = new HashSet(); for (String n : properties.stringPropertyNames()) { - if (n.contains(".")) { - names.add(n.split("\\.")[0]); + if (n.contains(".url")) { + names.add(n.split("\\.url")[0]); } } if (!names.isEmpty()) { 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 345bff84bc..b9f0d5c717 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 @@ -17,6 +17,8 @@ package com.alibaba.druid.pool.ha.selector; import com.alibaba.druid.pool.DruidDataSource; 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.Collection; @@ -33,6 +35,8 @@ import java.util.concurrent.CopyOnWriteArrayList; * @author DigitalSonic */ public class RandomDataSourceSelector implements DataSourceSelector { + private final static Log LOG = LogFactory.getLog(RandomDataSourceSelector.class); + private Random random = new Random(); private List blacklist = new CopyOnWriteArrayList(); private HighAvailableDataSource highAvailableDataSource; @@ -41,10 +45,14 @@ public class RandomDataSourceSelector implements DataSourceSelector { public RandomDataSourceSelector(HighAvailableDataSource highAvailableDataSource) { this.highAvailableDataSource = highAvailableDataSource; - validateThread = new RandomDataSourceValidateThread(this); - recoverThread = new RandomDataSourceRecoverThread(this); - new Thread(validateThread, "RandomDataSourceSelector-validate-thread").start(); - new Thread(recoverThread, "RandomDataSourceSelector-recover-thread").start(); + if (!highAvailableDataSource.isTestOnBorrow() && !highAvailableDataSource.isTestOnReturn()) { + validateThread = new RandomDataSourceValidateThread(this); + recoverThread = new RandomDataSourceRecoverThread(this); + new Thread(validateThread, "RandomDataSourceSelector-validate-thread").start(); + new Thread(recoverThread, "RandomDataSourceSelector-recover-thread").start(); + } else { + LOG.info("testOnBorrow or testOnReturn has been set to true, ignore validateThread"); + } } @Override 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 a1900a8634..e2887fe349 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 @@ -18,6 +18,7 @@ package com.alibaba.druid.pool.ha.selector; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.logging.Log; import com.alibaba.druid.support.logging.LogFactory; +import com.alibaba.druid.util.JdbcUtils; import javax.sql.DataSource; import java.sql.Connection; @@ -95,25 +96,28 @@ public class RandomDataSourceValidateThread implements Runnable { private boolean check(String name, DruidDataSource dataSource) { boolean result = true; Driver driver = dataSource.getRawDriver(); - Properties info = dataSource.getConnectProperties(); - String url = dataSource.getRawJdbcUrl(); + Properties info = new Properties(dataSource.getConnectProperties()); + String username = dataSource.getUsername(); + String password = dataSource.getPassword(); + String url = dataSource.getUrl(); // We can't use rawUrl here, because the schema maybe set in url. Connection conn = null; + if (info.getProperty("user") == null && username != null) { + info.setProperty("user", username); + } + if (info.getProperty("password") == null && password != null) { + info.setProperty("password", password); + } try { LOG.debug("Validating " + name + " every " + sleepSeconds + " seconds."); conn = driver.connect(url, info); dataSource.validateConnection(conn); } catch (SQLException e) { - LOG.warn("Validation FAILED for " + name + ". Exception: " + e.getMessage()); + LOG.warn("Validation FAILED for " + name + " with url [" + url + "] and username [" + + info.getProperty("user") + "]. Exception: " + e.getMessage()); result = false; } finally { - if (conn != null) { - try { - conn.close(); - } catch (SQLException e) { - LOG.error("Can not close connection for HostAndPort Validation.", e); - } - } + JdbcUtils.close(conn); } return result; From f355d62981b491f1361057b66fbc028b13720737 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Sun, 5 Aug 2018 23:42:59 +0800 Subject: [PATCH 2/4] split the getter/setter for selector MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some old versions of Spring Framework would check the getter/setter of a bean property. While they were not the same type, exception would be thrown. So selector’s getter&setter are all String now. --- .../druid/pool/ha/HighAvailableDataSource.java | 14 +++++++++++--- .../druid/pool/ha/selector/DataSourceSelector.java | 1 + .../pool/ha/selector/NamedDataSourceSelector.java | 7 ++++++- .../pool/ha/selector/RandomDataSourceSelector.java | 7 ++++++- ...RandomDataSourceSelectorWithValidationTest.java | 14 +++++++------- 5 files changed, 31 insertions(+), 12 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 0946c1bcc8..c69ea02f2e 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java @@ -145,12 +145,20 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc } DataSourceSelector selector = DataSourceSelectorFactory.getSelector(name, this); if (selector != null) { - this.selector = selector; + setDataSourceSelector(selector); } } - public DataSourceSelector getSelector() { - return selector; + public String getSelector() { + return selector == null ? null : selector.getName(); + } + + public DataSourceSelector getDataSourceSelector() { + return this.selector; + } + + public void setDataSourceSelector(DataSourceSelector dataSourceSelector) { + this.selector = dataSourceSelector; } @Override diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/DataSourceSelector.java b/src/main/java/com/alibaba/druid/pool/ha/selector/DataSourceSelector.java index d8497e3f84..d6646a2cd2 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/selector/DataSourceSelector.java +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/DataSourceSelector.java @@ -28,4 +28,5 @@ public interface DataSourceSelector { DataSource get(); void setTarget(String name); boolean isSame(String name); + String getName(); } 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 b53e7caa5a..11cbf293bf 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 @@ -37,7 +37,12 @@ public class NamedDataSourceSelector implements DataSourceSelector { @Override public boolean isSame(String name) { - return "byName".equalsIgnoreCase(name); + return getName().equalsIgnoreCase(name); + } + + @Override + public String getName() { + return "byName"; } @Override 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 b9f0d5c717..f5434d391a 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 @@ -57,7 +57,12 @@ public class RandomDataSourceSelector implements DataSourceSelector { @Override public boolean isSame(String name) { - return "random".equalsIgnoreCase(name); + return getName().equalsIgnoreCase(name); + } + + @Override + public String getName() { + return "random"; } @Override 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 6a10653abb..a38cf40f4b 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,8 +30,8 @@ public class RandomDataSourceSelectorWithValidationTest { @Test public void testOneDataSourceFailAndRecover() throws Exception { - ((RandomDataSourceSelector) highAvailableDataSource.getSelector()).getValidateThread().setSleepSeconds(3); - ((RandomDataSourceSelector) highAvailableDataSource.getSelector()).getRecoverThread().setSleepSeconds(3); + ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector()).getValidateThread().setSleepSeconds(3); + ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector()).getRecoverThread().setSleepSeconds(3); Thread.sleep(30 * 1000); @@ -40,7 +40,7 @@ public class RandomDataSourceSelectorWithValidationTest { Thread.sleep(10 * 1000); assertTrue(dataSource.isTestOnReturn()); for (int i = 0; i < 100; i++) { - assertNotEquals(dataSource, highAvailableDataSource.getSelector().get()); + assertNotEquals(dataSource, highAvailableDataSource.getDataSourceSelector().get()); } dataSource.setValidationQuery(null); @@ -48,7 +48,7 @@ public class RandomDataSourceSelectorWithValidationTest { assertFalse(dataSource.isTestOnReturn()); int count = 0; for (int i = 0; i < 100; i++) { - if (dataSource == highAvailableDataSource.getSelector().get()) { + if (dataSource == highAvailableDataSource.getDataSourceSelector().get()) { count++; } } @@ -57,8 +57,8 @@ public class RandomDataSourceSelectorWithValidationTest { @Test public void testAllDataSourceFail() throws Exception { - ((RandomDataSourceSelector) highAvailableDataSource.getSelector()).getValidateThread().setSleepSeconds(3); - ((RandomDataSourceSelector) highAvailableDataSource.getSelector()).getRecoverThread().setSleepSeconds(3); + ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector()).getValidateThread().setSleepSeconds(3); + ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector()).getRecoverThread().setSleepSeconds(3); Thread.sleep(30 * 1000); @@ -73,7 +73,7 @@ public class RandomDataSourceSelectorWithValidationTest { int[] count = new int[2]; for (int i = 0; i < 100; i++) { - DataSource dataSource = highAvailableDataSource.getSelector().get(); + DataSource dataSource = highAvailableDataSource.getDataSourceSelector().get(); if (foo == dataSource) { count[0]++; } else if (bar == dataSource) { From 8630cf9b5f319904f997df8ec1e2633dd3ac9c98 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Fri, 17 Aug 2018 00:16:10 +0800 Subject: [PATCH 3/4] Optimize getConnection() method The initialization process is optimized. If init() is not invoked before getConnection(), it will call init() first. When selector obtain a null DataSource, getConnection() will return null instead of throwing a NullPointerException. --- .../druid/pool/ha/HighAvailableDataSource.java | 17 +++++++++++++++++ 1 file changed, 17 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 c69ea02f2e..b347944293 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java @@ -89,14 +89,26 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc private DataSourceSelector selector = new RandomDataSourceSelector(this); private String dataSourceFile = DEFAULT_DATA_SOURCE_FILE; + private boolean inited = false; + public void init() throws SQLException { + if (inited) { + return; + } synchronized (this) { + if (inited) { + return; + } if (dataSourceMap == null || dataSourceMap.isEmpty()) { dataSourceMap = new DataSourceCreator(dataSourceFile).createMap(this); } if (selector == null) { selector = new RandomDataSourceSelector(this); } + if (dataSourceMap == null || dataSourceMap.isEmpty()) { + LOG.warn("There is NO DataSource available!!! Please check your configuration."); + } + inited = true; } } @@ -117,7 +129,12 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc @Override public Connection getConnection() throws SQLException { + init(); DataSource dataSource = selector.get(); + if (dataSource == null) { + LOG.warn("Can NOT obtain DataSource, return null."); + return null; + } return dataSource.getConnection(); } From 54349c6f091a33437e882f2052e9dff63722e185 Mon Sep 17 00:00:00 2001 From: DigitalSonic Date: Thu, 23 Aug 2018 00:30:38 +0800 Subject: [PATCH 4/4] fix datasource name bug add an identityHashCode suffix --- src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5d2d4906a4..be1e9b69f1 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java +++ b/src/main/java/com/alibaba/druid/pool/ha/DataSourceCreator.java @@ -72,7 +72,7 @@ public class DataSourceCreator { HighAvailableDataSource haDataSource) throws SQLException { DruidDataSource dataSource = new DruidDataSource(); - dataSource.setName(name); + dataSource.setName(name + "-" + System.identityHashCode(dataSource)); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password);