Search in sources :

Example 16 with TableConfig

use of com.actiontech.dble.config.model.TableConfig in project dble by actiontech.

the class ShowTables method getTableSet.

public static Map<String, String> getTableSet(String cSchema, ShowCreateStmtInfo info) {
    // remove the table which is not created but configured
    SchemaMeta schemata = DbleServer.getInstance().getTmManager().getCatalogs().get(cSchema);
    if (schemata == null) {
        return new HashMap<>();
    }
    Map meta = schemata.getTableMetas();
    TreeMap<String, String> tableMap = new TreeMap<>();
    Map<String, SchemaConfig> schemas = DbleServer.getInstance().getConfig().getSchemas();
    if (null != info.getLike()) {
        String p = "^" + info.getLike().replaceAll("%", ".*");
        Pattern pattern = Pattern.compile(p, Pattern.CASE_INSENSITIVE);
        Matcher maLike;
        for (TableConfig tbConfig : schemas.get(cSchema).getTables().values()) {
            String tbName = tbConfig.getName();
            maLike = pattern.matcher(tbName);
            if (maLike.matches() && meta.get(tbName) != null) {
                String tbType = tbConfig.getTableType() == TableConfig.TableTypeEnum.TYPE_GLOBAL_TABLE ? "GLOBAL TABLE" : "SHARDING TABLE";
                tableMap.put(tbName, tbType);
            }
        }
    } else {
        for (TableConfig tbConfig : schemas.get(cSchema).getTables().values()) {
            String tbName = tbConfig.getName();
            if (meta.get(tbName) != null) {
                String tbType = tbConfig.getTableType() == TableConfig.TableTypeEnum.TYPE_GLOBAL_TABLE ? "GLOBAL TABLE" : "SHARDING TABLE";
                tableMap.put(tbName, tbType);
            }
        }
    }
    return tableMap;
}
Also used : Pattern(java.util.regex.Pattern) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) Matcher(java.util.regex.Matcher) SchemaMeta(com.actiontech.dble.meta.SchemaMeta) TableConfig(com.actiontech.dble.config.model.TableConfig)

Example 17 with TableConfig

use of com.actiontech.dble.config.model.TableConfig in project dble by actiontech.

the class GlobalTableUtil method consistencyCheck.

public static void consistencyCheck() {
    ServerConfig config = DbleServer.getInstance().getConfig();
    for (TableConfig table : globalTableMap.values()) {
        Map<String, ArrayList<PhysicalDBNode>> executedMap = new HashMap<>();
        // <table name="travelrecord" dataNode="dn1,dn2,dn3"
        for (String nodeName : table.getDataNodes()) {
            Map<String, PhysicalDBNode> map = config.getDataNodes();
            for (PhysicalDBNode dbNode : map.values()) {
                // <dataNode name="dn1" dataHost="localhost1" database="db1" />
                if (nodeName.equals(dbNode.getName())) {
                    // dn1,dn2,dn3
                    PhysicalDBPool pool = dbNode.getDbPool();
                    Collection<PhysicalDatasource> allDS = pool.getAllDataSources();
                    for (PhysicalDatasource pds : allDS) {
                        if (pds instanceof MySQLDataSource) {
                            ArrayList<PhysicalDBNode> nodes = executedMap.get(pds.getName());
                            if (nodes == null) {
                                nodes = new ArrayList<>();
                            }
                            nodes.add(dbNode);
                            executedMap.put(pds.getName(), nodes);
                        }
                    }
                }
            }
        }
        for (Map.Entry<String, ArrayList<PhysicalDBNode>> entry : executedMap.entrySet()) {
            ArrayList<PhysicalDBNode> nodes = entry.getValue();
            String[] schemas = new String[nodes.size()];
            for (int index = 0; index < nodes.size(); index++) {
                schemas[index] = StringUtil.removeBackQuote(nodes.get(index).getDatabase());
            }
            Collection<PhysicalDatasource> allDS = nodes.get(0).getDbPool().getAllDataSources();
            for (PhysicalDatasource pds : allDS) {
                if (pds instanceof MySQLDataSource && entry.getKey().equals(pds.getName())) {
                    MySQLDataSource mds = (MySQLDataSource) pds;
                    MySQLConsistencyChecker checker = new MySQLConsistencyChecker(mds, schemas, table.getName());
                    isInnerColumnCheckFinished = 0;
                    checker.checkInnerColumnExist();
                    while (isInnerColumnCheckFinished <= 0) {
                        LOGGER.debug("isInnerColumnCheckFinished:" + isInnerColumnCheckFinished);
                        try {
                            TimeUnit.SECONDS.sleep(1);
                        } catch (InterruptedException e) {
                            LOGGER.info(e.getMessage());
                        }
                    }
                    LOGGER.debug("isInnerColumnCheckFinished:" + isInnerColumnCheckFinished);
                    // check another measure
                    checker = new MySQLConsistencyChecker(mds, schemas, table.getName());
                    isColumnCountCheckFinished = 0;
                    checker.checkRecordCount();
                    while (isColumnCountCheckFinished <= 0) {
                        LOGGER.debug("isColumnCountCheckFinished:" + isColumnCountCheckFinished);
                        try {
                            TimeUnit.SECONDS.sleep(1);
                        } catch (InterruptedException e) {
                            LOGGER.info(e.getMessage());
                        }
                    }
                    LOGGER.debug("isColumnCountCheckFinished:" + isColumnCountCheckFinished);
                    checker = new MySQLConsistencyChecker(mds, schemas, table.getName());
                    checker.checkMaxTimeStamp();
                }
            }
        }
    }
}
Also used : PhysicalDBNode(com.actiontech.dble.backend.datasource.PhysicalDBNode) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PhysicalDBPool(com.actiontech.dble.backend.datasource.PhysicalDBPool) ServerConfig(com.actiontech.dble.config.ServerConfig) MySQLConsistencyChecker(com.actiontech.dble.backend.heartbeat.MySQLConsistencyChecker) PhysicalDatasource(com.actiontech.dble.backend.datasource.PhysicalDatasource) MySQLDataSource(com.actiontech.dble.backend.mysql.nio.MySQLDataSource) TableConfig(com.actiontech.dble.config.model.TableConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 18 with TableConfig

use of com.actiontech.dble.config.model.TableConfig in project dble by actiontech.

the class RouterUtil method findRouterWithConditionsForTables.

/**
 * findRouterWithConditionsForTables
 */
public static void findRouterWithConditionsForTables(SchemaConfig schema, RouteResultset rrs, Map<String, Map<String, Set<ColumnRoutePair>>> tablesAndConditions, Map<String, Set<String>> tablesRouteMap, LayerCachePool cachePool, boolean isSelect, boolean isSingleTable) throws SQLNonTransientException {
    // router for shard-ing tables
    for (Map.Entry<String, Map<String, Set<ColumnRoutePair>>> entry : tablesAndConditions.entrySet()) {
        String tableName = entry.getKey();
        if (DbleServer.getInstance().getSystemVariables().isLowerCaseTableNames()) {
            tableName = tableName.toLowerCase();
        }
        if (tableName.startsWith(schema.getName() + ".")) {
            tableName = tableName.substring(schema.getName().length() + 1);
        }
        TableConfig tableConfig = schema.getTables().get(tableName);
        if (tableConfig == null) {
            if (isSingleTable) {
                String msg = "can't find table [" + tableName + "[ define in schema " + ":" + schema.getName();
                LOGGER.info(msg);
                throw new SQLNonTransientException(msg);
            } else {
                // cross to other schema
                continue;
            }
        }
        if (tableConfig.isGlobalTable() || schema.getTables().get(tableName).getDataNodes().size() == 1) {
            // global table or single node shard-ing table will router later
            continue;
        } else {
            // shard-ing table,childTable or others
            Map<String, Set<ColumnRoutePair>> columnsMap = entry.getValue();
            if (tryRouteWithPrimaryCache(rrs, tablesRouteMap, cachePool, columnsMap, schema, tableName, tableConfig.getPrimaryKey(), isSelect)) {
                continue;
            }
            String joinKey = tableConfig.getJoinKey();
            String partitionCol = tableConfig.getPartitionColumn();
            boolean isFoundPartitionValue = partitionCol != null && columnsMap.get(partitionCol) != null;
            // where filter contains partition column
            if (isFoundPartitionValue) {
                Set<ColumnRoutePair> partitionValue = columnsMap.get(partitionCol);
                if (partitionValue.size() == 0) {
                    if (tablesRouteMap.get(tableName) == null) {
                        tablesRouteMap.put(tableName, new HashSet<String>());
                    }
                    tablesRouteMap.get(tableName).addAll(tableConfig.getDataNodes());
                } else {
                    routeWithPartition(tablesRouteMap, tableName, tableConfig, partitionValue);
                }
            } else if (joinKey != null && columnsMap.get(joinKey) != null && columnsMap.get(joinKey).size() != 0) {
                routerForJoinTable(rrs, tableConfig, columnsMap, joinKey);
                return;
            } else {
                // no partition column,router to all nodes
                if (tablesRouteMap.get(tableName) == null) {
                    tablesRouteMap.put(tableName, new HashSet<String>());
                }
                tablesRouteMap.get(tableName).addAll(tableConfig.getDataNodes());
            }
        }
    }
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) ColumnRoutePair(com.actiontech.dble.sqlengine.mpp.ColumnRoutePair) TableConfig(com.actiontech.dble.config.model.TableConfig)

Example 19 with TableConfig

use of com.actiontech.dble.config.model.TableConfig in project dble by actiontech.

the class RouterUtil method tryRouteForTables.

/**
 * tryRouteFor multiTables
 */
public static RouteResultset tryRouteForTables(SchemaConfig schema, DruidShardingParseInfo ctx, RouteCalculateUnit routeUnit, RouteResultset rrs, boolean isSelect, LayerCachePool cachePool) throws SQLException {
    List<String> tables = ctx.getTables();
    // no sharding table
    if (isNoSharding(schema, tables.get(0))) {
        return routeToSingleNode(rrs, schema.getDataNode());
    }
    if (tables.size() == 1) {
        return RouterUtil.tryRouteForOneTable(schema, routeUnit, tables.get(0), rrs, isSelect, cachePool);
    }
    /**
     * multi-table it must be ER OR   global* normal , global* er
     */
    // map <table,data_nodes>
    Map<String, Set<String>> tablesRouteMap = new HashMap<>();
    Map<String, Map<String, Set<ColumnRoutePair>>> tablesAndConditions = routeUnit.getTablesAndConditions();
    if (tablesAndConditions != null && tablesAndConditions.size() > 0) {
        // findRouter for shard-ing table
        RouterUtil.findRouterWithConditionsForTables(schema, rrs, tablesAndConditions, tablesRouteMap, cachePool, isSelect, false);
        if (rrs.isFinishedRoute()) {
            return rrs;
        }
    }
    // if global table and normal table has no intersection ,they had treat as normal join
    for (String tableName : tables) {
        TableConfig tableConfig = schema.getTables().get(tableName);
        if (tableConfig != null && !tableConfig.isGlobalTable() && tablesRouteMap.get(tableName) == null) {
            // the other is single table
            tablesRouteMap.put(tableName, new HashSet<String>());
            tablesRouteMap.get(tableName).addAll(tableConfig.getDataNodes());
        }
    }
    Set<String> retNodesSet = new HashSet<>();
    boolean isFirstAdd = true;
    for (Map.Entry<String, Set<String>> entry : tablesRouteMap.entrySet()) {
        if (entry.getValue() == null || entry.getValue().size() == 0) {
            throw new SQLNonTransientException("parent key can't find any valid datanode ");
        } else {
            if (isFirstAdd) {
                retNodesSet.addAll(entry.getValue());
                isFirstAdd = false;
            } else {
                retNodesSet.retainAll(entry.getValue());
                if (retNodesSet.size() == 0) {
                    // two tables has no no intersection
                    String errMsg = "invalid route in sql, multi tables found but datanode has no intersection " + " sql:" + rrs.getStatement();
                    LOGGER.info(errMsg);
                    throw new SQLNonTransientException(errMsg);
                }
            }
        }
    }
    // retNodesSet.size() >0
    routeToMultiNode(isSelect, rrs, retNodesSet);
    return rrs;
}
Also used : ColumnRoutePair(com.actiontech.dble.sqlengine.mpp.ColumnRoutePair) SQLNonTransientException(java.sql.SQLNonTransientException) TableConfig(com.actiontech.dble.config.model.TableConfig)

Example 20 with TableConfig

use of com.actiontech.dble.config.model.TableConfig in project dble by actiontech.

the class RouterUtil method tryRouteForOneTable.

/**
 * tryRouteForOneTable
 */
public static RouteResultset tryRouteForOneTable(SchemaConfig schema, RouteCalculateUnit routeUnit, String tableName, RouteResultset rrs, boolean isSelect, LayerCachePool cachePool) throws SQLException {
    TableConfig tc = schema.getTables().get(tableName);
    if (tc == null) {
        String msg = "Table '" + schema.getName() + "." + tableName + "' doesn't exist";
        throw new SQLException(msg, "42S02", ErrorCode.ER_NO_SUCH_TABLE);
    }
    if (tc.isGlobalTable()) {
        if (isSelect) {
            // global select ,not cache route result
            rrs.setCacheAble(false);
            rrs.setGlobalTable(true);
            return routeToSingleNode(rrs, tc.getRandomDataNode());
        } else {
            // insert into all global table's node
            return routeToMultiNode(false, rrs, tc.getDataNodes(), true);
        }
    } else {
        // single table or shard-ing table
        if (!checkRuleRequired(schema, routeUnit, tc)) {
            throw new IllegalArgumentException("route rule for table " + tc.getName() + " is required: " + rrs.getStatement());
        }
        if ((tc.getPartitionColumn() == null && tc.getParentTC() == null) || (tc.getParentTC() != null && tc.getDirectRouteTC() == null)) {
            // single table or one of the children of complex ER table
            return routeToMultiNode(rrs.isCacheAble(), rrs, tc.getDataNodes());
        } else {
            Map<String, Set<String>> tablesRouteMap = new HashMap<>();
            if (routeUnit.getTablesAndConditions() != null && routeUnit.getTablesAndConditions().size() > 0) {
                RouterUtil.findRouterWithConditionsForTables(schema, rrs, routeUnit.getTablesAndConditions(), tablesRouteMap, cachePool, isSelect, true);
                if (rrs.isFinishedRoute()) {
                    return rrs;
                }
            }
            if (tablesRouteMap.get(tableName) == null) {
                return routeToMultiNode(rrs.isCacheAble(), rrs, tc.getDataNodes());
            } else {
                return routeToMultiNode(rrs.isCacheAble(), rrs, tablesRouteMap.get(tableName));
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) TableConfig(com.actiontech.dble.config.model.TableConfig)

Aggregations

TableConfig (com.actiontech.dble.config.model.TableConfig)32 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)16 SQLNonTransientException (java.sql.SQLNonTransientException)16 SQLException (java.sql.SQLException)7 RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)6 SchemaInfo (com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)5 RouteResultset (com.actiontech.dble.route.RouteResultset)4 AbstractPartitionAlgorithm (com.actiontech.dble.route.function.AbstractPartitionAlgorithm)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)4 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)4 CacheService (com.actiontech.dble.cache.CacheService)3 ServerConfig (com.actiontech.dble.config.ServerConfig)3 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)3 Matcher (java.util.regex.Matcher)3 PhysicalDBNode (com.actiontech.dble.backend.datasource.PhysicalDBNode)2 FetchStoreNodeOfChildTableHandler (com.actiontech.dble.backend.mysql.nio.handler.FetchStoreNodeOfChildTableHandler)2 EOFPacket (com.actiontech.dble.net.mysql.EOFPacket)2 FieldPacket (com.actiontech.dble.net.mysql.FieldPacket)2 RowDataPacket (com.actiontech.dble.net.mysql.RowDataPacket)2