Merge pull request #3694 from digitalsonic/master

针对 HighAvailableDataSource 的多项优化
This commit is contained in:
温绍锦
2020-06-27 22:33:39 +08:00
committed by GitHub
35 changed files with 2840 additions and 291 deletions
@@ -15,23 +15,9 @@
*/
package com.alibaba.druid.pool.ha;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
/**
* An utility class to create DruidDataSource dynamically.
@@ -39,45 +25,8 @@ import com.alibaba.druid.support.logging.LogFactory;
* @author DigitalSonic
*/
public class DataSourceCreator {
private final static Log LOG = LogFactory.getLog(DataSourceCreator.class);
private Properties properties = new Properties();
private List<String> nameList = new ArrayList<String>();
public DataSourceCreator(String file) {
this(file, "");
}
public DataSourceCreator(String file, String propertyPrefix) {
loadProperties(file);
loadNameList(propertyPrefix);
}
public Map<String, DataSource> createMap(HighAvailableDataSource haDataSource) throws SQLException {
Map<String, DataSource> map = new ConcurrentHashMap<String, DataSource>();
if (nameList == null || nameList.isEmpty()) {
LOG.error("No DataSource will be created!");
return map;
}
for (String n : nameList) {
String url = properties.getProperty(n + ".url");
String username = properties.getProperty(n + ".username");
String password = properties.getProperty(n + ".password");
LOG.info("Creating " + n + " with url[" + url + "] and username[" + username + "].");
try {
DruidDataSource dataSource = create(n, url, username, password, haDataSource);
map.put(n, dataSource);
} catch(Exception e) {
LOG.error("Can NOT create DruidDataSource for " + n, e);
}
}
LOG.info(map.size() + " DruidDataSource(s) created. ");
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();
@@ -127,61 +76,4 @@ public class DataSourceCreator {
return dataSource;
}
private void loadNameList(String propertyPrefix) {
Set<String> names = new HashSet<String>();
for (String n : properties.stringPropertyNames()) {
if (propertyPrefix != null && !propertyPrefix.isEmpty()
&& !n.startsWith(propertyPrefix)) {
continue;
}
if (n.endsWith(".url")) {
names.add(n.split("\\.url")[0]);
}
}
if (!names.isEmpty()) {
nameList.addAll(names);
}
}
private void loadProperties(String file) {
Properties properties = new Properties();
if (file == null) {
return;
}
InputStream is = null;
try {
LOG.debug("Trying to load " + file + " from FileSystem.");
is = new FileInputStream(file);
} catch(FileNotFoundException e) {
LOG.debug("Trying to load " + file + " from Classpath.");
try {
is = DataSourceCreator.class.getResourceAsStream(file);
} catch (Exception ex) {
LOG.warn("Can not load resource " + file, ex);
}
}
if (is != null) {
try {
properties.load(is);
this.properties = properties;
} catch(Exception e) {
LOG.error("Exception occurred while loading " + file, e);
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
// ignore
}
}
}
} else {
LOG.warn("File " + file + " can't be loaded!");
}
}
public List<String> getNameList() {
return nameList;
}
}
@@ -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;
@@ -33,6 +35,9 @@ import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.pool.DruidAbstractDataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.WrapperAdapter;
import com.alibaba.druid.pool.ha.node.FileNodeListener;
import com.alibaba.druid.pool.ha.node.NodeListener;
import com.alibaba.druid.pool.ha.node.PoolUpdater;
import com.alibaba.druid.pool.ha.selector.DataSourceSelector;
import com.alibaba.druid.pool.ha.selector.DataSourceSelectorEnum;
import com.alibaba.druid.pool.ha.selector.DataSourceSelectorFactory;
@@ -87,13 +92,19 @@ 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 int poolPurgeIntervalSeconds = PoolUpdater.DEFAULT_INTERVAL;
private boolean allowEmptyPoolWhenUpdate = false;
private volatile boolean inited = false;
public void init() throws SQLException {
private PoolUpdater poolUpdater = new PoolUpdater(this);
private NodeListener nodeListener;
public void init() {
if (inited) {
return;
}
@@ -102,7 +113,10 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
return;
}
if (dataSourceMap == null || dataSourceMap.isEmpty()) {
dataSourceMap = new DataSourceCreator(dataSourceFile, propertyPrefix).createMap(this);
poolUpdater.setIntervalSeconds(poolPurgeIntervalSeconds);
poolUpdater.setAllowEmptyPool(allowEmptyPoolWhenUpdate);
poolUpdater.init();
createNodeMap();
}
if (selector == null) {
setSelector(DataSourceSelectorEnum.RANDOM.getName());
@@ -114,7 +128,20 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
}
}
public void close() {
destroy();
}
public void destroy() {
if (nodeListener != null) {
nodeListener.destroy();
}
if (poolUpdater != null) {
poolUpdater.destroy();
}
if (selector != null) {
selector.destroy();
}
if (dataSourceMap == null || dataSourceMap.isEmpty()) {
return;
}
@@ -166,6 +193,32 @@ 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 boolean isInBlackList(String name) {
return blacklist.contains(name);
}
public void setSelector(String name) {
DataSourceSelector selector = DataSourceSelectorFactory.getSelector(name, this);
if (selector != null) {
@@ -178,29 +231,11 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
return selector == null ? null : selector.getName();
}
public DataSourceSelector getDataSourceSelector() {
return this.selector;
}
public void setDataSourceSelector(DataSourceSelector dataSourceSelector) {
this.selector = dataSourceSelector;
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
throw new UnsupportedOperationException("Not supported by HighAvailableDataSource.");
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return logWriter;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
this.logWriter = out;
}
@Override
public void setLoginTimeout(int seconds) {
DriverManager.setLoginTimeout(seconds);
@@ -215,16 +250,34 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
throw new SQLFeatureNotSupportedException();
}
public String getDriverClassName() {
return driverClassName;
}
public void setConnectionProperties(String connectionProperties) {
this.connectionProperties = connectionProperties;
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
// COPIED FROM DruidAbstractDataSource.setConnectionProperties()
public Properties getConnectProperties() {
return connectProperties;
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 void setConnectProperties(Properties connectProperties) {
@@ -239,6 +292,76 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
}
}
private void createNodeMap() {
if (nodeListener == null) {
// Compatiable with the old version.
// Create a FileNodeListener to watch the dataSourceFile.
FileNodeListener listener = new FileNodeListener();
listener.setFile(dataSourceFile);
listener.setPrefix(propertyPrefix);
nodeListener = listener;
}
nodeListener.setObserver(poolUpdater);
nodeListener.init();
nodeListener.update(); // Do update in the current Thread at the startup
}
public boolean isAllowEmptyPoolWhenUpdate() {
return allowEmptyPoolWhenUpdate;
}
public void setAllowEmptyPoolWhenUpdate(boolean allowEmptyPoolWhenUpdate) {
this.allowEmptyPoolWhenUpdate = allowEmptyPoolWhenUpdate;
}
// Getters & Setters
public int getPoolPurgeIntervalSeconds() {
return poolPurgeIntervalSeconds;
}
public void setPoolPurgeIntervalSeconds(int poolPurgeIntervalSeconds) {
this.poolPurgeIntervalSeconds = poolPurgeIntervalSeconds;
}
public NodeListener getNodeListener() {
return nodeListener;
}
public void setNodeListener(NodeListener nodeListener) {
this.nodeListener = nodeListener;
}
public DataSourceSelector getDataSourceSelector() {
return this.selector;
}
public void setDataSourceSelector(DataSourceSelector dataSourceSelector) {
this.selector = dataSourceSelector;
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return logWriter;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
this.logWriter = out;
}
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public Properties getConnectProperties() {
return connectProperties;
}
public int getInitialSize() {
return initialSize;
}
@@ -419,36 +542,6 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
return connectionProperties;
}
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() {
return filters;
}
@@ -0,0 +1,115 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.pool.ha;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.StringUtils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
/**
* Utilities for Properties.
*
* @author DigitalSonic
*/
public class PropertiesUtils {
private final static Log LOG = LogFactory.getLog(PropertiesUtils.class);
/**
* Load properties from the given file into Properties.
*/
public static Properties loadProperties(String file) {
Properties properties = new Properties();
if (file == null) {
return properties;
}
InputStream is = null;
try {
LOG.debug("Trying to load " + file + " from FileSystem.");
is = new FileInputStream(file);
} catch(FileNotFoundException e) {
LOG.debug("Trying to load " + file + " from Classpath.");
try {
is = PropertiesUtils.class.getResourceAsStream(file);
} catch (Exception ex) {
LOG.warn("Can not load resource " + file, ex);
}
}
if (is != null) {
try {
properties.load(is);
} catch(Exception e) {
LOG.error("Exception occurred while loading " + file, e);
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
LOG.debug("Can not close Inputstream.", e);
}
}
}
} else {
LOG.warn("File " + file + " can't be loaded!");
}
return properties;
}
/**
* Pick the name of a JDBC url. Such as xxx.url, xxx is the name.
*/
public static List<String> loadNameList(Properties properties, String propertyPrefix) {
List<String> nameList = new ArrayList<String>();
Set<String> names = new HashSet<String>();
for (String n : properties.stringPropertyNames()) {
if (propertyPrefix != null && !propertyPrefix.isEmpty()
&& !n.startsWith(propertyPrefix)) {
continue;
}
if (n.endsWith(".url")) {
names.add(n.split("\\.url")[0]);
}
}
if (!names.isEmpty()) {
nameList.addAll(names);
}
return nameList;
}
public static Properties filterPrefix(Properties properties, String prefix) {
if (properties == null || prefix == null || prefix.isEmpty()) {
return properties;
}
Properties result = new Properties();
for (String n : properties.stringPropertyNames()) {
if (n.startsWith(prefix)) {
result.setProperty(n, properties.getProperty(n));
}
}
return result;
}
}
@@ -0,0 +1,145 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.pool.ha.node;
import com.alibaba.druid.pool.ha.PropertiesUtils;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* A NodeList that monitors the change of a file.
*
* @author DigitalSonic
*/
public class FileNodeListener extends NodeListener {
private final static Log LOG = LogFactory.getLog(FileNodeListener.class);
private Lock lock = new ReentrantLock();
private String file = null;
private int intervalSeconds = 60;
private ScheduledExecutorService executor;
/**
* Start a Scheduler to check the specified file.
*
* @see #setIntervalSeconds(int)
* @see #update()
*/
@Override
public void init() {
super.init();
if (intervalSeconds <= 0) {
intervalSeconds = 60;
}
executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
LOG.debug("Checking file " + file + " every " + intervalSeconds + "s.");
if (!lock.tryLock()) {
LOG.info("Can not acquire the lock, skip this time.");
return;
}
try {
update();
} catch (Exception e) {
LOG.error("Can NOT update the node list.", e);
} finally {
lock.unlock();
}
}
}, intervalSeconds, intervalSeconds, TimeUnit.SECONDS);
}
/**
* Load the properties file and diff with the stored Properties.
*
* @return A List of the modification
*/
@Override
public List<NodeEvent> refresh() {
Properties originalProperties = PropertiesUtils.loadProperties(file);
List<String> nameList = PropertiesUtils.loadNameList(originalProperties, getPrefix());
Properties properties = new Properties();
for (String n : nameList) {
String url = originalProperties.getProperty(n + ".url");
String username = originalProperties.getProperty(n + ".username");
String password = originalProperties.getProperty(n + ".password");
if (url == null || url.isEmpty()) {
LOG.warn(n + ".url is EMPTY! IGNORE!");
continue;
} else {
properties.setProperty(n + ".url", url);
}
if (username == null || username.isEmpty()) {
LOG.debug(n + ".username is EMPTY. Maybe you should check the config.");
} else {
properties.setProperty(n + ".username", username);
}
if (password == null || password.isEmpty()) {
LOG.debug(n + ".password is EMPTY. Maybe you should check the config.");
} else {
properties.setProperty(n + ".password", password);
}
}
List<NodeEvent> events = NodeEvent.getEventsByDiffProperties(getProperties(), properties);
if (events != null && !events.isEmpty()) {
LOG.info(events.size() + " different(s) detected.");
setProperties(properties);
}
return events;
}
/**
* Close the ScheduledExecutorService.
*/
@Override
public void destroy() {
if (executor == null || executor.isShutdown()) {
return;
}
try {
executor.shutdown();
} catch (Exception e) {
LOG.error("Can NOT shutdown the ScheduledExecutorService.", e);
}
}
public int getIntervalSeconds() {
return intervalSeconds;
}
public void setIntervalSeconds(int intervalSeconds) {
this.intervalSeconds = intervalSeconds;
}
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
}
@@ -0,0 +1,132 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.pool.ha.node;
import com.alibaba.druid.pool.ha.PropertiesUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* Define the detail of a node update event.
*
* @author DigitalSonic
*/
public class NodeEvent {
private NodeEventTypeEnum type;
private String nodeName;
private String url;
private String username;
private String password;
/**
* Diff the given two Properties.
*
* @return A List of AddEvent and DelEvent
*/
public static List<NodeEvent> getEventsByDiffProperties(Properties previous, Properties next) {
List<String> prevNames = PropertiesUtils.loadNameList(previous, "");
List<String> nextNames = PropertiesUtils.loadNameList(next, "");
List<String> namesToAdd = new ArrayList<String>();
List<String> namesToDel = new ArrayList<String>();
for (String n : prevNames) {
if (n != null && !n.trim().isEmpty() && !nextNames.contains(n)) {
namesToDel.add(n);
}
}
for (String n : nextNames) {
if (n != null && !n.trim().isEmpty() && !prevNames.contains(n)) {
namesToAdd.add(n);
}
}
List<NodeEvent> list = new ArrayList<NodeEvent>();
list.addAll(generateEvents(next, namesToAdd, NodeEventTypeEnum.ADD));
list.addAll(generateEvents(previous, namesToDel, NodeEventTypeEnum.DELETE));
return list;
}
public static List<NodeEvent> generateEvents(Properties properties, List<String> names, NodeEventTypeEnum type) {
List<NodeEvent> list = new ArrayList<NodeEvent>();
for (String n : names) {
NodeEvent event = new NodeEvent();
event.setType(type);
event.setNodeName(n);
event.setUrl(properties.getProperty(n + ".url"));
event.setUsername(properties.getProperty(n + ".username"));
event.setPassword(properties.getProperty(n + ".password"));
list.add(event);
}
return list;
}
@Override
public String toString() {
String str = "NodeEvent{" +
"type=" + type +
", nodeName='" + nodeName + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'';
if (password != null) {
str += ", password.length=" + password.length();
}
str += '}';
return str;
}
public NodeEventTypeEnum getType() {
return type;
}
public void setType(NodeEventTypeEnum type) {
this.type = type;
}
public String getNodeName() {
return nodeName;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.pool.ha.node;
/**
* Enum that specifies the type of a NodeEvent.
*
* @author DigitalSonic
*/
public enum NodeEventTypeEnum {
ADD, DELETE
}
@@ -0,0 +1,123 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.pool.ha.node;
import com.alibaba.druid.DruidRuntimeException;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import java.util.Date;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Properties;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* This abstract class defines a listener to monitor the change of DataSource nodes.
*
* @author DigitalSonic
* @see Observable
*/
public abstract class NodeListener extends Observable {
private final static Log LOG = LogFactory.getLog(NodeListener.class);
private String prefix = "";
private Properties properties = new Properties();
private Date lastUpdateTime = null;
private Observer observer = null;
private Lock lock = new ReentrantLock();
/**
* The method implements the detail update logic.
*/
public abstract List<NodeEvent> refresh();
/**
* Do some cleanup.
*/
public abstract void destroy();
/**
* Add the given PoolUpdater as the Observer.
*
* @see #setObserver(Observer)
*/
public void init() {
if (observer == null) {
throw new DruidRuntimeException("No Observer(such as PoolUpdater) specified.");
}
this.addObserver(observer);
}
/**
* Fire the refresh() method and notify the Observer.
*
* @see #refresh()
* @see #update(List)
*/
public void update() {
update(refresh());
}
/**
* Notify the Observer.
*/
public void update(List<NodeEvent> events) {
if (events != null && !events.isEmpty()) {
this.lastUpdateTime = new Date();
NodeEvent[] arr = new NodeEvent[events.size()];
for (int i = 0; i < events.size(); i++) {
arr[i] = events.get(i);
}
this.setChanged();
this.notifyObservers(arr);
}
}
public Observer getObserver() {
return observer;
}
public void setObserver(Observer observer) {
this.observer = observer;
}
public Date getLastUpdateTime() {
return lastUpdateTime;
}
public void setLastUpdateTime(Date lastUpdateTime) {
this.lastUpdateTime = lastUpdateTime;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
}
@@ -0,0 +1,264 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.pool.ha.node;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.ha.DataSourceCreator;
import com.alibaba.druid.pool.ha.HighAvailableDataSource;
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.util.HashSet;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Update the DataSource Connection Pool when notified.
*
* @author DigitalSonic
*/
public class PoolUpdater implements Observer {
public final static int DEFAULT_INTERVAL = 60;
private final static Log LOG = LogFactory.getLog(PoolUpdater.class);
private Set<String> nodesToDel = new CopyOnWriteArraySet<String>();
private HighAvailableDataSource highAvailableDataSource;
private Lock lock = new ReentrantLock();
private ScheduledExecutorService executor;
private int intervalSeconds = DEFAULT_INTERVAL;
private volatile boolean inited = false;
private boolean allowEmptyPool = false;
public PoolUpdater(HighAvailableDataSource highAvailableDataSource) {
setHighAvailableDataSource(highAvailableDataSource);
}
/**
* Create a ScheduledExecutorService to remove unused DataSources.
*/
public void init() {
if (inited) {
return;
}
synchronized (this) {
if (inited) {
return;
}
if (intervalSeconds < 10) {
LOG.warn("CAUTION: Purge interval has been set to " + intervalSeconds
+ ". This value should NOT be too small.");
}
if (intervalSeconds <= 0) {
intervalSeconds = DEFAULT_INTERVAL;
}
executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
LOG.debug("Purging the DataSource Pool every " + intervalSeconds + "s.");
try {
removeDataSources();
} catch (Exception e) {
LOG.error("Exception occurred while removing DataSources.", e);
}
}
}, intervalSeconds, intervalSeconds, TimeUnit.SECONDS);
}
}
public void destroy() {
if (executor == null || executor.isShutdown()) {
return;
}
try {
executor.shutdown();
} catch (Exception e) {
LOG.error("Can NOT shutdown the ScheduledExecutorService.", e);
}
}
/**
* Process the given NodeEvent[]. Maybe add / delete some nodes.
*/
@Override
public void update(Observable o, Object arg) {
if (!(o instanceof NodeListener)) {
return;
}
if (arg == null || !(arg instanceof NodeEvent[])) {
return;
}
NodeEvent[] events = (NodeEvent[]) arg;
if (events.length <= 0) {
return;
}
try {
LOG.info("Waiting for Lock to start processing NodeEvents.");
lock.lock();
LOG.info("Start processing the NodeEvent[" + events.length + "].");
for (NodeEvent e : events) {
if (e.getType() == NodeEventTypeEnum.ADD) {
addNode(e);
} else if (e.getType() == NodeEventTypeEnum.DELETE) {
deleteNode(e);
}
}
} catch (Exception e) {
LOG.error("Exception occurred while updating Pool.", e);
} finally {
lock.unlock();
}
}
/**
* Remove unused DataSources.
*/
public void removeDataSources() {
if (nodesToDel == null || nodesToDel.isEmpty()) {
return;
}
try {
lock.lock();
Map<String, DataSource> map = highAvailableDataSource.getDataSourceMap();
Set<String> copySet = new HashSet<String>(nodesToDel);
for (String nodeName : copySet) {
LOG.info("Start removing Node " + nodeName + ".");
if (!map.containsKey(nodeName)) {
LOG.info("Node " + nodeName + " is NOT existed in the map.");
cancelBlacklistNode(nodeName);
continue;
}
DataSource ds = map.get(nodeName);
if (ds instanceof DruidDataSource) {
DruidDataSource dds = (DruidDataSource) ds;
int activeCount = dds.getActiveCount(); // CAUTION, activeCount MAYBE changed!
if (activeCount > 0) {
LOG.warn("Node " + nodeName + " is still running [activeCount=" + activeCount
+ "], try next time.");
continue;
} else {
LOG.info("Close Node " + nodeName + " and remove it.");
try {
dds.close();
} catch (Exception e) {
LOG.error("Exception occurred while closing Node " + nodeName
+ ", just remove it.", e);
}
}
}
map.remove(nodeName); // Remove the node directly if it is NOT a DruidDataSource.
cancelBlacklistNode(nodeName);
}
} catch (Exception e) {
LOG.error("Exception occurred while removing DataSources.", e);
} finally {
lock.unlock();
}
}
protected void addNode(NodeEvent event) {
String nodeName = event.getNodeName();
String url = event.getUrl();
String username = event.getUsername();
String password = event.getPassword();
Map<String, DataSource> map = highAvailableDataSource.getDataSourceMap();
if (nodeName == null || nodeName.isEmpty()) {
return;
}
LOG.info("Adding Node " + nodeName + "[url: " + url + ", username: " + username + "].");
if (map.containsKey(nodeName)) {
cancelBlacklistNode(nodeName);
return;
}
DruidDataSource dataSource = null;
try {
dataSource = DataSourceCreator.create(nodeName, url, username,
password, this.highAvailableDataSource);
map.put(nodeName, dataSource);
LOG.info("Creating Node " + nodeName + "[url: " + url + ", username: " + username + "].");
} catch (Exception e) {
LOG.error("Can NOT create DataSource " + nodeName + ". IGNORE IT.", e);
JdbcUtils.close(dataSource);
}
}
protected void deleteNode(NodeEvent event) {
String nodeName = event.getNodeName();
Map<String, DataSource> map = highAvailableDataSource.getDataSourceMap();
if (nodeName == null || nodeName.isEmpty() || !map.containsKey(nodeName)) {
return;
}
map = highAvailableDataSource.getAvailableDataSourceMap();
if (!allowEmptyPool && map.size() == 1 && map.containsKey(nodeName)) {
LOG.warn(nodeName + " is the only DataSource left, don't remove it.");
return;
}
blacklistNode(nodeName);
}
private void cancelBlacklistNode(String nodeName) {
LOG.info("Cancel the deletion of Node " + nodeName + ".");
nodesToDel.remove(nodeName);
highAvailableDataSource.removeBlackList(nodeName);
}
private void blacklistNode(String nodeName) {
LOG.info("Deleting Node " + nodeName + ", just add it into blacklist.");
nodesToDel.add(nodeName);
highAvailableDataSource.addBlackList(nodeName);
}
public HighAvailableDataSource getHighAvailableDataSource() {
return highAvailableDataSource;
}
public void setHighAvailableDataSource(HighAvailableDataSource highAvailableDataSource) {
this.highAvailableDataSource = highAvailableDataSource;
}
public Set<String> getNodesToDel() {
return nodesToDel;
}
public int getIntervalSeconds() {
return intervalSeconds;
}
public void setIntervalSeconds(int intervalSeconds) {
this.intervalSeconds = intervalSeconds;
}
public boolean isAllowEmptyPool() {
return allowEmptyPool;
}
public void setAllowEmptyPool(boolean allowEmptyPool) {
this.allowEmptyPool = allowEmptyPool;
}
}
@@ -0,0 +1,89 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.pool.ha.node;
/**
* The information that will be added to an ephemeral node.
*
* @author DigitalSonic
*/
public class ZookeeperNodeInfo {
private String prefix = "";
private String host;
private Integer port;
/**
* Database can be the following ones:
* 1. Database in MySQL and PostgreSQL JDBC URL
* 2. ServiceName or SID in Oracle JDBC URL
* etc.
*/
private String database;
private String username;
private String password;
public void setPrefix(String prefix) {
if (prefix != null && !prefix.trim().isEmpty()) {
this.prefix = prefix;
if (!prefix.endsWith(".")) {
this.prefix = prefix + ".";
}
}
}
public String getPrefix() {
return prefix;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDatabase() {
return database;
}
public void setDatabase(String database) {
this.database = database;
}
}
@@ -0,0 +1,314 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.pool.ha.node;
import com.alibaba.druid.DruidRuntimeException;
import com.alibaba.druid.pool.ha.PropertiesUtils;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.StringUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* A NodeListener that watches a Zookeeper Path.
* e.g.
* Path to watch:
* <pre>
* + ha-druid-datasources
* |---- NodeA
* |---- NodeB
* </pre>
* <p>
* The Data of NodeA (in Java properties format):
* <pre>
* foo.host=xxx.xxx.xxx.xxx
* foo.port=3308
* foo.username=foo
* foo.password=foo_password
*
* bar.host=xxx.xxx.xxx.xxx
* bar.port=3309
* bar.username=bar
* bar.password=bar_password
* bar.database=bar_database
* </pre>
*
* @author DigitalSonic
*/
public class ZookeeperNodeListener extends NodeListener {
private final static Log LOG = LogFactory.getLog(ZookeeperNodeListener.class);
private String zkConnectString;
private String path = "/ha-druid-datasources";
private Lock lock = new ReentrantLock();
private boolean privateZkClient = false; // Should I close the client?
private PathChildrenCache cache;
private CuratorFramework client;
/**
* URL Template, e.g.
* jdbc:mysql://${host}:${port}/${database}?useUnicode=true
* ${host}, ${port} and ${database} will be replaced by values in ZK
*/
private String urlTemplate;
/**
* Init a PathChildrenCache to watch the given path.
*/
@Override
public void init() {
checkParameters();
super.init();
if (client == null) {
client = CuratorFrameworkFactory.builder()
.canBeReadOnly(true)
.connectionTimeoutMs(3000)
.connectString(zkConnectString)
.retryPolicy(new ExponentialBackoffRetry(5000, 3, 30000))
.sessionTimeoutMs(30000)
.build();
client.start();
privateZkClient = true;
}
cache = new PathChildrenCache(client, path, true);
cache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
try {
lock.lock();
LOG.info("Receive an event: " + event.getType());
PathChildrenCacheEvent.Type eventType = event.getType();
switch (eventType) {
case CHILD_REMOVED:
updateSingleNode(event, NodeEventTypeEnum.DELETE);
break;
case CHILD_ADDED:
updateSingleNode(event, NodeEventTypeEnum.ADD);
break;
case CONNECTION_RECONNECTED:
refreshAllNodes();
break;
default:
// CHILD_UPDATED
// INITIALIZED
// CONNECTION_LOST
// CONNECTION_SUSPENDED
LOG.info("Received a PathChildrenCacheEvent, IGNORE it: " + event);
}
} finally {
lock.unlock();
}
}
});
try {
// Use BUILD_INITIAL_CACHE to force build cache in the current Thread.
// We don't use POST_INITIALIZED_EVENT, so there's no INITIALIZED event.
cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
} catch (Exception e) {
LOG.error("Can't start PathChildrenCache", e);
}
}
/**
* Close PathChildrenCache and CuratorFramework.
*/
@Override
public void destroy() {
if (cache != null) {
try {
cache.close();
} catch (IOException e) {
LOG.error("IOException occurred while closing PathChildrenCache.", e);
}
}
if (client != null && privateZkClient) {
client.close();
}
}
/**
* Build Properties from PathChildrenCache.
* Should be called after init().
*
* @see #getPropertiesFromCache()
*/
@Override
public List<NodeEvent> refresh() {
Properties properties = getPropertiesFromCache();
List<NodeEvent> events = NodeEvent.getEventsByDiffProperties(getProperties(), properties);
if (events != null && !events.isEmpty()) {
setProperties(properties);
}
return events;
}
private void checkParameters() {
if (client == null && StringUtils.isEmpty(zkConnectString)) {
throw new DruidRuntimeException("ZK Client is NULL, Please set the zkConnectString.");
}
if (StringUtils.isEmpty(path)) {
throw new DruidRuntimeException("Please set the ZooKeeper node path.");
}
if (StringUtils.isEmpty(urlTemplate)) {
throw new DruidRuntimeException("Please set the urlTemplate.");
}
}
private void updateSingleNode(PathChildrenCacheEvent event, NodeEventTypeEnum type) {
ChildData data = event.getData();
String nodeName = getNodeName(data);
List<String> names = new ArrayList<String>();
names.add(getPrefix() + "." + nodeName);
Properties properties = getPropertiesFromChildData(data);
List<NodeEvent> events = NodeEvent.generateEvents(properties, names, type);
if (events.isEmpty()) {
return;
}
if (type == NodeEventTypeEnum.ADD) {
getProperties().putAll(properties);
} else {
for (String n : properties.stringPropertyNames()) {
getProperties().remove(n);
}
}
super.update(events);
}
private void refreshAllNodes() {
try {
if (client.checkExists().forPath(path) == null) {
LOG.warn("PATH[" + path + "] is NOT existed, can NOT refresh nodes.");
return;
}
cache.rebuild();
Properties properties = getPropertiesFromCache();
List<NodeEvent> events = NodeEvent.getEventsByDiffProperties(getProperties(), properties);
if (events != null && !events.isEmpty()) {
setProperties(properties);
super.update(events);
}
} catch (Exception e) {
LOG.error("Can NOT refresh Cache Nodes.", e);
}
}
private Properties getPropertiesFromCache() {
List<ChildData> data = cache.getCurrentData();
Properties properties = new Properties();
for (ChildData c : data) {
properties.putAll(getPropertiesFromChildData(c));
}
return properties;
}
private Properties getPropertiesFromChildData(ChildData data) {
String dataPrefix = getPrefix();
Properties properties = new Properties();
if (data == null) {
return properties;
}
String nodeName = getNodeName(data);
String str = new String(data.getData());
Properties full = new Properties();
try {
full.load(new StringReader(str));
} catch (IOException e) {
LOG.error("Can't load Properties from String. " + str, e);
}
Properties filtered = PropertiesUtils.filterPrefix(full, dataPrefix);
for (String n : filtered.stringPropertyNames()) {
properties.setProperty(
n.replaceFirst(dataPrefix, dataPrefix + "\\." + nodeName),
filtered.getProperty(n));
}
if (!properties.containsKey(dataPrefix + "." + nodeName + ".url")) {
properties.setProperty(dataPrefix + "." + nodeName + ".url", formatUrl(filtered));
}
return properties;
}
private String formatUrl(Properties properties) {
String url = urlTemplate;
String dataPrefix = getPrefix();
if (properties.containsKey(dataPrefix + ".host")) {
url = url.replace("${host}", properties.getProperty(dataPrefix + ".host"));
}
if (properties.containsKey(dataPrefix + ".port")) {
url = url.replace("${port}", properties.getProperty(dataPrefix + ".port"));
}
if (properties.containsKey(dataPrefix + ".database")) {
url = url.replace("${database}", properties.getProperty(dataPrefix + ".database"));
}
return url;
}
private String getNodeName(ChildData data) {
String eventZkPath = data.getPath();
if (eventZkPath.startsWith(path + "/")) {
return eventZkPath.substring(path.length() + 1);
} else {
return eventZkPath;
}
}
public void setClient(CuratorFramework client) {
if (client != null) {
this.client = client;
privateZkClient = false;
}
}
public CuratorFramework getClient() {
return client;
}
public String getZkConnectString() {
return zkConnectString;
}
public void setZkConnectString(String zkConnectString) {
this.zkConnectString = zkConnectString;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getUrlTemplate() {
return urlTemplate;
}
public void setUrlTemplate(String urlTemplate) {
this.urlTemplate = urlTemplate;
}
}
@@ -0,0 +1,179 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.pool.ha.node;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.nodes.GroupMember;
import org.apache.curator.retry.ExponentialBackoffRetry;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* A register which is used to register a node to an ephemeral node.
*
* @author DigitalSonic
*/
public class ZookeeperNodeRegister {
private final static Log LOG = LogFactory.getLog(ZookeeperNodeRegister.class);
private String zkConnectString;
private String path = "/ha-druid-datasources";
private CuratorFramework client;
private GroupMember member;
private boolean privateZkClient = false; // Should I close the client?
private Lock lock = new ReentrantLock();
/**
* Init a CuratorFramework if there's no CuratorFramework provided.
*/
public void init() {
if (client == null) {
client = CuratorFrameworkFactory.builder()
.connectionTimeoutMs(3000)
.connectString(zkConnectString)
.retryPolicy(new ExponentialBackoffRetry(5000, 3, 30000))
.sessionTimeoutMs(30000)
.build();
client.start();
privateZkClient = true;
}
}
/**
* Register a Node which has a Properties as the payload.
* <pre>
* CAUTION: only one node can be registered,
* if you want to register another one,
* call deregister first
* </pre>
*
* @param payload The information used to generate the payload Properties
* @return true, register successfully; false, skip the registeration
*/
public boolean register(String nodeId, List<ZookeeperNodeInfo> payload) {
if (payload == null || payload.isEmpty()) {
return false;
}
try {
lock.lock();
createPathIfNotExisted();
if (member != null) {
LOG.warn("GroupMember has already registered. Please deregister first.");
return false;
}
String payloadString = getPropertiesString(payload);
member = new GroupMember(client, path, nodeId, payloadString.getBytes());
member.start();
LOG.info("Register Node["+ nodeId + "] in path[" + path + "].");
return true;
} finally {
lock.unlock();
}
}
/**
* Close the current GroupMember.
*/
public void deregister() {
if (member != null) {
member.close();
member = null;
}
if (client != null && privateZkClient) {
client.close();
}
}
/**
* @see #deregister()
*/
public void destroy() {
deregister();
}
private void createPathIfNotExisted() {
try {
if (client.checkExists().forPath(path) == null) {
LOG.info("Path[" + path + "] is NOT existed, create it.");
client.create().creatingParentsIfNeeded().forPath(path);
}
} catch(Exception e) {
LOG.error("Can NOT check the path.", e);
}
}
private String getPropertiesString(List<ZookeeperNodeInfo> payload) {
Properties properties = new Properties();
for (ZookeeperNodeInfo n : payload) {
if (n.getHost() != null) {
properties.setProperty(n.getPrefix() + "host", n.getHost());
}
if (n.getPort() != null) {
properties.setProperty(n.getPrefix() + "port", n.getPort().toString());
}
if (n.getDatabase() != null) {
properties.setProperty(n.getPrefix() + "database", n.getDatabase());
}
if (n.getUsername() != null) {
properties.setProperty(n.getPrefix() + "username", n.getUsername());
}
if (n.getPassword() != null) {
properties.setProperty(n.getPrefix() + "password", n.getPassword());
}
}
StringWriter sw = new StringWriter();
try {
properties.store(sw, "");
} catch (IOException e) {
LOG.error("Why Properties.store goes wrong?", e);
}
return sw.toString();
}
public void setClient(CuratorFramework client) {
if (client != null) {
this.client = client;
privateZkClient = false;
}
}
public CuratorFramework getClient() {
return client;
}
public String getZkConnectString() {
return zkConnectString;
}
public void setZkConnectString(String zkConnectString) {
this.zkConnectString = zkConnectString;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
@@ -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();
@@ -51,7 +55,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;
}
@@ -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;
}
}
@@ -50,11 +50,13 @@ public class RandomDataSourceSelector implements DataSourceSelector {
private HighAvailableDataSource highAvailableDataSource;
private RandomDataSourceValidateThread validateThread;
private RandomDataSourceRecoverThread recoverThread;
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;
@@ -74,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();
@@ -86,21 +103,10 @@ public class RandomDataSourceSelector implements DataSourceSelector {
return null;
}
Collection<DataSource> targetDataSourceSet;
if (blacklist == null || blacklist.isEmpty() || blacklist.size() >= dataSourceMap.size()) {
targetDataSourceSet = dataSourceMap.values();
} else {
targetDataSourceSet = new HashSet<DataSource>(dataSourceMap.values());
for (DataSource b : blacklist) {
targetDataSourceSet.remove(b);
}
}
DataSource[] dataSources = targetDataSourceSet.toArray(new DataSource[] {});
if (dataSources != null && dataSources.length > 0) {
return dataSources[random.nextInt(targetDataSourceSet.size())];
}
return null;
Collection<DataSource> targetDataSourceSet = removeBlackList(dataSourceMap);
removeBusyDataSource(targetDataSourceSet);
DataSource dataSource = getRandomDataSource(targetDataSourceSet);
return dataSource;
}
@Override
@@ -108,13 +114,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;
}
@@ -171,15 +184,65 @@ public class RandomDataSourceSelector implements DataSourceSelector {
validateThread.setCheckingIntervalSeconds(checkingIntervalSeconds);
validateThread.setValidationSleepSeconds(validationSleepSeconds);
validateThread.setBlacklistThreshold(blacklistThreshold);
} else {
validateThread.setSelector(this);
}
new Thread(validateThread, "RandomDataSourceSelector-validate-thread").start();
if (runningValidateThread != null) {
runningValidateThread.interrupt();
}
runningValidateThread = new Thread(validateThread, "RandomDataSourceSelector-validate-thread");
runningValidateThread.start();
if (recoverThread == null) {
recoverThread = new RandomDataSourceRecoverThread(this);
recoverThread.setSleepSeconds(recoveryIntervalSeconds);
recoverThread.setRecoverIntervalSeconds(recoveryIntervalSeconds);
recoverThread.setValidationSleepSeconds(validationSleepSeconds);
} else {
recoverThread.setSelector(this);
}
new Thread(recoverThread, "RandomDataSourceSelector-recover-thread").start();
if (runningRecoverThread != null) {
runningRecoverThread.interrupt();
}
runningRecoverThread = new Thread(recoverThread, "RandomDataSourceSelector-recover-thread");
runningRecoverThread.start();
}
private Collection<DataSource> removeBlackList(Map<String, DataSource> dataSourceMap) {
Collection<DataSource> dataSourceSet;
if (blacklist == null || blacklist.isEmpty() || blacklist.size() >= dataSourceMap.size()) {
dataSourceSet = dataSourceMap.values();
} else {
dataSourceSet = new HashSet<DataSource>(dataSourceMap.values());
for (DataSource b : blacklist) {
dataSourceSet.remove(b);
}
LOG.info(blacklist.size() + " Blacklist DataSource removed, return "
+ dataSourceSet.size() + " DataSource(s).");
}
return dataSourceSet;
}
private void removeBusyDataSource(Collection<DataSource> dataSourceSet) {
Collection<DataSource> busyDataSourceSet = new HashSet<DataSource>();
for (DataSource ds : dataSourceSet) {
if (ds instanceof DruidDataSource && ((DruidDataSource) ds).getPoolingCount() <= 0) {
busyDataSourceSet.add(ds);
}
}
if (!busyDataSourceSet.isEmpty() && busyDataSourceSet.size() < dataSourceSet.size()) {
LOG.info("Busy DataSouces: " + busyDataSourceSet.size() + "/" + dataSourceSet.size());
for (DataSource ds : busyDataSourceSet) {
dataSourceSet.remove(ds);
}
}
}
private DataSource getRandomDataSource(Collection<DataSource> dataSourceSet) {
DataSource[] dataSources = dataSourceSet.toArray(new DataSource[] {});
if (dataSources != null && dataSources.length > 0) {
return dataSources[random.nextInt(dataSourceSet.size())];
}
return null;
}
public HighAvailableDataSource getHighAvailableDataSource() {
@@ -0,0 +1,53 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.pool.ha.selector;
import com.alibaba.druid.filter.FilterEventAdapter;
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
import com.alibaba.druid.proxy.jdbc.ResultSetProxy;
import com.alibaba.druid.proxy.jdbc.StatementProxy;
/**
* A Druid Filter that records the last success execute time.
*
* @author DigitalSonic
*/
public class RandomDataSourceValidateFilter extends FilterEventAdapter {
protected void statementExecuteUpdateAfter(StatementProxy statement, String sql, int updateCount) {
recordTime(statement);
}
protected void statementExecuteQueryAfter(StatementProxy statement, String sql, ResultSetProxy resultSet) {
recordTime(statement);
}
protected void statementExecuteAfter(StatementProxy statement, String sql, boolean result) {
recordTime(statement);
}
protected void statementExecuteBatchAfter(StatementProxy statement, int[] result) {
recordTime(statement);
}
private void recordTime(StatementProxy statement) {
ConnectionProxy conn = statement.getConnectionProxy();
if (conn != null) {
DataSourceProxy dataSource = conn.getDirectDataSource();
RandomDataSourceValidateThread.logSuccessTime(dataSource);
}
}
}
@@ -17,11 +17,12 @@ package com.alibaba.druid.pool.ha.selector;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
@@ -30,9 +31,11 @@ import java.util.concurrent.Executors;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.JdbcUtils;
import com.alibaba.druid.util.StringUtils;
/**
* A Thread trying to test all DataSource provided by HADataSource.
@@ -41,15 +44,34 @@ import com.alibaba.druid.util.JdbcUtils;
* @author DigitalSonic
*/
public class RandomDataSourceValidateThread implements Runnable {
public static final int DEFAULT_CHECKING_INTERVAL_SECONDS = 10;
public static final int DEFAULT_BLACKLIST_THRESHOLD = 3;
private final static Log LOG = LogFactory.getLog(RandomDataSourceValidateThread.class);
private int checkingIntervalSeconds = 15;
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>();
private Map<String, Long> lastCheckTimes = new ConcurrentHashMap<String, Long>();
/**
* Provide a static method to record the last success time of a DataSource
*/
public static void logSuccessTime(DataSourceProxy dataSource) {
if (dataSource != null && !StringUtils.isEmpty(dataSource.getName())) {
String name = dataSource.getName();
long time = System.currentTimeMillis();
LOG.debug("Log successTime [" + time + "] for " + name);
successTimes.put(name, time);
}
}
public RandomDataSourceValidateThread(RandomDataSourceSelector selector) {
LOG.debug("Create a RandomDataSourceValidateThread, hashCode=" + this.hashCode());
this.selector = selector;
}
@@ -59,6 +81,9 @@ public class RandomDataSourceValidateThread implements Runnable {
if (selector != null) {
checkAllDataSources();
maintainBlacklist();
cleanup();
} else {
break;
}
sleepForNextValidation();
}
@@ -78,30 +103,66 @@ 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
}
}
private void cleanup() {
Map<String, DataSource> dataSourceMap = selector.getFullDataSourceMap();
Set<String> dataSources = new HashSet<String>();
for (DataSource ds : dataSourceMap.values()) {
if (ds instanceof DruidDataSource) {
dataSources.add(((DruidDataSource) ds).getName());
}
}
cleanupMap(successTimes, dataSources);
cleanupMap(errorCounts, dataSources);
cleanupMap(lastCheckTimes, dataSources);
}
private void cleanupMap(Map<String, ?> mapToClean, Set<String> keys) {
Set<String> keysToRemove = new HashSet<String>();
for (String k : mapToClean.keySet()) {
if (!keys.contains(k)) {
keysToRemove.add(k);
}
}
for (String k : keysToRemove) {
mapToClean.remove(k);
}
}
private void maintainBlacklist() {
Map<String, DataSource> dataSourceMap = selector.getDataSourceMap();
for (Map.Entry<String, Integer> e : errorCounts.entrySet()) {
if (e.getValue() <= 0) {
selector.removeBlacklist(dataSourceMap.get(e.getKey()));
} else if (e.getValue() >= blacklistThreshold
&& !selector.containInBlacklist(dataSourceMap.get(e.getKey()))) {
LOG.warn("Adding " + e.getKey() + " to blacklist.");
selector.addBlacklist(dataSourceMap.get(e.getKey()));
Map<String, DataSource> dataSourceMap = selector.getFullDataSourceMap();
for (String name : errorCounts.keySet()) {
Integer count = errorCounts.get(name);
DataSource dataSource = dataSourceMap.get(name);
if (dataSource == null) {
for (DataSource ds : dataSourceMap.values()) {
if (ds instanceof DruidDataSource
&& name.equals(((DruidDataSource) ds).getName())) {
dataSource = ds;
}
}
}
if (count == null || count <= 0) {
selector.removeBlacklist(dataSource);
} else if (count != null && count >= blacklistThreshold
&& !selector.containInBlacklist(dataSource)) {
LOG.warn("Adding " + name + " to blacklist.");
selector.addBlacklist(dataSource);
}
}
}
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(s).");
for (final Map.Entry<String, DataSource> e : dataSourceMap.entrySet()) {
if (!(e.getValue() instanceof DruidDataSource)) {
continue;
@@ -115,17 +176,28 @@ public class RandomDataSourceValidateThread implements Runnable {
tasks.add(new Callable<Boolean>() {
@Override
public Boolean call() {
boolean flag = check(e.getKey(), (DruidDataSource) e.getValue());
DruidDataSource dataSource = (DruidDataSource) e.getValue();
String name = dataSource.getName();
if (isSkipChecking(dataSource)) {
LOG.debug("Skip checking DataSource[" + name + "] this time.");
return true;
}
LOG.debug("Start checking " + name + ".");
boolean flag = check(dataSource);
if (flag) {
errorCounts.put(e.getKey(), 0);
logSuccessTime(dataSource);
errorCounts.put(name, 0);
} else {
if (!errorCounts.containsKey(e.getKey())) {
errorCounts.put(e.getKey(), 0);
if (!errorCounts.containsKey(name)) {
errorCounts.put(name, 0);
}
int count = errorCounts.get(e.getKey());
errorCounts.put(e.getKey(), count + 1);
int count = errorCounts.get(name);
errorCounts.put(name, count + 1);
}
lastCheckTimes.put(name, System.currentTimeMillis());
return flag;
}
@@ -138,8 +210,22 @@ public class RandomDataSourceValidateThread implements Runnable {
}
}
private boolean check(String name, DruidDataSource dataSource) {
private boolean isSkipChecking(DruidDataSource dataSource) {
String name = dataSource.getName();
Long lastSuccessTime = successTimes.get(dataSource.getName());
Long lastCheckTime = lastCheckTimes.get(dataSource.getName());
long currentTime = System.currentTimeMillis();
LOG.debug("DataSource=" + name + ", lastSuccessTime=" + lastSuccessTime
+ ", lastCheckTime=" + lastCheckTime + ", currentTime=" + currentTime);
return lastSuccessTime != null && lastCheckTime != null
&& (currentTime - lastSuccessTime) <= checkingIntervalSeconds * 1000
&& (currentTime - lastCheckTime) <= 5 * checkingIntervalSeconds * 1000
&& (!errorCounts.containsKey(name) || errorCounts.get(name) < 1);
}
private boolean check(DruidDataSource dataSource) {
boolean result = true;
String name = dataSource.getName();
Driver driver = dataSource.getRawDriver();
Properties info = new Properties(dataSource.getConnectProperties());
String username = dataSource.getUsername();
@@ -154,11 +240,11 @@ public class RandomDataSourceValidateThread implements Runnable {
info.setProperty("password", password);
}
try {
LOG.debug("Validating " + name + " every " + checkingIntervalSeconds + " seconds.");
LOG.debug("[RandomDataSourceValidateThread@" + this.hashCode() + "] Validating " + name + " every " + checkingIntervalSeconds + " seconds.");
conn = driver.connect(url, info);
sleepBeforeValidation();
dataSource.validateConnection(conn);
} catch (SQLException e) {
} catch (Exception e) {
LOG.warn("Validation FAILED for " + name + " with url [" + url + "] and username ["
+ info.getProperty("user") + "]. Exception: " + e.getMessage());
result = false;
@@ -204,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;
}
}
@@ -48,7 +48,7 @@ public class StickyRandomDataSourceSelector extends RandomDataSourceSelector {
@Override
public DataSource get() {
StickyDataSourceHolder holder = holders.get();
if (holder != null && isValid(holder) && !isExpired(holder)) {
if (holder != null && isAvailable(holder)) {
LOG.debug("Return the sticky DataSource " + holder.getDataSource().toString() + " directly.");
return holder.getDataSource();
}
@@ -60,6 +60,14 @@ public class StickyRandomDataSourceSelector extends RandomDataSourceSelector {
return dataSource;
}
private boolean isAvailable(StickyDataSourceHolder holder) {
boolean flag = isValid(holder) && !isExpired(holder);
if (flag && holder.getDataSource() instanceof DruidDataSource) {
flag = ((DruidDataSource) holder.getDataSource()).getPoolingCount() > 0;
}
return flag;
}
private boolean isValid(StickyDataSourceHolder holder) {
boolean flag = holder.isValid() && !getBlacklist().contains(holder.getDataSource());
if (!(holder.getDataSource() instanceof DruidDataSource) || !flag) {