RandomDataSourceSelector can load some parameters from Properties now

This commit is contained in:
DigitalSonic
2019-02-02 18:30:56 +08:00
parent 6508554897
commit f681871e49
5 changed files with 124 additions and 15 deletions
@@ -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() {
@@ -83,6 +83,10 @@ public class RandomDataSourceRecoverThread implements Runnable {
}
}
public int getSleepSeconds() {
return sleepSeconds;
}
public void setSleepSeconds(int sleepSeconds) {
this.sleepSeconds = sleepSeconds;
}
@@ -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;
}
@@ -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;
}