use of io.mycat.backend.datasource.PhysicalDBPool 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.PhysicalDBPool in project Mycat_plus by coderczp.
the class StopHeartbeat method execute.
public static void execute(String stmt, ManagerConnection c) {
int count = 0;
Pair<String[], Integer> keys = ManagerParseStop.getPair(stmt);
if (keys.getKey() != null && keys.getValue() != null) {
long time = keys.getValue().intValue() * 1000L;
Map<String, PhysicalDBPool> dns = MycatServer.getInstance().getConfig().getDataHosts();
for (String key : keys.getKey()) {
PhysicalDBPool dn = dns.get(key);
if (dn != null) {
dn.getSource().setHeartbeatRecoveryTime(TimeUtil.currentTimeMillis() + time);
++count;
StringBuilder s = new StringBuilder();
s.append(dn.getHostName()).append(" stop heartbeat '");
logger.warn(s.append(FormatUtil.formatTime(time, 3)).append("' by manager.").toString());
}
}
}
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 MycatConfig method apply.
private void apply(Map<String, UserConfig> newUsers, Map<String, SchemaConfig> newSchemas, Map<String, PhysicalDBNode> newDataNodes, Map<String, PhysicalDBPool> newDataHosts, MycatCluster newCluster, FirewallConfig newFirewall, boolean isLoadAll) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
// --------------------------------------------
if (isLoadAll) {
Map<String, PhysicalDBPool> oldDataHosts = this.dataHosts;
if (oldDataHosts != null) {
for (PhysicalDBPool oldDbPool : oldDataHosts.values()) {
if (oldDbPool != null) {
oldDbPool.stopHeartbeat();
}
}
}
this._dataNodes = this.dataNodes;
this._dataHosts = this.dataHosts;
}
this._users = this.users;
this._schemas = this.schemas;
this._cluster = this.cluster;
this._firewall = this.firewall;
// ---------------------------------------------------
if (isLoadAll) {
if (newDataHosts != null) {
for (PhysicalDBPool newDbPool : newDataHosts.values()) {
if (newDbPool != null) {
newDbPool.startHeartbeat();
}
}
}
this.dataNodes = newDataNodes;
this.dataHosts = newDataHosts;
}
this.users = newUsers;
this.schemas = newSchemas;
this.cluster = newCluster;
this.firewall = newFirewall;
} finally {
lock.unlock();
}
}
use of io.mycat.backend.datasource.PhysicalDBPool 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.PhysicalDBPool in project Mycat-Server by MyCATApache.
the class ConfigInitializer method initDataNodes.
private Map<String, PhysicalDBNode> initDataNodes(ConfigLoader configLoader) {
Map<String, DataNodeConfig> nodeConfs = configLoader.getDataNodes();
Map<String, PhysicalDBNode> nodes = new HashMap<String, PhysicalDBNode>(nodeConfs.size());
for (DataNodeConfig conf : nodeConfs.values()) {
PhysicalDBPool pool = this.dataHosts.get(conf.getDataHost());
if (pool == null) {
throw new ConfigException("dataHost not exists " + conf.getDataHost());
}
PhysicalDBNode dataNode = new PhysicalDBNode(conf.getName(), conf.getDatabase(), pool);
nodes.put(dataNode.getName(), dataNode);
}
return nodes;
}
Aggregations