some small refactors & add tests

This commit is contained in:
DigitalSonic
2020-02-06 13:53:28 +08:00
parent eb90260afe
commit 34aa0c8cd1
10 changed files with 113 additions and 43 deletions
@@ -37,7 +37,6 @@ public class FileNodeListener extends NodeListener {
private Lock lock = new ReentrantLock();
private String file = null;
private String prefix = "";
private int intervalSeconds = 60;
private ScheduledExecutorService executor;
@@ -81,7 +80,7 @@ public class FileNodeListener extends NodeListener {
@Override
public List<NodeEvent> refresh() {
Properties originalProperties = PropertiesUtils.loadProperties(file);
List<String> nameList = PropertiesUtils.loadNameList(originalProperties, prefix);
List<String> nameList = PropertiesUtils.loadNameList(originalProperties, getPrefix());
Properties properties = new Properties();
for (String n : nameList) {
String url = originalProperties.getProperty(n + ".url");
@@ -143,12 +142,4 @@ public class FileNodeListener extends NodeListener {
public void setFile(String file) {
this.file = file;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
}
@@ -36,6 +36,7 @@ import java.util.concurrent.locks.ReentrantLock;
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;
@@ -111,4 +112,12 @@ public abstract class NodeListener extends Observable {
public void setProperties(Properties properties) {
this.properties = properties;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
}
@@ -231,7 +231,7 @@ public class PoolUpdater implements Observer {
}
private void blacklistNode(String nodeName) {
LOG.info("Deleting Node " + nodeName + ", just add into blacklist.");
LOG.info("Deleting Node " + nodeName + ", just add it into blacklist.");
nodesToDel.add(nodeName);
highAvailableDataSource.addBlackList(nodeName);
}
@@ -24,6 +24,13 @@ 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;
@@ -71,4 +78,12 @@ public class ZookeeperNodeInfo {
public void setPassword(String password) {
this.password = password;
}
public String getDatabase() {
return database;
}
public void setDatabase(String database) {
this.database = database;
}
}
@@ -57,6 +57,7 @@ import java.util.concurrent.locks.ReentrantLock;
* bar.port=3309
* bar.username=bar
* bar.password=bar_password
* bar.database=bar_database
* </pre>
*
* @author DigitalSonic
@@ -65,15 +66,14 @@ public class ZookeeperNodeListener extends NodeListener {
private final static Log LOG = LogFactory.getLog(ZookeeperNodeListener.class);
private String zkConnectString;
private String path = "/ha-druid-datasources";
private String dataPrefix = "";
private Lock lock = new ReentrantLock();
private boolean privateZkClient = false; // Should I close the client?
private PathChildrenCache cache;
private CuratorFramework client;
/**
* Template Format:
* jdbc:mysql://${host}:${port}/foo?useUnicode=true
* ${host} and ${port} will be replaced by values in ZK
* 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;
@@ -183,7 +183,7 @@ public class ZookeeperNodeListener extends NodeListener {
ChildData data = event.getData();
String nodeName = getNodeName(data);
List<String> names = new ArrayList<String>();
names.add(dataPrefix + "." + nodeName);
names.add(getPrefix() + "." + nodeName);
Properties properties = getPropertiesFromChildData(data);
List<NodeEvent> events = NodeEvent.generateEvents(properties, names, type);
@@ -228,6 +228,7 @@ public class ZookeeperNodeListener extends NodeListener {
}
private Properties getPropertiesFromChildData(ChildData data) {
String dataPrefix = getPrefix();
Properties properties = new Properties();
if (data == null) {
return properties;
@@ -243,7 +244,7 @@ public class ZookeeperNodeListener extends NodeListener {
Properties filtered = PropertiesUtils.filterPrefix(full, dataPrefix);
for (String n : filtered.stringPropertyNames()) {
properties.setProperty(
n.replaceFirst(dataPrefix, dataPrefix + "." + nodeName),
n.replaceFirst(dataPrefix, dataPrefix + "\\." + nodeName),
filtered.getProperty(n));
}
if (!properties.containsKey(dataPrefix + "." + nodeName + ".url")) {
@@ -254,12 +255,16 @@ public class ZookeeperNodeListener extends NodeListener {
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;
}
@@ -299,14 +304,6 @@ public class ZookeeperNodeListener extends NodeListener {
this.path = path;
}
public String getDataPrefix() {
return dataPrefix;
}
public void setDataPrefix(String dataPrefix) {
this.dataPrefix = dataPrefix;
}
public String getUrlTemplate() {
return urlTemplate;
}
@@ -131,6 +131,9 @@ public class ZookeeperNodeRegister {
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());
}