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 899c4a1350..0b305f8395 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java +++ b/src/main/java/com/alibaba/druid/pool/ha/HighAvailableDataSource.java @@ -20,6 +20,7 @@ import com.alibaba.druid.pool.DruidAbstractDataSource; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.WrapperAdapter; import com.alibaba.druid.pool.ha.selector.DataSourceSelector; +import com.alibaba.druid.pool.ha.selector.DataSourceSelectorEnum; import com.alibaba.druid.pool.ha.selector.DataSourceSelectorFactory; import com.alibaba.druid.pool.ha.selector.RandomDataSourceSelector; import com.alibaba.druid.support.logging.Log; @@ -104,8 +105,7 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc dataSourceMap = new DataSourceCreator(dataSourceFile, propertyPrefix).createMap(this); } if (selector == null) { - selector = new RandomDataSourceSelector(this); - selector.init(); + setSelector(DataSourceSelectorEnum.RANDOM.getName()); } if (dataSourceMap == null || dataSourceMap.isEmpty()) { LOG.warn("There is NO DataSource available!!! Please check your configuration."); @@ -188,7 +188,7 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc @Override public Connection getConnection(String username, String password) throws SQLException { - throw new UnsupportedOperationException("Not supported by DruidDataSource"); + throw new UnsupportedOperationException("Not supported by HighAvailableDataSource."); } @Override @@ -228,7 +228,15 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc } public void setConnectProperties(Properties connectProperties) { - this.connectProperties = connectProperties; + if (connectProperties == null) { + connectProperties = new Properties(); + } + + if (this.connectProperties != null) { + this.connectProperties.putAll(connectProperties); + } else { + this.connectProperties = connectProperties; + } } public int getInitialSize() { @@ -413,6 +421,32 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc 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() { diff --git a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceRecoverThread.java b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceRecoverThread.java index 0ab3ed67b5..38249e024e 100644 --- a/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceRecoverThread.java +++ b/src/main/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceRecoverThread.java @@ -83,6 +83,10 @@ public class RandomDataSourceRecoverThread implements Runnable { } } + public int getSleepSeconds() { + return sleepSeconds; + } + public void setSleepSeconds(int sleepSeconds) { this.sleepSeconds = sleepSeconds; } 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 faa520f905..fac6ba65a2 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 @@ -26,6 +26,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.Random; import java.util.concurrent.CopyOnWriteArrayList; @@ -35,6 +36,12 @@ import java.util.concurrent.CopyOnWriteArrayList; * @author DigitalSonic */ public class RandomDataSourceSelector implements DataSourceSelector { + private static final String PROP_PREFIX = "druid.ha.random."; + public static final String PROP_CHECKING_INTERVAL = PROP_PREFIX + "checkingIntervalSeconds"; + public static final String PROP_RECOVERY_INTERVAL = PROP_PREFIX + "recoveryIntervalSeconds"; + public static final String PROP_VALIDATION_SLEEP = PROP_PREFIX + "validationSleepSeconds"; + public static final String PROP_BLACKLIST_THRESHOLD = PROP_PREFIX + "blacklistThreshold"; + private final static Log LOG = LogFactory.getLog(RandomDataSourceSelector.class); private Random random = new Random(); @@ -58,17 +65,9 @@ public class RandomDataSourceSelector implements DataSourceSelector { LOG.warn("highAvailableDataSource is NULL!"); return; } - if (!highAvailableDataSource.isTestOnBorrow() && !highAvailableDataSource.isTestOnReturn()) { - validateThread = new RandomDataSourceValidateThread(this); - validateThread.setCheckingIntervalSeconds(checkingIntervalSeconds); - validateThread.setValidationSleepSeconds(validationSleepSeconds); - validateThread.setBlacklistThreshold(blacklistThreshold); - new Thread(validateThread, "RandomDataSourceSelector-isValid-thread").start(); - - recoverThread = new RandomDataSourceRecoverThread(this); - recoverThread.setSleepSeconds(recoveryIntervalSeconds); - new Thread(recoverThread, "RandomDataSourceSelector-recover-thread").start(); + loadProperties(); + initThreads(); } else { LOG.info("testOnBorrow or testOnReturn has been set to true, ignore validateThread"); } @@ -137,6 +136,42 @@ public class RandomDataSourceSelector implements DataSourceSelector { } } + private void loadProperties() { + checkingIntervalSeconds = loadInteger(PROP_CHECKING_INTERVAL, checkingIntervalSeconds); + recoveryIntervalSeconds = loadInteger(PROP_RECOVERY_INTERVAL, recoveryIntervalSeconds); + validationSleepSeconds = loadInteger(PROP_VALIDATION_SLEEP, validationSleepSeconds); + blacklistThreshold = loadInteger(PROP_BLACKLIST_THRESHOLD, blacklistThreshold); + } + + private int loadInteger(String name, int defaultValue) { + if (name == null) { + return defaultValue; + } + + Properties properties = highAvailableDataSource.getConnectProperties(); + int value = defaultValue; + try { + if (properties.containsKey(name)) { + value = Integer.parseInt(properties.getProperty(name)); + } + } catch (Exception e) { + LOG.error("Exception occurred while parsing " + name, e); + } + return value; + } + + private void initThreads() { + validateThread = new RandomDataSourceValidateThread(this); + validateThread.setCheckingIntervalSeconds(checkingIntervalSeconds); + validateThread.setValidationSleepSeconds(validationSleepSeconds); + validateThread.setBlacklistThreshold(blacklistThreshold); + new Thread(validateThread, "RandomDataSourceSelector-isValid-thread").start(); + + recoverThread = new RandomDataSourceRecoverThread(this); + recoverThread.setSleepSeconds(recoveryIntervalSeconds); + new Thread(recoverThread, "RandomDataSourceSelector-recover-thread").start(); + } + public HighAvailableDataSource getHighAvailableDataSource() { return highAvailableDataSource; } 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 9f5430f1de..9beca1e65d 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 @@ -174,14 +174,26 @@ public class RandomDataSourceValidateThread implements Runnable { } } + public int getCheckingIntervalSeconds() { + return checkingIntervalSeconds; + } + public void setCheckingIntervalSeconds(int checkingIntervalSeconds) { this.checkingIntervalSeconds = checkingIntervalSeconds; } + public int getValidationSleepSeconds() { + return validationSleepSeconds; + } + public void setValidationSleepSeconds(int validationSleepSeconds) { this.validationSleepSeconds = validationSleepSeconds; } + public int getBlacklistThreshold() { + return blacklistThreshold; + } + public void setBlacklistThreshold(int blacklistThreshold) { this.blacklistThreshold = blacklistThreshold; } diff --git a/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorTest.java b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorTest.java index cad11e45da..c37b2337b6 100644 --- a/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorTest.java +++ b/src/test/java/com/alibaba/druid/pool/ha/selector/RandomDataSourceSelectorTest.java @@ -3,6 +3,7 @@ package com.alibaba.druid.pool.ha.selector; import com.alibaba.druid.pool.ha.MockDataSource; import org.junit.Test; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; public class RandomDataSourceSelectorTest extends BaseRandomDataSourceSelectorTest { @@ -20,4 +21,27 @@ public class RandomDataSourceSelectorTest extends BaseRandomDataSourceSelectorTe assertTrue(i > 0); } } -} \ No newline at end of file + + @Test + public void testCreateSelectorWithProperties() { + String props = "druid.ha.random.checkingIntervalSeconds=60;"; + props += "druid.ha.random.recoveryIntervalSeconds=120;"; + props += "druid.ha.random.validationSleepSeconds=10;"; + props += "druid.ha.random.blacklistThreshold=5;"; + dataSource.setConnectionProperties(props); + + RandomDataSourceSelector selector = new RandomDataSourceSelector(dataSource); + selector.init(); + assertEquals(60, selector.getCheckingIntervalSeconds()); + assertEquals(60, selector.getValidateThread().getCheckingIntervalSeconds()); + + assertEquals(120, selector.getRecoveryIntervalSeconds()); + assertEquals(120, selector.getRecoverThread().getSleepSeconds()); + + assertEquals(10, selector.getValidationSleepSeconds()); + assertEquals(10, selector.getValidateThread().getValidationSleepSeconds()); + + assertEquals(5, selector.getBlacklistThreshold()); + assertEquals(5, selector.getValidateThread().getBlacklistThreshold()); + } +}