Search in sources :

Example 1 with TaskNode

use of io.mycat.migrate.TaskNode 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());
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) TaskNode(io.mycat.migrate.TaskNode) SchemaConfig(io.mycat.config.model.SchemaConfig) MigrateTask(io.mycat.migrate.MigrateTask) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) TableConfig(io.mycat.config.model.TableConfig) PartitionByCRC32PreSlot(io.mycat.route.function.PartitionByCRC32PreSlot)

Example 2 with TaskNode

use of io.mycat.migrate.TaskNode in project Mycat-Server by MyCATApache.

the class MigrateHandler method main.

public static void main(String[] args) {
    String sql = "migrate    -table=test  -add=dn2,dn3,dn4  " + " \n -additional=\"a=b\"";
    Map map = parse(sql);
    System.out.println();
    for (int i = 0; i < 100; i++) {
        System.out.println(i % 5);
    }
    TaskNode taskNode = new TaskNode();
    taskNode.setSql(sql);
    System.out.println(new String(JSON.toJSONBytes(taskNode)));
}
Also used : TaskNode(io.mycat.migrate.TaskNode)

Aggregations

TaskNode (io.mycat.migrate.TaskNode)2 SchemaConfig (io.mycat.config.model.SchemaConfig)1 TableConfig (io.mycat.config.model.TableConfig)1 MigrateTask (io.mycat.migrate.MigrateTask)1 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)1 PartitionByCRC32PreSlot (io.mycat.route.function.PartitionByCRC32PreSlot)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 CuratorTransactionFinal (org.apache.curator.framework.api.transaction.CuratorTransactionFinal)1