Search in sources :

Example 71 with TableConfig

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

the class DDLRouteTest method testDDL.

/**
 * ddl deal test
 *
 * @throws Exception
 */
@Test
public void testDDL() throws Exception {
    SchemaConfig schema = schemaMap.get("TESTDB");
    CacheService cacheService = new CacheService();
    RouteService routerService = new RouteService(cacheService);
    // create table/view/function/..
    String sql = " create table company(idd int)";
    sql = RouterUtil.getFixedSql(sql);
    String upsql = sql.toUpperCase();
    // TODO : modify by zhuam
    // 小写表名,需要额外转为 大写 做比较
    String tablename = RouterUtil.getTableName(sql, RouterUtil.getCreateTablePos(upsql, 0));
    tablename = tablename.toUpperCase();
    List<String> dataNodes = new ArrayList<>();
    Map<String, TableConfig> tables = schema.getTables();
    TableConfig tc;
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    int nodeSize = dataNodes.size();
    int rs = ServerParse.parse(sql);
    int sqlType = rs & 0xff;
    RouteResultset rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    // drop table test
    sql = " drop table COMPANY";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getDropTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    // alter table
    sql = "   alter table COMPANY add COLUMN name int ;";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getAlterTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
    // truncate table;
    sql = " truncate table COMPANY";
    sql = RouterUtil.getFixedSql(sql);
    upsql = sql.toUpperCase();
    tablename = RouterUtil.getTableName(sql, RouterUtil.getTruncateTablePos(upsql, 0));
    tables = schema.getTables();
    if (tables != null && (tc = tables.get(tablename)) != null) {
        dataNodes = tc.getDataNodes();
    }
    nodeSize = dataNodes.size();
    rs = ServerParse.parse(sql);
    sqlType = rs & 0xff;
    rrs = routerService.route(new SystemConfig(), schema, sqlType, sql, "UTF-8", null);
    Assert.assertTrue("COMPANY".equals(tablename));
    Assert.assertTrue(rrs.getNodes().length == nodeSize);
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) ArrayList(java.util.ArrayList) TableConfig(io.mycat.config.model.TableConfig) RouteService(io.mycat.route.RouteService) CacheService(io.mycat.cache.CacheService) RouteResultset(io.mycat.route.RouteResultset) Test(org.junit.Test)

Example 72 with TableConfig

use of io.mycat.config.model.TableConfig 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)

Example 73 with TableConfig

use of io.mycat.config.model.TableConfig 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 74 with TableConfig

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

the class ConfigTest method testDynamicYYYYMMTable.

/**
 * 测试 动态日期表
 *
 * @throws Exception
 */
@Test
public void testDynamicYYYYMMTable() throws Exception {
    SchemaConfig sc = this.schemas.get("dbtest1");
    Map<String, TableConfig> tbm = sc.getTables();
    Assert.assertTrue(tbm.size() == 32);
}
Also used : SchemaConfig(io.mycat.config.model.SchemaConfig) TableConfig(io.mycat.config.model.TableConfig) Test(org.junit.Test)

Example 75 with TableConfig

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

the class SwitchCommitListener method checkCommit.

private void checkCommit(PathChildrenCacheEvent event) {
    InterProcessMutex taskLock = null;
    try {
        String path = event.getData().getPath();
        String taskPath = path.substring(0, path.lastIndexOf("/_commit/"));
        String taskID = taskPath.substring(taskPath.lastIndexOf('/') + 1, taskPath.length());
        String lockPath = ZKUtils.getZKBasePath() + "lock/" + taskID + ".lock";
        List<String> sucessDataHost = ZKUtils.getConnection().getChildren().forPath(path.substring(0, path.lastIndexOf('/')));
        String custerName = ZkConfig.getInstance().getValue(ZkParamCfg.ZK_CFG_CLUSTERID);
        ClusterInfo clusterInfo = JSON.parseObject(ZKUtils.getConnection().getData().forPath("/mycat/" + custerName), ClusterInfo.class);
        List<String> clusterNodeList = Splitter.on(',').omitEmptyStrings().splitToList(clusterInfo.getClusterNodes());
        if (sucessDataHost.size() == clusterNodeList.size()) {
            List<String> taskDataHost = ZKUtils.getConnection().getChildren().forPath(taskPath);
            List<MigrateTask> allTaskList = MigrateUtils.queryAllTask(taskPath, taskDataHost);
            taskLock = new InterProcessMutex(ZKUtils.getConnection(), lockPath);
            taskLock.acquire(120, TimeUnit.SECONDS);
            TaskNode taskNode = JSON.parseObject(ZKUtils.getConnection().getData().forPath(taskPath), TaskNode.class);
            if (taskNode.getStatus() == 2) {
                taskNode.setStatus(3);
                // 开始切换 且个节点已经禁止写入并且无原有写入在执行
                try {
                    CuratorTransactionFinal transactionFinal = null;
                    check(taskID, allTaskList);
                    SchemaConfig schemaConfig = MycatServer.getInstance().getConfig().getSchemas().get(taskNode.getSchema());
                    TableConfig tableConfig = schemaConfig.getTables().get(taskNode.getTable().toUpperCase());
                    List<String> newDataNodes = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(taskNode.getAdd());
                    List<String> allNewDataNodes = tableConfig.getDataNodes();
                    allNewDataNodes.addAll(newDataNodes);
                    // 先修改rule config
                    InterProcessMutex ruleLock = new InterProcessMutex(ZKUtils.getConnection(), ZKUtils.getZKBasePath() + "lock/rules.lock");
                    ;
                    try {
                        ruleLock.acquire(30, TimeUnit.SECONDS);
                        transactionFinal = modifyZkRules(transactionFinal, tableConfig.getRule().getFunctionName(), newDataNodes);
                        transactionFinal = modifyTableConfigRules(transactionFinal, taskNode.getSchema(), taskNode.getTable(), newDataNodes);
                    } finally {
                        ruleLock.release();
                    }
                    transactionFinal = modifyRuleData(transactionFinal, allTaskList, tableConfig, allNewDataNodes);
                    transactionFinal.setData().forPath(taskPath, JSON.toJSONBytes(taskNode));
                    clean(taskID, allTaskList);
                    transactionFinal.commit();
                    forceTableRuleToLocal();
                    pushACKToClean(taskPath);
                } catch (Exception e) {
                    // todo 异常to  Zk
                    LOGGER.error("error:", e);
                }
            // todo   清理规则     顺利拉下ruledata保证一定更新到本地
            } else if (taskNode.getStatus() == 3) {
                forceTableRuleToLocal();
                pushACKToClean(taskPath);
            }
        }
    } catch (Exception e) {
        LOGGER.error("error:", e);
    } finally {
        if (taskLock != null) {
            try {
                taskLock.release();
            } catch (Exception ignored) {
            }
        }
    }
}
Also used : ClusterInfo(io.mycat.config.loader.zkprocess.zookeeper.ClusterInfo) SchemaConfig(io.mycat.config.model.SchemaConfig) CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) TableConfig(io.mycat.config.model.TableConfig) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex) SQLException(java.sql.SQLException) IOException(java.io.IOException)

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