Search in sources :

Example 6 with RouteResultsetNode

use of io.mycat.route.RouteResultsetNode in project Mycat-Server by MyCATApache.

the class MySQLConnection method query.

/**
	 * by wuzh ,execute a query and ignore transaction settings for performance
	 * 
	 * @param sql
	 * @throws UnsupportedEncodingException
	 */
public void query(String query) throws UnsupportedEncodingException {
    RouteResultsetNode rrn = new RouteResultsetNode("default", ServerParse.SELECT, query);
    synAndDoExecute(null, rrn, this.charsetIndex, this.txIsolation, true);
}
Also used : RouteResultsetNode(io.mycat.route.RouteResultsetNode)

Example 7 with RouteResultsetNode

use of io.mycat.route.RouteResultsetNode 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 RouteResultsetNode

use of io.mycat.route.RouteResultsetNode 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 9 with RouteResultsetNode

use of io.mycat.route.RouteResultsetNode 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)

Example 10 with RouteResultsetNode

use of io.mycat.route.RouteResultsetNode in project Mycat-Server by MyCATApache.

the class SwitchPrepareCheckRunner method checkIsInTransation.

private boolean checkIsInTransation(BackendConnection backendConnection) {
    if (!taskNode.getSchema().equalsIgnoreCase(backendConnection.getSchema()))
        return false;
    Object attach = backendConnection.getAttachment();
    if (attach instanceof RouteResultsetNode) {
        RouteResultsetNode resultsetNode = (RouteResultsetNode) attach;
        RouteResultset rrs = resultsetNode.getSource();
        for (String table : rrs.getTables()) {
            if (table.equalsIgnoreCase(taskNode.getTable())) {
                int slot = resultsetNode.getSlot();
                if (slot < 0 && resultsetNode.isUpdateSql()) {
                    return true;
                } else if (resultsetNode.isUpdateSql()) {
                    for (PartitionByCRC32PreSlot.Range range : rangeList) {
                        if (slot >= range.start && slot <= range.end) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}
Also used : RouteResultsetNode(io.mycat.route.RouteResultsetNode) RouteResultset(io.mycat.route.RouteResultset)

Aggregations

RouteResultsetNode (io.mycat.route.RouteResultsetNode)43 RouteResultset (io.mycat.route.RouteResultset)15 SQLNonTransientException (java.sql.SQLNonTransientException)8 BackendConnection (io.mycat.backend.BackendConnection)7 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)7 MycatConfig (io.mycat.config.MycatConfig)7 SchemaConfig (io.mycat.config.model.SchemaConfig)7 SystemConfig (io.mycat.config.model.SystemConfig)7 TableConfig (io.mycat.config.model.TableConfig)4 SlotFunction (io.mycat.route.function.SlotFunction)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)3 RouteCalculateUnit (io.mycat.route.parser.druid.RouteCalculateUnit)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)2 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)2 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)2 MySQLConnection (io.mycat.backend.mysql.nio.MySQLConnection)2 CachePool (io.mycat.cache.CachePool)2