Search in sources :

Example 16 with RouteResultsetNode

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

the class RouterUtil method routeToDDLNode.

/**
	 * 修复DDL路由
	 *
	 * @return RouteResultset
	 * @author aStoneGod
	 */
public static RouteResultset routeToDDLNode(RouteResultset rrs, int sqlType, String stmt, SchemaConfig schema) throws SQLSyntaxErrorException {
    stmt = getFixedSql(stmt);
    String tablename = "";
    final String upStmt = stmt.toUpperCase();
    if (upStmt.startsWith("CREATE")) {
        if (upStmt.contains("CREATE INDEX ")) {
            tablename = RouterUtil.getTableName(stmt, RouterUtil.getCreateIndexPos(upStmt, 0));
        } else {
            tablename = RouterUtil.getTableName(stmt, RouterUtil.getCreateTablePos(upStmt, 0));
        }
    } else if (upStmt.startsWith("DROP")) {
        if (upStmt.contains("DROP INDEX ")) {
            tablename = RouterUtil.getTableName(stmt, RouterUtil.getDropIndexPos(upStmt, 0));
        } else {
            tablename = RouterUtil.getTableName(stmt, RouterUtil.getDropTablePos(upStmt, 0));
        }
    } else if (upStmt.startsWith("ALTER")) {
        tablename = RouterUtil.getTableName(stmt, RouterUtil.getAlterTablePos(upStmt, 0));
    } else if (upStmt.startsWith("TRUNCATE")) {
        tablename = RouterUtil.getTableName(stmt, RouterUtil.getTruncateTablePos(upStmt, 0));
    }
    tablename = tablename.toUpperCase();
    if (schema.getTables().containsKey(tablename)) {
        if (ServerParse.DDL == sqlType) {
            List<String> dataNodes = new ArrayList<>();
            Map<String, TableConfig> tables = schema.getTables();
            TableConfig tc = tables.get(tablename);
            if (tables != null && (tc != null)) {
                dataNodes = tc.getDataNodes();
            }
            boolean isSlotFunction = tc.getRule() != null && tc.getRule().getRuleAlgorithm() instanceof SlotFunction;
            Iterator<String> iterator1 = dataNodes.iterator();
            int nodeSize = dataNodes.size();
            RouteResultsetNode[] nodes = new RouteResultsetNode[nodeSize];
            if (isSlotFunction) {
                stmt = changeCreateTable(schema, tablename, stmt);
            }
            for (int i = 0; i < nodeSize; i++) {
                String name = iterator1.next();
                nodes[i] = new RouteResultsetNode(name, sqlType, stmt);
                nodes[i].setSource(rrs);
                if (rrs.getDataNodeSlotMap().containsKey(name)) {
                    nodes[i].setSlot(rrs.getDataNodeSlotMap().get(name));
                } else if (isSlotFunction) {
                    nodes[i].setSlot(-1);
                }
            }
            rrs.setNodes(nodes);
        }
        return rrs;
    } else if (schema.getDataNode() != null) {
        //默认节点ddl
        RouteResultsetNode[] nodes = new RouteResultsetNode[1];
        nodes[0] = new RouteResultsetNode(schema.getDataNode(), sqlType, stmt);
        nodes[0].setSource(rrs);
        rrs.setNodes(nodes);
        return rrs;
    }
    //both tablename and defaultnode null
    LOGGER.error("table not in schema----" + tablename);
    throw new SQLSyntaxErrorException("op table not in schema----" + tablename);
}
Also used : RouteResultsetNode(io.mycat.route.RouteResultsetNode) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) TableConfig(io.mycat.config.model.TableConfig) SlotFunction(io.mycat.route.function.SlotFunction)

Example 17 with RouteResultsetNode

use of io.mycat.route.RouteResultsetNode 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);
            }
        }
    }
}
Also used : PhysicalDBNode(io.mycat.backend.datasource.PhysicalDBNode) BackendConnection(io.mycat.backend.BackendConnection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RouteResultsetNode(io.mycat.route.RouteResultsetNode) MycatConfig(io.mycat.config.MycatConfig)

Example 18 with RouteResultsetNode

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

the class ServerLoadDataInfileHandler method tryDirectRoute.

private RouteResultset tryDirectRoute(String sql, String[] lineList) {
    RouteResultset rrs = new RouteResultset(sql, ServerParse.INSERT);
    rrs.setLoadData(true);
    if (tableConfig == null && schema.getDataNode() != null) {
        //走默认节点
        RouteResultsetNode rrNode = new RouteResultsetNode(schema.getDataNode(), ServerParse.INSERT, sql);
        rrNode.setSource(rrs);
        rrs.setNodes(new RouteResultsetNode[] { rrNode });
        return rrs;
    } else if (tableConfig != null && tableConfig.isGlobalTable()) {
        ArrayList<String> dataNodes = tableConfig.getDataNodes();
        RouteResultsetNode[] rrsNodes = new RouteResultsetNode[dataNodes.size()];
        for (int i = 0, dataNodesSize = dataNodes.size(); i < dataNodesSize; i++) {
            String dataNode = dataNodes.get(i);
            RouteResultsetNode rrNode = new RouteResultsetNode(dataNode, ServerParse.INSERT, sql);
            rrsNodes[i] = rrNode;
            if (rrs.getDataNodeSlotMap().containsKey(dataNode)) {
                rrsNodes[i].setSlot(rrs.getDataNodeSlotMap().get(dataNode));
            }
            rrsNodes[i].setSource(rrs);
        }
        rrs.setNodes(rrsNodes);
        return rrs;
    } else if (tableConfig != null) {
        DruidShardingParseInfo ctx = new DruidShardingParseInfo();
        ctx.addTable(tableName);
        if (partitionColumnIndex == -1 || partitionColumnIndex >= lineList.length) {
            return null;
        } else {
            String value = lineList[partitionColumnIndex];
            RouteCalculateUnit routeCalculateUnit = new RouteCalculateUnit();
            routeCalculateUnit.addShardingExpr(tableName, getPartitionColumn(), parseFieldString(value, loadData.getEnclose()));
            ctx.addRouteCalculateUnit(routeCalculateUnit);
            try {
                SortedSet<RouteResultsetNode> nodeSet = new TreeSet<RouteResultsetNode>();
                for (RouteCalculateUnit unit : ctx.getRouteCalculateUnits()) {
                    RouteResultset rrsTmp = RouterUtil.tryRouteForTables(schema, ctx, unit, rrs, false, tableId2DataNodeCache);
                    if (rrsTmp != null) {
                        for (RouteResultsetNode node : rrsTmp.getNodes()) {
                            nodeSet.add(node);
                        }
                    }
                }
                RouteResultsetNode[] nodes = new RouteResultsetNode[nodeSet.size()];
                int i = 0;
                for (Iterator<RouteResultsetNode> iterator = nodeSet.iterator(); iterator.hasNext(); ) {
                    nodes[i] = (RouteResultsetNode) iterator.next();
                    i++;
                }
                rrs.setNodes(nodes);
                return rrs;
            } catch (SQLNonTransientException e) {
                throw new RuntimeException(e);
            }
        }
    }
    return null;
}
Also used : DruidShardingParseInfo(io.mycat.route.parser.druid.DruidShardingParseInfo) RouteCalculateUnit(io.mycat.route.parser.druid.RouteCalculateUnit) ArrayList(java.util.ArrayList) SQLNonTransientException(java.sql.SQLNonTransientException) TreeSet(java.util.TreeSet) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RouteResultset(io.mycat.route.RouteResultset)

Example 19 with RouteResultsetNode

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

the class ServerLoadDataInfileHandler method buildResultSet.

private RouteResultset buildResultSet(Map<String, LoadData> routeMap) {
    //强制local
    statement.setLocal(true);
    //默认druid会过滤掉路径的分隔符,所以这里重新设置下
    SQLLiteralExpr fn = new SQLCharExpr(fileName);
    statement.setFileName(fn);
    String srcStatement = statement.toString();
    RouteResultset rrs = new RouteResultset(srcStatement, ServerParse.LOAD_DATA_INFILE_SQL);
    rrs.setLoadData(true);
    rrs.setStatement(srcStatement);
    rrs.setAutocommit(serverConnection.isAutocommit());
    rrs.setFinishedRoute(true);
    int size = routeMap.size();
    RouteResultsetNode[] routeResultsetNodes = new RouteResultsetNode[size];
    int index = 0;
    for (String dn : routeMap.keySet()) {
        RouteResultsetNode rrNode = new RouteResultsetNode(dn, ServerParse.LOAD_DATA_INFILE_SQL, srcStatement);
        rrNode.setSource(rrs);
        rrNode.setTotalNodeSize(size);
        rrNode.setStatement(srcStatement);
        LoadData newLoadData = new LoadData();
        ObjectUtil.copyProperties(loadData, newLoadData);
        newLoadData.setLocal(true);
        LoadData loadData1 = routeMap.get(dn);
        //  if (isHasStoreToFile)
        if (//此处判断是否有保存分库load的临时文件dn1.txt/dn2.txt,不是判断是否有clientTemp.txt
        loadData1.getFileName() != null) {
            newLoadData.setFileName(loadData1.getFileName());
        } else {
            newLoadData.setData(loadData1.getData());
        }
        rrNode.setLoadData(newLoadData);
        routeResultsetNodes[index] = rrNode;
        index++;
    }
    rrs.setNodes(routeResultsetNodes);
    return rrs;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) LoadData(io.mycat.sqlengine.mpp.LoadData) RouteResultsetNode(io.mycat.route.RouteResultsetNode) RouteResultset(io.mycat.route.RouteResultset)

Example 20 with RouteResultsetNode

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

the class DruidMysqlRouteStrategyTest method testGroupLimit.

public void testGroupLimit() throws Exception {
    final SchemaConfig schema = schemaMap.get("cndb");
    String sql = null;
    RouteResultset rrs = null;
    sql = "select count(*) from (select * from(select * from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    // Assert.assertEquals(88L, rrs.getLimitSize());
    // Assert.assertEquals(RouteResultset.SUM_FLAG, rrs.getFlag());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 2);
    NodeNameAsserter nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sql = "select count(*) from (select * from(select max(id) from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sql = "select * from (select * from(select max(id) from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    sql = "select * from (select count(*) from(select * from offer_detail where offer_id='123' or offer_id='234' limit 88)offer  where offer.member_id='abc' limit 60) w " + " where w.member_id ='pavarotti17' limit 99";
    rrs = routeStrategy.route(new SystemConfig(), schema, 1, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    // Assert.assertEquals(88L, rrs.getLimitSize());
    // Assert.assertEquals(RouteResultset.SUM_FLAG, rrs.getFlag());
    nodeMap = getNodeMap(rrs, 2);
    nameAsserter = new NodeNameAsserter("detail_dn29", "detail_dn15");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) 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