Search in sources :

Example 1 with NotifyChannelEvent

use of com.alibaba.otter.shared.communication.model.config.NotifyChannelEvent in project otter by alibaba.

the class NodeTaskServiceTest method test_b_channel.

@Test
public void test_b_channel() {
    Long channelId = 100L;
    Long pipelineId = 101L;
    // 先加一个pipeline的同步任务
    Long oppositePipelineId = 102L;
    final Channel channel = new Channel();
    channel.setId(channelId);
    channel.setStatus(ChannelStatus.START);
    Pipeline pipeline1 = new Pipeline();
    pipeline1.setChannelId(channelId);
    pipeline1.setId(pipelineId);
    Pipeline pipeline2 = new Pipeline();
    pipeline2.setChannelId(channelId);
    pipeline2.setId(oppositePipelineId);
    channel.setPipelines(Arrays.asList(pipeline1, pipeline2));
    Node node1 = new Node();
    node1.setId(1L);
    Node node2 = new Node();
    node2.setId(2L);
    pipeline1.setSelectNodes(Arrays.asList(node1, node2));
    pipeline1.setExtractNodes(Arrays.asList(node1, node2));
    pipeline1.setLoadNodes(Arrays.asList(node2));
    pipeline2.setSelectNodes(Arrays.asList(node1));
    pipeline2.setExtractNodes(Arrays.asList(node1));
    pipeline2.setLoadNodes(Arrays.asList(node1, node2));
    Mockit.setUpMock(NodeCommmunicationClient.class, new Object() {

        @Mock
        public Object callManager(final Event event) {
            if (event instanceof FindNodeEvent) {
                Node node = new Node();
                Long nid = ((FindNodeEvent) event).getNid();
                node.setId(nid);
                return node;
            } else {
                return Arrays.asList(channel);
            }
        }
    });
    // 初始化一下数据
    NodeTaskServiceImpl nodeTaskSerivce = (NodeTaskServiceImpl) spring.getBean("nodeTaskService");
    List<NodeTask> tasks = null;
    NotifyChannelEvent event = new NotifyChannelEvent();
    event.setChannel(channel);
    reflector.invoke(nodeTaskSerivce, "onNotifyChannel", event);
    tasks = reflector.invoke(nodeTaskSerivce, "mergeIncNodeTasks");
    want.bool(tasks.size() == 2).is(true);
    tasks = nodeTaskSerivce.listAllNodeTasks();
    want.bool(tasks.size() == 2).is(true);
}
Also used : FindNodeEvent(com.alibaba.otter.shared.communication.model.config.FindNodeEvent) NodeTaskServiceImpl(com.alibaba.otter.node.common.config.impl.NodeTaskServiceImpl) NotifyChannelEvent(com.alibaba.otter.shared.communication.model.config.NotifyChannelEvent) Channel(com.alibaba.otter.shared.common.model.config.channel.Channel) Node(com.alibaba.otter.shared.common.model.config.node.Node) Event(com.alibaba.otter.shared.communication.core.model.Event) FindNodeEvent(com.alibaba.otter.shared.communication.model.config.FindNodeEvent) NotifyChannelEvent(com.alibaba.otter.shared.communication.model.config.NotifyChannelEvent) NodeTask(com.alibaba.otter.node.common.config.model.NodeTask) Mock(mockit.Mock) Pipeline(com.alibaba.otter.shared.common.model.config.pipeline.Pipeline) Test(org.testng.annotations.Test) BaseOtterTest(com.alibaba.otter.node.common.BaseOtterTest)

Example 2 with NotifyChannelEvent

use of com.alibaba.otter.shared.communication.model.config.NotifyChannelEvent in project otter by alibaba.

the class ConfigRemoteServiceImpl method notifyChannel.

public boolean notifyChannel(final Channel channel) {
    Assert.notNull(channel);
    // 获取所有的Node节点
    NotifyChannelEvent event = new NotifyChannelEvent();
    event.setChannel(channel);
    Set<String> addrsSet = new HashSet<String>();
    // 组装当前pipeline下的存活的node节点
    for (Pipeline pipeline : channel.getPipelines()) {
        List<Node> nodes = new ArrayList<Node>();
        nodes.addAll(pipeline.getSelectNodes());
        nodes.addAll(pipeline.getExtractNodes());
        nodes.addAll(pipeline.getLoadNodes());
        for (Node node : nodes) {
            if (node.getStatus().isStart() && StringUtils.isNotEmpty(node.getIp()) && node.getPort() != 0) {
                String addr = node.getIp() + ":" + node.getPort();
                if (node.getParameters().getUseExternalIp()) {
                    addr = node.getParameters().getExternalIp() + ":" + node.getPort();
                }
                addrsSet.add(addr);
            }
        }
    }
    List<String> addrsList = new ArrayList<String>(addrsSet);
    if (CollectionUtils.isEmpty(addrsList) && channel.getStatus().isStart()) {
        throw new ManagerException("no live node for notifyChannel");
    } else if (CollectionUtils.isEmpty(addrsList)) {
        // 针对关闭操作,可直接处理
        return true;
    } else {
        // 做一下随机,避免每次选择的机器都是同一台
        Collections.shuffle(addrsList);
        try {
            String[] addrs = addrsList.toArray(new String[addrsList.size()]);
            // 推送配置
            List<Boolean> result = (List<Boolean>) communicationClient.call(addrs, event);
            logger.info("## notifyChannel to [{}] channel[{}] result[{}]", new Object[] { ArrayUtils.toString(addrs), channel.toString(), result });
            boolean flag = true;
            for (Boolean f : result) {
                flag &= f;
            }
            return flag;
        } catch (Exception e) {
            logger.error("## notifyChannel error!", e);
            throw new ManagerException(e);
        }
    }
}
Also used : NotifyChannelEvent(com.alibaba.otter.shared.communication.model.config.NotifyChannelEvent) Node(com.alibaba.otter.shared.common.model.config.node.Node) ArrayList(java.util.ArrayList) ManagerException(com.alibaba.otter.manager.biz.common.exceptions.ManagerException) Pipeline(com.alibaba.otter.shared.common.model.config.pipeline.Pipeline) ArrayList(java.util.ArrayList) List(java.util.List) ManagerException(com.alibaba.otter.manager.biz.common.exceptions.ManagerException) HashSet(java.util.HashSet)

Aggregations

Node (com.alibaba.otter.shared.common.model.config.node.Node)2 Pipeline (com.alibaba.otter.shared.common.model.config.pipeline.Pipeline)2 NotifyChannelEvent (com.alibaba.otter.shared.communication.model.config.NotifyChannelEvent)2 ManagerException (com.alibaba.otter.manager.biz.common.exceptions.ManagerException)1 BaseOtterTest (com.alibaba.otter.node.common.BaseOtterTest)1 NodeTaskServiceImpl (com.alibaba.otter.node.common.config.impl.NodeTaskServiceImpl)1 NodeTask (com.alibaba.otter.node.common.config.model.NodeTask)1 Channel (com.alibaba.otter.shared.common.model.config.channel.Channel)1 Event (com.alibaba.otter.shared.communication.core.model.Event)1 FindNodeEvent (com.alibaba.otter.shared.communication.model.config.FindNodeEvent)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Mock (mockit.Mock)1 Test (org.testng.annotations.Test)1