mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Add a HA DataSource implementation
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package com.alibaba.druid.pool.ha;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class DataSourceCreatorTest {
|
||||
@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"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.alibaba.druid.pool.ha;
|
||||
|
||||
import com.alibaba.druid.mock.MockConnection;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class HighAvailableDataSourceTest {
|
||||
@Test
|
||||
public void testByNameGet() throws Exception {
|
||||
HighAvailableDataSource haDataSource = new HighAvailableDataSource();
|
||||
Map<String, DataSource> map = new HashMap<String, DataSource>();
|
||||
map.put("foo", new MockDataSource("foo"));
|
||||
map.put("bar", new MockDataSource("bar"));
|
||||
haDataSource.setDataSourceMap(map);
|
||||
haDataSource.setSelector("byname");
|
||||
haDataSource.init();
|
||||
haDataSource.setTargetDataSource("foo");
|
||||
assertNotNull(haDataSource.getConnection());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRandomGet() throws Exception {
|
||||
HighAvailableDataSource haDataSource = new HighAvailableDataSource();
|
||||
Map<String, DataSource> map = new HashMap<String, DataSource>();
|
||||
map.put("foo", new MockDataSource("foo"));
|
||||
map.put("bar", new MockDataSource("bar"));
|
||||
int[] count = new int[2];
|
||||
|
||||
haDataSource.setDataSourceMap(map);
|
||||
for (int i = 0; i < 100; i++) {
|
||||
String name = ((MockConnection)haDataSource.getConnection()).getUrl();
|
||||
if (name.equalsIgnoreCase("foo")) {
|
||||
count[0]++;
|
||||
} else if (name.equalsIgnoreCase("bar")) {
|
||||
count[1]++;
|
||||
}
|
||||
}
|
||||
assertTrue(count[0] > 0);
|
||||
assertTrue(count[1] > 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.alibaba.druid.pool.ha;
|
||||
|
||||
import com.alibaba.druid.mock.MockConnection;
|
||||
import com.alibaba.druid.pool.WrapperAdapter;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.PrintWriter;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLFeatureNotSupportedException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class MockDataSource extends WrapperAdapter implements DataSource {
|
||||
private String name;
|
||||
|
||||
public MockDataSource(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
return new MockConnection(null, name, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection(String username, String password) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintWriter getLogWriter() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLogWriter(PrintWriter out) throws SQLException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLoginTimeout(int seconds) throws SQLException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLoginTimeout() throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.alibaba.druid.pool.ha.selector;
|
||||
|
||||
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.*;
|
||||
|
||||
public class NamedDataSourceSelectorTest {
|
||||
private Map<String, DataSource> dataSourceMap;
|
||||
private HighAvailableDataSource dataSource;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
dataSourceMap = new HashMap<String, DataSource>();
|
||||
dataSourceMap.put("foo", new MockDataSource("foo"));
|
||||
dataSourceMap.put("bar", new MockDataSource("bar"));
|
||||
dataSource = new HighAvailableDataSource();
|
||||
dataSource.setDataSourceMap(dataSourceMap);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
dataSourceMap = null;
|
||||
dataSource = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyMap() {
|
||||
dataSourceMap.clear();
|
||||
NamedDataSourceSelector selector = new NamedDataSourceSelector(null);
|
||||
assertNull(selector.get());
|
||||
selector = new NamedDataSourceSelector(dataSource);
|
||||
assertNull(selector.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnlyOne() {
|
||||
dataSourceMap.remove("foo");
|
||||
NamedDataSourceSelector selector = new NamedDataSourceSelector(dataSource);
|
||||
for (int i = 0; i < 50; i++) {
|
||||
assertEquals("bar", ((MockDataSource)selector.get()).getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetByName() throws Exception {
|
||||
NamedDataSourceSelector selector = new NamedDataSourceSelector(dataSource);
|
||||
|
||||
assertNull(selector.get());
|
||||
selector.setTarget("foo");
|
||||
assertEquals("foo", ((MockDataSource)selector.get()).getName());
|
||||
selector.resetDataSourceName();
|
||||
selector.setDefaultName("bar");
|
||||
assertEquals("bar", ((MockDataSource)selector.get()).getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.alibaba.druid.pool.ha.selector;
|
||||
|
||||
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.assertTrue;
|
||||
|
||||
public class RandomDataSourceSelectorTest {
|
||||
private Map<String, DataSource> dataSourceMap;
|
||||
private HighAvailableDataSource dataSource;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
dataSourceMap = new HashMap<String, DataSource>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
dataSourceMap.put(Integer.toString(i), new MockDataSource(Integer.toString(i)));
|
||||
}
|
||||
dataSource = new HighAvailableDataSource();
|
||||
dataSource.setDataSourceMap(dataSourceMap);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
dataSourceMap = null;
|
||||
dataSource = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRandomGet() throws Exception {
|
||||
RandomDataSourceSelector selector = new RandomDataSourceSelector(dataSource);
|
||||
int[] count = new int[10];
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
MockDataSource dataSource = (MockDataSource) selector.get();
|
||||
count[Integer.parseInt(dataSource.getName())]++;
|
||||
}
|
||||
|
||||
for (int i : count) {
|
||||
assertTrue(i > 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneDataSourceFail() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package com.alibaba.druid.pool.ha.selector;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ha.HighAvailableDataSource;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class RandomDataSourceSelectorWithValidationTest {
|
||||
private HighAvailableDataSource highAvailableDataSource;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
highAvailableDataSource = new HighAvailableDataSource();
|
||||
String file = "/com/alibaba/druid/pool/ha/ha-datasource.properties";
|
||||
highAvailableDataSource.setDataSourceFile(file);
|
||||
highAvailableDataSource.init();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
highAvailableDataSource = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneDataSourceFailAndRecover() throws Exception {
|
||||
((RandomDataSourceSelector) highAvailableDataSource.getSelector()).getValidateThread().setSleepSeconds(3);
|
||||
((RandomDataSourceSelector) highAvailableDataSource.getSelector()).getRecoverThread().setSleepSeconds(3);
|
||||
|
||||
Thread.sleep(30 * 1000);
|
||||
|
||||
DruidDataSource dataSource = (DruidDataSource) highAvailableDataSource.getDataSourceMap().get("foo");
|
||||
dataSource.setValidationQuery("select xxx from yyy");
|
||||
Thread.sleep(10 * 1000);
|
||||
assertTrue(dataSource.isTestOnReturn());
|
||||
for (int i = 0; i < 100; i++) {
|
||||
assertNotEquals(dataSource, highAvailableDataSource.getSelector().get());
|
||||
}
|
||||
|
||||
dataSource.setValidationQuery(null);
|
||||
Thread.sleep(4 * 1000);
|
||||
assertFalse(dataSource.isTestOnReturn());
|
||||
int count = 0;
|
||||
for (int i = 0; i < 100; i++) {
|
||||
if (dataSource == highAvailableDataSource.getSelector().get()) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
assertTrue(count > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllDataSourceFail() throws Exception {
|
||||
((RandomDataSourceSelector) highAvailableDataSource.getSelector()).getValidateThread().setSleepSeconds(3);
|
||||
((RandomDataSourceSelector) highAvailableDataSource.getSelector()).getRecoverThread().setSleepSeconds(3);
|
||||
|
||||
Thread.sleep(30 * 1000);
|
||||
|
||||
DruidDataSource foo = (DruidDataSource) highAvailableDataSource.getDataSourceMap().get("foo");
|
||||
DruidDataSource bar = (DruidDataSource) highAvailableDataSource.getDataSourceMap().get("bar");
|
||||
foo.setValidationQuery("select xxx from yyy");
|
||||
bar.setValidationQuery("select xxx from yyy");
|
||||
|
||||
Thread.sleep(10 * 1000);
|
||||
assertTrue(foo.isTestOnReturn());
|
||||
assertTrue(bar.isTestOnReturn());
|
||||
|
||||
int[] count = new int[2];
|
||||
for (int i = 0; i < 100; i++) {
|
||||
DataSource dataSource = highAvailableDataSource.getSelector().get();
|
||||
if (foo == dataSource) {
|
||||
count[0]++;
|
||||
} else if (bar == dataSource) {
|
||||
count[1]++;
|
||||
}
|
||||
}
|
||||
assertTrue(count[0] > 0);
|
||||
assertTrue(count[1] > 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user