Search in sources :

Example 1 with ServerRunningMonitor

use of com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor in project canal by alibaba.

the class ServerRunningTest method testOneServer.

@Test
public void testOneServer() {
    final CountDownLatch countLatch = new CountDownLatch(2);
    ServerRunningMonitor runningMonitor = buildServerRunning(countLatch, 1L, "127.0.0.1", 2088);
    runningMonitor.start();
    sleep(2000L);
    runningMonitor.stop();
    sleep(2000L);
    if (countLatch.getCount() != 0) {
        Assert.fail();
    }
}
Also used : ServerRunningMonitor(com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with ServerRunningMonitor

use of com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor in project canal by alibaba.

the class ServerRunningTest method buildServerRunning.

private ServerRunningMonitor buildServerRunning(final CountDownLatch countLatch, final Long cid, final String ip, final int port) {
    ServerRunningData serverData = new ServerRunningData(cid, ip + ":" + port);
    ServerRunningMonitor runningMonitor = new ServerRunningMonitor(serverData);
    runningMonitor.setDestination(destination);
    runningMonitor.setListener(new ServerRunningListener() {

        public void processActiveEnter() {
            System.out.println(String.format("cid:%s ip:%s:%s has start", cid, ip, port));
            countLatch.countDown();
        }

        public void processActiveExit() {
            System.out.println(String.format("cid:%s ip:%s:%s has stop", cid, ip, port));
            countLatch.countDown();
        }

        public void processStart() {
            System.out.println(String.format("cid:%s ip:%s:%s processStart", cid, ip, port));
        }

        public void processStop() {
            System.out.println(String.format("cid:%s ip:%s:%s processStop", cid, ip, port));
        }
    });
    runningMonitor.setZkClient(zkclientx);
    runningMonitor.setDelayTime(1);
    return runningMonitor;
}
Also used : ServerRunningData(com.alibaba.otter.canal.common.zookeeper.running.ServerRunningData) ServerRunningMonitor(com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor) ServerRunningListener(com.alibaba.otter.canal.common.zookeeper.running.ServerRunningListener)

Example 3 with ServerRunningMonitor

use of com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor in project canal by alibaba.

the class ServerRunningTest method testMultiServer.

@Test
public void testMultiServer() {
    final CountDownLatch countLatch = new CountDownLatch(30);
    final ServerRunningMonitor runningMonitor1 = buildServerRunning(countLatch, 1L, "127.0.0.1", 2088);
    final ServerRunningMonitor runningMonitor2 = buildServerRunning(countLatch, 2L, "127.0.0.1", 2089);
    final ServerRunningMonitor runningMonitor3 = buildServerRunning(countLatch, 3L, "127.0.0.1", 2090);
    final ExecutorService executor = Executors.newFixedThreadPool(3);
    executor.submit(new Runnable() {

        public void run() {
            for (int i = 0; i < 10; i++) {
                if (!runningMonitor1.isStart()) {
                    runningMonitor1.start();
                }
                sleep(2000L + RandomUtils.nextInt(500));
                if (runningMonitor1.check()) {
                    runningMonitor1.stop();
                }
                sleep(2000L + RandomUtils.nextInt(500));
            }
        }
    });
    executor.submit(new Runnable() {

        public void run() {
            for (int i = 0; i < 10; i++) {
                if (!runningMonitor2.isStart()) {
                    runningMonitor2.start();
                }
                sleep(2000L + RandomUtils.nextInt(500));
                if (runningMonitor2.check()) {
                    runningMonitor2.stop();
                }
                sleep(2000L + RandomUtils.nextInt(500));
            }
        }
    });
    executor.submit(new Runnable() {

        public void run() {
            for (int i = 0; i < 10; i++) {
                if (!runningMonitor3.isStart()) {
                    runningMonitor3.start();
                }
                sleep(2000L + RandomUtils.nextInt(500));
                if (runningMonitor3.check()) {
                    runningMonitor3.stop();
                }
                sleep(2000L + RandomUtils.nextInt(500));
            }
        }
    });
    sleep(30000L);
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ServerRunningMonitor(com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with ServerRunningMonitor

use of com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor in project canal by alibaba.

the class CanalController method start.

public void start() throws Throwable {
    logger.info("## start the canal server[{}:{}]", ip, port);
    // 创建整个canal的工作节点
    final String path = ZookeeperPathUtils.getCanalClusterNode(ip + ":" + port);
    initCid(path);
    if (zkclientx != null) {
        this.zkclientx.subscribeStateChanges(new IZkStateListener() {

            public void handleStateChanged(KeeperState state) throws Exception {
            }

            public void handleNewSession() throws Exception {
                initCid(path);
            }
        });
    }
    // 优先启动embeded服务
    embededCanalServer.start();
    // 尝试启动一下非lazy状态的通道
    for (Map.Entry<String, InstanceConfig> entry : instanceConfigs.entrySet()) {
        final String destination = entry.getKey();
        InstanceConfig config = entry.getValue();
        // 创建destination的工作节点
        if (!embededCanalServer.isStart(destination)) {
            // HA机制启动
            ServerRunningMonitor runningMonitor = ServerRunningMonitors.getRunningMonitor(destination);
            if (!config.getLazy() && !runningMonitor.isStart()) {
                runningMonitor.start();
            }
        }
        if (autoScan) {
            instanceConfigMonitors.get(config.getMode()).register(destination, defaultAction);
        }
    }
    if (autoScan) {
        instanceConfigMonitors.get(globalInstanceConfig.getMode()).start();
        for (InstanceConfigMonitor monitor : instanceConfigMonitors.values()) {
            if (!monitor.isStart()) {
                monitor.start();
            }
        }
    }
    // 启动网络接口
    canalServer.start();
}
Also used : IZkStateListener(org.I0Itec.zkclient.IZkStateListener) ServerRunningMonitor(com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor) KeeperState(org.apache.zookeeper.Watcher.Event.KeeperState) Map(java.util.Map) MigrateMap(com.google.common.collect.MigrateMap) ManagerInstanceConfigMonitor(com.alibaba.otter.canal.deployer.monitor.ManagerInstanceConfigMonitor) SpringInstanceConfigMonitor(com.alibaba.otter.canal.deployer.monitor.SpringInstanceConfigMonitor) InstanceConfigMonitor(com.alibaba.otter.canal.deployer.monitor.InstanceConfigMonitor) CanalServerException(com.alibaba.otter.canal.server.exception.CanalServerException) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) CanalException(com.alibaba.otter.canal.common.CanalException) ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException)

Example 5 with ServerRunningMonitor

use of com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor in project canal by alibaba.

the class CanalController method stop.

public void stop() throws Throwable {
    canalServer.stop();
    if (autoScan) {
        for (InstanceConfigMonitor monitor : instanceConfigMonitors.values()) {
            if (monitor.isStart()) {
                monitor.stop();
            }
        }
    }
    for (ServerRunningMonitor runningMonitor : ServerRunningMonitors.getRunningMonitors().values()) {
        if (runningMonitor.isStart()) {
            runningMonitor.stop();
        }
    }
    // 释放canal的工作节点
    releaseCid(ZookeeperPathUtils.getCanalClusterNode(ip + ":" + port));
    logger.info("## stop the canal server[{}:{}]", ip, port);
}
Also used : ServerRunningMonitor(com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor) ManagerInstanceConfigMonitor(com.alibaba.otter.canal.deployer.monitor.ManagerInstanceConfigMonitor) SpringInstanceConfigMonitor(com.alibaba.otter.canal.deployer.monitor.SpringInstanceConfigMonitor) InstanceConfigMonitor(com.alibaba.otter.canal.deployer.monitor.InstanceConfigMonitor)

Aggregations

ServerRunningMonitor (com.alibaba.otter.canal.common.zookeeper.running.ServerRunningMonitor)7 InstanceConfigMonitor (com.alibaba.otter.canal.deployer.monitor.InstanceConfigMonitor)2 ManagerInstanceConfigMonitor (com.alibaba.otter.canal.deployer.monitor.ManagerInstanceConfigMonitor)2 SpringInstanceConfigMonitor (com.alibaba.otter.canal.deployer.monitor.SpringInstanceConfigMonitor)2 Packet (com.alibaba.otter.canal.protocol.CanalPacket.Packet)2 ClientIdentity (com.alibaba.otter.canal.protocol.ClientIdentity)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)2 Test (org.junit.Test)2 CanalException (com.alibaba.otter.canal.common.CanalException)1 ServerRunningData (com.alibaba.otter.canal.common.zookeeper.running.ServerRunningData)1 ServerRunningListener (com.alibaba.otter.canal.common.zookeeper.running.ServerRunningListener)1 Entry (com.alibaba.otter.canal.protocol.CanalEntry.Entry)1 CanalPacket (com.alibaba.otter.canal.protocol.CanalPacket)1 ClientAck (com.alibaba.otter.canal.protocol.CanalPacket.ClientAck)1 ClientAuth (com.alibaba.otter.canal.protocol.CanalPacket.ClientAuth)1 ClientRollback (com.alibaba.otter.canal.protocol.CanalPacket.ClientRollback)1 Get (com.alibaba.otter.canal.protocol.CanalPacket.Get)1 Messages (com.alibaba.otter.canal.protocol.CanalPacket.Messages)1 Sub (com.alibaba.otter.canal.protocol.CanalPacket.Sub)1