Search in sources :

Example 6 with BackendConnection

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);
    }
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) BackendConnection(io.mycat.backend.BackendConnection) ServerConnection(io.mycat.server.ServerConnection) MycatConfig(io.mycat.config.MycatConfig)

Example 7 with BackendConnection

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);
        }
    }
}
Also used : BackendConnection(io.mycat.backend.BackendConnection) OkPacket(io.mycat.net.mysql.OkPacket) RouteResultsetNode(io.mycat.route.RouteResultsetNode)

Example 8 with BackendConnection

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);
            }
        }
    }
}
Also used : BackendConnection(io.mycat.backend.BackendConnection) Entry(java.util.Map.Entry) LinkedList(java.util.LinkedList)

Example 9 with BackendConnection

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;
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) BackendConnection(io.mycat.backend.BackendConnection) RouteResultsetNode(io.mycat.route.RouteResultsetNode) MycatConfig(io.mycat.config.MycatConfig) IOException(java.io.IOException) CachePool(io.mycat.cache.CachePool)

Example 10 with BackendConnection

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);
        }
    }
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) BackendConnection(io.mycat.backend.BackendConnection) RouteResultsetNode(io.mycat.route.RouteResultsetNode) MycatConfig(io.mycat.config.MycatConfig)

Aggregations

BackendConnection (io.mycat.backend.BackendConnection)25 RouteResultsetNode (io.mycat.route.RouteResultsetNode)7 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)6 MycatConfig (io.mycat.config.MycatConfig)6 MySQLConnection (io.mycat.backend.mysql.nio.MySQLConnection)3 NIOProcessor (io.mycat.net.NIOProcessor)3 RowDataPacket (io.mycat.net.mysql.RowDataPacket)3 ByteBuffer (java.nio.ByteBuffer)3 Entry (java.util.Map.Entry)3 ConQueue (io.mycat.backend.ConQueue)2 EOFPacket (io.mycat.net.mysql.EOFPacket)2 FieldPacket (io.mycat.net.mysql.FieldPacket)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 PhysicalDBPool (io.mycat.backend.datasource.PhysicalDBPool)1 PhysicalDatasource (io.mycat.backend.datasource.PhysicalDatasource)1 JDBCConnection (io.mycat.backend.jdbc.JDBCConnection)1 GetConnectionHandler (io.mycat.backend.mysql.nio.handler.GetConnectionHandler)1 CoordinatorLogEntry (io.mycat.backend.mysql.xa.CoordinatorLogEntry)1