Search in sources :

Example 41 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat_plus by coderczp.

the class XMLSchemaLoader method loadSchemas.

private void loadSchemas(Element root) {
    NodeList list = root.getElementsByTagName("schema");
    for (int i = 0, n = list.getLength(); i < n; i++) {
        Element schemaElement = (Element) list.item(i);
        // 读取各个属性
        String name = schemaElement.getAttribute("name");
        String dataNode = schemaElement.getAttribute("dataNode");
        String checkSQLSchemaStr = schemaElement.getAttribute("checkSQLschema");
        String sqlMaxLimitStr = schemaElement.getAttribute("sqlMaxLimit");
        int sqlMaxLimit = -1;
        // 读取sql返回结果集限制
        if (sqlMaxLimitStr != null && !sqlMaxLimitStr.isEmpty()) {
            sqlMaxLimit = Integer.parseInt(sqlMaxLimitStr);
        }
        // check dataNode already exists or not,看schema标签中是否有datanode
        String defaultDbType = null;
        // 校验检查并添加dataNode
        if (dataNode != null && !dataNode.isEmpty()) {
            List<String> dataNodeLst = new ArrayList<String>(1);
            dataNodeLst.add(dataNode);
            checkDataNodeExists(dataNodeLst);
            String dataHost = dataNodes.get(dataNode).getDataHost();
            defaultDbType = dataHosts.get(dataHost).getDbType();
        } else {
            dataNode = null;
        }
        // 加载schema下所有tables
        Map<String, TableConfig> tables = loadTables(schemaElement);
        // 判断schema是否重复
        if (schemas.containsKey(name)) {
            throw new ConfigException("schema " + name + " duplicated!");
        }
        // 设置了table的不需要设置dataNode属性,没有设置table的必须设置dataNode属性
        if (dataNode == null && tables.size() == 0) {
            throw new ConfigException("schema " + name + " didn't config tables,so you must set dataNode property!");
        }
        SchemaConfig schemaConfig = new SchemaConfig(name, dataNode, tables, sqlMaxLimit, "true".equalsIgnoreCase(checkSQLSchemaStr));
        // 设定DB类型,这对之后的sql语句路由解析有帮助
        if (defaultDbType != null) {
            schemaConfig.setDefaultDataNodeDbType(defaultDbType);
            if (!"mysql".equalsIgnoreCase(defaultDbType)) {
                schemaConfig.setNeedSupportMultiDBType(true);
            }
        }
        // 判断是否有不是mysql的数据库类型,方便解析判断是否启用多数据库分页语法解析
        for (TableConfig tableConfig : tables.values()) {
            if (isHasMultiDbType(tableConfig)) {
                schemaConfig.setNeedSupportMultiDBType(true);
                break;
            }
        }
        // 记录每种dataNode的DB类型
        Map<String, String> dataNodeDbTypeMap = new HashMap<>();
        for (String dataNodeName : dataNodes.keySet()) {
            DataNodeConfig dataNodeConfig = dataNodes.get(dataNodeName);
            String dataHost = dataNodeConfig.getDataHost();
            DataHostConfig dataHostConfig = dataHosts.get(dataHost);
            if (dataHostConfig != null) {
                String dbType = dataHostConfig.getDbType();
                dataNodeDbTypeMap.put(dataNodeName, dbType);
            }
        }
        schemaConfig.setDataNodeDbTypeMap(dataNodeDbTypeMap);
        schemas.put(name, schemaConfig);
    }
}
Also used : SchemaConfig(io.mycat.config.model.SchemaConfig) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ConfigException(io.mycat.config.util.ConfigException) TableConfig(io.mycat.config.model.TableConfig) DataHostConfig(io.mycat.config.model.DataHostConfig) DataNodeConfig(io.mycat.config.model.DataNodeConfig)

Example 42 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat_plus by coderczp.

the class RuleDataPathChildrenCacheListener method reloadRuleData.

private void reloadRuleData(String name) {
    String tableName = name.substring(name.lastIndexOf("_") + 1, name.indexOf("."));
    String ruleName = name.substring(0, name.indexOf("."));
    Map<String, SchemaConfig> schemaConfigMap = MycatServer.getInstance().getConfig().getSchemas();
    for (SchemaConfig schemaConfig : schemaConfigMap.values()) {
        TableConfig tableConfig = schemaConfig.getTables().get(tableName.toUpperCase());
        if (tableConfig == null)
            continue;
        RuleConfig rule = tableConfig.getRule();
        AbstractPartitionAlgorithm function = rule.getRuleAlgorithm();
        if (function instanceof ReloadFunction) {
            ((ReloadFunction) function).reload();
        }
    }
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) ReloadFunction(io.mycat.route.function.ReloadFunction) SchemaConfig(io.mycat.config.model.SchemaConfig) TableConfig(io.mycat.config.model.TableConfig) RuleConfig(io.mycat.config.model.rule.RuleConfig)

Example 43 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat_plus by coderczp.

the class RouterUtil method tryRouteForOneTable.

/**
 * 单表路由
 */
public static RouteResultset tryRouteForOneTable(SchemaConfig schema, DruidShardingParseInfo ctx, RouteCalculateUnit routeUnit, String tableName, RouteResultset rrs, boolean isSelect, LayerCachePool cachePool) throws SQLNonTransientException {
    if (isNoSharding(schema, tableName)) {
        return routeToSingleNode(rrs, schema.getDataNode(), ctx.getSql());
    }
    TableConfig tc = schema.getTables().get(tableName);
    if (tc == null) {
        String msg = "can't find table define in schema " + tableName + " schema:" + schema.getName();
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    if (tc.isDistTable()) {
        return routeToDistTableNode(tableName, schema, rrs, ctx.getSql(), routeUnit.getTablesAndConditions(), cachePool, isSelect);
    }
    if (tc.isGlobalTable()) {
        // 全局表
        if (isSelect) {
            // global select ,not cache route result
            rrs.setCacheAble(false);
            return routeToSingleNode(rrs, getAliveRandomDataNode(tc), /*getRandomDataNode(tc)*/
            ctx.getSql());
        } else {
            // insert into 全局表的记录
            return routeToMultiNode(false, rrs, tc.getDataNodes(), ctx.getSql(), true);
        }
    } else {
        // 单表或者分库表
        if (!checkRuleRequired(schema, ctx, routeUnit, tc)) {
            throw new IllegalArgumentException("route rule for table " + tc.getName() + " is required: " + ctx.getSql());
        }
        if (tc.getPartitionColumn() == null && !tc.isSecondLevel()) {
            // return RouterUtil.routeToSingleNode(rrs, tc.getDataNodes().get(0),ctx.getSql());
            return routeToMultiNode(rrs.isCacheAble(), rrs, tc.getDataNodes(), ctx.getSql());
        } else {
            // 每个表对应的路由映射
            Map<String, Set<String>> tablesRouteMap = new HashMap<String, Set<String>>();
            if (routeUnit.getTablesAndConditions() != null && routeUnit.getTablesAndConditions().size() > 0) {
                RouterUtil.findRouteWithcConditionsForTables(schema, rrs, routeUnit.getTablesAndConditions(), tablesRouteMap, ctx.getSql(), cachePool, isSelect);
                if (rrs.isFinishedRoute()) {
                    return rrs;
                }
            }
            if (tablesRouteMap.get(tableName) == null) {
                return routeToMultiNode(rrs.isCacheAble(), rrs, tc.getDataNodes(), ctx.getSql());
            } else {
                return routeToMultiNode(rrs.isCacheAble(), rrs, tablesRouteMap.get(tableName), ctx.getSql());
            }
        }
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) TableConfig(io.mycat.config.model.TableConfig)

Example 44 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat_plus by coderczp.

the class RouterUtil method routeToDistTableNode.

private static RouteResultset routeToDistTableNode(String tableName, SchemaConfig schema, RouteResultset rrs, String orgSql, Map<String, Map<String, Set<ColumnRoutePair>>> tablesAndConditions, LayerCachePool cachePool, boolean isSelect) throws SQLNonTransientException {
    TableConfig tableConfig = schema.getTables().get(tableName);
    if (tableConfig == null) {
        String msg = "can't find table define in schema " + tableName + " schema:" + schema.getName();
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    if (tableConfig.isGlobalTable()) {
        String msg = "can't suport district table  " + tableName + " schema:" + schema.getName() + " for global table ";
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    String partionCol = tableConfig.getPartitionColumn();
    // String primaryKey = tableConfig.getPrimaryKey();
    boolean isLoadData = false;
    Set<String> tablesRouteSet = new HashSet<String>();
    List<String> dataNodes = tableConfig.getDataNodes();
    if (dataNodes.size() > 1) {
        String msg = "can't suport district table  " + tableName + " schema:" + schema.getName() + " for mutiple dataNode " + dataNodes;
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    String dataNode = dataNodes.get(0);
    // 主键查找缓存暂时不实现
    if (tablesAndConditions.isEmpty()) {
        List<String> subTables = tableConfig.getDistTables();
        tablesRouteSet.addAll(subTables);
    }
    for (Map.Entry<String, Map<String, Set<ColumnRoutePair>>> entry : tablesAndConditions.entrySet()) {
        boolean isFoundPartitionValue = partionCol != null && entry.getValue().get(partionCol) != null;
        Map<String, Set<ColumnRoutePair>> columnsMap = entry.getValue();
        Set<ColumnRoutePair> partitionValue = columnsMap.get(partionCol);
        if (partitionValue == null || partitionValue.size() == 0) {
            tablesRouteSet.addAll(tableConfig.getDistTables());
        } else {
            for (ColumnRoutePair pair : partitionValue) {
                AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
                if (pair.colValue != null) {
                    Integer tableIndex = algorithm.calculate(pair.colValue);
                    if (tableIndex == null) {
                        String msg = "can't find any valid datanode :" + tableConfig.getName() + " -> " + tableConfig.getPartitionColumn() + " -> " + pair.colValue;
                        LOGGER.warn(msg);
                        throw new SQLNonTransientException(msg);
                    }
                    String subTable = tableConfig.getDistTables().get(tableIndex);
                    if (subTable != null) {
                        tablesRouteSet.add(subTable);
                        if (algorithm instanceof SlotFunction) {
                            rrs.getDataNodeSlotMap().put(subTable, ((SlotFunction) algorithm).slotValue());
                        }
                    }
                }
                if (pair.rangeValue != null) {
                    Integer[] tableIndexs = algorithm.calculateRange(pair.rangeValue.beginValue.toString(), pair.rangeValue.endValue.toString());
                    for (Integer idx : tableIndexs) {
                        String subTable = tableConfig.getDistTables().get(idx);
                        if (subTable != null) {
                            tablesRouteSet.add(subTable);
                            if (algorithm instanceof SlotFunction) {
                                rrs.getDataNodeSlotMap().put(subTable, ((SlotFunction) algorithm).slotValue());
                            }
                        }
                    }
                }
            }
        }
    }
    Object[] subTables = tablesRouteSet.toArray();
    RouteResultsetNode[] nodes = new RouteResultsetNode[subTables.length];
    Map<String, Integer> dataNodeSlotMap = rrs.getDataNodeSlotMap();
    for (int i = 0; i < nodes.length; i++) {
        String table = String.valueOf(subTables[i]);
        String changeSql = orgSql;
        // rrs.getStatement()
        nodes[i] = new RouteResultsetNode(dataNode, rrs.getSqlType(), changeSql);
        nodes[i].setSubTableName(table);
        nodes[i].setSource(rrs);
        if (rrs.getDataNodeSlotMap().containsKey(dataNode)) {
            nodes[i].setSlot(rrs.getDataNodeSlotMap().get(dataNode));
        }
        if (rrs.getCanRunInReadDB() != null) {
            nodes[i].setCanRunInReadDB(rrs.getCanRunInReadDB());
        }
        if (dataNodeSlotMap.containsKey(table)) {
            nodes[i].setSlot(dataNodeSlotMap.get(table));
        }
        if (rrs.getRunOnSlave() != null) {
            nodes[0].setRunOnSlave(rrs.getRunOnSlave());
        }
    }
    rrs.setNodes(nodes);
    rrs.setSubTables(tablesRouteSet);
    rrs.setFinishedRoute(true);
    return rrs;
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) ColumnRoutePair(io.mycat.sqlengine.mpp.ColumnRoutePair) SlotFunction(io.mycat.route.function.SlotFunction) SQLNonTransientException(java.sql.SQLNonTransientException) RouteResultsetNode(io.mycat.route.RouteResultsetNode) TableConfig(io.mycat.config.model.TableConfig) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 45 with TableConfig

use of io.mycat.config.model.TableConfig in project Mycat_plus by coderczp.

the class RouterUtil method processInsert.

public static boolean processInsert(SchemaConfig schema, int sqlType, String origSQL, ServerConnection sc) throws SQLNonTransientException {
    String tableName = StringUtil.getTableName(origSQL).toUpperCase();
    TableConfig tableConfig = schema.getTables().get(tableName);
    boolean processedInsert = false;
    // 判断是有自增字段
    if (null != tableConfig && tableConfig.isAutoIncrement()) {
        String primaryKey = tableConfig.getPrimaryKey();
        processedInsert = processInsert(sc, schema, sqlType, origSQL, tableName, primaryKey);
    }
    return processedInsert;
}
Also used : TableConfig(io.mycat.config.model.TableConfig)

Aggregations

TableConfig (io.mycat.config.model.TableConfig)84 SchemaConfig (io.mycat.config.model.SchemaConfig)26 SQLNonTransientException (java.sql.SQLNonTransientException)26 Test (org.junit.Test)18 RuleConfig (io.mycat.config.model.rule.RuleConfig)16 RouteResultset (io.mycat.route.RouteResultset)14 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)14 HashMap (java.util.HashMap)14 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)12 SlotFunction (io.mycat.route.function.SlotFunction)12 ArrayList (java.util.ArrayList)12 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)10 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)8 PhysicalDBNode (io.mycat.backend.datasource.PhysicalDBNode)8 ConfigException (io.mycat.config.util.ConfigException)8 RouteResultsetNode (io.mycat.route.RouteResultsetNode)8 HashSet (java.util.HashSet)8 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)6 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)6 CacheService (io.mycat.cache.CacheService)6