use of io.mycat.migrate.MigrateTask in project Mycat_plus by coderczp.
the class MigrateUtilsTest method balanceExpand.
@Test
public void balanceExpand() {
String table = "test";
Map<Integer, List<Range>> integerListMap = new TreeMap<>();
integerListMap.put(0, Lists.newArrayList(new Range(0, 32)));
integerListMap.put(1, Lists.newArrayList(new Range(33, 65)));
integerListMap.put(2, Lists.newArrayList(new Range(66, 99)));
pringList("beforse balance :", integerListMap);
// dn1=0-32 dn2=33-65 dn3=66-99
int totalSlots = 100;
List<String> oldDataNodes = Lists.newArrayList("dn1", "dn2", "dn3");
List<String> newDataNodes = Lists.newArrayList("dn4", "dn5");
Map<String, List<MigrateTask>> tasks = MigrateUtils.balanceExpand(table, integerListMap, oldDataNodes, newDataNodes, totalSlots);
for (Map.Entry<String, List<MigrateTask>> stringListEntry : tasks.entrySet()) {
String key = stringListEntry.getKey();
List<Range> rangeList = new ArrayList<>();
List<MigrateTask> value = stringListEntry.getValue();
for (MigrateTask task : value) {
rangeList.addAll(task.getSlots());
}
Assert.assertEquals(true, value.size() == 2);
if ("dn4".equals(key)) {
Assert.assertEquals(0, rangeList.get(0).start);
Assert.assertEquals(12, rangeList.get(0).end);
Assert.assertEquals(33, rangeList.get(1).start);
Assert.assertEquals(39, rangeList.get(1).end);
} else if ("dn5".equals(key)) {
Assert.assertEquals(40, rangeList.get(0).start);
Assert.assertEquals(45, rangeList.get(0).end);
Assert.assertEquals(66, rangeList.get(1).start);
Assert.assertEquals(79, rangeList.get(1).end);
}
integerListMap.put(Integer.parseInt(key.substring(2)) - 1, rangeList);
}
pringList("after balance :", integerListMap);
System.out.println("agin balance .....................");
oldDataNodes = Lists.newArrayList("dn1", "dn2", "dn3", "dn4", "dn5");
newDataNodes = Lists.newArrayList("dn6", "dn7", "dn8", "dn9");
Map<String, List<MigrateTask>> tasks1 = MigrateUtils.balanceExpand(table, integerListMap, oldDataNodes, newDataNodes, totalSlots);
for (Map.Entry<String, List<MigrateTask>> stringListEntry : tasks1.entrySet()) {
String key = stringListEntry.getKey();
List<Range> rangeList = new ArrayList<>();
List<MigrateTask> value = stringListEntry.getValue();
for (MigrateTask task : value) {
rangeList.addAll(task.getSlots());
}
if ("dn6".equals(key)) {
Assert.assertEquals(13, rangeList.get(0).start);
Assert.assertEquals(21, rangeList.get(0).end);
Assert.assertEquals(46, rangeList.get(1).start);
Assert.assertEquals(48, rangeList.get(1).end);
} else if ("dn7".equals(key)) {
Assert.assertEquals(49, rangeList.get(0).start);
Assert.assertEquals(54, rangeList.get(0).end);
Assert.assertEquals(80, rangeList.get(1).start);
Assert.assertEquals(84, rangeList.get(1).end);
} else if ("dn8".equals(key)) {
Assert.assertEquals(85, rangeList.get(0).start);
Assert.assertEquals(88, rangeList.get(0).end);
Assert.assertEquals(0, rangeList.get(1).start);
Assert.assertEquals(6, rangeList.get(1).end);
} else if ("dn9".equals(key)) {
Assert.assertEquals(7, rangeList.get(0).start);
Assert.assertEquals(8, rangeList.get(0).end);
Assert.assertEquals(40, rangeList.get(1).start);
Assert.assertEquals(45, rangeList.get(1).end);
}
integerListMap.put(Integer.parseInt(key.substring(2)) - 1, rangeList);
}
pringList("agin balance :", integerListMap);
oldDataNodes = Lists.newArrayList("dn1", "dn2", "dn3", "dn4", "dn5", "dn6", "dn7", "dn8", "dn9");
newDataNodes = Lists.newArrayList("dn10", "dn11", "dn12", "dn13", "dn14", "dn15", "dn16", "dn17", "dn18", "dn19", "dn20", "dn21", "dn22", "dn23", "dn24", "dn25", "dn26", "dn27", "dn28", "dn29", "dn30", "dn31", "dn32", "dn33", "dn34", "dn35", "dn36", "dn37", "dn38", "dn39", "dn40", "dn41", "dn42", "dn43", "dn44", "dn45", "dn46", "dn47", "dn48", "dn49", "dn50", "dn51", "dn52", "dn53", "dn54", "dn55", "dn56", "dn57", "dn58", "dn59", "dn60", "dn61", "dn62", "dn63", "dn64", "dn65", "dn66", "dn67", "dn68", "dn69", "dn70", "dn71", "dn72", "dn73", "dn74", "dn75", "dn76", "dn77", "dn78", "dn79", "dn80", "dn81", "dn82", "dn83", "dn84", "dn85", "dn86", "dn87", "dn88", "dn89", "dn90", "dn91", "dn92", "dn93", "dn94", "dn95", "dn96", "dn97", "dn98", "dn99", "dn100");
Map<String, List<MigrateTask>> tasks2 = MigrateUtils.balanceExpand(table, integerListMap, oldDataNodes, newDataNodes, totalSlots);
for (Map.Entry<String, List<MigrateTask>> stringListEntry : tasks2.entrySet()) {
String key = stringListEntry.getKey();
List<Range> rangeList = new ArrayList<>();
List<MigrateTask> value = stringListEntry.getValue();
for (MigrateTask task : value) {
rangeList.addAll(task.getSlots());
}
if ("dn10".equals(key)) {
Assert.assertEquals(22, rangeList.get(0).start);
Assert.assertEquals(22, rangeList.get(0).end);
} else if ("dn100".equals(key)) {
Assert.assertEquals(67, rangeList.get(0).start);
Assert.assertEquals(67, rangeList.get(0).end);
} else if ("dn50".equals(key)) {
Assert.assertEquals(69, rangeList.get(0).start);
Assert.assertEquals(69, rangeList.get(0).end);
} else if ("dn99".equals(key)) {
Assert.assertEquals(66, rangeList.get(0).start);
Assert.assertEquals(66, rangeList.get(0).end);
}
integerListMap.put(Integer.parseInt(key.substring(2)) - 1, rangeList);
}
pringList("agin agin balance :", integerListMap);
}
use of io.mycat.migrate.MigrateTask in project Mycat_plus by coderczp.
the class MigrateUtilsTest method balanceExpand1.
@Test
public void balanceExpand1() {
String table = "test1";
// 4=81920-102399
// 3=61440-81919
// 2=40960-61439
// 1=20480-40959
// 0=0-20479
Map<Integer, List<Range>> integerListMap = new TreeMap<>();
integerListMap.put(0, Lists.newArrayList(new Range(0, 20479)));
integerListMap.put(1, Lists.newArrayList(new Range(20480, 40959)));
integerListMap.put(2, Lists.newArrayList(new Range(40960, 61439)));
integerListMap.put(3, Lists.newArrayList(new Range(61440, 81919)));
integerListMap.put(4, Lists.newArrayList(new Range(81920, 102399)));
pringList("beforse balance :", integerListMap);
// dn1=0-32 dn2=33-65 dn3=66-99
int totalSlots = 102400;
List<String> oldDataNodes = Lists.newArrayList("dn1", "dn2", "dn3", "dn4", "dn5");
List<String> newDataNodes = Lists.newArrayList("dn6", "dn7", "dn8", "dn9", "dn10");
Map<String, List<MigrateTask>> tasks = MigrateUtils.balanceExpand(table, integerListMap, oldDataNodes, newDataNodes, totalSlots);
List<MigrateTask> allTaskList = new ArrayList<>();
for (Map.Entry<String, List<MigrateTask>> stringListEntry : tasks.entrySet()) {
String key = stringListEntry.getKey();
List<Range> rangeList = new ArrayList<>();
List<MigrateTask> value = stringListEntry.getValue();
allTaskList.addAll(value);
for (MigrateTask task : value) {
rangeList.addAll(task.getSlots());
}
integerListMap.put(Integer.parseInt(key.substring(2)) - 1, rangeList);
}
pringList("after balance :", integerListMap);
List<String> allNewDataNodes = new ArrayList<>();
allNewDataNodes.addAll(oldDataNodes);
allNewDataNodes.addAll(newDataNodes);
Properties prop = new Properties();
prop.put("0", "0-20479");
prop.put("1", "20480-40959");
prop.put("2", "40960-61439");
prop.put("3", "61440-81919");
prop.put("4", "81920-102399");
for (MigrateTask migrateTask : allTaskList) {
modifyRuleData(prop, migrateTask, allNewDataNodes);
}
System.out.println();
}
use of io.mycat.migrate.MigrateTask in project Mycat-Server by MyCATApache.
the class MigrateHandler method handle.
public static void handle(String stmt, ServerConnection c) {
Map<String, String> map = parse(stmt);
String table = map.get("table");
String add = map.get("add");
if (table == null) {
writeErrMessage(c, "table cannot be null");
return;
}
if (add == null) {
writeErrMessage(c, "add cannot be null");
return;
}
String taskID = getUUID();
try {
SchemaConfig schemaConfig = MycatServer.getInstance().getConfig().getSchemas().get(c.getSchema());
TableConfig tableConfig = schemaConfig.getTables().get(table.toUpperCase());
AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
if (!(algorithm instanceof PartitionByCRC32PreSlot)) {
writeErrMessage(c, "table: " + table + " rule is not be PartitionByCRC32PreSlot");
return;
}
Map<Integer, List<Range>> integerListMap = ((PartitionByCRC32PreSlot) algorithm).getRangeMap();
integerListMap = (Map<Integer, List<Range>>) ObjectUtil.copyObject(integerListMap);
ArrayList<String> oldDataNodes = tableConfig.getDataNodes();
List<String> newDataNodes = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(add);
Map<String, List<MigrateTask>> tasks = MigrateUtils.balanceExpand(table, integerListMap, oldDataNodes, newDataNodes, PartitionByCRC32PreSlot.DEFAULT_SLOTS_NUM);
CuratorTransactionFinal transactionFinal = null;
String taskBase = ZKUtils.getZKBasePath() + "migrate/" + c.getSchema();
String taskPath = taskBase + "/" + taskID;
CuratorFramework client = ZKUtils.getConnection();
// 校验 之前同一个表的迁移任务未完成,则jzhi禁止继续
if (client.checkExists().forPath(taskBase) != null) {
List<String> childTaskList = client.getChildren().forPath(taskBase);
for (String child : childTaskList) {
TaskNode taskNode = JSON.parseObject(ZKUtils.getConnection().getData().forPath(taskBase + "/" + child), TaskNode.class);
if (taskNode.getSchema().equalsIgnoreCase(c.getSchema()) && table.equalsIgnoreCase(taskNode.getTable()) && taskNode.getStatus() < 5) {
writeErrMessage(c, "table: " + table + " previous migrate task is still running,on the same time one table only one task");
return;
}
}
}
client.create().creatingParentsIfNeeded().forPath(taskPath);
TaskNode taskNode = new TaskNode();
taskNode.setSchema(c.getSchema());
taskNode.setSql(stmt);
taskNode.setTable(table);
taskNode.setAdd(add);
taskNode.setStatus(0);
Map<String, Integer> fromNodeSlaveIdMap = new HashMap<>();
List<MigrateTask> allTaskList = new ArrayList<>();
for (Map.Entry<String, List<MigrateTask>> entry : tasks.entrySet()) {
String key = entry.getKey();
List<MigrateTask> value = entry.getValue();
for (MigrateTask migrateTask : value) {
migrateTask.setSchema(c.getSchema());
// 分配slaveid只需要一个dataHost分配一个即可,后续任务执行模拟从节点只需要一个dataHost一个
String dataHost = getDataHostNameFromNode(migrateTask.getFrom());
if (fromNodeSlaveIdMap.containsKey(dataHost)) {
migrateTask.setSlaveId(fromNodeSlaveIdMap.get(dataHost));
} else {
migrateTask.setSlaveId(getSlaveIdFromZKForDataNode(migrateTask.getFrom()));
fromNodeSlaveIdMap.put(dataHost, migrateTask.getSlaveId());
}
}
allTaskList.addAll(value);
}
transactionFinal = client.inTransaction().setData().forPath(taskPath, JSON.toJSONBytes(taskNode)).and();
// 合并成dataHost级别任务
Map<String, List<MigrateTask>> dataHostMigrateMap = mergerTaskForDataHost(allTaskList);
for (Map.Entry<String, List<MigrateTask>> entry : dataHostMigrateMap.entrySet()) {
String key = entry.getKey();
List<MigrateTask> value = entry.getValue();
String path = taskPath + "/" + key;
transactionFinal = transactionFinal.create().forPath(path, JSON.toJSONBytes(value)).and();
}
transactionFinal.commit();
} catch (Exception e) {
LOGGER.error("migrate error", e);
writeErrMessage(c, "migrate error:" + e);
return;
}
writePackToClient(c, taskID);
LOGGER.info("task start", new Date());
}
use of io.mycat.migrate.MigrateTask in project Mycat-Server by MyCATApache.
the class MigrateUtilsTest method balanceExpand.
@Test
public void balanceExpand() {
String table = "test";
Map<Integer, List<Range>> integerListMap = new TreeMap<>();
integerListMap.put(0, Lists.newArrayList(new Range(0, 32)));
integerListMap.put(1, Lists.newArrayList(new Range(33, 65)));
integerListMap.put(2, Lists.newArrayList(new Range(66, 99)));
pringList("beforse balance :", integerListMap);
// dn1=0-32 dn2=33-65 dn3=66-99
int totalSlots = 100;
List<String> oldDataNodes = Lists.newArrayList("dn1", "dn2", "dn3");
List<String> newDataNodes = Lists.newArrayList("dn4", "dn5");
Map<String, List<MigrateTask>> tasks = MigrateUtils.balanceExpand(table, integerListMap, oldDataNodes, newDataNodes, totalSlots);
for (Map.Entry<String, List<MigrateTask>> stringListEntry : tasks.entrySet()) {
String key = stringListEntry.getKey();
List<Range> rangeList = new ArrayList<>();
List<MigrateTask> value = stringListEntry.getValue();
for (MigrateTask task : value) {
rangeList.addAll(task.getSlots());
}
Assert.assertEquals(true, value.size() == 2);
if ("dn4".equals(key)) {
Assert.assertEquals(0, rangeList.get(0).start);
Assert.assertEquals(12, rangeList.get(0).end);
Assert.assertEquals(33, rangeList.get(1).start);
Assert.assertEquals(39, rangeList.get(1).end);
} else if ("dn5".equals(key)) {
Assert.assertEquals(40, rangeList.get(0).start);
Assert.assertEquals(45, rangeList.get(0).end);
Assert.assertEquals(66, rangeList.get(1).start);
Assert.assertEquals(79, rangeList.get(1).end);
}
integerListMap.put(Integer.parseInt(key.substring(2)) - 1, rangeList);
}
pringList("after balance :", integerListMap);
System.out.println("agin balance .....................");
oldDataNodes = Lists.newArrayList("dn1", "dn2", "dn3", "dn4", "dn5");
newDataNodes = Lists.newArrayList("dn6", "dn7", "dn8", "dn9");
Map<String, List<MigrateTask>> tasks1 = MigrateUtils.balanceExpand(table, integerListMap, oldDataNodes, newDataNodes, totalSlots);
for (Map.Entry<String, List<MigrateTask>> stringListEntry : tasks1.entrySet()) {
String key = stringListEntry.getKey();
List<Range> rangeList = new ArrayList<>();
List<MigrateTask> value = stringListEntry.getValue();
for (MigrateTask task : value) {
rangeList.addAll(task.getSlots());
}
if ("dn6".equals(key)) {
Assert.assertEquals(13, rangeList.get(0).start);
Assert.assertEquals(21, rangeList.get(0).end);
Assert.assertEquals(46, rangeList.get(1).start);
Assert.assertEquals(48, rangeList.get(1).end);
} else if ("dn7".equals(key)) {
Assert.assertEquals(49, rangeList.get(0).start);
Assert.assertEquals(54, rangeList.get(0).end);
Assert.assertEquals(80, rangeList.get(1).start);
Assert.assertEquals(84, rangeList.get(1).end);
} else if ("dn8".equals(key)) {
Assert.assertEquals(85, rangeList.get(0).start);
Assert.assertEquals(88, rangeList.get(0).end);
Assert.assertEquals(0, rangeList.get(1).start);
Assert.assertEquals(6, rangeList.get(1).end);
} else if ("dn9".equals(key)) {
Assert.assertEquals(7, rangeList.get(0).start);
Assert.assertEquals(8, rangeList.get(0).end);
Assert.assertEquals(40, rangeList.get(1).start);
Assert.assertEquals(45, rangeList.get(1).end);
}
integerListMap.put(Integer.parseInt(key.substring(2)) - 1, rangeList);
}
pringList("agin balance :", integerListMap);
oldDataNodes = Lists.newArrayList("dn1", "dn2", "dn3", "dn4", "dn5", "dn6", "dn7", "dn8", "dn9");
newDataNodes = Lists.newArrayList("dn10", "dn11", "dn12", "dn13", "dn14", "dn15", "dn16", "dn17", "dn18", "dn19", "dn20", "dn21", "dn22", "dn23", "dn24", "dn25", "dn26", "dn27", "dn28", "dn29", "dn30", "dn31", "dn32", "dn33", "dn34", "dn35", "dn36", "dn37", "dn38", "dn39", "dn40", "dn41", "dn42", "dn43", "dn44", "dn45", "dn46", "dn47", "dn48", "dn49", "dn50", "dn51", "dn52", "dn53", "dn54", "dn55", "dn56", "dn57", "dn58", "dn59", "dn60", "dn61", "dn62", "dn63", "dn64", "dn65", "dn66", "dn67", "dn68", "dn69", "dn70", "dn71", "dn72", "dn73", "dn74", "dn75", "dn76", "dn77", "dn78", "dn79", "dn80", "dn81", "dn82", "dn83", "dn84", "dn85", "dn86", "dn87", "dn88", "dn89", "dn90", "dn91", "dn92", "dn93", "dn94", "dn95", "dn96", "dn97", "dn98", "dn99", "dn100");
Map<String, List<MigrateTask>> tasks2 = MigrateUtils.balanceExpand(table, integerListMap, oldDataNodes, newDataNodes, totalSlots);
for (Map.Entry<String, List<MigrateTask>> stringListEntry : tasks2.entrySet()) {
String key = stringListEntry.getKey();
List<Range> rangeList = new ArrayList<>();
List<MigrateTask> value = stringListEntry.getValue();
for (MigrateTask task : value) {
rangeList.addAll(task.getSlots());
}
if ("dn10".equals(key)) {
Assert.assertEquals(22, rangeList.get(0).start);
Assert.assertEquals(22, rangeList.get(0).end);
} else if ("dn100".equals(key)) {
Assert.assertEquals(67, rangeList.get(0).start);
Assert.assertEquals(67, rangeList.get(0).end);
} else if ("dn50".equals(key)) {
Assert.assertEquals(69, rangeList.get(0).start);
Assert.assertEquals(69, rangeList.get(0).end);
} else if ("dn99".equals(key)) {
Assert.assertEquals(66, rangeList.get(0).start);
Assert.assertEquals(66, rangeList.get(0).end);
}
integerListMap.put(Integer.parseInt(key.substring(2)) - 1, rangeList);
}
pringList("agin agin balance :", integerListMap);
}
use of io.mycat.migrate.MigrateTask in project Mycat_plus by coderczp.
the class MigrateHandler method handle.
public static void handle(String stmt, ServerConnection c) {
Map<String, String> map = parse(stmt);
String table = map.get("table");
String add = map.get("add");
if (table == null) {
writeErrMessage(c, "table cannot be null");
return;
}
if (add == null) {
writeErrMessage(c, "add cannot be null");
return;
}
String taskID = getUUID();
try {
SchemaConfig schemaConfig = MycatServer.getInstance().getConfig().getSchemas().get(c.getSchema());
TableConfig tableConfig = schemaConfig.getTables().get(table.toUpperCase());
AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
if (!(algorithm instanceof PartitionByCRC32PreSlot)) {
writeErrMessage(c, "table: " + table + " rule is not be PartitionByCRC32PreSlot");
return;
}
Map<Integer, List<Range>> integerListMap = ((PartitionByCRC32PreSlot) algorithm).getRangeMap();
integerListMap = (Map<Integer, List<Range>>) ObjectUtil.copyObject(integerListMap);
ArrayList<String> oldDataNodes = tableConfig.getDataNodes();
List<String> newDataNodes = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(add);
Map<String, List<MigrateTask>> tasks = MigrateUtils.balanceExpand(table, integerListMap, oldDataNodes, newDataNodes, PartitionByCRC32PreSlot.DEFAULT_SLOTS_NUM);
CuratorTransactionFinal transactionFinal = null;
String taskBase = ZKUtils.getZKBasePath() + "migrate/" + c.getSchema();
String taskPath = taskBase + "/" + taskID;
CuratorFramework client = ZKUtils.getConnection();
// 校验 之前同一个表的迁移任务未完成,则jzhi禁止继续
if (client.checkExists().forPath(taskBase) != null) {
List<String> childTaskList = client.getChildren().forPath(taskBase);
for (String child : childTaskList) {
TaskNode taskNode = JSON.parseObject(ZKUtils.getConnection().getData().forPath(taskBase + "/" + child), TaskNode.class);
if (taskNode.getSchema().equalsIgnoreCase(c.getSchema()) && table.equalsIgnoreCase(taskNode.getTable()) && taskNode.getStatus() < 5) {
writeErrMessage(c, "table: " + table + " previous migrate task is still running,on the same time one table only one task");
return;
}
}
}
client.create().creatingParentsIfNeeded().forPath(taskPath);
TaskNode taskNode = new TaskNode();
taskNode.setSchema(c.getSchema());
taskNode.setSql(stmt);
taskNode.setTable(table);
taskNode.setAdd(add);
taskNode.setStatus(0);
Map<String, Integer> fromNodeSlaveIdMap = new HashMap<>();
List<MigrateTask> allTaskList = new ArrayList<>();
for (Map.Entry<String, List<MigrateTask>> entry : tasks.entrySet()) {
String key = entry.getKey();
List<MigrateTask> value = entry.getValue();
for (MigrateTask migrateTask : value) {
migrateTask.setSchema(c.getSchema());
// 分配slaveid只需要一个dataHost分配一个即可,后续任务执行模拟从节点只需要一个dataHost一个
String dataHost = getDataHostNameFromNode(migrateTask.getFrom());
if (fromNodeSlaveIdMap.containsKey(dataHost)) {
migrateTask.setSlaveId(fromNodeSlaveIdMap.get(dataHost));
} else {
migrateTask.setSlaveId(getSlaveIdFromZKForDataNode(migrateTask.getFrom()));
fromNodeSlaveIdMap.put(dataHost, migrateTask.getSlaveId());
}
}
allTaskList.addAll(value);
}
transactionFinal = client.inTransaction().setData().forPath(taskPath, JSON.toJSONBytes(taskNode)).and();
// 合并成dataHost级别任务
Map<String, List<MigrateTask>> dataHostMigrateMap = mergerTaskForDataHost(allTaskList);
for (Map.Entry<String, List<MigrateTask>> entry : dataHostMigrateMap.entrySet()) {
String key = entry.getKey();
List<MigrateTask> value = entry.getValue();
String path = taskPath + "/" + key;
transactionFinal = transactionFinal.create().forPath(path, JSON.toJSONBytes(value)).and();
}
transactionFinal.commit();
} catch (Exception e) {
LOGGER.error("migrate error", e);
writeErrMessage(c, "migrate error:" + e);
return;
}
writePackToClient(c, taskID);
LOGGER.info("task start", new Date());
}
Aggregations