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);
}
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);
}
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);
}
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);
}
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) {
}
}
}
}
Aggregations