use of io.mycat.route.function.PartitionByCRC32PreSlot 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.route.function.PartitionByCRC32PreSlot 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