Search in sources :

Example 1 with FindNodeEvent

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

the class ConfigClientServiceImpl method afterPropertiesSet.

public void afterPropertiesSet() throws Exception {
    // 获取一下nid变量
    String nid = System.getProperty(NID_NAME);
    if (StringUtils.isEmpty(nid)) {
        throw new ConfigException("nid is not set!");
    }
    this.nid = Long.valueOf(nid);
    channelMapping = OtterMigrateMap.makeComputingMap(new Function<Long, Long>() {

        public Long apply(Long pipelineId) {
            // 处理下pipline -> channel映射关系不存在的情况
            FindChannelEvent event = new FindChannelEvent();
            event.setPipelineId(pipelineId);
            try {
                Object obj = nodeCommmunicationClient.callManager(event);
                if (obj != null && obj instanceof Channel) {
                    Channel channel = (Channel) obj;
                    // 排除下自己
                    updateMapping(channel, pipelineId);
                    // 更新下channelCache
                    channelCache.put(channel.getId(), channel);
                    return channel.getId();
                }
            } catch (Exception e) {
                logger.error("call_manager_error", event.toString(), e);
            }
            throw new ConfigException("No Such Channel by pipelineId[" + pipelineId + "]");
        }
    });
    nodeCache = new RefreshMemoryMirror<Long, Node>(timeout, new ComputeFunction<Long, Node>() {

        public Node apply(Long key, Node oldValue) {
            FindNodeEvent event = new FindNodeEvent();
            event.setNid(key);
            try {
                Object obj = nodeCommmunicationClient.callManager(event);
                if (obj != null && obj instanceof Node) {
                    return (Node) obj;
                } else {
                    throw new ConfigException("No Such Node by id[" + key + "]");
                }
            } catch (Exception e) {
                logger.error("call_manager_error", event.toString(), e);
            }
            // 其他情况直接返回内存中的旧值
            return oldValue;
        }
    });
    channelCache = new RefreshMemoryMirror<Long, Channel>(timeout, new ComputeFunction<Long, Channel>() {

        public Channel apply(Long key, Channel oldValue) {
            FindChannelEvent event = new FindChannelEvent();
            event.setChannelId(key);
            try {
                Object obj = nodeCommmunicationClient.callManager(event);
                if (obj != null && obj instanceof Channel) {
                    // 排除下自己
                    updateMapping((Channel) obj, null);
                    return (Channel) obj;
                } else {
                    throw new ConfigException("No Such Channel by pipelineId[" + key + "]");
                }
            } catch (Exception e) {
                logger.error("call_manager_error", event.toString(), e);
            }
            // 其他情况直接返回内存中的旧值
            return oldValue;
        }
    });
}
Also used : ComputeFunction(com.alibaba.otter.shared.common.utils.cache.RefreshMemoryMirror.ComputeFunction) Channel(com.alibaba.otter.shared.common.model.config.channel.Channel) Node(com.alibaba.otter.shared.common.model.config.node.Node) ConfigException(com.alibaba.otter.shared.common.model.config.ConfigException) ConfigException(com.alibaba.otter.shared.common.model.config.ConfigException) Function(com.google.common.base.Function) ComputeFunction(com.alibaba.otter.shared.common.utils.cache.RefreshMemoryMirror.ComputeFunction) FindNodeEvent(com.alibaba.otter.shared.communication.model.config.FindNodeEvent) FindChannelEvent(com.alibaba.otter.shared.communication.model.config.FindChannelEvent)

Example 2 with FindNodeEvent

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

the class ConfigClientServiceTest method test_node.

@Test
public void test_node() {
    final Node node = new Node();
    Mockit.setUpMock(NodeCommmunicationClient.class, new Object() {

        @Mock
        public Object callManager(final Event event) {
            if (event instanceof FindNodeEvent) {
                Long nid = ((FindNodeEvent) event).getNid();
                node.setId(nid);
            }
            return node;
        }
    });
    Node cnode = configClientService.currentNode();
    want.bool(cnode.getId() == 1L);
    Node fnode = configClientService.findNode(2L);
    want.bool(fnode.getId() == 2L);
}
Also used : FindNodeEvent(com.alibaba.otter.shared.communication.model.config.FindNodeEvent) 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) Mock(mockit.Mock) Test(org.testng.annotations.Test) BaseOtterTest(com.alibaba.otter.node.common.BaseOtterTest)

Example 3 with FindNodeEvent

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

the class NodeTaskServiceTest method test_a_reload.

@Test
public void test_a_reload() throws Exception {
    Long channelId = 100L;
    Long pipelineId = 100L;
    Long oppositePipelineId = 101L;
    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(node1));
    pipeline2.setSelectNodes(Arrays.asList(node2));
    pipeline2.setExtractNodes(Arrays.asList(node2));
    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);
            }
        }
    });
    List<NodeTask> tasks = null;
    NodeTaskServiceImpl nodeTaskSerivce = (NodeTaskServiceImpl) spring.getBean("nodeTaskService");
    reflector.invoke(nodeTaskSerivce, "initNodeTask");
    tasks = reflector.invoke(nodeTaskSerivce, "mergeIncNodeTasks");
    want.number(tasks.size()).isEqualTo(2);
    reflector.invoke(nodeTaskSerivce, "initNodeTask");
    tasks = reflector.invoke(nodeTaskSerivce, "mergeIncNodeTasks");
    want.bool(tasks.size() == 0).is(true);
    channel.setStatus(ChannelStatus.STOP);
    reflector.invoke(nodeTaskSerivce, "initNodeTask");
    tasks = reflector.invoke(nodeTaskSerivce, "mergeIncNodeTasks");
    want.bool(tasks.size() == 2).is(true);
    tasks = nodeTaskSerivce.listAllNodeTasks();
    want.bool(tasks.size() == 2).is(true);
    // 清理内存
    TestUtils.setField(nodeTaskSerivce, "allTasks", Lists.newArrayList());
    TestUtils.setField(nodeTaskSerivce, "incTasks", Lists.newArrayList());
    // 删除某个pipeline的node
    channel.setStatus(ChannelStatus.START);
    reflector.invoke(nodeTaskSerivce, "initNodeTask");
    tasks = reflector.invoke(nodeTaskSerivce, "mergeIncNodeTasks");
    want.number(tasks.size()).isEqualTo(2);
    pipeline1.setSelectNodes(Arrays.asList(node2));
    pipeline1.setExtractNodes(Arrays.asList(node2));
    pipeline1.setLoadNodes(Arrays.asList(node2));
    channel.setStatus(ChannelStatus.START);
    reflector.invoke(nodeTaskSerivce, "initNodeTask");
    tasks = reflector.invoke(nodeTaskSerivce, "mergeIncNodeTasks");
    want.bool(tasks.size() == 1).is(true);
    // 清理内存
    TestUtils.setField(nodeTaskSerivce, "allTasks", Lists.newArrayList());
    TestUtils.setField(nodeTaskSerivce, "incTasks", Lists.newArrayList());
    channel.setStatus(ChannelStatus.START);
    reflector.invoke(nodeTaskSerivce, "initNodeTask");
    tasks = reflector.invoke(nodeTaskSerivce, "mergeIncNodeTasks");
    want.number(tasks.size()).isEqualTo(1);
    // 删除某个pipeline
    channel.setPipelines(Arrays.asList(pipeline1));
    reflector.invoke(nodeTaskSerivce, "initNodeTask");
    tasks = reflector.invoke(nodeTaskSerivce, "mergeIncNodeTasks");
    want.number(tasks.size()).isEqualTo(1);
}
Also used : FindNodeEvent(com.alibaba.otter.shared.communication.model.config.FindNodeEvent) NodeTaskServiceImpl(com.alibaba.otter.node.common.config.impl.NodeTaskServiceImpl) 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 4 with FindNodeEvent

use of com.alibaba.otter.shared.communication.model.config.FindNodeEvent 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)

Aggregations

Node (com.alibaba.otter.shared.common.model.config.node.Node)4 FindNodeEvent (com.alibaba.otter.shared.communication.model.config.FindNodeEvent)4 BaseOtterTest (com.alibaba.otter.node.common.BaseOtterTest)3 Channel (com.alibaba.otter.shared.common.model.config.channel.Channel)3 Event (com.alibaba.otter.shared.communication.core.model.Event)3 Mock (mockit.Mock)3 Test (org.testng.annotations.Test)3 NodeTaskServiceImpl (com.alibaba.otter.node.common.config.impl.NodeTaskServiceImpl)2 NodeTask (com.alibaba.otter.node.common.config.model.NodeTask)2 Pipeline (com.alibaba.otter.shared.common.model.config.pipeline.Pipeline)2 NotifyChannelEvent (com.alibaba.otter.shared.communication.model.config.NotifyChannelEvent)2 ConfigException (com.alibaba.otter.shared.common.model.config.ConfigException)1 ComputeFunction (com.alibaba.otter.shared.common.utils.cache.RefreshMemoryMirror.ComputeFunction)1 FindChannelEvent (com.alibaba.otter.shared.communication.model.config.FindChannelEvent)1 Function (com.google.common.base.Function)1