Delete URL Filter related codes committed previously

This commit is contained in:
DigitalSonic
2018-05-05 23:25:49 +08:00
parent ed3861395c
commit 2b3c50c8b3
9 changed files with 0 additions and 633 deletions
@@ -1,70 +0,0 @@
package com.alibaba.druid.filter.url;
import com.alibaba.druid.filter.FilterChain;
import com.alibaba.druid.filter.FilterChainImpl;
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
import com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl;
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;
/**
* FilterChain to create new connection with placeholder in URL
*
* @author DigitalSonic
*/
public class ConnectionConnectFilterChainImpl extends FilterChainImpl {
private final static Log LOG = LogFactory.getLog(ConnectionConnectFilterChainImpl.class);
public ConnectionConnectFilterChainImpl(FilterChain filterChain) {
this(filterChain.getDataSource(), filterChain.getPos());
}
public ConnectionConnectFilterChainImpl(DataSourceProxy dataSource, int pos) {
super(dataSource, pos);
}
@Override
public ConnectionProxy connection_connect(Properties info) throws SQLException {
if (this.pos < getFilterSize()) {
return getFilters().get(pos++).connection_connect(this, info);
}
DataSourceProxy dataSource = getDataSource();
Driver driver = dataSource.getRawDriver();
String url = dataSource.getRawJdbcUrl();
url = processUrl(url);
Connection nativeConnection = driver.connect(url, info);
if (nativeConnection == null) {
return null;
}
return new ConnectionProxyImpl(dataSource, nativeConnection, info, dataSource.createConnectionId());
}
protected String processUrl(String url) {
if (url == null || (url != null &&
(url.indexOf("${") == -1 || url.indexOf("}") == -1))) {
return url;
}
String processedUrl = url;
String placeHolder = url.substring(url.indexOf("${") + 2, url.indexOf("}"));
String value = HostAndPortHolder.getInstance().get(placeHolder);
if (value != null) {
LOG.info("PlaceHolder ${" + placeHolder + "} is found in url, it will be replace by " + value);
processedUrl = url.replace("${" + placeHolder + "}", value);
} else {
LOG.info("PlaceHolder ${" + placeHolder + "} is found in url, but no value is bind.");
}
return processedUrl;
}
}
@@ -1,150 +0,0 @@
package com.alibaba.druid.filter.url;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
/**
* A singleton object to hold Host & Port information.
*
* @author DigitalSonic
*/
public class HostAndPortHolder {
private static final Log LOG = LogFactory.getLog(ConnectionConnectFilterChainImpl.class);
public static final String UNAVAILABLE = "UNAVAILABLE_HOST";
private static HostAndPortHolder instance = new HostAndPortHolder();
private Map<String, List<String>> holder = new HashMap<String, List<String>>();
private List<String> blacklist = new ArrayList<String>();
private Random random = new Random();
public static HostAndPortHolder getInstance() {
return instance;
}
public static 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 = HostAndPortHolder.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) {
// ignore
}
}
}
} else {
LOG.warn("File " + file + " can't be loaded!");
}
for (Object k : properties.keySet()) {
String v = properties.getProperty((String) k);
if (v != null && v.trim().length() > 0) {
LOG.info("Loading placeholder " + k + " = [" + v + "]");
List<String> values = Arrays.asList(v.split(";"));
getInstance().putHostAndPorts((String) k, values);
}
}
}
private HostAndPortHolder() {
}
public void putHostAndPorts(String key, List<String> values) {
if (key == null || values == null || values.isEmpty()) {
return;
}
holder.put(key, values);
}
public Map<String, List<String>> getHolder() {
return holder;
}
public void clearBlacklist() {
blacklist.clear();
}
public void addBlacklist(String hostAndPort) {
blacklist.add(hostAndPort);
}
public void removeBlacklist(String hostAndPort) {
if (hostAndPort == null) {
return;
}
blacklist.remove(hostAndPort);
}
public int getBlacklistSize() {
return blacklist.size();
}
public String get(String name) {
if (name == null || !holder.containsKey(name)) {
LOG.warn("Can not find placeholder " + name + ".");
return UNAVAILABLE;
}
List<String> hostAndPorts = holder.get(name);
int idx = random.nextInt(hostAndPorts.size());
String returnValue = hostAndPorts.get(idx);
String hostAndPort = returnValue;
for (int i = 0; i < hostAndPorts.size(); i++) {
if (isAvailable(hostAndPort)) {
returnValue = hostAndPort;
break;
}
if (++idx >= hostAndPorts.size()) {
idx = 0;
}
hostAndPort = hostAndPorts.get(idx);
}
return returnValue;
}
public List<String> getAll(String name) {
if (name == null || !holder.containsKey(name)) {
return new ArrayList<String>();
}
return holder.get(name);
}
private boolean isAvailable(String hostAndPort) {
return !blacklist.contains(hostAndPort);
}
}
@@ -1,118 +0,0 @@
package com.alibaba.druid.filter.url;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;
/**
* A thread to run the validation on each host:port.
* If some host:port is failed in the validation, we can set test flags in DruidDataSource.
*
* @author DigitalSonic
*/
public class HostAndPortValidatorThread implements Runnable {
private final static Log LOG = LogFactory.getLog(HostAndPortValidatorThread.class);
private DruidDataSource dataSource;
private int sleepSeconds = 30;
private boolean modifyTestFlags = false;
private boolean oldTestOnBorrow = false;
private boolean oldTestOnReturn = false;
@Override
public void run() {
oldTestOnBorrow = dataSource.isTestOnBorrow();
oldTestOnReturn = dataSource.isTestOnReturn();
while (true) {
try {
check();
} catch(Exception e) {
LOG.error("Validation Failed!", e);
} finally {
try {
Thread.sleep(sleepSeconds * 1000);
} catch (InterruptedException e) {
// ignore
}
}
}
}
protected void check() {
String url = dataSource.getRawJdbcUrl();
if (url == null || (url != null &&
(url.indexOf("${") == -1 || url.indexOf("}") == -1))) {
return;
}
String placeHolder = url.substring(url.indexOf("${") + 2, url.indexOf("}"));
List<String> values = HostAndPortHolder.getInstance().getAll(placeHolder);
for (String v : values) {
validateConnection(dataSource, placeHolder, v);
}
if (HostAndPortHolder.getInstance().getBlacklistSize() == 0) {
resetFlagsIfNeeded();
}
}
private void validateConnection(DruidDataSource dataSource, String placeHolder, String target) {
Driver driver = dataSource.getRawDriver();
Properties info = dataSource.getConnectProperties();
String url = dataSource.getRawJdbcUrl();
Connection conn = null;
try {
LOG.debug("Validating " + target + " every " + sleepSeconds + " seconds.");
String realUrl = url.replace("${" + placeHolder + "}", target);
conn = driver.connect(realUrl, info);
dataSource.validateConnection(conn);
HostAndPortHolder.getInstance().removeBlacklist(target);
} catch (SQLException e) {
LOG.warn("Validation FAILED for " + target, e);
HostAndPortHolder.getInstance().addBlacklist(target);
modifyFlagsIfNeeded();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
LOG.error("Can not close connection for HostAndPort Validation.", e);
}
}
}
}
private void resetFlagsIfNeeded() {
if (modifyTestFlags) {
dataSource.setTestOnBorrow(oldTestOnBorrow);
dataSource.setTestOnReturn(oldTestOnReturn);
}
}
private void modifyFlagsIfNeeded() {
if (!modifyTestFlags) {
return;
}
if (!oldTestOnReturn || !oldTestOnBorrow) {
dataSource.setTestOnBorrow(true);
dataSource.setTestOnReturn(true);
}
}
public void setDataSource(DruidDataSource dataSource) {
this.dataSource = dataSource;
}
public void setSleepSeconds(int sleepSeconds) {
this.sleepSeconds = sleepSeconds;
}
public void setModifyTestFlags(boolean modifyTestFlags) {
this.modifyTestFlags = modifyTestFlags;
}
}
@@ -1,86 +0,0 @@
package com.alibaba.druid.filter.url;
import com.alibaba.druid.filter.FilterAdapter;
import com.alibaba.druid.filter.FilterChain;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import java.sql.SQLException;
import java.util.Properties;
/**
* The filter to replace the placeholder in JDBC URL.
*
* @author DigitalSonic
*/
public class JdbcUrlHostAndPortReplaceFilter extends FilterAdapter {
private final static Log LOG = LogFactory.getLog(HostAndPortValidatorThread.class);
public static final String DEFAULT_FILE = "datasource.properties";
private String file = DEFAULT_FILE;
private int healthCheckIntervalSeconds = 0;
private HostAndPortValidatorThread validatorThread;
private Thread runningValidatorThread;
private boolean isValidatorThreadStarted = false;
private boolean modifyTestFlags = false;
public void init() {
if (file == null || file.trim().length() == 0) {
file = DEFAULT_FILE;
}
HostAndPortHolder.loadProperties(file);
}
public void close() {
if (runningValidatorThread != null) {
runningValidatorThread.interrupt();
}
}
@Override
public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException {
startValidationThreadIfNeeded(chain);
return super.connection_connect(new ConnectionConnectFilterChainImpl(chain), info);
}
private void startValidationThreadIfNeeded(FilterChain chain) {
if (isValidatorThreadStarted || healthCheckIntervalSeconds <= 0) {
return;
}
DataSourceProxy dataSource = chain.getDataSource();
if (dataSource instanceof DruidDataSource) {
if (validatorThread == null) {
validatorThread = new HostAndPortValidatorThread();
validatorThread.setSleepSeconds(healthCheckIntervalSeconds);
validatorThread.setModifyTestFlags(modifyTestFlags);
}
validatorThread.setDataSource((DruidDataSource) dataSource);
runningValidatorThread = new Thread(validatorThread);
runningValidatorThread.start();
isValidatorThreadStarted = true;
} else {
LOG.warn("The DataSource is not a DruidDataSource, ignore HostAndPortValidator.");
}
}
public void setFile(String file) {
this.file = file;
}
public void setHealthCheckIntervalSeconds(int healthCheckIntervalSeconds) {
this.healthCheckIntervalSeconds = healthCheckIntervalSeconds;
}
public void setValidatorThread(HostAndPortValidatorThread validatorThread) {
this.validatorThread = validatorThread;
}
public void setModifyTestFlags(boolean modifyTestFlags) {
this.modifyTestFlags = modifyTestFlags;
}
}