Search in sources :

Example 6 with AbstractPartitionAlgorithm

use of com.actiontech.dble.route.function.AbstractPartitionAlgorithm in project dble by actiontech.

the class RouterUtil method routeWithPartition.

private static void routeWithPartition(Map<String, Set<String>> tablesRouteMap, String tableName, TableConfig tableConfig, Set<ColumnRoutePair> partitionValue) throws SQLNonTransientException {
    for (ColumnRoutePair pair : partitionValue) {
        AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
        if (pair.colValue != null) {
            if (NEED_REPLACE.equals(pair.colValue)) {
                return;
            }
            Integer nodeIndex = algorithm.calculate(pair.colValue);
            if (nodeIndex == null) {
                String msg = "can't find any valid data node :" + tableConfig.getName() + " -> " + tableConfig.getPartitionColumn() + " -> " + pair.colValue;
                LOGGER.info(msg);
                throw new SQLNonTransientException(msg);
            }
            ArrayList<String> dataNodes = tableConfig.getDataNodes();
            String node;
            if (nodeIndex >= 0 && nodeIndex < dataNodes.size()) {
                node = dataNodes.get(nodeIndex);
            } else {
                node = null;
                String msg = "Can't find a valid data node for specified node index :" + tableConfig.getName() + " -> " + tableConfig.getPartitionColumn() + " -> " + pair.colValue + " -> " + "Index : " + nodeIndex;
                LOGGER.info(msg);
                throw new SQLNonTransientException(msg);
            }
            if (node != null) {
                if (tablesRouteMap.get(tableName) == null) {
                    tablesRouteMap.put(tableName, new HashSet<String>());
                }
                tablesRouteMap.get(tableName).add(node);
            }
        }
        if (pair.rangeValue != null) {
            Integer[] nodeIndexes = algorithm.calculateRange(pair.rangeValue.getBeginValue().toString(), pair.rangeValue.getEndValue().toString());
            ArrayList<String> dataNodes = tableConfig.getDataNodes();
            String node;
            for (Integer idx : nodeIndexes) {
                if (idx >= 0 && idx < dataNodes.size()) {
                    node = dataNodes.get(idx);
                } else {
                    String msg = "Can't find valid data node(s) for some of specified node indexes :" + tableConfig.getName() + " -> " + tableConfig.getPartitionColumn();
                    LOGGER.info(msg);
                    throw new SQLNonTransientException(msg);
                }
                if (node != null) {
                    if (tablesRouteMap.get(tableName) == null) {
                        tablesRouteMap.put(tableName, new HashSet<String>());
                    }
                    tablesRouteMap.get(tableName).add(node);
                }
            }
        }
    }
}
Also used : AbstractPartitionAlgorithm(com.actiontech.dble.route.function.AbstractPartitionAlgorithm) SQLNonTransientException(java.sql.SQLNonTransientException) ColumnRoutePair(com.actiontech.dble.sqlengine.mpp.ColumnRoutePair)

Example 7 with AbstractPartitionAlgorithm

use of com.actiontech.dble.route.function.AbstractPartitionAlgorithm in project dble by actiontech.

the class XMLSchemaLoader method checkRuleSuitTable.

/**
 * shard table datanode(2) < function count(3) and check failed
 */
private void checkRuleSuitTable(TableConfig tableConf) {
    AbstractPartitionAlgorithm function = tableConf.getRule().getRuleAlgorithm();
    int suitValue = function.suitableFor(tableConf);
    if (suitValue < 0) {
        throw new ConfigException("Illegal table conf : table [ " + tableConf.getName() + " ] rule function [ " + tableConf.getRule().getFunctionName() + " ] partition size : " + tableConf.getRule().getRuleAlgorithm().getPartitionNum() + " > table datanode size : " + tableConf.getDataNodes().size() + ", please make sure table datanode size = function partition size");
    } else if (suitValue > 0) {
        LOGGER.info("table conf : table [ {} ] rule function [ {} ] partition size : {} < table datanode size : {} , this cause some datanode to be redundant", new String[] { tableConf.getName(), tableConf.getRule().getFunctionName(), String.valueOf(tableConf.getRule().getRuleAlgorithm().getPartitionNum()), String.valueOf(tableConf.getDataNodes().size()) });
    } else {
    // table data node size == rule function partition size
    }
}
Also used : AbstractPartitionAlgorithm(com.actiontech.dble.route.function.AbstractPartitionAlgorithm) ConfigException(com.actiontech.dble.config.util.ConfigException)

Aggregations

AbstractPartitionAlgorithm (com.actiontech.dble.route.function.AbstractPartitionAlgorithm)7 SQLNonTransientException (java.sql.SQLNonTransientException)5 TableConfig (com.actiontech.dble.config.model.TableConfig)4 RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)2 ColumnRoutePair (com.actiontech.dble.sqlengine.mpp.ColumnRoutePair)2 RuleConfig (com.actiontech.dble.config.model.rule.RuleConfig)1 ConfigException (com.actiontech.dble.config.util.ConfigException)1 ReplaceTemp (com.actiontech.dble.route.parser.druid.ReplaceTemp)1 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)1 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)1 ValuesClause (com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)1