use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat-Server by MyCATApache.
the class SwitchDataSource method response.
public static void response(String stmt, ManagerConnection c) {
int count = 0;
Pair<String[], Integer> pair = ManagerParseSwitch.getPair(stmt);
Map<String, PhysicalDBPool> dns = MycatServer.getInstance().getConfig().getDataHosts();
Integer idx = pair.getValue();
for (String key : pair.getKey()) {
PhysicalDBPool dn = dns.get(key);
if (dn != null) {
int m = dn.getActivedIndex();
int n = (idx == null) ? dn.next(m) : idx.intValue();
if (dn.switchSource(n, false, "MANAGER")) {
++count;
}
}
}
OkPacket packet = new OkPacket();
packet.packetId = 1;
packet.affectedRows = count;
packet.serverStatus = 2;
packet.write(c);
}
use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat-Server by MyCATApache.
the class MySQLHeartbeat method switchSourceIfNeed.
/**
* switch data source
*/
private void switchSourceIfNeed(String reason) {
int switchType = source.getHostConfig().getSwitchType();
if (switchType == DataHostConfig.NOT_SWITCH_DS) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("not switch datasource ,for switchType is " + DataHostConfig.NOT_SWITCH_DS);
return;
}
return;
}
PhysicalDBPool pool = this.source.getDbPool();
int curDatasourceHB = pool.getSource().getHeartbeat().getStatus();
// read node can't switch ,only write node can switch
if (pool.getWriteType() == PhysicalDBPool.WRITE_ONLYONE_NODE && !source.isReadNode() && curDatasourceHB != DBHeartbeat.OK_STATUS && pool.getSources().length > 1) {
synchronized (pool) {
// try to see if need switch datasource
curDatasourceHB = pool.getSource().getHeartbeat().getStatus();
if (curDatasourceHB != DBHeartbeat.INIT_STATUS && curDatasourceHB != DBHeartbeat.OK_STATUS) {
int curIndex = pool.getActivedIndex();
int nextId = pool.next(curIndex);
PhysicalDatasource[] allWriteNodes = pool.getSources();
while (true) {
if (nextId == curIndex) {
break;
}
PhysicalDatasource theSource = allWriteNodes[nextId];
DBHeartbeat theSourceHB = theSource.getHeartbeat();
int theSourceHBStatus = theSourceHB.getStatus();
if (theSourceHBStatus == DBHeartbeat.OK_STATUS) {
if (switchType == DataHostConfig.SYN_STATUS_SWITCH_DS) {
if (Integer.valueOf(0).equals(theSourceHB.getSlaveBehindMaster())) {
LOGGER.info("try to switch datasource ,slave is synchronized to master " + theSource.getConfig());
pool.switchSource(nextId, true, reason);
break;
} else {
LOGGER.warn("ignored datasource ,slave is not synchronized to master , slave behind master :" + theSourceHB.getSlaveBehindMaster() + " " + theSource.getConfig());
}
} else {
// normal switch
LOGGER.info("try to switch datasource ,not checked slave synchronize status " + theSource.getConfig());
pool.switchSource(nextId, true, reason);
break;
}
}
nextId = pool.next(nextId);
}
}
}
}
}
use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat-Server by MyCATApache.
the class ConfigTest method testTempReadHostAvailable.
/**
* 测试 临时读可用 配置
*/
@Test
public void testTempReadHostAvailable() {
PhysicalDBPool pool = this.dataHosts.get("localhost2");
DataHostConfig hostConfig = pool.getSource().getHostConfig();
Assert.assertTrue(hostConfig.isTempReadHostAvailable() == true);
}
use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat-Server by MyCATApache.
the class ConfigTest method testReadHostWeight.
/**
* 测试 读服务的 权重
*
* @throws Exception
*/
@Test
public void testReadHostWeight() throws Exception {
ArrayList<PhysicalDatasource> okSources = new ArrayList<PhysicalDatasource>();
PhysicalDBPool pool = this.dataHosts.get("localhost2");
okSources.addAll(pool.getAllDataSources());
PhysicalDatasource source = pool.randomSelect(okSources);
Assert.assertTrue(source != null);
}
use of io.mycat.backend.datasource.PhysicalDBPool in project Mycat-Server by MyCATApache.
the class MigrateUtils method execulteCount.
public static long execulteCount(String sql, String toDn) throws SQLException, IOException {
PhysicalDBNode dbNode = MycatServer.getInstance().getConfig().getDataNodes().get(toDn);
PhysicalDBPool dbPool = dbNode.getDbPool();
PhysicalDatasource datasource = dbPool.getSources()[dbPool.getActivedIndex()];
DBHostConfig config = datasource.getConfig();
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:mysql://" + config.getUrl() + "/" + dbNode.getDatabase(), config.getUser(), config.getPassword());
List<Map<String, Object>> result = JdbcUtils.executeQuery(con, sql, new ArrayList<>());
if (result.size() == 1) {
return (long) result.get(0).get("count");
}
} finally {
JdbcUtils.close(con);
}
return 0;
}
Aggregations