Search in sources :

Example 11 with RuleConfig

use of io.mycat.config.model.rule.RuleConfig in project Mycat-Server by MyCATApache.

the class RouterUtil method ruleCalculate.

/**
	 * @return dataNodeIndex -> [partitionKeysValueTuple+]
	 */
public static Set<String> ruleCalculate(TableConfig tc, Set<ColumnRoutePair> colRoutePairSet, Map<String, Integer> dataNodeSlotMap) {
    Set<String> routeNodeSet = new LinkedHashSet<String>();
    String col = tc.getRule().getColumn();
    RuleConfig rule = tc.getRule();
    AbstractPartitionAlgorithm algorithm = rule.getRuleAlgorithm();
    for (ColumnRoutePair colPair : colRoutePairSet) {
        if (colPair.colValue != null) {
            Integer nodeIndx = algorithm.calculate(colPair.colValue);
            if (nodeIndx == null) {
                throw new IllegalArgumentException("can't find datanode for sharding column:" + col + " val:" + colPair.colValue);
            } else {
                String dataNode = tc.getDataNodes().get(nodeIndx);
                routeNodeSet.add(dataNode);
                if (algorithm instanceof SlotFunction) {
                    dataNodeSlotMap.put(dataNode, ((SlotFunction) algorithm).slotValue());
                }
                colPair.setNodeId(nodeIndx);
            }
        } else if (colPair.rangeValue != null) {
            Integer[] nodeRange = algorithm.calculateRange(String.valueOf(colPair.rangeValue.beginValue), String.valueOf(colPair.rangeValue.endValue));
            if (nodeRange != null) {
                /**
					 * 不能确认 colPair的 nodeid是否会有其它影响
					 */
                if (nodeRange.length == 0) {
                    routeNodeSet.addAll(tc.getDataNodes());
                } else {
                    ArrayList<String> dataNodes = tc.getDataNodes();
                    String dataNode = null;
                    for (Integer nodeId : nodeRange) {
                        dataNode = dataNodes.get(nodeId);
                        if (algorithm instanceof SlotFunction) {
                            dataNodeSlotMap.put(dataNode, ((SlotFunction) algorithm).slotValue());
                        }
                        routeNodeSet.add(dataNode);
                    }
                }
            }
        }
    }
    return routeNodeSet;
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) ColumnRoutePair(io.mycat.sqlengine.mpp.ColumnRoutePair) RuleConfig(io.mycat.config.model.rule.RuleConfig) SlotFunction(io.mycat.route.function.SlotFunction)

Example 12 with RuleConfig

use of io.mycat.config.model.rule.RuleConfig in project Mycat-Server by MyCATApache.

the class RuleFunctionSuitTableTest method testPartitionByDate.

@Test
public void testPartitionByDate() {
    PartitionByDate partition = new PartitionByDate();
    partition.setDateFormat("yyyy-MM-dd");
    partition.setsBeginDate("2014-01-01");
    partition.setsEndDate("2014-01-31");
    partition.setsPartionDay("10");
    // partition = 4
    partition.init();
    Assert.assertEquals(4, partition.getPartitionNum());
    RuleConfig rule = new RuleConfig("col_date", "partition-date");
    rule.setRuleAlgorithm(partition);
    TableConfig tableConf = new TableConfig("test", "id", true, false, -1, "dn1,dn2,dn3", null, rule, true, null, false, null, null, null);
    int suit1 = partition.suitableFor(tableConf);
    Assert.assertEquals(-1, suit1);
    tableConf.getDataNodes().clear();
    tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2", "dn3", "dn4"));
    int suit2 = partition.suitableFor(tableConf);
    Assert.assertEquals(0, suit2);
    tableConf.getDataNodes().clear();
    tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2", "dn3", "dn4", "dn5"));
    int suit3 = partition.suitableFor(tableConf);
    Assert.assertEquals(1, suit3);
    PartitionByDate partition1 = new PartitionByDate();
    partition.setDateFormat("yyyy-MM-dd");
    partition.setsBeginDate("2014-01-01");
    partition.setsPartionDay("10");
    // partition no limit
    partition.init();
    int suit4 = partition1.suitableFor(tableConf);
    Assert.assertEquals(0, suit4);
}
Also used : TableConfig(io.mycat.config.model.TableConfig) RuleConfig(io.mycat.config.model.rule.RuleConfig) Test(org.junit.Test)

Example 13 with RuleConfig

use of io.mycat.config.model.rule.RuleConfig in project Mycat-Server by MyCATApache.

the class RuleFunctionSuitTableTest method testPartitionByHashMod.

@Test
public void testPartitionByHashMod() {
    PartitionByHashMod partition = new PartitionByHashMod();
    // partition = 3;
    partition.setCount(3);
    Assert.assertEquals(3, partition.getPartitionNum());
    RuleConfig rule = new RuleConfig("id", "partition-hash-mod");
    rule.setRuleAlgorithm(partition);
    TableConfig tableConf = new TableConfig("test", "id", true, false, -1, "dn1,dn2,dn3", null, rule, true, null, false, null, null, null);
    int suit1 = partition.suitableFor(tableConf);
    Assert.assertEquals(0, suit1);
    tableConf.getDataNodes().clear();
    tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2", "dn3", "dn4"));
    int suit2 = partition.suitableFor(tableConf);
    Assert.assertEquals(1, suit2);
    tableConf.getDataNodes().clear();
    tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2"));
    int suit3 = partition.suitableFor(tableConf);
    Assert.assertEquals(-1, suit3);
}
Also used : TableConfig(io.mycat.config.model.TableConfig) RuleConfig(io.mycat.config.model.rule.RuleConfig) Test(org.junit.Test)

Aggregations

RuleConfig (io.mycat.config.model.rule.RuleConfig)13 TableConfig (io.mycat.config.model.TableConfig)7 Test (org.junit.Test)5 ConfigException (io.mycat.config.util.ConfigException)4 TableRuleConfig (io.mycat.config.model.rule.TableRuleConfig)3 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)3 Element (org.w3c.dom.Element)3 SlotFunction (io.mycat.route.function.SlotFunction)2 NodeList (org.w3c.dom.NodeList)2 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)1 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)1 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)1 SchemaConfig (io.mycat.config.model.SchemaConfig)1 TableConfigMap (io.mycat.config.model.TableConfigMap)1 RouteResultset (io.mycat.route.RouteResultset)1 RouteResultsetNode (io.mycat.route.RouteResultsetNode)1 ReloadFunction (io.mycat.route.function.ReloadFunction)1 TableRuleAware (io.mycat.route.function.TableRuleAware)1 DruidParser (io.mycat.route.parser.druid.DruidParser)1