use of io.mycat.backend.datasource.PhysicalDBNode in project Mycat-Server by MyCATApache.
the class MySQLTableStructureDetector method run.
@Override
public void run() {
for (SchemaConfig schema : MycatServer.getInstance().getConfig().getSchemas().values()) {
for (TableConfig table : schema.getTables().values()) {
for (String dataNode : table.getDataNodes()) {
try {
table.getReentrantReadWriteLock().writeLock().lock();
ConcurrentHashMap<String, List<String>> map = new ConcurrentHashMap<>();
table.setDataNodeTableStructureSQLMap(map);
} finally {
table.getReentrantReadWriteLock().writeLock().unlock();
}
OneRawSQLQueryResultHandler resultHandler = new OneRawSQLQueryResultHandler(MYSQL_SHOW_CREATE_TABLE_COLMS, new MySQLTableStructureListener(dataNode, table));
resultHandler.setMark("Table Structure");
PhysicalDBNode dn = MycatServer.getInstance().getConfig().getDataNodes().get(dataNode);
SQLJob sqlJob = new SQLJob(sqlPrefix + table.getName(), dn.getDatabase(), resultHandler, dn.getDbPool().getSource());
sqlJob.run();
}
}
}
}
use of io.mycat.backend.datasource.PhysicalDBNode in project Mycat-Server by MyCATApache.
the class ShowDataSource method execute.
public static void execute(ManagerConnection c, String name) {
ByteBuffer buffer = c.allocate();
// write header
buffer = header.write(buffer, c, true);
// write fields
for (FieldPacket field : fields) {
buffer = field.write(buffer, c, true);
}
// write eof
buffer = eof.write(buffer, c, true);
// write rows
byte packetId = eof.packetId;
MycatConfig conf = MycatServer.getInstance().getConfig();
Map<String, List<PhysicalDatasource>> dataSources = new HashMap<String, List<PhysicalDatasource>>();
if (null != name) {
PhysicalDBNode dn = conf.getDataNodes().get(name);
if (dn != null) {
List<PhysicalDatasource> dslst = new LinkedList<PhysicalDatasource>();
dslst.addAll(dn.getDbPool().getAllDataSources());
dataSources.put(dn.getName(), dslst);
}
} else {
for (PhysicalDBNode dn : conf.getDataNodes().values()) {
List<PhysicalDatasource> dslst = new LinkedList<PhysicalDatasource>();
dslst.addAll(dn.getDbPool().getAllDataSources());
dataSources.put(dn.getName(), dslst);
}
}
for (Map.Entry<String, List<PhysicalDatasource>> dsEntry : dataSources.entrySet()) {
String dnName = dsEntry.getKey();
for (PhysicalDatasource ds : dsEntry.getValue()) {
RowDataPacket row = getRow(dnName, ds, c.getCharset());
row.packetId = ++packetId;
buffer = row.write(buffer, c, true);
}
}
// write last eof
EOFPacket lastEof = new EOFPacket();
lastEof.packetId = ++packetId;
buffer = lastEof.write(buffer, c, true);
// post write
c.write(buffer);
}
use of io.mycat.backend.datasource.PhysicalDBNode in project Mycat-Server by MyCATApache.
the class RollbackConfig method rollback.
private static boolean rollback() {
MycatConfig conf = MycatServer.getInstance().getConfig();
Map<String, UserConfig> users = conf.getBackupUsers();
Map<String, SchemaConfig> schemas = conf.getBackupSchemas();
Map<String, PhysicalDBNode> dataNodes = conf.getBackupDataNodes();
Map<String, PhysicalDBPool> dataHosts = conf.getBackupDataHosts();
MycatCluster cluster = conf.getBackupCluster();
FirewallConfig firewall = conf.getBackupFirewall();
// 检查可回滚状态
if (!conf.canRollback()) {
return false;
}
// 如果回滚已经存在的pool
boolean rollbackStatus = true;
Map<String, PhysicalDBPool> cNodes = conf.getDataHosts();
for (PhysicalDBPool dn : dataHosts.values()) {
dn.init(dn.getActivedIndex());
if (!dn.isInitSuccess()) {
rollbackStatus = false;
break;
}
}
// 如果回滚不成功,则清理已初始化的资源。
if (!rollbackStatus) {
for (PhysicalDBPool dn : dataHosts.values()) {
dn.clearDataSources("rollbackup config");
dn.stopHeartbeat();
}
return false;
}
// 应用回滚
conf.rollback(users, schemas, dataNodes, dataHosts, cluster, firewall);
// 处理旧的资源
for (PhysicalDBPool dn : cNodes.values()) {
dn.clearDataSources("clear old config ");
dn.stopHeartbeat();
}
//清理缓存
MycatServer.getInstance().getCacheService().clearCache();
return true;
}
use of io.mycat.backend.datasource.PhysicalDBNode in project Mycat-Server by MyCATApache.
the class SequenceVal method execute.
public void execute(SequenceVal seqVal) {
MycatConfig conf = MycatServer.getInstance().getConfig();
PhysicalDBNode mysqlDN = conf.getDataNodes().get(seqVal.dataNode);
try {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("execute in datanode " + seqVal.dataNode + " for fetch sequnce sql " + seqVal.sql);
}
// 修正获取seq的逻辑,在读写分离的情况下只能走写节点。修改Select模式为Update模式。
mysqlDN.getConnection(mysqlDN.getDatabase(), true, new RouteResultsetNode(seqVal.dataNode, ServerParse.UPDATE, seqVal.sql), this, seqVal);
} catch (Exception e) {
LOGGER.warn("get connection err " + e);
}
}
use of io.mycat.backend.datasource.PhysicalDBNode in project Mycat-Server by MyCATApache.
the class NonBlockingSession method kill.
// public boolean tryExistsCon(final BackendConnection conn,
// RouteResultsetNode node) {
//
// if (conn == null) {
// return false;
// }
// if (!conn.isFromSlaveDB()
// || node.canRunnINReadDB(getSource().isAutocommit())) {
// if (LOGGER.isDebugEnabled()) {
// LOGGER.debug("found connections in session to use " + conn
// + " for " + node);
// }
// conn.setAttachment(node);
// return true;
// } else {
// // slavedb connection and can't use anymore ,release it
// if (LOGGER.isDebugEnabled()) {
// LOGGER.debug("release slave connection,can't be used in trasaction "
// + conn + " for " + node);
// }
// releaseConnection(node, LOGGER.isDebugEnabled(), false);
// }
// return false;
// }
protected void kill() {
boolean hooked = false;
AtomicInteger count = null;
Map<RouteResultsetNode, BackendConnection> killees = null;
for (RouteResultsetNode node : target.keySet()) {
BackendConnection c = target.get(node);
if (c != null) {
if (!hooked) {
hooked = true;
killees = new HashMap<RouteResultsetNode, BackendConnection>();
count = new AtomicInteger(0);
}
killees.put(node, c);
count.incrementAndGet();
}
}
if (hooked) {
for (Entry<RouteResultsetNode, BackendConnection> en : killees.entrySet()) {
KillConnectionHandler kill = new KillConnectionHandler(en.getValue(), this);
MycatConfig conf = MycatServer.getInstance().getConfig();
PhysicalDBNode dn = conf.getDataNodes().get(en.getKey().getName());
try {
dn.getConnectionFromSameSource(null, true, en.getValue(), kill, en.getKey());
} catch (Exception e) {
LOGGER.error("get killer connection failed for " + en.getKey(), e);
kill.connectionError(e, null);
}
}
}
}
Aggregations