Search in sources :

Example 6 with ZkException

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

the class TransformZooKeeperArbitrateEvent method await.

/**
 * <pre>
 * 算法:
 * 1. 检查当前的Permit,阻塞等待其授权(解决Channel的pause状态处理)
 * 2. 开始阻塞获取符合条件的processId
 * 3. 检查当前的即时Permit状态 (在阻塞获取processId过程会出现一些error信号,process节点会被删除)
 * 4. 获取Select传递的EventData数据,添加next node信息后直接返回
 * </pre>
 *
 * @return
 */
public EtlEventData await(Long pipelineId) throws InterruptedException {
    Assert.notNull(pipelineId);
    PermitMonitor permitMonitor = ArbitrateFactory.getInstance(pipelineId, PermitMonitor.class);
    // 阻塞等待授权
    permitMonitor.waitForPermit();
    TransformStageListener transformStageListener = ArbitrateFactory.getInstance(pipelineId, TransformStageListener.class);
    // 符合条件的processId
    Long processId = transformStageListener.waitForProcess();
    ChannelStatus status = permitMonitor.getChannelPermit();
    if (status.isStart()) {
        // 即时查询一下当前的状态,状态随时可能会变
        // 根据pipelineId+processId构造对应的path
        String path = StagePathUtils.getExtractStage(pipelineId, processId);
        try {
            byte[] data = zookeeper.readData(path);
            EtlEventData eventData = JsonUtils.unmarshalFromByte(data, EtlEventData.class);
            // 下一个节点信息即为自己
            eventData.setNextNid(ArbitrateConfigUtils.getCurrentNid());
            // 只有这一条路返回
            return eventData;
        } catch (ZkNoNodeException e) {
            logger.error("pipeline[{}] processId[{}] is invalid , retry again", pipelineId, processId);
            // /出现节点不存在,说明出现了error情况,递归调用重新获取一次
            return await(pipelineId);
        } catch (ZkException e) {
            throw new ArbitrateException("transform_await", e.getMessage(), e);
        }
    } else {
        logger.info("pipelineId[{}] transform ignore processId[{}] by status[{}]", new Object[] { pipelineId, processId, status });
        // 释放下processId,因为load是等待processId最小值完成Tranform才继续,如果这里不释放,会一直卡死等待
        String path = StagePathUtils.getProcess(pipelineId, processId);
        zookeeper.delete(path);
        // 递归调用
        return await(pipelineId);
    }
}
Also used : TransformStageListener(com.alibaba.otter.shared.arbitrate.impl.setl.zookeeper.monitor.TransformStageListener) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) ZkException(org.I0Itec.zkclient.exception.ZkException) PermitMonitor(com.alibaba.otter.shared.arbitrate.impl.setl.monitor.PermitMonitor) ArbitrateException(com.alibaba.otter.shared.arbitrate.exception.ArbitrateException) ChannelStatus(com.alibaba.otter.shared.common.model.config.channel.ChannelStatus) EtlEventData(com.alibaba.otter.shared.arbitrate.model.EtlEventData)

Example 7 with ZkException

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

the class TransformZooKeeperArbitrateEvent method single.

/**
 * <pre>
 * 算法:
 * 1. 创建对应的transformed节点,标志transform已完成
 * </pre>
 *
 * @param pipelineId 同步流id
 */
public void single(EtlEventData data) {
    Assert.notNull(data);
    String path = StagePathUtils.getTransformStage(data.getPipelineId(), data.getProcessId());
    data.setCurrNid(ArbitrateConfigUtils.getCurrentNid());
    // 序列化
    byte[] bytes = JsonUtils.marshalToByte(data, SerializerFeature.WriteClassName);
    try {
        zookeeper.create(path, bytes, CreateMode.PERSISTENT);
    } catch (ZkNoNodeException e) {
        // process节点不存在,出现了rollback/shutdown操作,直接忽略
        logger.warn("pipelineId[{}] transform ignore processId[{}] single by data:{}", new Object[] { data.getPipelineId(), data.getProcessId(), data });
    } catch (ZkNodeExistsException e) {
        // process节点已存在,出现了ConnectionLoss retry操作
        logger.warn("pipelineId[{}] transform ignore processId[{}] single by data:{}", new Object[] { data.getPipelineId(), data.getProcessId(), data });
    } catch (ZkException e) {
        throw new ArbitrateException("transform_single", e.getMessage(), e);
    }
}
Also used : ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) ZkException(org.I0Itec.zkclient.exception.ZkException) ArbitrateException(com.alibaba.otter.shared.arbitrate.exception.ArbitrateException)

Example 8 with ZkException

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

the class SelectProcessListener method processChanged.

public void processChanged(List<Long> processIds) {
    super.processChanged(processIds);
    // add by ljh at 2012-09-13,解决zookeeper ConnectionLoss问题
    for (Long processId : processIds) {
        if (!replyProcessIds.contains(processId)) {
            logger.warn("process is not in order, please check processId:{}", processId);
            addReply(processId);
        }
    }
    try {
        String path = StagePathUtils.getProcessRoot(getPipelineId());
        // 根据并行度创建任务
        int size = ArbitrateConfigUtils.getParallelism(getPipelineId()) - processIds.size();
        if (size > 0) {
            // 创建一个节点
            PermitMonitor permit = ArbitrateFactory.getInstance(getPipelineId(), PermitMonitor.class);
            if (permit.isPermit() == false) {
                // 如果非授权,则不做任何处理
                return;
            }
            String mainStemPath = StagePathUtils.getMainStem(getPipelineId());
            byte[] bytes = zookeeper.readData(mainStemPath, true);
            if (bytes == null) {
                return;
            }
            MainStemEventData eventData = JsonUtils.unmarshalFromByte(bytes, MainStemEventData.class);
            if (eventData.getNid().equals(ArbitrateConfigUtils.getCurrentNid()) == false) {
                // 如果非自己设置的mainStem,则不做任何处理
                return;
            }
            synchronized (this) {
                // 重新再取一次, dobble-check
                List<String> currentProcesses = zookeeper.getChildren(path);
                size = ArbitrateConfigUtils.getParallelism(getPipelineId()) - currentProcesses.size();
                if (size > 0) {
                    // 创建一个节点
                    ProcessNodeEventData nodeData = new ProcessNodeEventData();
                    // 标记为未使用
                    nodeData.setStatus(ProcessNodeEventData.Status.UNUSED);
                    nodeData.setMode(ArbitrateMode.RPC);
                    nodeData.setNid(ArbitrateConfigUtils.getCurrentNid());
                    byte[] nodeBytes = JsonUtils.marshalToByte(nodeData);
                    String processPath = zookeeper.create(path + "/", nodeBytes, CreateMode.PERSISTENT_SEQUENTIAL);
                    // 创建为顺序的节点
                    String processNode = StringUtils.substringAfterLast(processPath, "/");
                    // 添加到当前的process列表
                    Long processId = StagePathUtils.getProcessId(processNode);
                    addReply(processId);
                }
            }
        }
    } catch (ZkException e) {
        // 出现异常后进行一次recovery,读取一下当前最新值,解决出现ConnectionLoss时create成功问题
        recovery(getPipelineId());
        logger.error("add process error!", e);
    }
}
Also used : ZkException(org.I0Itec.zkclient.exception.ZkException) PermitMonitor(com.alibaba.otter.shared.arbitrate.impl.setl.monitor.PermitMonitor) MainStemEventData(com.alibaba.otter.shared.arbitrate.model.MainStemEventData) ProcessNodeEventData(com.alibaba.otter.shared.arbitrate.model.ProcessNodeEventData)

Example 9 with ZkException

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

the class TerminZooKeeperArbitrateEvent method await.

/**
 * <pre>
 * 算法:
 * 1. 开始阻塞获取符合条件的processId,获取对应的data数据直接返回
 * </pre>
 */
public TerminEventData await(Long pipelineId) throws InterruptedException {
    Assert.notNull(pipelineId);
    TerminMonitor terminMonitor = ArbitrateFactory.getInstance(pipelineId, TerminMonitor.class);
    // 符合条件的processId
    Long processId = terminMonitor.waitForProcess();
    if (logger.isDebugEnabled()) {
        logger.debug("## await pipeline[{}] processId[{}] is termin", pipelineId, processId);
    }
    // 根据pipelineId+processId构造对应的path
    String path = StagePathUtils.getTermin(pipelineId, processId);
    try {
        byte[] data = zookeeper.readData(path);
        return JsonUtils.unmarshalFromByte(data, TerminEventData.class);
    } catch (ZkNoNodeException e) {
        logger.error("pipeline[{}] processId[{}] is process", pipelineId, processId);
        // modify for 2012-09-08, 发生主备切换时,await会进入死循环,针对NoNode后直接从内存队列中移除
        terminMonitor.ack(processId);
        // 再取下一个节点
        return await(pipelineId);
    } catch (ZkException e) {
        throw new ArbitrateException("Termin_await", e);
    }
}
Also used : ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) TerminMonitor(com.alibaba.otter.shared.arbitrate.impl.setl.monitor.TerminMonitor) ZkException(org.I0Itec.zkclient.exception.ZkException) ArbitrateException(com.alibaba.otter.shared.arbitrate.exception.ArbitrateException)

Example 10 with ZkException

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

the class TerminZooKeeperArbitrateEvent method ack.

/**
 * <pre>
 * 算法:
 * 1. 客户端处理完成对应的termin事件后,反馈给仲裁器处理完成。仲裁器根据对应S.E.T.L的反馈情况,判断是否删除对应的termin信号
 * </pre>
 */
public void ack(TerminEventData data) {
    Assert.notNull(data);
    // 目前只有select模块需要发送ack信号,这里一旦收到一个信号后就删除对应的termin节点,后续可扩展
    // 删除termin节点
    String path = StagePathUtils.getTermin(data.getPipelineId(), data.getProcessId());
    try {
        zookeeper.delete(path);
    } catch (ZkNoNodeException e) {
    // ignore,说明节点已经被删除
    } catch (ZkException e) {
        throw new ArbitrateException("Termin_ack", e);
    }
    TerminMonitor terminMonitor = ArbitrateFactory.getInstance(data.getPipelineId(), TerminMonitor.class);
    terminMonitor.ack(data.getProcessId());
}
Also used : ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) TerminMonitor(com.alibaba.otter.shared.arbitrate.impl.setl.monitor.TerminMonitor) ZkException(org.I0Itec.zkclient.exception.ZkException) ArbitrateException(com.alibaba.otter.shared.arbitrate.exception.ArbitrateException)

Aggregations

ZkException (org.I0Itec.zkclient.exception.ZkException)41 ArbitrateException (com.alibaba.otter.shared.arbitrate.exception.ArbitrateException)27 ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)22 PermitMonitor (com.alibaba.otter.shared.arbitrate.impl.setl.monitor.PermitMonitor)7 EtlEventData (com.alibaba.otter.shared.arbitrate.model.EtlEventData)7 ZkNodeExistsException (org.I0Itec.zkclient.exception.ZkNodeExistsException)7 MainStemEventData (com.alibaba.otter.shared.arbitrate.model.MainStemEventData)5 ProcessNodeEventData (com.alibaba.otter.shared.arbitrate.model.ProcessNodeEventData)5 ChannelStatus (com.alibaba.otter.shared.common.model.config.channel.ChannelStatus)5 IOException (java.io.IOException)4 Node (com.alibaba.otter.shared.common.model.config.node.Node)3 ArrayList (java.util.ArrayList)3 ZkInterruptedException (org.I0Itec.zkclient.exception.ZkInterruptedException)3 TerminMonitor (com.alibaba.otter.shared.arbitrate.impl.setl.monitor.TerminMonitor)2 InetSocketAddress (java.net.InetSocketAddress)2 Date (java.util.Date)2 List (java.util.List)2 Lock (java.util.concurrent.locks.Lock)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 ZkBadVersionException (org.I0Itec.zkclient.exception.ZkBadVersionException)2