mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add some logs to RandomDataSourceSelector
This commit is contained in:
@@ -216,6 +216,8 @@ public class RandomDataSourceSelector implements DataSourceSelector {
|
||||
for (DataSource b : blacklist) {
|
||||
dataSourceSet.remove(b);
|
||||
}
|
||||
LOG.info(blacklist.size() + " Blacklist DataSource removed, return "
|
||||
+ dataSourceSet.size() + " DataSource(s).");
|
||||
}
|
||||
return dataSourceSet;
|
||||
}
|
||||
@@ -228,6 +230,7 @@ public class RandomDataSourceSelector implements DataSourceSelector {
|
||||
}
|
||||
}
|
||||
if (!busyDataSourceSet.isEmpty() && busyDataSourceSet.size() < dataSourceSet.size()) {
|
||||
LOG.info("Busy DataSouces: " + busyDataSourceSet.size() + "/" + dataSourceSet.size());
|
||||
for (DataSource ds : busyDataSourceSet) {
|
||||
dataSourceSet.remove(ds);
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ public class FileNodeListenerTest {
|
||||
String file = "/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties";
|
||||
FileNodeListener listener = new FileNodeListener();
|
||||
listener.setFile(file);
|
||||
listener.setPrefix("prefix3");
|
||||
listener.setPrefix("prefix0");
|
||||
List<NodeEvent> list = listener.refresh();
|
||||
|
||||
assertTrue(listener.getProperties().isEmpty());
|
||||
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package com.alibaba.druid.pool.ha.selector;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ha.HighAvailableDataSource;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
|
||||
public class RandomDataSourceSelectorSingleDataSourceTest {
|
||||
private final static Log LOG = LogFactory
|
||||
.getLog(RandomDataSourceSelectorSingleDataSourceTest.class);
|
||||
|
||||
private HighAvailableDataSource highAvailableDataSource;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
highAvailableDataSource = new HighAvailableDataSource();
|
||||
String file = "/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties";
|
||||
highAvailableDataSource.setDataSourceFile(file);
|
||||
highAvailableDataSource.setPropertyPrefix("prefix3");
|
||||
highAvailableDataSource.setMaxActive(2);
|
||||
highAvailableDataSource.setMaxWait(500);
|
||||
highAvailableDataSource.setInitialSize(2);
|
||||
initSelector(highAvailableDataSource);
|
||||
highAvailableDataSource.init();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
highAvailableDataSource.destroy();
|
||||
highAvailableDataSource = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetWhileBlacklistIsEmpty() {
|
||||
RandomDataSourceSelector selector = (RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector();
|
||||
DruidDataSource dataSource = (DruidDataSource) highAvailableDataSource.getAvailableDataSourceMap()
|
||||
.get("prefix3.foo");
|
||||
assertNotNull(selector.get());
|
||||
assertFalse(dataSource.isTestOnReturn());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetWhileBlacklisted() throws Exception {
|
||||
RandomDataSourceSelector selector = (RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector();
|
||||
DruidDataSource dataSource = (DruidDataSource) highAvailableDataSource.getAvailableDataSourceMap()
|
||||
.get("prefix3.foo");
|
||||
dataSource.setValidationQuery("select xxx from yyy");
|
||||
|
||||
Thread.sleep(10 * 1000);
|
||||
assertTrue(dataSource.isTestOnReturn());
|
||||
for (int i = 0; i < 5; i++) {
|
||||
assertNotNull(selector.get());
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAfterRecovering() throws Exception {
|
||||
RandomDataSourceSelector selector = (RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector();
|
||||
DruidDataSource dataSource = (DruidDataSource) highAvailableDataSource.getAvailableDataSourceMap()
|
||||
.get("prefix3.foo");
|
||||
dataSource.setValidationQuery("select xxx from yyy");
|
||||
|
||||
Thread.sleep(10 * 1000);
|
||||
assertTrue(dataSource.isTestOnReturn());
|
||||
assertNotNull(selector.get());
|
||||
Thread.sleep(10 * 1000);
|
||||
assertTrue(dataSource.getPoolingCount() == 0);
|
||||
|
||||
dataSource.setValidationQuery("values current_timestamp");
|
||||
Thread.sleep(10 * 1000);
|
||||
assertFalse(dataSource.isTestOnReturn());
|
||||
assertNotNull(selector.get());
|
||||
assertTrue(dataSource.getPoolingCount() > 0);
|
||||
}
|
||||
|
||||
private RandomDataSourceSelector initSelector(HighAvailableDataSource dataSource) {
|
||||
RandomDataSourceSelector selector = new RandomDataSourceSelector(dataSource);
|
||||
RandomDataSourceValidateThread validateThread = new RandomDataSourceValidateThread(selector);
|
||||
RandomDataSourceRecoverThread recoverThread = new RandomDataSourceRecoverThread(selector);
|
||||
validateThread.setCheckingIntervalSeconds(3);
|
||||
recoverThread.setRecoverIntervalSeconds(3);
|
||||
selector.setValidateThread(validateThread);
|
||||
selector.setRecoverThread(recoverThread);
|
||||
selector.init();
|
||||
dataSource.setDataSourceSelector(selector);
|
||||
return selector;
|
||||
}
|
||||
}
|
||||
@@ -4,5 +4,6 @@ prefix1.foo.password=foo_password
|
||||
prefix2.foo.url=jdbc:derby:memory:foo2;create=true
|
||||
prefix2.foo.username=bar
|
||||
prefix2.foo.password=bar_password
|
||||
prefix3.foo.url=jdbc:derby:memory:foo3;create=true
|
||||
foo.url=jdbc:derby:memory:foo3;create=true
|
||||
bar=jdbc:derby:memory:bar;create=true
|
||||
|
||||
Reference in New Issue
Block a user