Keep at least one DataSource in the pool when update by events.

This commit is contained in:
DigitalSonic
2020-05-05 22:21:00 +08:00
parent 75391df4ad
commit 99270819ee
5 changed files with 65 additions and 6 deletions
@@ -97,6 +97,7 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
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;
@@ -113,6 +114,7 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
}
if (dataSourceMap == null || dataSourceMap.isEmpty()) {
poolUpdater.setIntervalSeconds(poolPurgeIntervalSeconds);
poolUpdater.setAllowEmptyPool(allowEmptyPoolWhenUpdate);
poolUpdater.init();
createNodeMap();
}
@@ -304,6 +306,14 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
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() {
@@ -50,6 +50,7 @@ public class PoolUpdater implements Observer {
private ScheduledExecutorService executor;
private int intervalSeconds = DEFAULT_INTERVAL;
private volatile boolean inited = false;
private boolean allowEmptyPool = false;
public PoolUpdater(HighAvailableDataSource highAvailableDataSource) {
setHighAvailableDataSource(highAvailableDataSource);
@@ -213,6 +214,11 @@ public class PoolUpdater implements Observer {
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);
}
@@ -247,4 +253,12 @@ public class PoolUpdater implements Observer {
public void setIntervalSeconds(int intervalSeconds) {
this.intervalSeconds = intervalSeconds;
}
public boolean isAllowEmptyPool() {
return allowEmptyPool;
}
public void setAllowEmptyPool(boolean allowEmptyPool) {
this.allowEmptyPool = allowEmptyPool;
}
}