Search in sources :

Example 6 with RuleConfig

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

the class RuleFunctionSuitTableTest method testPartitionByRangeMod.

@Test
public void testPartitionByRangeMod() {
    PartitionByRangeMod partition = new PartitionByRangeMod();
    partition.setMapFile("partition-range-mod.txt");
    partition.init();
    // partition = 20
    Assert.assertEquals(20, partition.getPartitionNum());
    RuleConfig rule = new RuleConfig("id", "partition-range-mod");
    rule.setRuleAlgorithm(partition);
    TableConfig tableConf = new TableConfig("test", "id", true, false, -1, "dn$1-10", null, rule, true, null, false, null, null, null);
    int suit1 = partition.suitableFor(tableConf);
    Assert.assertEquals(-1, suit1);
    tableConf.getDataNodes().clear();
    String[] dataNodes = SplitUtil.split("dn$1-20", ',', '$', '-');
    tableConf.getDataNodes().addAll(Arrays.asList(dataNodes));
    int suit2 = partition.suitableFor(tableConf);
    Assert.assertEquals(0, suit2);
    tableConf.getDataNodes().clear();
    dataNodes = SplitUtil.split("dn$1-30", ',', '$', '-');
    tableConf.getDataNodes().addAll(Arrays.asList(dataNodes));
    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)

Example 7 with RuleConfig

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

the class RuleFunctionSuitTableTest method testAutoPartitionByLong.

@Test
public void testAutoPartitionByLong() {
    AutoPartitionByLong autoPartition = new AutoPartitionByLong();
    autoPartition.setMapFile("autopartition-long.txt");
    // partition = 3
    autoPartition.init();
    Assert.assertEquals(3, autoPartition.getPartitionNum());
    RuleConfig rule = new RuleConfig("id", "auto-partition-long");
    rule.setRuleAlgorithm(autoPartition);
    TableConfig tableConf = new TableConfig("test", "id", true, false, -1, "dn1,dn2", null, rule, true, null, false, null, null, null);
    int suit1 = autoPartition.suitableFor(tableConf);
    Assert.assertEquals(-1, suit1);
    tableConf.getDataNodes().clear();
    tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2", "dn3"));
    int suit2 = autoPartition.suitableFor(tableConf);
    Assert.assertEquals(0, suit2);
    tableConf.getDataNodes().clear();
    tableConf.getDataNodes().addAll(Arrays.asList("dn1", "dn2", "dn3", "dn4"));
    int suit3 = autoPartition.suitableFor(tableConf);
    Assert.assertEquals(1, suit3);
    /* 
		 * autopartition-long-dupl.txt
		 * 0-1000=0
		 * 1001-2000=1
		 * 2001-3000=0
		 * 3001-4000=1
		*/
    AutoPartitionByLong autoPartition2 = new AutoPartitionByLong();
    autoPartition2.setMapFile("autopartition-long-dupl.txt");
    autoPartition2.init();
    Assert.assertEquals(2, autoPartition2.getPartitionNum());
    RuleConfig rule2 = new RuleConfig("id", "auto-partition-long-dupl");
    rule2.setRuleAlgorithm(autoPartition2);
    TableConfig tableConf2 = new TableConfig("test2", "id", true, false, -1, "dn1,dn2", null, rule, true, null, false, null, null, null);
    Assert.assertEquals(0, autoPartition2.suitableFor(tableConf2));
    Assert.assertEquals(0, autoPartition2.calculate("500").intValue());
    Assert.assertEquals(1, autoPartition2.calculate("1500").intValue());
    Assert.assertEquals(1, autoPartition2.calculate("2000").intValue());
    Assert.assertEquals(0, autoPartition2.calculate("3000").intValue());
    Assert.assertEquals(1, autoPartition2.calculate("3001").intValue());
}
Also used : TableConfig(io.mycat.config.model.TableConfig) RuleConfig(io.mycat.config.model.rule.RuleConfig) Test(org.junit.Test)

Example 8 with RuleConfig

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

the class RuleFunctionSuitTableTest method testPartitionByPrefixPattern.

@Test
public void testPartitionByPrefixPattern() {
    PartitionByPrefixPattern partition = new PartitionByPrefixPattern();
    partition.setMapFile("partition_prefix_pattern.txt");
    partition.init();
    /*
		 * partition_prefix_pattern.txt
		 * 1-4=0
         * 5-8=1
         * 9-12=2
         * 13-16=3
         * 17-20=4
	     * 21-24=5
         * 25-28=6
         * 29-32=7
         * 0-0=7
		 */
    Assert.assertEquals(8, partition.getPartitionNum());
    RuleConfig rule = new RuleConfig("id", "partition-prefix-pattern");
    rule.setRuleAlgorithm(partition);
    TableConfig tableConf = new TableConfig("test", "id", true, false, -1, "dn1,dn2", null, rule, true, null, false, null, null, null);
    int suit1 = partition.suitableFor(tableConf);
    Assert.assertEquals(-1, suit1);
    tableConf.getDataNodes().clear();
    String[] dataNodes = SplitUtil.split("dn$1-8", ',', '$', '-');
    tableConf.getDataNodes().addAll(Arrays.asList(dataNodes));
    int suit2 = partition.suitableFor(tableConf);
    Assert.assertEquals(0, suit2);
    tableConf.getDataNodes().clear();
    dataNodes = SplitUtil.split("dn$1-10", ',', '$', '-');
    tableConf.getDataNodes().addAll(Arrays.asList(dataNodes));
    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)

Example 9 with RuleConfig

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

the class XMLRuleLoader method loadTableRules.

/**
	 * tableRule标签结构:
	 * <tableRule name="sharding-by-month">
	 *     <rule>
	 *         <columns>create_date</columns>
	 *         <algorithm>partbymonth</algorithm>
	 *     </rule>
	 * </tableRule>
	 * @param root
	 * @throws SQLSyntaxErrorException
     */
private void loadTableRules(Element root) throws SQLSyntaxErrorException {
    //获取每个tableRule标签
    NodeList list = root.getElementsByTagName("tableRule");
    for (int i = 0, n = list.getLength(); i < n; ++i) {
        Node node = list.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            //先判断是否重复
            String name = e.getAttribute("name");
            if (tableRules.containsKey(name)) {
                throw new ConfigException("table rule " + name + " duplicated!");
            }
            //获取rule标签
            NodeList ruleNodes = e.getElementsByTagName("rule");
            int length = ruleNodes.getLength();
            if (length > 1) {
                throw new ConfigException("only one rule can defined :" + name);
            }
            //目前只处理第一个,未来可能有多列复合逻辑需求
            //RuleConfig是保存着rule与function对应关系的对象
            RuleConfig rule = loadRule((Element) ruleNodes.item(0));
            String funName = rule.getFunctionName();
            //判断function是否存在,获取function
            AbstractPartitionAlgorithm func = functions.get(funName);
            if (func == null) {
                throw new ConfigException("can't find function of name :" + funName);
            }
            rule.setRuleAlgorithm(func);
            //保存到tableRules
            tableRules.put(name, new TableRuleConfig(name, rule));
        }
    }
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ConfigException(io.mycat.config.util.ConfigException) TableRuleConfig(io.mycat.config.model.rule.TableRuleConfig) RuleConfig(io.mycat.config.model.rule.RuleConfig) TableRuleConfig(io.mycat.config.model.rule.TableRuleConfig)

Example 10 with RuleConfig

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

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)

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