Search in sources :

Example 1 with TransformStageListener

use of com.alibaba.otter.shared.arbitrate.impl.setl.zookeeper.monitor.TransformStageListener 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 });
        // 递归调用
        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 2 with TransformStageListener

use of com.alibaba.otter.shared.arbitrate.impl.setl.zookeeper.monitor.TransformStageListener in project otter by alibaba.

the class TransformStageListenerTest method testProcess_init.

@Test
public void testProcess_init() {
    final List<Long> initProcessIds = new ArrayList<Long>();
    final Map<Long, List<String>> stages = new HashMap<Long, List<String>>();
    try {
        Long p1 = initProcess();
        initStage(p1, ArbitrateConstants.NODE_SELECTED);
        initStage(p1, ArbitrateConstants.NODE_EXTRACTED);
        initStage(p1, ArbitrateConstants.NODE_TRANSFORMED);
        Long p2 = initProcess();
        initStage(p2, ArbitrateConstants.NODE_SELECTED);
        initStage(p2, ArbitrateConstants.NODE_EXTRACTED, getData(nid));
        Long p3 = initProcess();
        initStage(p3, ArbitrateConstants.NODE_SELECTED);
        initStage(p3, ArbitrateConstants.NODE_EXTRACTED);
        initStage(p3, ArbitrateConstants.NODE_TRANSFORMED);
        Long p4 = initProcess();
        initStage(p4, ArbitrateConstants.NODE_SELECTED);
        initStage(p4, ArbitrateConstants.NODE_EXTRACTED, getData(nid + 1));
        // 准备清理数据
        initProcessIds.add(p1);
        initProcessIds.add(p2);
        initProcessIds.add(p3);
        initProcessIds.add(p4);
        List<String> p1Stages = Arrays.asList(ArbitrateConstants.NODE_SELECTED, ArbitrateConstants.NODE_EXTRACTED, ArbitrateConstants.NODE_TRANSFORMED);
        stages.put(p1, p1Stages);
        List<String> p2Stages = Arrays.asList(ArbitrateConstants.NODE_SELECTED, ArbitrateConstants.NODE_EXTRACTED);
        stages.put(p2, p2Stages);
        List<String> p3Stages = Arrays.asList(ArbitrateConstants.NODE_SELECTED, ArbitrateConstants.NODE_EXTRACTED, ArbitrateConstants.NODE_TRANSFORMED);
        stages.put(p3, p3Stages);
        List<String> p4Stages = Arrays.asList(ArbitrateConstants.NODE_SELECTED, ArbitrateConstants.NODE_EXTRACTED);
        stages.put(p4, p4Stages);
        // 进行验证
        TransformStageListener transform = new TransformStageListener(pipelineId);
        Long processId = transform.waitForProcess();
        want.number(processId).isEqualTo(p2);
        // 验证下process信息
        StageMonitor monitor = ArbitrateFactory.getInstance(pipelineId, StageMonitor.class);
        List<Long> processIds = monitor.getCurrentProcessIds();
        // 获取下stage信息
        List<String> currentP1Stages = monitor.getCurrentStages(p1);
        List<String> currentP2Stages = monitor.getCurrentStages(p2);
        List<String> currentP3Stages = monitor.getCurrentStages(p3);
        List<String> currentP4Stages = monitor.getCurrentStages(p4);
        want.collection(processIds).isEqualTo(initProcessIds);
        want.collection(currentP1Stages).isEqualTo(stages.get(p1));
        want.collection(currentP2Stages).isEqualTo(stages.get(p2));
        want.collection(currentP3Stages).isEqualTo(stages.get(p3));
        want.collection(currentP4Stages).isEqualTo(stages.get(p4));
        transform.destory();
        ArbitrateFactory.destory(pipelineId);
    } catch (InterruptedException e) {
        want.fail();
    } finally {
        for (Long processId : initProcessIds) {
            List<String> ss = stages.get(processId);
            for (String stage : ss) {
                destoryStage(processId, stage);
            }
            destoryProcess(processId);
        }
    }
}
Also used : TransformStageListener(com.alibaba.otter.shared.arbitrate.impl.setl.zookeeper.monitor.TransformStageListener) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) StageMonitor(com.alibaba.otter.shared.arbitrate.impl.setl.zookeeper.monitor.StageMonitor) Test(org.testng.annotations.Test) BaseStageTest(com.alibaba.otter.shared.arbitrate.setl.BaseStageTest)

Example 3 with TransformStageListener

use of com.alibaba.otter.shared.arbitrate.impl.setl.zookeeper.monitor.TransformStageListener in project otter by alibaba.

the class TransformStageListenerTest method testProcess_dynamic.

@Test
public void testProcess_dynamic() {
    final List<Long> initProcessIds = new ArrayList<Long>();
    final Map<Long, List<String>> stages = new HashMap<Long, List<String>>();
    try {
        // 准备数据,准备5个process,一个已经完成extracted,一个满足条件,一个出现error,一个出现end,一个stage满足但不是自己机器处理
        Long p1 = initProcess();
        initStage(p1, ArbitrateConstants.NODE_SELECTED);
        initStage(p1, ArbitrateConstants.NODE_EXTRACTED);
        initStage(p1, ArbitrateConstants.NODE_TRANSFORMED);
        Long p2 = initProcess();
        initStage(p2, ArbitrateConstants.NODE_SELECTED);
        initStage(p2, ArbitrateConstants.NODE_EXTRACTED, getData(nid + 1));
        // 初始化信息
        TransformStageListener transform = new TransformStageListener(pipelineId);
        // 开始变化
        destoryStage(p1, ArbitrateConstants.NODE_SELECTED);
        destoryStage(p1, ArbitrateConstants.NODE_EXTRACTED);
        destoryStage(p1, ArbitrateConstants.NODE_TRANSFORMED);
        destoryProcess(p1);
        Long p3 = initProcess();
        initStage(p3, ArbitrateConstants.NODE_SELECTED);
        initStage(p3, ArbitrateConstants.NODE_EXTRACTED, getData(nid));
        Long p4 = initProcess();
        // 准备清理数据
        initProcessIds.add(p2);
        initProcessIds.add(p3);
        initProcessIds.add(p4);
        List<String> p1Stages = Lists.newArrayList();
        stages.put(p1, p1Stages);
        List<String> p2Stages = Arrays.asList(ArbitrateConstants.NODE_SELECTED, ArbitrateConstants.NODE_EXTRACTED);
        stages.put(p2, p2Stages);
        List<String> p3Stages = Arrays.asList(ArbitrateConstants.NODE_SELECTED, ArbitrateConstants.NODE_EXTRACTED);
        stages.put(p3, p3Stages);
        List<String> p4Stages = Lists.newArrayList();
        stages.put(p4, p4Stages);
        // sleep一下,等待数据同步
        sleep();
        // 进行验证
        Long processId = transform.waitForProcess();
        want.number(processId).isEqualTo(p3);
        // 验证下process信息
        StageMonitor monitor = ArbitrateFactory.getInstance(pipelineId, StageMonitor.class);
        List<Long> processIds = monitor.getCurrentProcessIds();
        // 获取下stage信息
        List<String> currentP1Stages = monitor.getCurrentStages(p1);
        List<String> currentP2Stages = monitor.getCurrentStages(p2);
        List<String> currentP3Stages = monitor.getCurrentStages(p3);
        List<String> currentP4Stages = monitor.getCurrentStages(p4);
        want.collection(processIds).isEqualTo(initProcessIds);
        want.collection(currentP1Stages).isEqualTo(stages.get(p1));
        want.collection(currentP2Stages).isEqualTo(stages.get(p2));
        want.collection(currentP3Stages).isEqualTo(stages.get(p3));
        want.collection(currentP4Stages).isEqualTo(stages.get(p4));
        transform.destory();
        ArbitrateFactory.destory(pipelineId);
    } catch (InterruptedException e) {
        want.fail();
    } finally {
        for (Long processId : initProcessIds) {
            List<String> ss = stages.get(processId);
            for (String stage : ss) {
                destoryStage(processId, stage);
            }
            destoryProcess(processId);
        }
    }
}
Also used : TransformStageListener(com.alibaba.otter.shared.arbitrate.impl.setl.zookeeper.monitor.TransformStageListener) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) StageMonitor(com.alibaba.otter.shared.arbitrate.impl.setl.zookeeper.monitor.StageMonitor) Test(org.testng.annotations.Test) BaseStageTest(com.alibaba.otter.shared.arbitrate.setl.BaseStageTest)

Aggregations

TransformStageListener (com.alibaba.otter.shared.arbitrate.impl.setl.zookeeper.monitor.TransformStageListener)3 StageMonitor (com.alibaba.otter.shared.arbitrate.impl.setl.zookeeper.monitor.StageMonitor)2 BaseStageTest (com.alibaba.otter.shared.arbitrate.setl.BaseStageTest)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Test (org.testng.annotations.Test)2 ArbitrateException (com.alibaba.otter.shared.arbitrate.exception.ArbitrateException)1 PermitMonitor (com.alibaba.otter.shared.arbitrate.impl.setl.monitor.PermitMonitor)1 EtlEventData (com.alibaba.otter.shared.arbitrate.model.EtlEventData)1 ChannelStatus (com.alibaba.otter.shared.common.model.config.channel.ChannelStatus)1 ZkException (org.I0Itec.zkclient.exception.ZkException)1 ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)1