Search in sources :

Example 6 with NodeTask

use of com.alibaba.otter.node.common.config.model.NodeTask in project otter by alibaba.

the class NodeTaskServiceImpl method parseNodeTask.

// 解析一下tasks为NodeTask对象
private List<NodeTask> parseNodeTask(Channel channel) {
    List<NodeTask> tasks = new ArrayList<NodeTask>();
    List<Pipeline> pipelines = channel.getPipelines();
    Long nid = configClientService.currentNode().getId();
    TaskEvent taksEvent = null;
    if (channel.getStatus().isStart()) {
        taksEvent = TaskEvent.CREATE;
    } else if (channel.getStatus().isStop()) {
        taksEvent = TaskEvent.DELETE;
    } else if (channel.getStatus().isPause()) {
        // modify by ljh at 2013-01-31 , pause状态也需要启动setl线程
        // 因为在发布的时候,restart指令不会推送指令,导致setl线程没有启动
        // return tasks;
        taksEvent = TaskEvent.CREATE;
    }
    // 处理当前最新的状态
    for (Pipeline pipeline : pipelines) {
        List<Node> sNodes = pipeline.getSelectNodes();
        for (Node node : sNodes) {
            if (nid.equals(node.getId())) {
                // 判断是否为当前的nid
                NodeTask task = new NodeTask();
                task.setPipeline(pipeline);
                NodeTask matchTask = getMatchTask(tasks, task);
                if (matchTask == null) {
                    matchTask = task;
                    tasks.add(task);
                }
                matchTask.setPipeline(pipeline);
                matchTask.getStage().add(StageType.SELECT);
                matchTask.getEvent().add(taksEvent);
            }
        }
        List<Node> eNodes = pipeline.getExtractNodes();
        for (Node node : eNodes) {
            if (nid.equals(node.getId())) {
                // 判断是否为当前的nid
                NodeTask task = new NodeTask();
                task.setPipeline(pipeline);
                NodeTask matchTask = getMatchTask(tasks, task);
                if (matchTask == null) {
                    matchTask = task;
                    tasks.add(task);
                }
                matchTask.getStage().add(StageType.EXTRACT);
                matchTask.getEvent().add(taksEvent);
            }
        }
        List<Node> tlNodes = pipeline.getLoadNodes();
        for (Node node : tlNodes) {
            if (nid.equals(node.getId())) {
                // 判断是否为当前的nid
                NodeTask task = new NodeTask();
                task.setPipeline(pipeline);
                NodeTask matchTask = getMatchTask(tasks, task);
                if (matchTask == null) {
                    matchTask = task;
                    tasks.add(task);
                }
                matchTask.getStage().add(StageType.TRANSFORM);
                matchTask.getEvent().add(taksEvent);
                matchTask.getStage().add(StageType.LOAD);
                matchTask.getEvent().add(taksEvent);
            }
        }
    }
    List<Long> pipelineIds = Lists.transform(channel.getPipelines(), new Function<Pipeline, Long>() {

        public Long apply(Pipeline input) {
            return input.getId();
        }
    });
    // 合并一下target中特有的记录,取一下反操作,表示要关闭
    for (NodeTask task : allTasks) {
        Pipeline pipeline = task.getPipeline();
        if (pipeline.getChannelId().equals(channel.getId()) && !pipelineIds.contains(pipeline.getId())) {
            // /是同一个channel,但对应的pipeline不在列表里
            // 处理pipeline删除
            NodeTask deletePipelineTask = new NodeTask();
            deletePipelineTask.setPipeline(pipeline);
            List<StageType> stages = task.getStage();
            List<TaskEvent> events = task.getEvent();
            for (int i = 0; i < stages.size(); i++) {
                StageType stage = stages.get(i);
                TaskEvent event = events.get(i);
                if (event.isCreate()) {
                    deletePipelineTask.getStage().add(stage);
                    // 添加为关闭
                    deletePipelineTask.getEvent().add(TaskEvent.DELETE);
                }
            }
            tasks.add(deletePipelineTask);
        }
        if (pipelineIds.contains(pipeline.getId())) {
            // 在当前的channel列表中
            boolean needAdd = false;
            // 找到对应的匹配
            NodeTask matchTask = getMatchTask(tasks, task);
            if (matchTask == null) {
                matchTask = new NodeTask();
                matchTask.setPipeline(pipeline);
                needAdd = true;
            }
            List<StageType> stages = task.getStage();
            List<TaskEvent> events = task.getEvent();
            for (int i = 0; i < stages.size(); i++) {
                StageType stage = stages.get(i);
                TaskEvent event = events.get(i);
                TaskEvent matchEvent = getMatchStage(matchTask, stage);
                if (matchEvent == null && event.isCreate()) {
                    // 对应的stage已经被移除,触发一个DELETE操作
                    matchTask.getStage().add(stage);
                    matchTask.getEvent().add(TaskEvent.DELETE);
                }
            }
            if (needAdd && matchTask.getStage().size() > 0) {
                tasks.add(matchTask);
            }
        }
    }
    // 判断当前的task是否需要全部关闭
    for (NodeTask task : tasks) {
        boolean shutdown = true;
        for (TaskEvent event : task.getEvent()) {
            // task已为当前最新节点信息
            shutdown &= event.isDelete();
        }
        task.setShutdown(shutdown);
    }
    return tasks;
}
Also used : Node(com.alibaba.otter.shared.common.model.config.node.Node) ArrayList(java.util.ArrayList) NodeTask(com.alibaba.otter.node.common.config.model.NodeTask) Pipeline(com.alibaba.otter.shared.common.model.config.pipeline.Pipeline) StageType(com.alibaba.otter.shared.common.model.config.enums.StageType) TaskEvent(com.alibaba.otter.node.common.config.model.NodeTask.TaskEvent) FindTaskEvent(com.alibaba.otter.shared.communication.model.config.FindTaskEvent)

Example 7 with NodeTask

use of com.alibaba.otter.node.common.config.model.NodeTask in project otter by alibaba.

the class OtterController method process.

public boolean process(List<NodeTask> nodeTasks) {
    if (nodeTasks == null || nodeTasks.isEmpty()) {
        return true;
    }
    for (NodeTask nodeTask : nodeTasks) {
        boolean shutdown = nodeTask.isShutdown();
        Long pipelineId = nodeTask.getPipeline().getId();
        if (shutdown) {
            Map<StageType, GlobalTask> tasks = controllers.remove(pipelineId);
            if (tasks != null) {
                logger.info("INFO ## shutdown this pipeline sync ,the pipelineId = {} and tasks = {}", pipelineId, tasks.keySet());
                stopPipeline(pipelineId, tasks);
            } else {
                logger.info("INFO ## this pipeline id = {} is not start sync", pipelineId);
            }
        } else {
            startPipeline(nodeTask);
        }
    }
    return true;
}
Also used : StageType(com.alibaba.otter.shared.common.model.config.enums.StageType) GlobalTask(com.alibaba.otter.node.etl.common.task.GlobalTask) NodeTask(com.alibaba.otter.node.common.config.model.NodeTask)

Aggregations

NodeTask (com.alibaba.otter.node.common.config.model.NodeTask)7 StageType (com.alibaba.otter.shared.common.model.config.enums.StageType)3 Node (com.alibaba.otter.shared.common.model.config.node.Node)3 Pipeline (com.alibaba.otter.shared.common.model.config.pipeline.Pipeline)3 ArrayList (java.util.ArrayList)3 BaseOtterTest (com.alibaba.otter.node.common.BaseOtterTest)2 NodeTaskServiceImpl (com.alibaba.otter.node.common.config.impl.NodeTaskServiceImpl)2 TaskEvent (com.alibaba.otter.node.common.config.model.NodeTask.TaskEvent)2 Channel (com.alibaba.otter.shared.common.model.config.channel.Channel)2 Event (com.alibaba.otter.shared.communication.core.model.Event)2 FindNodeEvent (com.alibaba.otter.shared.communication.model.config.FindNodeEvent)2 FindTaskEvent (com.alibaba.otter.shared.communication.model.config.FindTaskEvent)2 NotifyChannelEvent (com.alibaba.otter.shared.communication.model.config.NotifyChannelEvent)2 Mock (mockit.Mock)2 Test (org.testng.annotations.Test)2 NodeTaskListener (com.alibaba.otter.node.common.config.NodeTaskListener)1 GlobalTask (com.alibaba.otter.node.etl.common.task.GlobalTask)1