rename some fields, add one complex test for FileNodeListener

This commit is contained in:
DigitalSonic
2020-02-05 20:38:47 +08:00
parent 12848f1b95
commit eb90260afe
4 changed files with 83 additions and 21 deletions
@@ -96,7 +96,7 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
private DataSourceSelector selector;
private String dataSourceFile = DEFAULT_DATA_SOURCE_FILE;
private String propertyPrefix = "";
private int poolUpdatePeriodSeconds = PoolUpdater.DEFAULT_PERIOD;
private int poolPurgeIntervalSeconds = PoolUpdater.DEFAULT_INTERVAL;
private volatile boolean inited = false;
@@ -111,9 +111,9 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
if (inited) {
return;
}
poolUpdater.setPeriodSeconds(poolUpdatePeriodSeconds);
poolUpdater.init();
if (dataSourceMap == null || dataSourceMap.isEmpty()) {
poolUpdater.setIntervalSeconds(poolPurgeIntervalSeconds);
poolUpdater.init();
createNodeMap();
}
if (selector == null) {
@@ -294,21 +294,21 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
FileNodeListener listener = new FileNodeListener();
listener.setFile(dataSourceFile);
listener.setPrefix(propertyPrefix);
listener.setObserver(poolUpdater);
listener.init();
nodeListener = listener;
}
nodeListener.setObserver(poolUpdater);
nodeListener.init();
nodeListener.update(); // Do update in the current Thread at the startup
}
// Getters & Setters
public int getPoolUpdatePeriodSeconds() {
return poolUpdatePeriodSeconds;
public int getPoolPurgeIntervalSeconds() {
return poolPurgeIntervalSeconds;
}
public void setPoolUpdatePeriodSeconds(int poolUpdatePeriodSeconds) {
this.poolUpdatePeriodSeconds = poolUpdatePeriodSeconds;
public void setPoolPurgeIntervalSeconds(int poolPurgeIntervalSeconds) {
this.poolPurgeIntervalSeconds = poolPurgeIntervalSeconds;
}
public NodeListener getNodeListener() {
@@ -57,6 +57,7 @@ public class FileNodeListener extends NodeListener {
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;
@@ -93,12 +94,12 @@ public class FileNodeListener extends NodeListener {
properties.setProperty(n + ".url", url);
}
if (username == null || username.isEmpty()) {
LOG.warn(n + ".username is EMPTY. Maybe you should check the config.");
LOG.debug(n + ".username is EMPTY. Maybe you should check the config.");
} else {
properties.setProperty(n + ".username", username);
}
if (password == null || password.isEmpty()) {
LOG.warn(n + ".password is EMPTY. Maybe you should check the config.");
LOG.debug(n + ".password is EMPTY. Maybe you should check the config.");
} else {
properties.setProperty(n + ".password", password);
}
@@ -106,6 +107,7 @@ public class FileNodeListener extends NodeListener {
List<NodeEvent> events = NodeEvent.getEventsByDiffProperties(getProperties(), properties);
if (events != null && !events.isEmpty()) {
LOG.info(events.size() + " different(s) detected.");
setProperties(properties);
}
return events;
@@ -40,14 +40,14 @@ import java.util.concurrent.locks.ReentrantLock;
* @author DigitalSonic
*/
public class PoolUpdater implements Observer {
public final static int DEFAULT_PERIOD = 60;
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 periodSeconds = DEFAULT_PERIOD;
private int intervalSeconds = DEFAULT_INTERVAL;
private boolean inited = false;
public PoolUpdater() {
@@ -72,20 +72,21 @@ public class PoolUpdater implements Observer {
if (inited) {
return;
}
if (periodSeconds <= 0) {
periodSeconds = DEFAULT_PERIOD;
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);
}
}
}, periodSeconds, periodSeconds, TimeUnit.SECONDS);
}, intervalSeconds, intervalSeconds, TimeUnit.SECONDS);
}
}
@@ -201,6 +202,7 @@ public class PoolUpdater implements Observer {
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);
if (dataSource != null) {
@@ -246,11 +248,11 @@ public class PoolUpdater implements Observer {
return nodesToDel;
}
public int getPeriodSeconds() {
return periodSeconds;
public int getIntervalSeconds() {
return intervalSeconds;
}
public void setPeriodSeconds(int periodSeconds) {
this.periodSeconds = periodSeconds;
public void setIntervalSeconds(int intervalSeconds) {
this.intervalSeconds = intervalSeconds;
}
}
@@ -1,7 +1,12 @@
package com.alibaba.druid.pool.ha.node;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.ha.HighAvailableDataSource;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
@@ -9,9 +14,62 @@ import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class FileNodeListenerTest {
@Test
public void testHaWithPropertiesFile() throws Exception {
// init a properties file
String folder = System.getProperty("java.io.tmpdir");
File file = new File(folder + "/ha.properties");
file.deleteOnExit();
Properties properties = new Properties();
properties.setProperty("bar.username", "sa");
properties.setProperty("bar.password", "");
properties.setProperty("bar.url", "jdbc:derby:memory:bar;create=true");
writePropertiesFile(file, properties);
// init HighAvailableDataSource
FileNodeListener listener = new FileNodeListener();
listener.setFile(file.getAbsolutePath());
listener.setPrefix("foo");
listener.setIntervalSeconds(1);
HighAvailableDataSource dataSource = new HighAvailableDataSource();
dataSource.setPoolPurgeIntervalSeconds(5);
dataSource.setDataSourceFile(file.getAbsolutePath());
dataSource.setNodeListener(listener);
dataSource.init();
assertTrue(dataSource.getDataSourceMap().isEmpty());
// Add one valid DataSource
properties.setProperty("foo.username", "sa");
properties.setProperty("foo.password", "");
properties.setProperty("foo.url", "jdbc:derby:memory:foo;create=true");
writePropertiesFile(file, properties);
Thread.sleep(6000);
assertEquals(1, dataSource.getAvailableDataSourceMap().size());
DruidDataSource foo = (DruidDataSource) dataSource.getAvailableDataSourceMap().get("foo");
assertEquals("jdbc:derby:memory:foo;create=true", foo.getUrl());
// Remove all
writePropertiesFile(file, new Properties());
Thread.sleep(6000);
assertTrue(dataSource.getAvailableDataSourceMap().isEmpty());
dataSource.destroy();
}
private void writePropertiesFile(File file, Properties properties) throws IOException {
FileWriter writer = new FileWriter(file);
properties.store(writer, "");
writer.close();
}
@Test
public void testUpdate() throws InterruptedException {
final CountDownLatch cdl = new CountDownLatch(1);