Search in sources :

Example 1 with ServerNotFoundException

use of com.alibaba.otter.canal.client.impl.ServerNotFoundException in project canal by alibaba.

the class ClientRunningMonitor method initRunning.

// 改动记录:
// 1,在方法上加synchronized关键字,保证同步顺序执行;
// 2,判断Zk上已经存在的activeData是否是本机,是的话把mutex重置为true,否则会导致死锁
// 3,增加异常处理,保证出现异常时,running节点能被删除,否则会导致死锁
public synchronized void initRunning() {
    if (!isStart()) {
        return;
    }
    String path = ZookeeperPathUtils.getDestinationClientRunning(this.destination, clientData.getClientId());
    // 序列化
    byte[] bytes = JsonUtils.marshalToByte(clientData);
    try {
        mutex.set(false);
        zkClient.create(path, bytes, CreateMode.EPHEMERAL);
        // 触发一下事件
        processActiveEnter();
        activeData = clientData;
        mutex.set(true);
    } catch (ZkNodeExistsException e) {
        bytes = zkClient.readData(path, true);
        if (bytes == null) {
            // 如果不存在节点,立即尝试一次
            initRunning();
        } else {
            activeData = JsonUtils.unmarshalFromByte(bytes, ClientRunningData.class);
            // 如果发现已经存在,判断一下是否自己,避免活锁
            if (activeData.getAddress().contains(":") && isMine(activeData.getAddress())) {
                mutex.set(true);
            }
        }
    } catch (ZkNoNodeException e) {
        zkClient.createPersistent(ZookeeperPathUtils.getClientIdNodePath(this.destination, clientData.getClientId()), // 尝试创建父节点
        true);
        initRunning();
    } catch (Throwable t) {
        logger.error(MessageFormat.format("There is an error when execute initRunning method, with destination [{0}].", destination), t);
        // fixed issue 1220, 针对server节点不工作避免死循环
        if (t instanceof ServerNotFoundException) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        // 出现任何异常尝试release
        releaseRunning();
        throw new CanalClientException("something goes wrong in initRunning method. ", t);
    }
}
Also used : ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) ServerNotFoundException(com.alibaba.otter.canal.client.impl.ServerNotFoundException) ZkInterruptedException(org.I0Itec.zkclient.exception.ZkInterruptedException)

Aggregations

ServerNotFoundException (com.alibaba.otter.canal.client.impl.ServerNotFoundException)1 CanalClientException (com.alibaba.otter.canal.protocol.exception.CanalClientException)1 ZkInterruptedException (org.I0Itec.zkclient.exception.ZkInterruptedException)1 ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)1 ZkNodeExistsException (org.I0Itec.zkclient.exception.ZkNodeExistsException)1