use of io.mycat.backend.datasource.PhysicalDatasource in project Mycat_plus by coderczp.
the class ShowDatasourceSynDetail method getRows.
private static List<RowDataPacket> getRows(String name, String charset) {
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();
DataSourceSyncRecorder record = hb.getAsynRecorder();
Map<String, String> states = record.getRecords();
if (name.equals(ds.getName())) {
List<Record> data = record.getAsynRecords();
for (Record r : data) {
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(StringUtil.encode(ds.getName(), charset));
row.add(StringUtil.encode(ds.getConfig().getIp(), charset));
row.add(LongUtil.toBytes(ds.getConfig().getPort()));
row.add(StringUtil.encode(states.get("Master_Host"), charset));
row.add(LongUtil.toBytes(Long.valueOf(states.get("Master_Port"))));
row.add(StringUtil.encode(states.get("Master_Use"), charset));
// DateFormat非线程安全
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(new Date(r.getTime()));
row.add(StringUtil.encode(time, charset));
row.add(LongUtil.toBytes((Long) r.getValue()));
list.add(row);
}
break;
}
}
}
return list;
}
use of io.mycat.backend.datasource.PhysicalDatasource in project Mycat_plus by coderczp.
the class ShowHeartbeatDetail method getRows.
private static List<RowDataPacket> getRows(String name, String charset) {
List<RowDataPacket> list = new LinkedList<RowDataPacket>();
MycatConfig conf = MycatServer.getInstance().getConfig();
// host nodes
String type = "";
String ip = "";
int port = 0;
DBHeartbeat hb = null;
Map<String, PhysicalDBPool> dataHosts = conf.getDataHosts();
for (PhysicalDBPool pool : dataHosts.values()) {
for (PhysicalDatasource ds : pool.getAllDataSources()) {
if (name.equals(ds.getName())) {
hb = ds.getHeartbeat();
type = ds.getConfig().getDbType();
ip = ds.getConfig().getIp();
port = ds.getConfig().getPort();
break;
}
}
}
if (hb != null) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Queue<HeartbeatRecorder.Record> heatbeartRecorders = hb.getRecorder().getRecordsAll();
for (HeartbeatRecorder.Record record : heatbeartRecorders) {
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
row.add(StringUtil.encode(name, charset));
row.add(StringUtil.encode(type, charset));
row.add(StringUtil.encode(ip, charset));
row.add(IntegerUtil.toBytes(port));
long time = record.getTime();
String timeStr = sdf.format(new Date(time));
row.add(StringUtil.encode(timeStr, charset));
row.add(LongUtil.toBytes(record.getValue()));
list.add(row);
}
} else {
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
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 ConfigInitializer method getPhysicalDBPool.
private PhysicalDBPool getPhysicalDBPool(DataHostConfig conf, ConfigLoader configLoader) {
String name = conf.getName();
// 数据库类型,我们这里只讨论MySQL
String dbType = conf.getDbType();
// 连接数据库驱动,我们这里只讨论MyCat自己实现的native
String dbDriver = conf.getDbDriver();
// 针对所有写节点创建PhysicalDatasource
PhysicalDatasource[] writeSources = createDataSource(conf, name, dbType, dbDriver, conf.getWriteHosts(), false);
Map<Integer, DBHostConfig[]> readHostsMap = conf.getReadHosts();
Map<Integer, PhysicalDatasource[]> readSourcesMap = new HashMap<Integer, PhysicalDatasource[]>(readHostsMap.size());
// 对于每个读节点建立key为writeHost下标value为readHost的PhysicalDatasource[]的哈希表
for (Map.Entry<Integer, DBHostConfig[]> entry : readHostsMap.entrySet()) {
PhysicalDatasource[] readSources = createDataSource(conf, name, dbType, dbDriver, entry.getValue(), true);
readSourcesMap.put(entry.getKey(), readSources);
}
PhysicalDBPool pool = new PhysicalDBPool(conf.getName(), conf, writeSources, readSourcesMap, conf.getBalance(), conf.getWriteType());
pool.setSlaveIDs(conf.getSlaveIDs());
return pool;
}
use of io.mycat.backend.datasource.PhysicalDatasource in project Mycat-Server by MyCATApache.
the class ReloadConfig method reload_all.
public static boolean reload_all() {
/**
* 1、载入新的配置
* 1.1、ConfigInitializer 初始化,基本自检
* 1.2、DataNode/DataHost 实际链路检测
*/
ConfigInitializer loader = new ConfigInitializer(true);
Map<String, UserConfig> newUsers = loader.getUsers();
Map<String, SchemaConfig> newSchemas = loader.getSchemas();
Map<String, PhysicalDBNode> newDataNodes = loader.getDataNodes();
Map<String, PhysicalDBPool> newDataHosts = loader.getDataHosts();
MycatCluster newCluster = loader.getCluster();
FirewallConfig newFirewall = loader.getFirewall();
/**
* 1.2、实际链路检测
*/
loader.testConnection();
/**
* 2、承接
* 2.1、老的 dataSource 继续承接新建请求
* 2.2、新的 dataSource 开始初始化, 完毕后交由 2.3
* 2.3、新的 dataSource 开始承接新建请求
* 2.4、老的 dataSource 内部的事务执行完毕, 相继关闭
* 2.5、老的 dataSource 超过阀值的,强制关闭
*/
MycatConfig config = MycatServer.getInstance().getConfig();
/**
* 2.1 、老的 dataSource 继续承接新建请求, 此处什么也不需要做
*/
boolean isReloadStatusOK = true;
/**
* 2.2、新的 dataHosts 初始化
*/
for (PhysicalDBPool dbPool : newDataHosts.values()) {
String hostName = dbPool.getHostName();
// 设置 schemas
ArrayList<String> dnSchemas = new ArrayList<String>(30);
for (PhysicalDBNode dn : newDataNodes.values()) {
if (dn.getDbPool().getHostName().equals(hostName)) {
dnSchemas.add(dn.getDatabase());
}
}
dbPool.setSchemas(dnSchemas.toArray(new String[dnSchemas.size()]));
// 获取 data host
String dnIndex = DnPropertyUtil.loadDnIndexProps().getProperty(dbPool.getHostName(), "0");
if (!"0".equals(dnIndex)) {
LOGGER.info("init datahost: " + dbPool.getHostName() + " to use datasource index:" + dnIndex);
}
dbPool.init(Integer.valueOf(dnIndex));
if (!dbPool.isInitSuccess()) {
isReloadStatusOK = false;
break;
}
}
/**
* TODO: 确认初始化情况
*
* 新的 dataHosts 是否初始化成功
*/
if (isReloadStatusOK) {
/**
* 2.3、 在老的配置上,应用新的配置,开始准备承接任务
*/
config.reload(newUsers, newSchemas, newDataNodes, newDataHosts, newCluster, newFirewall, true);
/**
* 2.4、 处理旧的资源
*/
LOGGER.warn("1、clear old backend connection(size): " + NIOProcessor.backends_old.size());
// 清除前一次 reload 转移出去的 old Cons
Iterator<BackendConnection> iter = NIOProcessor.backends_old.iterator();
while (iter.hasNext()) {
BackendConnection con = iter.next();
con.close("clear old datasources");
iter.remove();
}
Map<String, PhysicalDBPool> oldDataHosts = config.getBackupDataHosts();
for (PhysicalDBPool dbPool : oldDataHosts.values()) {
dbPool.stopHeartbeat();
// 提取数据源下的所有连接
for (PhysicalDatasource ds : dbPool.getAllDataSources()) {
//
for (NIOProcessor processor : MycatServer.getInstance().getProcessors()) {
for (BackendConnection con : processor.getBackends().values()) {
if (con instanceof MySQLConnection) {
MySQLConnection mysqlCon = (MySQLConnection) con;
if (mysqlCon.getPool() == ds) {
NIOProcessor.backends_old.add(con);
}
} else if (con instanceof JDBCConnection) {
JDBCConnection jdbcCon = (JDBCConnection) con;
if (jdbcCon.getPool() == ds) {
NIOProcessor.backends_old.add(con);
}
}
}
}
}
}
LOGGER.warn("2、to be recycled old backend connection(size): " + NIOProcessor.backends_old.size());
// 清理缓存
MycatServer.getInstance().getCacheService().clearCache();
MycatServer.getInstance().initRuleData();
return true;
} else {
// 如果重载不成功,则清理已初始化的资源。
LOGGER.warn("reload failed, clear previously created datasources ");
for (PhysicalDBPool dbPool : newDataHosts.values()) {
dbPool.clearDataSources("reload config");
dbPool.stopHeartbeat();
}
return false;
}
}
use of io.mycat.backend.datasource.PhysicalDatasource in project Mycat-Server by MyCATApache.
the class ShowDatasourceCluster method getRows.
private static List<RowDataPacket> getRows(String charset) {
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();
DataSourceSyncRecorder record = hb.getAsynRecorder();
Map<String, String> states = record.getRecords();
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
if (!states.isEmpty()) {
row.add(StringUtil.encode(ds.getName(), charset));
row.add(StringUtil.encode(ds.getConfig().getIp(), charset));
row.add(LongUtil.toBytes(ds.getConfig().getPort()));
row.add(StringUtil.encode(states.get("wsrep_incoming_addresses") == null ? "" : states.get("wsrep_incoming_addresses"), charset));
row.add(StringUtil.encode(states.get("wsrep_cluster_size") == null ? "" : states.get("wsrep_cluster_size"), charset));
row.add(StringUtil.encode(states.get("wsrep_cluster_status") == null ? "" : states.get("wsrep_cluster_status"), charset));
row.add(StringUtil.encode(states.get("wsrep_connected") == null ? "" : states.get("wsrep_connected"), charset));
row.add(StringUtil.encode(states.get("wsrep_flow_control_paused") == null ? "" : states.get("wsrep_flow_control_paused"), charset));
row.add(StringUtil.encode(states.get("wsrep_local_state_comment") == null ? "" : states.get("wsrep_local_state_comment"), charset));
row.add(StringUtil.encode(states.get("wsrep_ready") == null ? "" : states.get("wsrep_ready"), charset));
row.add(StringUtil.encode(states.get("wsrep_flow_control_paused_ns") == null ? "" : states.get("wsrep_flow_control_paused_ns"), charset));
row.add(StringUtil.encode(states.get("wsrep_flow_control_recv") == null ? "" : states.get("wsrep_flow_control_recv"), charset));
row.add(StringUtil.encode(states.get("wsrep_local_bf_aborts") == null ? "" : states.get("wsrep_local_bf_aborts"), charset));
row.add(StringUtil.encode(states.get("wsrep_local_recv_queue_avg") == null ? "" : states.get("wsrep_local_recv_queue_avg"), charset));
row.add(StringUtil.encode(states.get("wsrep_local_send_queue_avg") == null ? "" : states.get("wsrep_local_recv_queue_avg"), charset));
row.add(StringUtil.encode(states.get("wsrep_apply_oool") == null ? "" : states.get("wsrep_apply_oool"), charset));
row.add(StringUtil.encode(states.get("wsrep_apply_oooe") == null ? "" : states.get("wsrep_apply_oooe"), charset));
list.add(row);
}
}
}
return list;
}
Aggregations