remove ha

This commit is contained in:
wenshao
2012-11-26 19:51:26 +08:00
parent c0308e1a30
commit d18f32dd12
30 changed files with 0 additions and 4057 deletions
@@ -1,216 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.pool.ha;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.mock.MockConnection;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.ha.cobar.CobarConfigLoader;
import com.alibaba.druid.pool.ha.cobar.CobarDataSource;
import com.alibaba.druid.pool.ha.cobar.CobarFailureDetecter;
import com.alibaba.druid.stat.DruidDataSourceStatManager;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.druid.util.JdbcUtils;
public class CobarDataSourceTest extends TestCase {
protected void setUp() throws Exception {
for (DruidDataSource item : DruidDataSourceStatManager.getDruidDataSourceInstances()) {
JdbcUtils.close(item);
}
}
public void test_cobarDataSource() throws Exception {
String url = "jdbc:cobar://127.0.0.1:8080/druid";
final HashMap<String, Object> config = new HashMap<String, Object>();
ArrayList<Object> cobarList = new ArrayList<Object>();
HashMap<String, Object> cobarA = new HashMap<String, Object>();
cobarA.put("ip", "mock");
cobarA.put("port", 80);
cobarA.put("schema", "cobarA");
cobarA.put("weight", 1);
cobarList.add(cobarA);
HashMap<String, Object> cobarB = new HashMap<String, Object>();
cobarB.put("ip", "mock");
cobarB.put("port", 81);
cobarB.put("schema", "cobarB");
cobarB.put("weight", 1);
cobarList.add(cobarB);
final String mockUrlA = "jdbc:mock://mock:80/cobarA";
final String mockUrlB = "jdbc:mock://mock:81/cobarB";
final AtomicBoolean statusA = new AtomicBoolean(true);
final AtomicBoolean statusB = new AtomicBoolean(true);
config.put("cobarList", cobarList);
final CobarDataSource dataSource = new CobarDataSource();
dataSource.setUrl(url);
dataSource.setUsername("test");
dataSource.setPassword("");
dataSource.setMaxWait(1);
dataSource.setConfigLoadPeriodMillis(10);
dataSource.setFailureDetectPeriodMillis(1);
CobarConfigLoader configLoader = new CobarConfigLoader(dataSource) {
public void load() throws SQLException {
responseMessage = JSONUtils.toJSONString(config);
handleResponseMessage();
}
protected String createJdbcUrl(String ip, int port, String schema) {
return "jdbc:mock://" + ip + ":" + port + "/" + schema;
}
};
dataSource.setConfigLoader(configLoader);
CobarFailureDetecter failureDetector = new CobarFailureDetecter() {
public boolean isValidConnection(DruidDataSource dataSource, Connection conn) {
MockConnection mockConn;
try {
mockConn = conn.unwrap(MockConnection.class);
} catch (SQLException e) {
e.printStackTrace();
return false;
}
if (mockConn.getUrl().equals(mockUrlA)) {
return statusA.get();
}
return statusB.get();
}
};
dataSource.setFailureDetector(failureDetector);
{
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select 1");
rs.next();
rs.close();
stmt.close();
conn.close();
}
cobarList.remove(cobarB);
Assert.assertEquals(1, cobarList.size());
Thread.sleep(dataSource.getConfigLoadPeriodMillis() * 2);
for (int i = 0; i < 100; ++i) {
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select 1");
MockConnection mockConn = conn.unwrap(MockConnection.class);
Assert.assertNotNull(mockConn);
Assert.assertEquals(mockUrlA, mockConn.getUrl());
rs.next();
rs.close();
stmt.close();
conn.close();
}
cobarList.add(cobarB);
Assert.assertEquals(2, cobarList.size());
cobarList.remove(cobarA);
Assert.assertEquals(1, cobarList.size());
Thread.sleep(dataSource.getConfigLoadPeriodMillis() * 2);
for (int i = 0; i < 100; ++i) {
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select 1");
MockConnection mockConn = conn.unwrap(MockConnection.class);
Assert.assertNotNull(mockConn);
Assert.assertEquals(mockUrlB, mockConn.getUrl());
rs.next();
rs.close();
stmt.close();
conn.close();
}
cobarList.add(cobarA);
Assert.assertEquals(2, cobarList.size());
Thread.sleep(dataSource.getConfigLoadPeriodMillis() * 2);
for (int i = 0; i < 100; ++i) {
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select 1");
rs.next();
rs.close();
stmt.close();
conn.close();
}
statusA.set(false);
Thread.sleep(dataSource.getFailureDetectPeriodMillis() * 2);
for (int i = 0; i < 100; ++i) {
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select 1");
MockConnection mockConn = conn.unwrap(MockConnection.class);
Assert.assertNotNull(mockConn);
Assert.assertEquals(mockUrlB, mockConn.getUrl());
rs.next();
rs.close();
stmt.close();
conn.close();
}
statusA.set(true);
statusB.set(false);
Thread.sleep(dataSource.getFailureDetectPeriodMillis() * 2);
for (int i = 0; i < 100; ++i) {
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select 1");
MockConnection mockConn = conn.unwrap(MockConnection.class);
Assert.assertNotNull(mockConn);
Assert.assertEquals(mockUrlA, mockConn.getUrl());
rs.next();
rs.close();
stmt.close();
conn.close();
}
dataSource.close();
}
}
@@ -1,229 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.pool.ha;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.mock.MockStatement;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.ha.HADataSource;
import com.alibaba.druid.pool.ha.MultiDataSourceStatement;
import com.alibaba.druid.stat.DruidDataSourceStatManager;
import com.alibaba.druid.util.JdbcUtils;
public class HADataSourceTest extends TestCase {
private DruidDataSource dataSourceA;
private DruidDataSource dataSourceB;
private HADataSource dataSourceHA;
protected void setUp() throws Exception {
for (DruidDataSource item : DruidDataSourceStatManager.getDruidDataSourceInstances()) {
JdbcUtils.close(item);
}
Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
dataSourceA = new DruidDataSource();
dataSourceA.setUrl("jdbc:mock:ha1");
dataSourceA.setFilters("trace");
dataSourceB = new DruidDataSource();
dataSourceB.setUrl("jdbc:mock:ha2");
dataSourceB.setFilters("stat");
dataSourceHA = new HADataSource();
dataSourceHA.setMaster(dataSourceA);
dataSourceHA.setSlave(dataSourceB);
}
protected void tearDown() throws Exception {
dataSourceHA.close();
for (DruidDataSource dataSource : DruidDataSourceStatManager.getDruidDataSourceInstances()) {
dataSource.close();
}
Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
}
public void test_createStatement_0() throws Exception {
Connection conn = dataSourceHA.getConnection();
Statement stmt = conn.createStatement();
stmt.setMaxFieldSize(100);
stmt.setMaxRows(201);
stmt.setEscapeProcessing(true);
stmt.setQueryTimeout(101);
stmt.setCursorName("cName");
stmt.setFetchDirection(ResultSet.FETCH_REVERSE);
stmt.setFetchSize(202);
{
MultiDataSourceStatement dsStmt = stmt.unwrap(MultiDataSourceStatement.class);
Assert.assertTrue(dsStmt.getId() > 0);
Assert.assertEquals(100, stmt.getMaxFieldSize());
Assert.assertEquals(201, stmt.getMaxRows());
Assert.assertEquals(true, dsStmt.isEscapeProcessing().booleanValue());
Assert.assertEquals(101, stmt.getQueryTimeout());
Assert.assertEquals("cName", dsStmt.getCursorName());
Assert.assertEquals(ResultSet.FETCH_REVERSE, stmt.getFetchDirection());
Assert.assertEquals(202, stmt.getFetchSize());
}
ResultSet rs = stmt.executeQuery("SELECT 1");
rs.close();
MockStatement mockStmt = stmt.unwrap(MockStatement.class);
Assert.assertEquals(100, mockStmt.getMaxFieldSize());
Assert.assertEquals(201, mockStmt.getMaxRows());
Assert.assertEquals(true, mockStmt.isEscapeProcessing());
Assert.assertEquals(101, mockStmt.getQueryTimeout());
Assert.assertEquals("cName", mockStmt.getCursorName());
Assert.assertEquals(ResultSet.FETCH_REVERSE, mockStmt.getFetchDirection());
Assert.assertEquals(202, mockStmt.getFetchSize());
{
MultiDataSourceStatement dsStmt = stmt.unwrap(MultiDataSourceStatement.class);
Assert.assertTrue(dsStmt.getId() > 0);
Assert.assertEquals(100, stmt.getMaxFieldSize());
Assert.assertEquals(201, stmt.getMaxRows());
Assert.assertEquals(true, dsStmt.isEscapeProcessing().booleanValue());
Assert.assertEquals(101, stmt.getQueryTimeout());
Assert.assertEquals("cName", dsStmt.getCursorName());
Assert.assertEquals(ResultSet.FETCH_REVERSE, stmt.getFetchDirection());
Assert.assertEquals(202, stmt.getFetchSize());
}
stmt.close();
conn.close();
}
public void test_createStatement_1() throws Exception {
Connection conn = dataSourceHA.getConnection();
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT 1");
rs.close();
MockStatement mockStmt = stmt.unwrap(MockStatement.class);
Assert.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, mockStmt.getResultSetType());
Assert.assertEquals(ResultSet.CONCUR_UPDATABLE, mockStmt.getResultSetConcurrency());
Assert.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, stmt.getResultSetType());
Assert.assertEquals(ResultSet.CONCUR_UPDATABLE, stmt.getResultSetConcurrency());
stmt.close();
conn.close();
}
public void test_createStatement_2() throws Exception {
Connection conn = dataSourceHA.getConnection();
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE,
ResultSet.CLOSE_CURSORS_AT_COMMIT);
ResultSet rs = stmt.executeQuery("SELECT 1");
rs.close();
MockStatement mockStmt = stmt.unwrap(MockStatement.class);
Assert.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, mockStmt.getResultSetType());
Assert.assertEquals(ResultSet.CONCUR_UPDATABLE, mockStmt.getResultSetConcurrency());
Assert.assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, mockStmt.getResultSetHoldability());
Assert.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, stmt.getResultSetType());
Assert.assertEquals(ResultSet.CONCUR_UPDATABLE, stmt.getResultSetConcurrency());
Assert.assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, stmt.getResultSetHoldability());
stmt.close();
conn.close();
}
public void test_createStatement_3() throws Exception {
Connection conn = dataSourceHA.getConnection();
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE,
ResultSet.CLOSE_CURSORS_AT_COMMIT);
ResultSet rs = stmt.executeQuery("SELECT 1");
rs.close();
MockStatement mockStmt = stmt.unwrap(MockStatement.class);
Assert.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, mockStmt.getResultSetType());
Assert.assertEquals(ResultSet.CONCUR_UPDATABLE, mockStmt.getResultSetConcurrency());
Assert.assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, mockStmt.getResultSetHoldability());
Assert.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, stmt.getResultSetType());
Assert.assertEquals(ResultSet.CONCUR_UPDATABLE, stmt.getResultSetConcurrency());
Assert.assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, stmt.getResultSetHoldability());
stmt.close();
conn.close();
}
public void test_createStatement_4() throws Exception {
Connection conn = dataSourceHA.getConnection();
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE,
ResultSet.CLOSE_CURSORS_AT_COMMIT);
stmt.executeUpdate("SET @user = 'xxx'");
MockStatement mockStmt = stmt.unwrap(MockStatement.class);
Assert.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, mockStmt.getResultSetType());
Assert.assertEquals(ResultSet.CONCUR_UPDATABLE, mockStmt.getResultSetConcurrency());
Assert.assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, mockStmt.getResultSetHoldability());
Assert.assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, stmt.getResultSetType());
Assert.assertEquals(ResultSet.CONCUR_UPDATABLE, stmt.getResultSetConcurrency());
Assert.assertEquals(ResultSet.CLOSE_CURSORS_AT_COMMIT, stmt.getResultSetHoldability());
stmt.close();
conn.close();
}
public void test_close() throws Exception {
Connection conn = dataSourceHA.getConnection();
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE,
ResultSet.CLOSE_CURSORS_AT_COMMIT);
stmt.getMaxFieldSize();
stmt.getMaxRows();
stmt.cancel();
stmt.getWarnings();
stmt.clearWarnings();
stmt.getResultSet();
stmt.getUpdateCount();
stmt.getMoreResults();
stmt.getMoreResults(1);
stmt.getResultSetConcurrency();
stmt.getResultSetHoldability();
stmt.getResultSetType();
stmt.getConnection();
stmt.getGeneratedKeys();
stmt.isClosed();
stmt.close();
conn.close();
}
}
@@ -1,98 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.pool.ha;
import java.sql.Connection;
import java.sql.Statement;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.mock.MockConnection;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.ha.HADataSource;
import com.alibaba.druid.stat.DruidDataSourceStatManager;
import com.alibaba.druid.util.JdbcUtils;
public class HADataSourceTest2 extends TestCase {
private DruidDataSource dataSourceA;
private DruidDataSource dataSourceB;
private HADataSource dataSourceHA;
private String MASTER_URL = "jdbc:mock:master";
private String SLAVE_URL = "jdbc:mock:slave";
protected void setUp() throws Exception {
for (DruidDataSource item : DruidDataSourceStatManager.getDruidDataSourceInstances()) {
JdbcUtils.close(item);
}
Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
dataSourceA = new DruidDataSource();
dataSourceA.setUrl(MASTER_URL);
dataSourceA.setFilters("trace");
dataSourceB = new DruidDataSource();
dataSourceB.setUrl(SLAVE_URL);
dataSourceB.setFilters("stat");
dataSourceHA = new HADataSource();
dataSourceHA.setMaster(dataSourceA);
dataSourceHA.setSlave(dataSourceB);
}
protected void tearDown() throws Exception {
dataSourceHA.close();
for (DruidDataSource dataSource : DruidDataSourceStatManager.getDruidDataSourceInstances()) {
dataSource.close();
}
Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
}
public void test_switch() throws Exception {
{
Connection conn = dataSourceHA.getConnection();
Statement stmt = conn.createStatement();
stmt.execute("select 1");
MockConnection mockConn = conn.unwrap(MockConnection.class);
Assert.assertNotNull(mockConn);
// Assert.assertEquals(dataSourceA.getUrl(), mockConn.getUrl());
conn.close();
}
dataSourceHA.setMasterEnable(false);
{
Connection conn = dataSourceHA.getConnection();
Statement stmt = conn.createStatement();
stmt.execute("select 1");
MockConnection mockConn = conn.unwrap(MockConnection.class);
Assert.assertNotNull(mockConn);
// Assert.assertEquals(dataSourceB.getUrl(), mockConn.getUrl());
conn.close();
}
}
}
@@ -1,29 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.pool.ha;
import junit.framework.TestCase;
import com.alibaba.druid.pool.ha.MultiDataSource;
import com.alibaba.druid.util.JdbcUtils;
public class MultiDataSourceTest extends TestCase {
public void test_0 () throws Exception {
MultiDataSource multiDataSource = new MultiDataSource();
multiDataSource.close();
}
}
@@ -1,69 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.pool.ha;
import java.sql.Connection;
import java.sql.Statement;
import java.util.concurrent.CountDownLatch;
import junit.framework.TestCase;
import com.alibaba.druid.pool.ha.cobar.CobarDataSource;
public class CobarDataSourceTest extends TestCase {
public void test_cobar() throws Exception {
String url = "jdbc:cobar://10.20.165.79:8080/druid";
final CobarDataSource dataSource = new CobarDataSource();
dataSource.setUrl(url);
dataSource.setUsername("test");
dataSource.setPassword("");
final int COUNT = 10;
final CountDownLatch latch = new CountDownLatch(COUNT);
Runnable task = new Runnable() {
public void run() {
try {
for (int i = 0; i < 1000 * 1000; ++i) {
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
stmt.execute("select 1");
stmt.close();
conn.close();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
latch.countDown();
}
}
};
for (int i = 0; i < 10; ++i) {
Thread thread = new Thread(task);
thread.setName("thread-" + i);
thread.start();
}
latch.await();
}
}