Merge pull request #2723 from digitalsonic/master

修复HADataSource的健康检查问题
This commit is contained in:
温绍锦
2018-09-14 21:37:45 +08:00
committed by GitHub
7 changed files with 77 additions and 29 deletions
@@ -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);
@@ -122,8 +122,8 @@ public class DataSourceCreator {
private void loadNameList() {
Set<String> names = new HashSet<String>();
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()) {
@@ -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();
}
@@ -145,12 +162,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
@@ -28,4 +28,5 @@ public interface DataSourceSelector {
DataSource get();
void setTarget(String name);
boolean isSame(String name);
String getName();
}
@@ -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
@@ -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<DataSource> blacklist = new CopyOnWriteArrayList<DataSource>();
private HighAvailableDataSource highAvailableDataSource;
@@ -41,15 +45,24 @@ 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
public boolean isSame(String name) {
return "random".equalsIgnoreCase(name);
return getName().equalsIgnoreCase(name);
}
@Override
public String getName() {
return "random";
}
@Override
@@ -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;