use of io.mycat.backend.datasource.PhysicalDBNode in project Mycat-Server by MyCATApache.
the class ConfigInitializer method testConnection.
public void testConnection() {
// 实际链路的连接测试
if (this.dataNodes != null && this.dataHosts != null) {
Map<String, Boolean> map = new HashMap<String, Boolean>();
for (PhysicalDBNode dataNode : dataNodes.values()) {
String database = dataNode.getDatabase();
PhysicalDBPool pool = dataNode.getDbPool();
for (PhysicalDatasource ds : pool.getAllDataSources()) {
String key = ds.getName() + "_" + database;
if (map.get(key) == null) {
map.put(key, false);
boolean isConnected = false;
try {
isConnected = ds.testConnection(database);
map.put(key, isConnected);
} catch (IOException e) {
LOGGER.warn("test conn error:", e);
}
}
}
}
//
boolean isConnectivity = true;
for (Map.Entry<String, Boolean> entry : map.entrySet()) {
String key = entry.getKey();
Boolean value = entry.getValue();
if (!value && isConnectivity) {
LOGGER.warn("SelfCheck### test " + key + " database connection failed ");
isConnectivity = false;
} else {
LOGGER.info("SelfCheck### test " + key + " database connection success ");
}
}
if (!isConnectivity) {
throw new ConfigException("SelfCheck### there are some datasource connection failed, pls check!");
}
}
}
use of io.mycat.backend.datasource.PhysicalDBNode in project Mycat-Server by MyCATApache.
the class MultiNodeQueryHandler method execute.
public void execute() throws Exception {
final ReentrantLock lock = this.lock;
lock.lock();
try {
this.reset(rrs.getNodes().length);
this.fieldsReturned = false;
this.affectedRows = 0L;
this.insertId = 0L;
} finally {
lock.unlock();
}
MycatConfig conf = MycatServer.getInstance().getConfig();
startTime = System.currentTimeMillis();
LOGGER.debug("rrs.getRunOnSlave()-" + rrs.getRunOnSlave());
for (final RouteResultsetNode node : rrs.getNodes()) {
BackendConnection conn = session.getTarget(node);
if (session.tryExistsCon(conn, node)) {
LOGGER.debug("node.getRunOnSlave()-" + node.getRunOnSlave());
// 实现 master/slave注解
node.setRunOnSlave(rrs.getRunOnSlave());
LOGGER.debug("node.getRunOnSlave()-" + node.getRunOnSlave());
_execute(conn, node);
} else {
// create new connection
LOGGER.debug("node.getRunOnSlave()1-" + node.getRunOnSlave());
// 实现 master/slave注解
node.setRunOnSlave(rrs.getRunOnSlave());
LOGGER.debug("node.getRunOnSlave()2-" + node.getRunOnSlave());
PhysicalDBNode dn = conf.getDataNodes().get(node.getName());
dn.getConnection(dn.getDatabase(), autocommit, node, this, node);
// 注意该方法不仅仅是获取连接,获取新连接成功之后,会通过层层回调,最后回调到本类 的connectionAcquired
// 这是通过 上面方法的 this 参数的层层传递完成的。
// connectionAcquired 进行执行操作:
// session.bindConnection(node, conn);
// _execute(conn, node);
}
}
}
use of io.mycat.backend.datasource.PhysicalDBNode in project Mycat-Server by MyCATApache.
the class SingleNodeHandler method execute.
public void execute() throws Exception {
startTime = System.currentTimeMillis();
ServerConnection sc = session.getSource();
this.isRunning = true;
this.packetId = 0;
final BackendConnection conn = session.getTarget(node);
LOGGER.debug("rrs.getRunOnSlave() " + rrs.getRunOnSlave());
// 实现 master/slave注解
node.setRunOnSlave(rrs.getRunOnSlave());
LOGGER.debug("node.getRunOnSlave() " + node.getRunOnSlave());
if (session.tryExistsCon(conn, node)) {
_execute(conn);
} else {
// create new connection
MycatConfig conf = MycatServer.getInstance().getConfig();
LOGGER.debug("node.getRunOnSlave() " + node.getRunOnSlave());
// 实现 master/slave注解
node.setRunOnSlave(rrs.getRunOnSlave());
LOGGER.debug("node.getRunOnSlave() " + node.getRunOnSlave());
PhysicalDBNode dn = conf.getDataNodes().get(node.getName());
dn.getConnection(dn.getDatabase(), sc.isAutocommit(), node, this, node);
}
}
use of io.mycat.backend.datasource.PhysicalDBNode in project Mycat-Server by MyCATApache.
the class FetchStoreNodeOfChildTableHandler method execute.
public String execute(String schema, String sql, List<String> dataNodes, ServerConnection sc) {
String key = schema + ":" + sql;
CachePool cache = MycatServer.getInstance().getCacheService().getCachePool("ER_SQL2PARENTID");
String result = (String) cache.get(key);
if (result != null) {
return result;
}
this.sql = sql;
int totalCount = dataNodes.size();
long startTime = System.currentTimeMillis();
long endTime = startTime + 5 * 60 * 1000L;
MycatConfig conf = MycatServer.getInstance().getConfig();
LOGGER.debug("find child node with sql:" + sql);
for (String dn : dataNodes) {
if (dataNode != null) {
return dataNode;
}
PhysicalDBNode mysqlDN = conf.getDataNodes().get(dn);
try {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("execute in datanode " + dn);
}
RouteResultsetNode node = new RouteResultsetNode(dn, ServerParse.SELECT, sql);
// 获取 子表节点,最好走master为好
node.setRunOnSlave(false);
/*
* fix #1370 默认应该先从已经持有的连接中取连接, 否则可能因为事务隔离性看不到当前事务内更新的数据
* Tips: 通过mysqlDN.getConnection获取到的连接不是当前连接
*
*/
BackendConnection conn = sc.getSession2().getTarget(node);
if (sc.getSession2().tryExistsCon(conn, node)) {
_execute(conn, node, sc);
} else {
mysqlDN.getConnection(mysqlDN.getDatabase(), sc.isAutocommit(), node, this, node);
}
} catch (Exception e) {
LOGGER.warn("get connection err " + e);
}
}
while (dataNode == null && System.currentTimeMillis() < endTime) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
break;
}
if (dataNode != null || finished.get() >= totalCount) {
break;
}
}
if (dataNode != null) {
cache.putIfAbsent(key, dataNode);
}
return dataNode;
}
use of io.mycat.backend.datasource.PhysicalDBNode in project Mycat-Server by MyCATApache.
the class LockTablesHandler method execute.
public void execute() throws Exception {
super.reset(this.rrs.getNodes().length);
MycatConfig conf = MycatServer.getInstance().getConfig();
for (final RouteResultsetNode node : rrs.getNodes()) {
BackendConnection conn = session.getTarget(node);
if (session.tryExistsCon(conn, node)) {
_execute(conn, node);
} else {
// create new connection
PhysicalDBNode dn = conf.getDataNodes().get(node.getName());
dn.getConnection(dn.getDatabase(), autocommit, node, this, node);
}
}
}
Aggregations