mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge pull request #3694 from digitalsonic/master
针对 HighAvailableDataSource 的多项优化
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package com.alibaba.druid.pool.ha;
|
||||
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class CounterValidConnectionChecker implements ValidConnectionChecker {
|
||||
private int countBeforeFailure = 100;
|
||||
private Map<String, Integer> counterMap = new ConcurrentHashMap<String, Integer>();
|
||||
|
||||
@Override
|
||||
public boolean isValidConnection(Connection c, String query, int validationQueryTimeout) throws Exception {
|
||||
String url = c.getMetaData().getURL();
|
||||
if (!counterMap.containsKey(url)) {
|
||||
counterMap.put(url, 0);
|
||||
}
|
||||
Integer counter = counterMap.get(url);
|
||||
counterMap.put(url, ++counter);
|
||||
if (counter >= countBeforeFailure) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configFromProperties(Properties properties) {
|
||||
|
||||
}
|
||||
|
||||
public int getCountValue(String url) {
|
||||
return counterMap.containsKey(url) ? counterMap.get(url) : 0;
|
||||
}
|
||||
|
||||
public int getCountBeforeFailure() {
|
||||
return countBeforeFailure;
|
||||
}
|
||||
|
||||
public void setCountBeforeFailure(int countBeforeFailure) {
|
||||
this.countBeforeFailure = countBeforeFailure;
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package com.alibaba.druid.pool.ha;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class DataSourceCreatorTest {
|
||||
@Test
|
||||
public void testNameList() {
|
||||
String file = "/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties";
|
||||
DataSourceCreator creator = new DataSourceCreator(file, "prefix1");
|
||||
List<String> list = creator.getNameList();
|
||||
assertFalse(list.isEmpty());
|
||||
assertEquals(1, list.size());
|
||||
assertEquals("prefix1.foo", list.get(0));
|
||||
|
||||
creator = new DataSourceCreator(file);
|
||||
list = creator.getNameList();
|
||||
assertEquals(3, list.size());
|
||||
for (String k : list) {
|
||||
assertNotEquals("bar", k);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createMap() throws Exception {
|
||||
HighAvailableDataSource highAvailableDataSource = new HighAvailableDataSource();
|
||||
String file = "/com/alibaba/druid/pool/ha/ha-datasource.properties";
|
||||
DataSourceCreator creator = new DataSourceCreator(file);
|
||||
Map<String, DataSource> map = creator.createMap(highAvailableDataSource);
|
||||
assertEquals(2, map.size());
|
||||
assertNotNull(map.get("foo"));
|
||||
assertNotNull(map.get("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createMapWithPrefix() throws Exception {
|
||||
HighAvailableDataSource highAvailableDataSource = new HighAvailableDataSource();
|
||||
String file = "/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties";
|
||||
DataSourceCreator creator = new DataSourceCreator(file, "prefix1");
|
||||
Map<String, DataSource> map = creator.createMap(highAvailableDataSource);
|
||||
assertEquals(1, map.size());
|
||||
assertNotNull(map.get("prefix1.foo"));
|
||||
assertNull(map.get("prefix2.foo"));
|
||||
|
||||
creator = new DataSourceCreator(file, "prefix2");
|
||||
map = creator.createMap(highAvailableDataSource);
|
||||
assertEquals(1, map.size());
|
||||
assertNull(map.get("prefix1.foo"));
|
||||
assertNotNull(map.get("prefix2.foo"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,6 +22,7 @@ public class HighAvailableDataSourceTest {
|
||||
haDataSource.init();
|
||||
haDataSource.setTargetDataSource("foo");
|
||||
assertNotNull(haDataSource.getConnection());
|
||||
haDataSource.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -43,5 +44,6 @@ public class HighAvailableDataSourceTest {
|
||||
}
|
||||
assertTrue(count[0] > 0);
|
||||
assertTrue(count[1] > 0);
|
||||
haDataSource.destroy();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
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;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
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());
|
||||
|
||||
// try to emove all but we have to keep one left
|
||||
writePropertiesFile(file, new Properties());
|
||||
|
||||
Thread.sleep(6000);
|
||||
assertEquals(1, dataSource.getAvailableDataSourceMap().size());
|
||||
|
||||
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);
|
||||
String file = "/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties";
|
||||
FileNodeListener listener = new FileNodeListener();
|
||||
listener.setFile(file);
|
||||
listener.setPrefix("prefix1");
|
||||
listener.setObserver(new Observer() {
|
||||
@Override
|
||||
public void update(Observable o, Object arg) {
|
||||
cdl.countDown();
|
||||
assertTrue(o instanceof FileNodeListener);
|
||||
assertTrue(arg instanceof NodeEvent[]);
|
||||
NodeEvent[] events = (NodeEvent[]) arg;
|
||||
assertEquals(1, events.length);
|
||||
assertEquals(NodeEventTypeEnum.ADD, events[0].getType());
|
||||
assertEquals("prefix1.foo", events[0].getNodeName());
|
||||
assertEquals("jdbc:derby:memory:foo1;create=true", events[0].getUrl());
|
||||
}
|
||||
});
|
||||
listener.init();
|
||||
listener.update();
|
||||
assertTrue(cdl.await(100, TimeUnit.MILLISECONDS));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRefresh_emptyPropertiesMatch() {
|
||||
String file = "/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties";
|
||||
FileNodeListener listener = new FileNodeListener();
|
||||
listener.setFile(file);
|
||||
listener.setPrefix("prefix0");
|
||||
List<NodeEvent> list = listener.refresh();
|
||||
|
||||
assertTrue(listener.getProperties().isEmpty());
|
||||
assertTrue(list.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRefresh() {
|
||||
String file = "/com/alibaba/druid/pool/ha/ha-with-prefix-datasource.properties";
|
||||
FileNodeListener listener = new FileNodeListener();
|
||||
listener.setFile(file);
|
||||
listener.setPrefix("prefix1");
|
||||
List<NodeEvent> list = listener.refresh();
|
||||
|
||||
Properties properties = listener.getProperties();
|
||||
assertEquals(3, properties.size());
|
||||
assertEquals("jdbc:derby:memory:foo1;create=true", properties.getProperty("prefix1.foo.url"));
|
||||
|
||||
assertEquals(1, list.size());
|
||||
NodeEvent event = list.get(0);
|
||||
assertEquals(NodeEventTypeEnum.ADD, event.getType());
|
||||
assertEquals("prefix1.foo", event.getNodeName());
|
||||
assertEquals("jdbc:derby:memory:foo1;create=true", event.getUrl());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.alibaba.druid.pool.ha.node;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class NodeEventTest {
|
||||
|
||||
@Test
|
||||
public void testGetEventListFromProperties_emptyProperties() {
|
||||
Properties p1 = new Properties();
|
||||
Properties p2 = new Properties();
|
||||
|
||||
List<NodeEvent> list = NodeEvent.getEventsByDiffProperties(p1, p2);
|
||||
assertTrue(list.isEmpty());
|
||||
|
||||
p1.setProperty("foo.url", "foo_url");
|
||||
p1.setProperty("foo.username", "foo_username");
|
||||
p1.setProperty("foo.password", "foo_password");
|
||||
|
||||
list = NodeEvent.getEventsByDiffProperties(p1, p2);
|
||||
assertEquals(1, list.size());
|
||||
NodeEvent event = list.get(0);
|
||||
assertEquals(NodeEventTypeEnum.DELETE, event.getType());
|
||||
assertEquals("foo", event.getNodeName());
|
||||
assertEquals("foo_url", event.getUrl());
|
||||
assertEquals("foo_username", event.getUsername());
|
||||
assertEquals("foo_password", event.getPassword());
|
||||
|
||||
list = NodeEvent.getEventsByDiffProperties(p2, p1);
|
||||
assertEquals(1, list.size());
|
||||
event = list.get(0);
|
||||
assertEquals(NodeEventTypeEnum.ADD, event.getType());
|
||||
assertEquals("foo", event.getNodeName());
|
||||
assertEquals("foo_url", event.getUrl());
|
||||
assertEquals("foo_username", event.getUsername());
|
||||
assertEquals("foo_password", event.getPassword());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package com.alibaba.druid.pool.ha.node;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ha.HighAvailableDataSource;
|
||||
import com.alibaba.druid.pool.ha.MockDataSource;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PoolUpdaterTest {
|
||||
private HighAvailableDataSource haDataSource;
|
||||
private PoolUpdater updater;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
haDataSource = new HighAvailableDataSource();
|
||||
Map<String, DataSource> map = new HashMap<String, DataSource>();
|
||||
haDataSource.setDataSourceMap(map);
|
||||
|
||||
updater = new PoolUpdater(haDataSource);
|
||||
updater.init();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
haDataSource.destroy();
|
||||
haDataSource = null;
|
||||
updater = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() {
|
||||
haDataSource.getDataSourceMap().put("foo", new MockDataSource("foo"));
|
||||
haDataSource.getDataSourceMap().put("bar", new MockDataSource("bar"));
|
||||
|
||||
NodeEvent event = new NodeEvent();
|
||||
event.setNodeName("foo");
|
||||
event.setType(NodeEventTypeEnum.DELETE);
|
||||
|
||||
updater.update(new FileNodeListener(), new NodeEvent[] { event });
|
||||
validateDeleteNode();
|
||||
|
||||
event = new NodeEvent();
|
||||
event.setNodeName("foo");
|
||||
event.setType(NodeEventTypeEnum.ADD);
|
||||
updater.update(new FileNodeListener(), new NodeEvent[] { event });
|
||||
assertFalse(updater.getNodesToDel().contains("foo"));
|
||||
assertFalse(haDataSource.isInBlackList("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveDataSources() {
|
||||
String url = "jdbc:derby:memory:foo;create=true";
|
||||
String name = "foo";
|
||||
addNode(url, name);
|
||||
DruidDataSource ds = (DruidDataSource) haDataSource.getDataSourceMap().get(name);
|
||||
|
||||
updater.getNodesToDel().add(name);
|
||||
haDataSource.addBlackList(name);
|
||||
|
||||
updater.removeDataSources();
|
||||
assertTrue(haDataSource.getDataSourceMap().isEmpty());
|
||||
assertFalse(haDataSource.isInBlackList(name));
|
||||
assertTrue(ds.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveDataSources_nodeNotExisted() {
|
||||
DataSource ds = new MockDataSource("foo");
|
||||
haDataSource.getDataSourceMap().put("foo", ds);
|
||||
updater.getNodesToDel().add("bar");
|
||||
updater.removeDataSources();
|
||||
assertFalse(updater.getNodesToDel().contains("bar"));
|
||||
assertEquals(1, haDataSource.getDataSourceMap().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveDataSources_notDruid() {
|
||||
DataSource ds = new MockDataSource("foo");
|
||||
haDataSource.getDataSourceMap().put("foo", ds);
|
||||
updater.getNodesToDel().add("foo");
|
||||
updater.removeDataSources();
|
||||
assertFalse(updater.getNodesToDel().contains("foo"));
|
||||
assertTrue(haDataSource.getDataSourceMap().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate_onlyOneLeftToRemove() {
|
||||
haDataSource.getDataSourceMap().put("foo", new MockDataSource("foo"));
|
||||
|
||||
NodeEvent event = new NodeEvent();
|
||||
event.setNodeName("foo");
|
||||
event.setType(NodeEventTypeEnum.DELETE);
|
||||
|
||||
updater.update(new FileNodeListener(), new NodeEvent[] { event });
|
||||
assertTrue(haDataSource.getDataSourceMap().containsKey("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddNode() {
|
||||
String url = "jdbc:derby:memory:foo;create=true";
|
||||
String name = "foo";
|
||||
addNode(url, name);
|
||||
String targetUrl = ((DruidDataSource) haDataSource.getDataSourceMap().get(name)).getUrl();
|
||||
assertEquals(1, haDataSource.getDataSourceMap().size());
|
||||
assertEquals(url, targetUrl);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddNode_alreadyExisted() {
|
||||
DataSource ds = new MockDataSource("foo");
|
||||
haDataSource.getDataSourceMap().put("foo", ds);
|
||||
updater.getNodesToDel().add("foo");
|
||||
|
||||
addNode("foo_url", "foo");
|
||||
|
||||
assertFalse(updater.getNodesToDel().contains("foo"));
|
||||
assertFalse(haDataSource.isInBlackList("foo"));
|
||||
assertEquals(ds, haDataSource.getDataSourceMap().get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteNode_notExisted() {
|
||||
NodeEvent event = new NodeEvent();
|
||||
event.setNodeName("foo");
|
||||
event.setType(NodeEventTypeEnum.DELETE);
|
||||
updater.deleteNode(event);
|
||||
assertTrue(updater.getNodesToDel().isEmpty());
|
||||
assertFalse(haDataSource.isInBlackList("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteNode() {
|
||||
haDataSource.getDataSourceMap().put("foo", new MockDataSource("foo"));
|
||||
haDataSource.getDataSourceMap().put("bar", new MockDataSource("bar"));
|
||||
|
||||
NodeEvent event = new NodeEvent();
|
||||
event.setNodeName("foo");
|
||||
event.setType(NodeEventTypeEnum.DELETE);
|
||||
updater.deleteNode(event);
|
||||
validateDeleteNode();
|
||||
}
|
||||
|
||||
private void validateDeleteNode() {
|
||||
assertEquals(1, updater.getNodesToDel().size());
|
||||
assertTrue(updater.getNodesToDel().contains("foo"));
|
||||
assertTrue(haDataSource.isInBlackList("foo"));
|
||||
}
|
||||
|
||||
private void addNode(String url, String name) {
|
||||
NodeEvent event = new NodeEvent();
|
||||
event.setNodeName(name);
|
||||
event.setUrl(url);
|
||||
event.setType(NodeEventTypeEnum.ADD);
|
||||
updater.addNode(event);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
package com.alibaba.druid.pool.ha.node;
|
||||
|
||||
import com.alibaba.druid.DruidRuntimeException;
|
||||
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;
|
||||
import org.apache.curator.test.TestingServer;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class ZookeeperNodeListenerTest {
|
||||
private final static Log LOG = LogFactory.getLog(ZookeeperNodeListenerTest.class);
|
||||
private static TestingServer server;
|
||||
private final String PATH = "/ha-druid-datasource";
|
||||
|
||||
@BeforeClass
|
||||
public static void init() throws Exception {
|
||||
server = new TestingServer();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void destroy() throws Exception {
|
||||
server.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHaWithZk() throws Exception {
|
||||
// 1. Init HighAvailableDataSource
|
||||
ZookeeperNodeListener listener = new ZookeeperNodeListener();
|
||||
listener.setZkConnectString(server.getConnectString());
|
||||
listener.setPrefix("foo");
|
||||
listener.setPath(PATH);
|
||||
listener.setUrlTemplate("jdbc:derby:memory:${database};create=true");
|
||||
HighAvailableDataSource dataSource = new HighAvailableDataSource();
|
||||
dataSource.setNodeListener(listener);
|
||||
dataSource.setPoolPurgeIntervalSeconds(10);
|
||||
dataSource.init();
|
||||
assertTrue(dataSource.getDataSourceMap().isEmpty());
|
||||
|
||||
// 2. Register one Node
|
||||
ZookeeperNodeRegister register = registerNodeAndReturnRegister();
|
||||
ZookeeperNodeRegister register2 = registerAnotherNodeAndReturnRegister();
|
||||
|
||||
Thread.sleep(3000); // Wait for the Node to be created.
|
||||
assertFalse(dataSource.getDataSourceMap().isEmpty());
|
||||
assertEquals(2, dataSource.getAvailableDataSourceMap().size());
|
||||
DruidDataSource ds = (DruidDataSource) dataSource.getAvailableDataSourceMap().get("foo.test-foo");
|
||||
assertEquals("jdbc:derby:memory:foo;create=true", ds.getUrl());
|
||||
assertNotNull(dataSource.getConnection());
|
||||
|
||||
// 3. Delete one Node
|
||||
register.destroy();
|
||||
boolean checkBlackList = false;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Thread.sleep(500);
|
||||
if (dataSource.isInBlackList("foo.test-foo")) {
|
||||
assertEquals(1, dataSource.getAvailableDataSourceMap().size());
|
||||
assertFalse(dataSource.getDataSourceMap().isEmpty());
|
||||
assertNotNull(dataSource.getConnection());
|
||||
checkBlackList = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assertTrue(checkBlackList);
|
||||
register2.destroy();
|
||||
Thread.sleep(3000); // Wait for the destory to be finished
|
||||
dataSource.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddOneNode() throws Exception {
|
||||
CountDownLatch cdl = new CountDownLatch(1);
|
||||
ZookeeperNodeListener listener = new ZookeeperNodeListener();
|
||||
listener.setZkConnectString(server.getConnectString());
|
||||
listener.setPath(PATH);
|
||||
listener.setUrlTemplate("jdbc:mysql://${host}:${port}/foo");
|
||||
listener.setPrefix("foo");
|
||||
listener.setObserver(new Observer() {
|
||||
@Override
|
||||
public void update(Observable o, Object arg) {
|
||||
NodeEvent[] events = (NodeEvent[]) arg;
|
||||
assertEquals(1, events.length);
|
||||
NodeEvent event = events[0];
|
||||
LOG.info("NodeEvent received: " + event);
|
||||
if (NodeEventTypeEnum.ADD == event.getType()) {
|
||||
assertEquals("sa", event.getUsername());
|
||||
assertEquals("", event.getPassword());
|
||||
assertEquals("foo.test-foo", event.getNodeName());
|
||||
assertEquals("jdbc:mysql://127.0.0.1:1234/foo",
|
||||
event.getUrl());
|
||||
cdl.countDown();
|
||||
}
|
||||
}
|
||||
});
|
||||
listener.init();
|
||||
|
||||
ZookeeperNodeRegister register = registerNodeAndReturnRegister();
|
||||
Thread.sleep(2000); // Wait for the Node to be created.
|
||||
|
||||
cdl.await(10, TimeUnit.SECONDS);
|
||||
Properties properties = listener.getProperties();
|
||||
assertEquals(6, properties.size());
|
||||
assertEquals("sa", properties.getProperty("foo.test-foo.username"));
|
||||
assertEquals("", properties.getProperty("foo.test-foo.password"));
|
||||
assertEquals("127.0.0.1", properties.getProperty("foo.test-foo.host"));
|
||||
assertEquals("1234", properties.getProperty("foo.test-foo.port"));
|
||||
assertEquals("jdbc:mysql://127.0.0.1:1234/foo",
|
||||
properties.getProperty("foo.test-foo.url"));
|
||||
register.destroy();
|
||||
listener.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveOneNode() throws Exception {
|
||||
ZookeeperNodeRegister register = registerNodeAndReturnRegister();
|
||||
Thread.sleep(2000); // Wait for the Node to be created.
|
||||
|
||||
CountDownLatch addCDL = new CountDownLatch(1);
|
||||
CountDownLatch removeCDL = new CountDownLatch(1);
|
||||
ZookeeperNodeListener listener = new ZookeeperNodeListener();
|
||||
listener.setZkConnectString(server.getConnectString());
|
||||
listener.setPath(PATH);
|
||||
listener.setUrlTemplate("jdbc:mysql://${host}:${port}/foo");
|
||||
listener.setPrefix("foo");
|
||||
listener.setObserver(new Observer() {
|
||||
@Override
|
||||
public void update(Observable o, Object arg) {
|
||||
NodeEvent[] events = (NodeEvent[]) arg;
|
||||
assertEquals(1, events.length);
|
||||
NodeEvent event = events[0];
|
||||
LOG.info("NodeEvent received: " + event);
|
||||
if (NodeEventTypeEnum.DELETE == event.getType()) {
|
||||
assertEquals("sa", event.getUsername());
|
||||
assertEquals("", event.getPassword());
|
||||
assertEquals("foo.test-foo", event.getNodeName());
|
||||
assertEquals("jdbc:mysql://127.0.0.1:1234/foo",
|
||||
event.getUrl());
|
||||
removeCDL.countDown();
|
||||
} else {
|
||||
addCDL.countDown();
|
||||
}
|
||||
}
|
||||
});
|
||||
listener.init();
|
||||
listener.update();
|
||||
addCDL.await(10, TimeUnit.SECONDS);
|
||||
Properties properties = listener.getProperties();
|
||||
assertEquals(6, properties.size());
|
||||
|
||||
register.destroy();
|
||||
removeCDL.await(10, TimeUnit.SECONDS);
|
||||
assertTrue(properties.isEmpty());
|
||||
listener.destroy();
|
||||
}
|
||||
|
||||
private ZookeeperNodeRegister registerNodeAndReturnRegister() {
|
||||
ZookeeperNodeRegister register = new ZookeeperNodeRegister();
|
||||
register.setZkConnectString(server.getConnectString());
|
||||
register.setPath(PATH);
|
||||
register.init();
|
||||
|
||||
List<ZookeeperNodeInfo> payload = new ArrayList<ZookeeperNodeInfo>();
|
||||
ZookeeperNodeInfo node = new ZookeeperNodeInfo();
|
||||
node.setPrefix("foo");
|
||||
node.setHost("127.0.0.1");
|
||||
node.setPort(1234);
|
||||
node.setDatabase("foo");
|
||||
node.setUsername("sa");
|
||||
node.setPassword("");
|
||||
payload.add(node);
|
||||
register.register("test-foo", payload);
|
||||
return register;
|
||||
}
|
||||
|
||||
private ZookeeperNodeRegister registerAnotherNodeAndReturnRegister() {
|
||||
ZookeeperNodeRegister register = new ZookeeperNodeRegister();
|
||||
register.setZkConnectString(server.getConnectString());
|
||||
register.setPath(PATH);
|
||||
register.init();
|
||||
|
||||
List<ZookeeperNodeInfo> payload = new ArrayList<ZookeeperNodeInfo>();
|
||||
ZookeeperNodeInfo node = new ZookeeperNodeInfo();
|
||||
node.setPrefix("bar");
|
||||
node.setHost("127.0.0.1");
|
||||
node.setPort(5678);
|
||||
node.setDatabase("bar");
|
||||
node.setUsername("sa");
|
||||
node.setPassword("");
|
||||
payload.add(node);
|
||||
register.register("test-bar", payload);
|
||||
return register;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitFailure() {
|
||||
ZookeeperNodeListener listener = new ZookeeperNodeListener();
|
||||
try {
|
||||
listener.init();
|
||||
fail("zkClient is null and zkConnectString is null");
|
||||
} catch (DruidRuntimeException e) {
|
||||
}
|
||||
listener.setZkConnectString("127.0.0.1:2181");
|
||||
try {
|
||||
listener.init();
|
||||
fail("path is null");
|
||||
} catch (DruidRuntimeException e) {
|
||||
}
|
||||
listener.setPath(PATH);
|
||||
try {
|
||||
listener.init();
|
||||
fail("urlTemplate is null");
|
||||
} catch (DruidRuntimeException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package com.alibaba.druid.pool.ha.node;
|
||||
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import org.apache.curator.framework.CuratorFramework;
|
||||
import org.apache.curator.test.TestingServer;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ZookeeperNodeRegisterTest {
|
||||
private final static Log LOG = LogFactory.getLog(ZookeeperNodeRegisterTest.class);
|
||||
private static TestingServer server;
|
||||
private final String PATH = "/ha-druid-datasource";
|
||||
private ZookeeperNodeRegister register;
|
||||
|
||||
@BeforeClass
|
||||
public static void init() throws Exception {
|
||||
server = new TestingServer();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void destroy() throws Exception {
|
||||
server.close();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
register = new ZookeeperNodeRegister();
|
||||
register.setZkConnectString(server.getConnectString());
|
||||
register.setPath(PATH);
|
||||
register.init();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
register.destroy();
|
||||
register.getClient().close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegister() throws Exception {
|
||||
List<ZookeeperNodeInfo> payload = new ArrayList<ZookeeperNodeInfo>();
|
||||
ZookeeperNodeInfo node1 = new ZookeeperNodeInfo();
|
||||
node1.setPrefix("foo");
|
||||
node1.setHost("127.0.0.1");
|
||||
node1.setPort(1234);
|
||||
node1.setDatabase("foo_db");
|
||||
node1.setUsername("foo");
|
||||
node1.setPassword("password");
|
||||
payload.add(node1);
|
||||
|
||||
ZookeeperNodeInfo node2 = new ZookeeperNodeInfo();
|
||||
node2.setPrefix("bar");
|
||||
node2.setHost("127.0.0.1");
|
||||
node2.setPort(5678);
|
||||
node2.setUsername("bar");
|
||||
node2.setPassword("password");
|
||||
payload.add(node2);
|
||||
|
||||
assertFalse(register.register("test-foo", null));
|
||||
assertTrue(register.register("test-foo", payload));
|
||||
assertFalse(register.register("test-foo", payload));
|
||||
|
||||
Thread.sleep(1000); // Wait for the node to be created.
|
||||
|
||||
CuratorFramework client = register.getClient();
|
||||
List<String> children = client.getChildren().forPath(PATH);
|
||||
assertFalse(children.isEmpty());
|
||||
assertEquals(1, children.size());
|
||||
assertEquals("test-foo", children.get(0));
|
||||
|
||||
byte[] bytes = client.getData().forPath(PATH + "/test-foo");
|
||||
Properties properties = new Properties();
|
||||
String str = new String(bytes);
|
||||
LOG.info("ZK Data: " + str);
|
||||
properties.load(new StringReader(str));
|
||||
validateNodeProperties(node1, properties);
|
||||
validateNodeProperties(node2, properties);
|
||||
assertTrue(properties.containsKey("foo.database"));
|
||||
assertFalse(properties.containsKey("bar.database"));
|
||||
assertEquals("foo_db", properties.getProperty("foo.database"));
|
||||
}
|
||||
|
||||
private void validateNodeProperties(ZookeeperNodeInfo node, Properties properties) {
|
||||
assertTrue(properties.containsKey(node.getPrefix() + "host"));
|
||||
assertTrue(properties.containsKey(node.getPrefix() + "port"));
|
||||
assertTrue(properties.containsKey(node.getPrefix() + "username"));
|
||||
assertTrue(properties.containsKey(node.getPrefix() + "password"));
|
||||
|
||||
assertEquals(node.getHost(), properties.getProperty(node.getPrefix() + "host"));
|
||||
assertEquals(node.getPort().toString(), properties.getProperty(node.getPrefix() + "port"));
|
||||
assertEquals(node.getUsername(), properties.getProperty(node.getPrefix() + "username"));
|
||||
assertEquals(node.getPassword(), properties.getProperty(node.getPrefix() + "password"));
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ public class BaseRandomDataSourceSelectorTest {
|
||||
@After
|
||||
public void tearDown() {
|
||||
dataSourceMap = null;
|
||||
dataSource.destroy();
|
||||
dataSource = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ public class NamedDataSourceSelectorTest {
|
||||
@After
|
||||
public void tearDown() {
|
||||
dataSourceMap = null;
|
||||
dataSource.destroy();
|
||||
dataSource = null;
|
||||
}
|
||||
|
||||
|
||||
+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;
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,7 @@ public class RandomDataSourceSelectorTest extends BaseRandomDataSourceSelectorTe
|
||||
assertEquals(60, selector.getValidateThread().getCheckingIntervalSeconds());
|
||||
|
||||
assertEquals(120, selector.getRecoveryIntervalSeconds());
|
||||
assertEquals(120, selector.getRecoverThread().getSleepSeconds());
|
||||
assertEquals(120, selector.getRecoverThread().getRecoverIntervalSeconds());
|
||||
|
||||
assertEquals(10, selector.getValidationSleepSeconds());
|
||||
assertEquals(10, selector.getValidateThread().getValidationSleepSeconds());
|
||||
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
package com.alibaba.druid.pool.ha.selector;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ha.CounterValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.ha.HighAvailableDataSource;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class RandomDataSourceSelectorWithFilterTest {
|
||||
private final static Log LOG = LogFactory.getLog(RandomDataSourceSelectorWithFilterTest.class);
|
||||
private HighAvailableDataSource highAvailableDataSource;
|
||||
private CounterValidConnectionChecker checker;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
LOG.info("setUp()");
|
||||
checker = new CounterValidConnectionChecker();
|
||||
highAvailableDataSource = new HighAvailableDataSource();
|
||||
String file = "/com/alibaba/druid/pool/ha/ha-datasource.properties";
|
||||
highAvailableDataSource.setDataSourceFile(file);
|
||||
highAvailableDataSource.setFilters("haRandomValidator");
|
||||
|
||||
RandomDataSourceSelector selector = new RandomDataSourceSelector(highAvailableDataSource);
|
||||
RandomDataSourceValidateThread thread = new RandomDataSourceValidateThread(selector);
|
||||
thread.setCheckingIntervalSeconds(2);
|
||||
selector.setValidateThread(thread);
|
||||
selector.init();
|
||||
|
||||
highAvailableDataSource.setDataSourceSelector(selector);
|
||||
highAvailableDataSource.init();
|
||||
initChecker();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
LOG.info("tearDown()");
|
||||
highAvailableDataSource.destroy();
|
||||
highAvailableDataSource = null;
|
||||
checker = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipValidation() throws Exception {
|
||||
Connection conn = highAvailableDataSource.getConnection();
|
||||
createTable(conn);
|
||||
String url = conn.getMetaData().getURL();
|
||||
Thread.sleep(5000);
|
||||
int initValue = checker.getCountValue(url);
|
||||
|
||||
executeSQL(conn, "select * from foo");
|
||||
Thread.sleep(1000);
|
||||
executeSQL(conn, "select * from foo");
|
||||
Thread.sleep(1000);
|
||||
assertEquals(initValue, checker.getCountValue(url));
|
||||
|
||||
Thread.sleep(3000);
|
||||
int value = checker.getCountValue(url);
|
||||
LOG.info("URL: " + url + " Value: " + value + " Init: " + initValue);
|
||||
assertEquals(initValue + 1, value);
|
||||
|
||||
Thread.sleep(2000);
|
||||
value = checker.getCountValue(url);
|
||||
LOG.info("URL: " + url + " Value: " + value + " Init: " + initValue);
|
||||
assertEquals(initValue + 2, value);
|
||||
|
||||
conn.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForceValidation() throws Exception {
|
||||
Connection conn = highAvailableDataSource.getConnection();
|
||||
createTable(conn);
|
||||
String url = conn.getMetaData().getURL();
|
||||
Thread.sleep(2000);
|
||||
LOG.info("Start execute SQL");
|
||||
int initValue = 0;
|
||||
int value = 0;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
executeSQL(conn, "select * from foo");
|
||||
if (initValue == 0) {
|
||||
initValue = checker.getCountValue(url);
|
||||
}
|
||||
Thread.sleep(600);
|
||||
value = checker.getCountValue(url);
|
||||
LOG.info("URL: " + url + " Value: " + value + " Init: " + initValue);
|
||||
assertEquals(initValue, value);
|
||||
}
|
||||
LOG.info("URL: " + url + " Value: " + value + " Init: " + initValue);
|
||||
Thread.sleep(5000);
|
||||
value = checker.getCountValue(url);
|
||||
assertTrue(value > initValue + 1);
|
||||
}
|
||||
|
||||
private void createTable(Connection connection) {
|
||||
try {
|
||||
executeSQL(connection, "create table foo (bar varchar(1))");
|
||||
} catch(SQLException e) {
|
||||
try {
|
||||
executeSQL(connection, "drop table foo");
|
||||
executeSQL(connection, "create table foo (bar varchar(1))");
|
||||
} catch (SQLException ex) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void executeSQL(Connection connection, String sql) throws SQLException {
|
||||
Statement stmt = connection.createStatement();
|
||||
stmt.execute(sql);
|
||||
LOG.info("Executing " + sql);
|
||||
stmt.close();
|
||||
}
|
||||
|
||||
private void initChecker() {
|
||||
Map<String, DataSource> map = highAvailableDataSource.getAvailableDataSourceMap();
|
||||
for (DataSource ds : map.values()) {
|
||||
DruidDataSource dds = (DruidDataSource) ds;
|
||||
dds.setValidConnectionChecker(checker);
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
-11
@@ -20,29 +20,27 @@ public class RandomDataSourceSelectorWithValidationTest {
|
||||
highAvailableDataSource = new HighAvailableDataSource();
|
||||
String file = "/com/alibaba/druid/pool/ha/ha-datasource.properties";
|
||||
highAvailableDataSource.setDataSourceFile(file);
|
||||
initSelector(highAvailableDataSource);
|
||||
highAvailableDataSource.init();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
highAvailableDataSource.destroy();
|
||||
highAvailableDataSource = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneDataSourceFailAndRecover() throws Exception {
|
||||
RandomDataSourceSelector selector = ((RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector());
|
||||
selector.getValidateThread().setCheckingIntervalSeconds(3);
|
||||
selector.getRecoverThread().setSleepSeconds(3);
|
||||
selector.init();
|
||||
RandomDataSourceSelector selector = (RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector();
|
||||
|
||||
DruidDataSource dataSource = (DruidDataSource) highAvailableDataSource.getDataSourceMap().get("foo");
|
||||
DruidDataSource dataSource = (DruidDataSource) highAvailableDataSource.getAvailableDataSourceMap().get("foo");
|
||||
dataSource.setValidationQuery("select xxx from yyy");
|
||||
Thread.sleep(10 * 1000);
|
||||
assertTrue(dataSource.isTestOnReturn());
|
||||
for (int i = 0; i < 100; i++) {
|
||||
assertNotEquals(dataSource, selector.get());
|
||||
}
|
||||
|
||||
dataSource.setValidationQuery(null);
|
||||
Thread.sleep(4 * 1000);
|
||||
assertFalse(dataSource.isTestOnReturn());
|
||||
@@ -58,12 +56,9 @@ public class RandomDataSourceSelectorWithValidationTest {
|
||||
@Test
|
||||
public void testAllDataSourceFail() throws Exception {
|
||||
RandomDataSourceSelector selector = (RandomDataSourceSelector) highAvailableDataSource.getDataSourceSelector();
|
||||
selector.setCheckingIntervalSeconds(3);
|
||||
selector.setRecoveryIntervalSeconds(3);
|
||||
selector.init();
|
||||
|
||||
DruidDataSource foo = (DruidDataSource) highAvailableDataSource.getDataSourceMap().get("foo");
|
||||
DruidDataSource bar = (DruidDataSource) highAvailableDataSource.getDataSourceMap().get("bar");
|
||||
DruidDataSource foo = (DruidDataSource) highAvailableDataSource.getAvailableDataSourceMap().get("foo");
|
||||
DruidDataSource bar = (DruidDataSource) highAvailableDataSource.getAvailableDataSourceMap().get("bar");
|
||||
foo.setValidationQuery("select xxx from yyy");
|
||||
bar.setValidationQuery("select xxx from yyy");
|
||||
|
||||
@@ -83,4 +78,17 @@ public class RandomDataSourceSelectorWithValidationTest {
|
||||
assertTrue(count[0] > 0);
|
||||
assertTrue(count[1] > 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user