Search in sources :

Example 1 with TerminMonitor

use of com.alibaba.otter.shared.arbitrate.impl.setl.monitor.TerminMonitor 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 2 with TerminMonitor

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

the class TerminZooKeeperArbitrateEvent method exhaust.

/**
 * 消耗掉所有的termin信号
 */
public void exhaust(Long pipelineId) {
    Assert.notNull(pipelineId);
    TerminMonitor terminMonitor = ArbitrateFactory.getInstance(pipelineId, TerminMonitor.class);
    int size = terminMonitor.size();
    try {
        for (int i = 0; i < size; i++) {
            Long processId;
            processId = terminMonitor.waitForProcess();
            TerminEventData data = new TerminEventData();
            data.setPipelineId(pipelineId);
            data.setProcessId(processId);
            ack(data);
        }
    } catch (InterruptedException e) {
        throw new ArbitrateException(e);
    }
}
Also used : TerminEventData(com.alibaba.otter.shared.arbitrate.model.TerminEventData) TerminMonitor(com.alibaba.otter.shared.arbitrate.impl.setl.monitor.TerminMonitor) ArbitrateException(com.alibaba.otter.shared.arbitrate.exception.ArbitrateException)

Example 3 with TerminMonitor

use of com.alibaba.otter.shared.arbitrate.impl.setl.monitor.TerminMonitor 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)

Example 4 with TerminMonitor

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

the class TerminZooKeeperArbitrateEvent method size.

/**
 * 查询当前待处理的termin信号的总数
 */
public int size(Long pipelineId) {
    Assert.notNull(pipelineId);
    TerminMonitor terminMonitor = ArbitrateFactory.getInstance(pipelineId, TerminMonitor.class);
    return terminMonitor.size();
}
Also used : TerminMonitor(com.alibaba.otter.shared.arbitrate.impl.setl.monitor.TerminMonitor)

Example 5 with TerminMonitor

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

the class TerminMonitorTest method test_init.

@Test
public void test_init() {
    initTermin(1L);
    initTermin(2L);
    try {
        terminMonitor = new TerminMonitor(pipelineId);
        Long p1 = terminMonitor.waitForProcess();
        terminMonitor.ack(p1);
        Long p2 = terminMonitor.waitForProcess();
        terminMonitor.ack(p2);
        want.bool(p1.equals(1L)).is(true);
        want.bool(p2.equals(2L)).is(true);
        terminMonitor.destory();
    } catch (InterruptedException e) {
        want.fail();
    } finally {
        destoryTermin(1L);
        destoryTermin(2L);
    }
}
Also used : TerminMonitor(com.alibaba.otter.shared.arbitrate.impl.setl.monitor.TerminMonitor) Test(org.testng.annotations.Test) BaseEventTest(com.alibaba.otter.shared.arbitrate.BaseEventTest)

Aggregations

TerminMonitor (com.alibaba.otter.shared.arbitrate.impl.setl.monitor.TerminMonitor)6 ArbitrateException (com.alibaba.otter.shared.arbitrate.exception.ArbitrateException)3 BaseEventTest (com.alibaba.otter.shared.arbitrate.BaseEventTest)2 ZkException (org.I0Itec.zkclient.exception.ZkException)2 ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)2 Test (org.testng.annotations.Test)2 TerminEventData (com.alibaba.otter.shared.arbitrate.model.TerminEventData)1