mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
some small refactors
1. refactor RandomDataSourceSelector related classes 2. fix some testcases
This commit is contained in:
@@ -137,6 +137,9 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
|
||||
if (poolUpdater != null) {
|
||||
poolUpdater.destroy();
|
||||
}
|
||||
if (selector != null) {
|
||||
selector.destroy();
|
||||
}
|
||||
if (dataSourceMap == null || dataSourceMap.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -24,8 +24,30 @@ import javax.sql.DataSource;
|
||||
* @author DigitalSonic
|
||||
*/
|
||||
public interface DataSourceSelector {
|
||||
/**
|
||||
* Return a DataSource according to the implemention.
|
||||
*/
|
||||
DataSource get();
|
||||
|
||||
/**
|
||||
* Set the target DataSource name to return.
|
||||
* Wether to use this or not, it's decided by the implemention.
|
||||
*/
|
||||
void setTarget(String name);
|
||||
|
||||
/**
|
||||
* Return the name of this DataSourceSelector.
|
||||
* e.g. byName
|
||||
*/
|
||||
String getName();
|
||||
|
||||
/**
|
||||
* Init the DataSourceSelector before use it.
|
||||
*/
|
||||
void init();
|
||||
|
||||
/**
|
||||
* Destroy the DataSourceSelector, maybe interrupt the Thread.
|
||||
*/
|
||||
void destroy();
|
||||
}
|
||||
|
||||
@@ -40,6 +40,10 @@ public class NamedDataSourceSelector implements DataSourceSelector {
|
||||
public void init() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return DataSourceSelectorEnum.BY_NAME.getName();
|
||||
|
||||
+18
-6
@@ -30,9 +30,11 @@ import com.alibaba.druid.util.JdbcUtils;
|
||||
* @author DigitalSonic
|
||||
*/
|
||||
public class RandomDataSourceRecoverThread implements Runnable {
|
||||
public static final int DEFAULT_RECOVER_INTERVAL_SECONDS = 120;
|
||||
private final static Log LOG = LogFactory.getLog(RandomDataSourceRecoverThread.class);
|
||||
|
||||
private RandomDataSourceSelector selector;
|
||||
private int sleepSeconds = 120;
|
||||
private int recoverIntervalSeconds = DEFAULT_RECOVER_INTERVAL_SECONDS;
|
||||
private int validationSleepSeconds = 0;
|
||||
|
||||
public RandomDataSourceRecoverThread(RandomDataSourceSelector selector) {
|
||||
@@ -51,6 +53,8 @@ public class RandomDataSourceRecoverThread implements Runnable {
|
||||
}
|
||||
tryOneDataSource((DruidDataSource) dataSource);
|
||||
}
|
||||
} else if (selector == null) {
|
||||
break;
|
||||
}
|
||||
sleep();
|
||||
}
|
||||
@@ -88,18 +92,18 @@ public class RandomDataSourceRecoverThread implements Runnable {
|
||||
|
||||
private void sleep() {
|
||||
try {
|
||||
Thread.sleep(sleepSeconds * 1000);
|
||||
Thread.sleep(recoverIntervalSeconds * 1000);
|
||||
} catch (InterruptedException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
public int getSleepSeconds() {
|
||||
return sleepSeconds;
|
||||
public int getRecoverIntervalSeconds() {
|
||||
return recoverIntervalSeconds;
|
||||
}
|
||||
|
||||
public void setSleepSeconds(int sleepSeconds) {
|
||||
this.sleepSeconds = sleepSeconds;
|
||||
public void setRecoverIntervalSeconds(int recoverIntervalSeconds) {
|
||||
this.recoverIntervalSeconds = recoverIntervalSeconds;
|
||||
}
|
||||
|
||||
public int getValidationSleepSeconds() {
|
||||
@@ -109,4 +113,12 @@ public class RandomDataSourceRecoverThread implements Runnable {
|
||||
public void setValidationSleepSeconds(int validationSleepSeconds) {
|
||||
this.validationSleepSeconds = validationSleepSeconds;
|
||||
}
|
||||
|
||||
public RandomDataSourceSelector getSelector() {
|
||||
return selector;
|
||||
}
|
||||
|
||||
public void setSelector(RandomDataSourceSelector selector) {
|
||||
this.selector = selector;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,10 +53,10 @@ public class RandomDataSourceSelector implements DataSourceSelector {
|
||||
private Thread runningValidateThread;
|
||||
private Thread runningRecoverThread;
|
||||
|
||||
private int checkingIntervalSeconds = 15;
|
||||
private int recoveryIntervalSeconds = 120;
|
||||
private int checkingIntervalSeconds = RandomDataSourceValidateThread.DEFAULT_CHECKING_INTERVAL_SECONDS;
|
||||
private int recoveryIntervalSeconds = RandomDataSourceRecoverThread.DEFAULT_RECOVER_INTERVAL_SECONDS;
|
||||
private int validationSleepSeconds = 0;
|
||||
private int blacklistThreshold = 3;
|
||||
private int blacklistThreshold = RandomDataSourceValidateThread.DEFAULT_BLACKLIST_THRESHOLD;
|
||||
|
||||
public RandomDataSourceSelector(HighAvailableDataSource highAvailableDataSource) {
|
||||
this.highAvailableDataSource = highAvailableDataSource;
|
||||
@@ -76,6 +76,21 @@ public class RandomDataSourceSelector implements DataSourceSelector {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Interrupt Threads if needed.
|
||||
*/
|
||||
@Override
|
||||
public void destroy() {
|
||||
if (runningValidateThread != null) {
|
||||
runningValidateThread.interrupt();
|
||||
validateThread.setSelector(null);
|
||||
}
|
||||
if (runningRecoverThread != null) {
|
||||
runningRecoverThread.interrupt();
|
||||
recoverThread.setSelector(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return DataSourceSelectorEnum.RANDOM.getName();
|
||||
@@ -169,6 +184,8 @@ public class RandomDataSourceSelector implements DataSourceSelector {
|
||||
validateThread.setCheckingIntervalSeconds(checkingIntervalSeconds);
|
||||
validateThread.setValidationSleepSeconds(validationSleepSeconds);
|
||||
validateThread.setBlacklistThreshold(blacklistThreshold);
|
||||
} else {
|
||||
validateThread.setSelector(this);
|
||||
}
|
||||
if (runningValidateThread != null) {
|
||||
runningValidateThread.interrupt();
|
||||
@@ -178,8 +195,10 @@ public class RandomDataSourceSelector implements DataSourceSelector {
|
||||
|
||||
if (recoverThread == null) {
|
||||
recoverThread = new RandomDataSourceRecoverThread(this);
|
||||
recoverThread.setSleepSeconds(recoveryIntervalSeconds);
|
||||
recoverThread.setRecoverIntervalSeconds(recoveryIntervalSeconds);
|
||||
recoverThread.setValidationSleepSeconds(validationSleepSeconds);
|
||||
} else {
|
||||
recoverThread.setSelector(this);
|
||||
}
|
||||
if (runningRecoverThread != null) {
|
||||
runningRecoverThread.interrupt();
|
||||
|
||||
+23
-8
@@ -44,12 +44,15 @@ import com.alibaba.druid.util.StringUtils;
|
||||
* @author DigitalSonic
|
||||
*/
|
||||
public class RandomDataSourceValidateThread implements Runnable {
|
||||
private final static Log LOG = LogFactory.getLog(RandomDataSourceValidateThread.class);
|
||||
private final static Map<String, Long> SUCCESS_TIMES = new ConcurrentHashMap<String, Long>();
|
||||
public static final int DEFAULT_CHECKING_INTERVAL_SECONDS = 10;
|
||||
public static final int DEFAULT_BLACKLIST_THRESHOLD = 3;
|
||||
|
||||
private int checkingIntervalSeconds = 10; // This value should NOT be too small.
|
||||
private final static Log LOG = LogFactory.getLog(RandomDataSourceValidateThread.class);
|
||||
private static Map<String, Long> successTimes = new ConcurrentHashMap<String, Long>();
|
||||
|
||||
private int checkingIntervalSeconds = DEFAULT_CHECKING_INTERVAL_SECONDS; // This value should NOT be too small.
|
||||
private int validationSleepSeconds = 0;
|
||||
private int blacklistThreshold = 3;
|
||||
private int blacklistThreshold = DEFAULT_BLACKLIST_THRESHOLD;
|
||||
private RandomDataSourceSelector selector;
|
||||
private ExecutorService checkExecutor = Executors.newFixedThreadPool(5);
|
||||
private Map<String, Integer> errorCounts = new ConcurrentHashMap<String, Integer>();
|
||||
@@ -63,11 +66,12 @@ public class RandomDataSourceValidateThread implements Runnable {
|
||||
String name = dataSource.getName();
|
||||
long time = System.currentTimeMillis();
|
||||
LOG.debug("Log successTime [" + time + "] for " + name);
|
||||
SUCCESS_TIMES.put(name, time);
|
||||
successTimes.put(name, time);
|
||||
}
|
||||
}
|
||||
|
||||
public RandomDataSourceValidateThread(RandomDataSourceSelector selector) {
|
||||
LOG.debug("Create a RandomDataSourceValidateThread, hashCode=" + this.hashCode());
|
||||
this.selector = selector;
|
||||
}
|
||||
|
||||
@@ -78,6 +82,8 @@ public class RandomDataSourceValidateThread implements Runnable {
|
||||
checkAllDataSources();
|
||||
maintainBlacklist();
|
||||
cleanup();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
sleepForNextValidation();
|
||||
}
|
||||
@@ -97,7 +103,8 @@ public class RandomDataSourceValidateThread implements Runnable {
|
||||
newSleepSeconds = 1;
|
||||
}
|
||||
try {
|
||||
LOG.debug("Sleep " + newSleepSeconds + " second(s) until next checking.");
|
||||
LOG.debug("[RandomDataSourceValidateThread@" + hashCode() + "] Sleep " + newSleepSeconds
|
||||
+ " second(s) until next checking.");
|
||||
Thread.sleep(newSleepSeconds * 1000);
|
||||
} catch (InterruptedException e) {
|
||||
// ignore
|
||||
@@ -112,7 +119,7 @@ public class RandomDataSourceValidateThread implements Runnable {
|
||||
dataSources.add(((DruidDataSource) ds).getName());
|
||||
}
|
||||
}
|
||||
cleanupMap(SUCCESS_TIMES, dataSources);
|
||||
cleanupMap(successTimes, dataSources);
|
||||
cleanupMap(errorCounts, dataSources);
|
||||
cleanupMap(lastCheckTimes, dataSources);
|
||||
}
|
||||
@@ -205,7 +212,7 @@ public class RandomDataSourceValidateThread implements Runnable {
|
||||
|
||||
private boolean isSkipChecking(DruidDataSource dataSource) {
|
||||
String name = dataSource.getName();
|
||||
Long lastSuccessTime = SUCCESS_TIMES.get(dataSource.getName());
|
||||
Long lastSuccessTime = successTimes.get(dataSource.getName());
|
||||
Long lastCheckTime = lastCheckTimes.get(dataSource.getName());
|
||||
long currentTime = System.currentTimeMillis();
|
||||
LOG.debug("DataSource=" + name + ", lastSuccessTime=" + lastSuccessTime
|
||||
@@ -283,4 +290,12 @@ public class RandomDataSourceValidateThread implements Runnable {
|
||||
public void setBlacklistThreshold(int blacklistThreshold) {
|
||||
this.blacklistThreshold = blacklistThreshold;
|
||||
}
|
||||
|
||||
public RandomDataSourceSelector getSelector() {
|
||||
return selector;
|
||||
}
|
||||
|
||||
public void setSelector(RandomDataSourceSelector selector) {
|
||||
this.selector = selector;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user