use of io.mycat.backend.datasource.PhysicalDatasource in project Mycat-Server by MyCATApache.
the class ShowHeartbeat method getRows.
private static List<RowDataPacket> getRows() {
List<RowDataPacket> list = new LinkedList<RowDataPacket>();
MycatConfig conf = MycatServer.getInstance().getConfig();
// host nodes
Map<String, PhysicalDBPool> dataHosts = conf.getDataHosts();
for (PhysicalDBPool pool : dataHosts.values()) {
for (PhysicalDatasource ds : pool.getAllDataSources()) {
DBHeartbeat hb = ds.getHeartbeat();
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(ds.getName().getBytes());
row.add(ds.getConfig().getDbType().getBytes());
if (hb != null) {
row.add(ds.getConfig().getIp().getBytes());
row.add(IntegerUtil.toBytes(ds.getConfig().getPort()));
row.add(IntegerUtil.toBytes(hb.getStatus()));
row.add(IntegerUtil.toBytes(hb.getErrorCount()));
row.add(hb.isChecking() ? "checking".getBytes() : "idle".getBytes());
row.add(LongUtil.toBytes(hb.getTimeout()));
row.add(hb.getRecorder().get().getBytes());
String lat = hb.getLastActiveTime();
row.add(lat == null ? null : lat.getBytes());
row.add(hb.isStop() ? "true".getBytes() : "false".getBytes());
} else {
row.add(null);
row.add(null);
row.add(null);
row.add(null);
row.add(null);
row.add(null);
row.add(null);
row.add(null);
row.add(null);
}
list.add(row);
}
}
return list;
}
use of io.mycat.backend.datasource.PhysicalDatasource 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.PhysicalDatasource 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.PhysicalDatasource 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;
}
use of io.mycat.backend.datasource.PhysicalDatasource in project Mycat-Server by MyCATApache.
the class MigrateUtils method execulteSql.
public static void execulteSql(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());
JdbcUtils.execute(con, sql, new ArrayList<>());
} finally {
JdbcUtils.close(con);
}
}
Aggregations