Search in sources :

Example 16 with ZkNoNodeException

use of org.I0Itec.zkclient.exception.ZkNoNodeException in project canal by alibaba.

the class ZooKeeperMetaManager method getFirstBatch.

public PositionRange getFirstBatch(ClientIdentity clientIdentity) {
    String path = ZookeeperPathUtils.getBatchMarkPath(clientIdentity.getDestination(), clientIdentity.getClientId());
    List<String> nodes = null;
    try {
        nodes = zkClientx.getChildren(path);
    } catch (ZkNoNodeException e) {
    // ignore
    }
    if (CollectionUtils.isEmpty(nodes)) {
        return null;
    }
    // 找到最小的Id
    ArrayList<Long> batchIds = new ArrayList<Long>(nodes.size());
    for (String batchIdString : nodes) {
        batchIds.add(Long.valueOf(batchIdString));
    }
    Long minBatchId = Collections.min(batchIds);
    PositionRange result = getBatch(clientIdentity, minBatchId);
    if (result == null) {
        // 出现为null,说明zk节点有变化,重新获取
        return getFirstBatch(clientIdentity);
    } else {
        return result;
    }
}
Also used : ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) ArrayList(java.util.ArrayList) PositionRange(com.alibaba.otter.canal.protocol.position.PositionRange)

Example 17 with ZkNoNodeException

use of org.I0Itec.zkclient.exception.ZkNoNodeException in project canal by alibaba.

the class ZooKeeperMetaManager method listAllSubscribeInfo.

public List<ClientIdentity> listAllSubscribeInfo(String destination) throws CanalMetaManagerException {
    String path = ZookeeperPathUtils.getDestinationPath(destination);
    List<String> childs = null;
    try {
        childs = zkClientx.getChildren(path);
    } catch (ZkNoNodeException e) {
    // ignore
    }
    if (CollectionUtils.isEmpty(childs)) {
        return new ArrayList<ClientIdentity>();
    }
    List<Short> clientIds = new ArrayList<Short>();
    for (String child : childs) {
        if (StringUtils.isNumeric(child)) {
            clientIds.add(ZookeeperPathUtils.getClientId(child));
        }
    }
    // 进行一个排序
    Collections.sort(clientIds);
    List<ClientIdentity> clientIdentities = Lists.newArrayList();
    for (Short clientId : clientIds) {
        path = ZookeeperPathUtils.getFilterPath(destination, clientId);
        byte[] bytes = zkClientx.readData(path, true);
        String filter = null;
        if (bytes != null) {
            try {
                filter = new String(bytes, ENCODE);
            } catch (UnsupportedEncodingException e) {
                throw new CanalMetaManagerException(e);
            }
        }
        clientIdentities.add(new ClientIdentity(destination, clientId, filter));
    }
    return clientIdentities;
}
Also used : ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) ClientIdentity(com.alibaba.otter.canal.protocol.ClientIdentity) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CanalMetaManagerException(com.alibaba.otter.canal.meta.exception.CanalMetaManagerException)

Example 18 with ZkNoNodeException

use of org.I0Itec.zkclient.exception.ZkNoNodeException 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);
        // 出现任何异常尝试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)

Example 19 with ZkNoNodeException

use of org.I0Itec.zkclient.exception.ZkNoNodeException in project otter by alibaba.

the class NormalTerminProcess method doProcess.

private boolean doProcess(TerminEventData data, boolean retry) {
    Long pipelineId = data.getPipelineId();
    Long processId = data.getProcessId();
    List<String> currentStages = null;
    try {
        currentStages = zookeeper.getChildren(StagePathUtils.getProcess(pipelineId, processId));
        Collections.sort(currentStages, new StageComparator());
    } catch (ZkNoNodeException e) {
        // ignore,说明节点已经被删除了
        return false;
    } catch (ZkException e) {
        throw new ArbitrateException("Termin_process", e);
    }
    // s节点
    if (currentStages == null || currentStages.contains(ArbitrateConstants.NODE_SELECTED)) {
        try {
            boolean successed = zookeeper.delete(StagePathUtils.getSelectStage(pipelineId, processId));
            if (!successed) {
                processDeleteFailed();
            }
        } catch (ZkException e) {
            throw new ArbitrateException("Termin_process", e);
        }
    }
    // e节点
    if (currentStages == null || currentStages.contains(ArbitrateConstants.NODE_EXTRACTED)) {
        try {
            boolean successed = zookeeper.delete(StagePathUtils.getExtractStage(pipelineId, processId));
            if (!successed) {
                processDeleteFailed();
            }
        } catch (ZkException e) {
            throw new ArbitrateException("Termin_process", e);
        }
    }
    // t节点
    if (currentStages == null || currentStages.contains(ArbitrateConstants.NODE_TRANSFORMED)) {
        try {
            boolean successed = zookeeper.delete(StagePathUtils.getTransformStage(pipelineId, processId));
            if (!successed) {
                processDeleteFailed();
            }
        } catch (ZkException e) {
            throw new ArbitrateException("Termin_process", e);
        }
    }
    // 不过会有遗漏判断,比如并发时都是一个线程全删除成功
    return processDelete(data, CollectionUtils.isEmpty(currentStages), retry);
}
Also used : StageComparator(com.alibaba.otter.shared.arbitrate.impl.setl.helper.StageComparator) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) ZkException(org.I0Itec.zkclient.exception.ZkException) ArbitrateException(com.alibaba.otter.shared.arbitrate.exception.ArbitrateException)

Example 20 with ZkNoNodeException

use of org.I0Itec.zkclient.exception.ZkNoNodeException in project otter by alibaba.

the class NormalTerminProcess method processDelete.

private boolean processDelete(TerminEventData data, boolean noStage, boolean retry) {
    Long pipelineId = data.getPipelineId();
    Long processId = data.getProcessId();
    boolean result = false;
    // process节点
    // 最后删除一下process节点
    String path = StagePathUtils.getProcess(pipelineId, processId);
    byte[] bytes = null;
    try {
        bytes = zookeeper.readData(path);
    } catch (ZkNoNodeException e) {
        // 说明节点已经被删除了,直接忽略
        return false;
    }
    ProcessNodeEventData nodeData = JsonUtils.unmarshalFromByte(bytes, ProcessNodeEventData.class);
    if (nodeData.getStatus().isUsed()) {
        if (noStage && nodeData.getMode().isZookeeper()) {
            // 针对rpc mode就是没有stage,不需要进行sleep
            // 处理一种case:
            // 针对两个并发操作,一个已经完成了s/e/t/l模块的所有delete,另一个刚好进来发现没有可delete的
            // 这时两个线程就一起进入createTermin操作,存在一些并发问题,针对这种case,需要错开一下
            // 不过这种情况可能会有误判,针对s模块没有处理完成,发起了一次rollback/shutdown操作就会碰上,概率比较小,忽略这种误判吧
            processDeleteFailed();
            // 再重新尝试访问一下process,看下是否已经被删除了
            return processDelete(data, false, retry);
        }
        // 在这段sleep的过程中,process可能还会跑一段,产生新的s/e/t节点,导致process删除失败,从而重复执行了createTermin
        if (!retry) {
            // modify at 2012-08-14 , 遇到一个并发bug
            // 1. 两个process a和b,a先执行完毕删除了process节点,b立马得到触发并在极端的时间内处理完成
            // 2. 最后的一个结果b创建的termin要早于a创建的termin,导致termin发送顺序不对
            // 这里修改为,先创建termin节点,再删除对应的process,触发下一个process,保证termin创建为顺序
            // 同样可以避免删除了process后,termin信号创建失败的问题
            // modify at 2012-09-06 , 遇到一个并发bug
            // 一个process只完成了s/e模块,然后进行shutdown操作,完成了termin节点创建,但在process delete时,老的process创建了t节点
            // 这时会出现process删除失败,从而触发进行一次retry操作,此时retry又会再一次创建了termin信号,导致调度出错
            // 所以这里做了一个控制,只有针对非retry模式下才会创建termin信号
            // 创建termin节点
            result = createTermin(data, pipelineId, processId);
        }
    }
    try {
        // 修改为false,已经有另一个线程添加了该节点
        result = zookeeper.deleteRecursive(StagePathUtils.getProcess(pipelineId, processId));
        if (!result) {
            // 做一次重试,可能做manager关闭的时侯,node节点还跑了一段,导致stage节点又创建了一个
            doProcess(data, true);
        }
    } catch (ZkInterruptedException e) {
        throw e;
    } catch (ZkException e) {
        // 做一次重试,可能做manager关闭的时侯,node节点还跑了一段,导致stage节点又创建了一个
        doProcess(data, true);
    }
    return result;
}
Also used : ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) ZkException(org.I0Itec.zkclient.exception.ZkException) ProcessNodeEventData(com.alibaba.otter.shared.arbitrate.model.ProcessNodeEventData) ZkInterruptedException(org.I0Itec.zkclient.exception.ZkInterruptedException)

Aggregations

ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)27 ZkException (org.I0Itec.zkclient.exception.ZkException)22 ArbitrateException (com.alibaba.otter.shared.arbitrate.exception.ArbitrateException)17 EtlEventData (com.alibaba.otter.shared.arbitrate.model.EtlEventData)7 PermitMonitor (com.alibaba.otter.shared.arbitrate.impl.setl.monitor.PermitMonitor)5 ChannelStatus (com.alibaba.otter.shared.common.model.config.channel.ChannelStatus)5 ZkNodeExistsException (org.I0Itec.zkclient.exception.ZkNodeExistsException)5 ArrayList (java.util.ArrayList)4 PositionRange (com.alibaba.otter.canal.protocol.position.PositionRange)3 MainStemEventData (com.alibaba.otter.shared.arbitrate.model.MainStemEventData)3 Node (com.alibaba.otter.shared.common.model.config.node.Node)3 ZkInterruptedException (org.I0Itec.zkclient.exception.ZkInterruptedException)3 TerminMonitor (com.alibaba.otter.shared.arbitrate.impl.setl.monitor.TerminMonitor)2 Date (java.util.Date)2 ZkBadVersionException (org.I0Itec.zkclient.exception.ZkBadVersionException)2 Stat (org.apache.zookeeper.data.Stat)2 CanalMetaManagerException (com.alibaba.otter.canal.meta.exception.CanalMetaManagerException)1 ClientIdentity (com.alibaba.otter.canal.protocol.ClientIdentity)1 CanalClientException (com.alibaba.otter.canal.protocol.exception.CanalClientException)1 StageComparator (com.alibaba.otter.shared.arbitrate.impl.setl.helper.StageComparator)1