use of io.mycat.backend.BackendConnection 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.BackendConnection in project Mycat-Server by MyCATApache.
the class UnLockTablesHandler method execute.
public void execute() {
Map<RouteResultsetNode, BackendConnection> lockedConns = session.getTargetMap();
Set<RouteResultsetNode> dnSet = lockedConns.keySet();
this.reset(lockedConns.size());
// 客户端直接发送unlock tables命令,由于之前未发送lock tables语句,无法获取后端绑定的连接,此时直接返回OK包
if (lockedConns.size() == 0) {
LOGGER.warn("find no locked backend connection!" + session.getSource());
OkPacket ok = new OkPacket();
ok.packetId = ++packetId;
// unlock table 命令返回MySQL协议包长度为7
ok.packetLength = 7;
ok.serverStatus = session.getSource().isAutocommit() ? 2 : 1;
ok.write(session.getSource());
return;
}
for (RouteResultsetNode dataNode : dnSet) {
RouteResultsetNode node = new RouteResultsetNode(dataNode.getName(), ServerParse.UNLOCK, srcStatement);
BackendConnection conn = lockedConns.get(dataNode);
if (clearIfSessionClosed(session)) {
return;
}
conn.setResponseHandler(this);
try {
conn.execute(node, session.getSource(), autocommit);
} catch (Exception e) {
connectionError(e, conn);
}
}
}
use of io.mycat.backend.BackendConnection in project Mycat-Server by MyCATApache.
the class HeartBeatCon method abandTimeOuttedConns.
/**
* remove timeout connections
*/
public void abandTimeOuttedConns() {
if (allCons.isEmpty()) {
return;
}
Collection<BackendConnection> abandCons = new LinkedList<BackendConnection>();
long curTime = System.currentTimeMillis();
Iterator<Entry<Long, HeartBeatCon>> itors = allCons.entrySet().iterator();
while (itors.hasNext()) {
HeartBeatCon hbCon = itors.next().getValue();
if (hbCon.timeOutTimestamp < curTime) {
abandCons.add(hbCon.conn);
itors.remove();
}
}
if (!abandCons.isEmpty()) {
for (BackendConnection con : abandCons) {
try {
// if(con.isBorrowed())
con.close("heartbeat timeout ");
} catch (Exception e) {
LOGGER.warn("close err:" + e);
}
}
}
}
use of io.mycat.backend.BackendConnection 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.BackendConnection 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