mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add a blacklist in HighAvailableDataSource
Filter the DataSourceMap with the blacklist before returning
This commit is contained in:
@@ -79,7 +79,7 @@ public class DataSourceCreator {
|
||||
return map;
|
||||
}
|
||||
|
||||
protected DruidDataSource create(String name, String url, String username, String password,
|
||||
public static DruidDataSource create(String name, String url, String username, String password,
|
||||
HighAvailableDataSource haDataSource) throws SQLException {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
|
||||
|
||||
@@ -23,8 +23,10 @@ import java.sql.SQLFeatureNotSupportedException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
@@ -87,13 +89,14 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
|
||||
// Properties copied from DruidAbstractDataSource END
|
||||
|
||||
private Map<String, DataSource> dataSourceMap = new ConcurrentHashMap<String, DataSource>();
|
||||
private Set<String> blacklist = new CopyOnWriteArraySet<String>();
|
||||
private DataSourceSelector selector;
|
||||
private String dataSourceFile = DEFAULT_DATA_SOURCE_FILE;
|
||||
private String propertyPrefix = "";
|
||||
|
||||
private volatile boolean inited = false;
|
||||
|
||||
public void init() throws SQLException {
|
||||
public void init() {
|
||||
if (inited) {
|
||||
return;
|
||||
}
|
||||
@@ -114,6 +117,10 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
destroy();
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
if (dataSourceMap == null || dataSourceMap.isEmpty()) {
|
||||
return;
|
||||
@@ -166,6 +173,28 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
|
||||
return dataSourceMap;
|
||||
}
|
||||
|
||||
public Map<String, DataSource> getAvailableDataSourceMap() {
|
||||
Map<String, DataSource> map = new ConcurrentHashMap<String, DataSource>(this.dataSourceMap);
|
||||
for (String n : blacklist) {
|
||||
if (map.containsKey(n)) {
|
||||
map.remove(n);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public void addBlackList(String name) {
|
||||
if (dataSourceMap.containsKey(name)) {
|
||||
blacklist.add(name);
|
||||
} else {
|
||||
LOG.info("Key " + name + " is NOT existed, ignore it.");
|
||||
}
|
||||
}
|
||||
|
||||
public void removeBlackList(String name) {
|
||||
blacklist.remove(name);
|
||||
}
|
||||
|
||||
public void setSelector(String name) {
|
||||
DataSourceSelector selector = DataSourceSelectorFactory.getSelector(name, this);
|
||||
if (selector != null) {
|
||||
|
||||
@@ -51,7 +51,7 @@ public class NamedDataSourceSelector implements DataSourceSelector {
|
||||
return null;
|
||||
}
|
||||
|
||||
Map<String, DataSource> dataSourceMap = highAvailableDataSource.getDataSourceMap();
|
||||
Map<String, DataSource> dataSourceMap = highAvailableDataSource.getAvailableDataSourceMap();
|
||||
if (dataSourceMap == null || dataSourceMap.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -99,13 +99,20 @@ public class RandomDataSourceSelector implements DataSourceSelector {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public Map<String, DataSource> getDataSourceMap() {
|
||||
public Map<String, DataSource> getFullDataSourceMap() {
|
||||
if (highAvailableDataSource != null) {
|
||||
return highAvailableDataSource.getDataSourceMap();
|
||||
}
|
||||
return new HashMap<String, DataSource>();
|
||||
}
|
||||
|
||||
public Map<String, DataSource> getDataSourceMap() {
|
||||
if (highAvailableDataSource != null) {
|
||||
return highAvailableDataSource.getAvailableDataSourceMap();
|
||||
}
|
||||
return new HashMap<String, DataSource>();
|
||||
}
|
||||
|
||||
public List<DataSource> getBlacklist() {
|
||||
return blacklist;
|
||||
}
|
||||
|
||||
+1
-1
@@ -116,7 +116,7 @@ public class RandomDataSourceValidateThread implements Runnable {
|
||||
}
|
||||
|
||||
private void checkAllDataSources() {
|
||||
Map<String, DataSource> dataSourceMap = selector.getDataSourceMap();
|
||||
Map<String, DataSource> dataSourceMap = selector.getFullDataSourceMap();
|
||||
List<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>();
|
||||
LOG.debug("Checking all DataSource.");
|
||||
for (final Map.Entry<String, DataSource> e : dataSourceMap.entrySet()) {
|
||||
|
||||
Reference in New Issue
Block a user