Search in sources :

Example 11 with MainStemEventData

use of com.alibaba.otter.shared.arbitrate.model.MainStemEventData in project otter by alibaba.

the class PermitMonitor method initMainStemStatus.

private void initMainStemStatus(byte[] bytes) {
    MainStemEventData eventData = JsonUtils.unmarshalFromByte(bytes, MainStemEventData.class);
    MainStemEventData.Status newStatus = eventData.getStatus();
    if (logger.isDebugEnabled()) {
        logger.debug("pipeline[{}] new mainStemStatus is [{}]", getPipelineId(), newStatus);
    }
    synchronized (this) {
        if (!mainStemStatus.equals(newStatus)) {
            mainStemStatus = newStatus;
            permitSem();
        }
    }
}
Also used : MainStemEventData(com.alibaba.otter.shared.arbitrate.model.MainStemEventData)

Example 12 with MainStemEventData

use of com.alibaba.otter.shared.arbitrate.model.MainStemEventData in project otter by alibaba.

the class SystemArbitrateEvent method switchWarmup.

/**
     * 手工触发一次主备切换
     */
public void switchWarmup(Long channelId, Long pipelineId) {
    String path = ManagePathUtils.getMainStem(channelId, pipelineId);
    try {
        while (true) {
            Stat stat = new Stat();
            byte[] bytes = zookeeper.readData(path, stat);
            MainStemEventData mainStemData = JsonUtils.unmarshalFromByte(bytes, MainStemEventData.class);
            mainStemData.setActive(false);
            try {
                zookeeper.writeData(path, JsonUtils.marshalToByte(mainStemData), stat.getVersion());
                logger.warn("relase channelId[{}],pipelineId[{}] mainstem successed! ", channelId, pipelineId);
                break;
            } catch (ZkBadVersionException e) {
            // ignore , retrying
            }
        }
    } catch (ZkNoNodeException e) {
    // ignore
    } catch (ZkException e) {
        throw new ArbitrateException("releaseMainStem", pipelineId.toString(), e);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) ZkException(org.I0Itec.zkclient.exception.ZkException) MainStemEventData(com.alibaba.otter.shared.arbitrate.model.MainStemEventData) ZkBadVersionException(org.I0Itec.zkclient.exception.ZkBadVersionException) ArbitrateException(com.alibaba.otter.shared.arbitrate.exception.ArbitrateException)

Example 13 with MainStemEventData

use of com.alibaba.otter.shared.arbitrate.model.MainStemEventData in project otter by alibaba.

the class MainStemArbitrateEvent method await.

/**
     * <pre>
     * 算法:
     * 1. 检查当前的Permit,阻塞等待其授权(解决Channel的pause状态处理)
     * 2. 尝试创建对应的mainStem节点(非持久化节点)
     *  a. 如果创建成功,则直接构造结果返回
     *  b. 如果创建失败,则关注该节点的exist信号. 继续执行步骤2
     * </pre>
     */
public void await(Long pipelineId) throws InterruptedException {
    Assert.notNull(pipelineId);
    PermitMonitor permitMonitor = ArbitrateFactory.getInstance(pipelineId, PermitMonitor.class);
    ChannelStatus status = permitMonitor.getChannelPermit(true);
    boolean isRuning = check(pipelineId);
    if (!status.isStart() && isRuning) {
        // 当前状态不为启动,强制设置为taking,下次授权启动后重新追数据
        MainStemEventData data = new MainStemEventData();
        data.setPipelineId(pipelineId);
        data.setStatus(MainStemEventData.Status.TAKEING);
        single(data);
        // 阻塞等待挂起
        permitMonitor.waitForChannelPermit();
        return;
    } else if (status.isStart() && isRuning) {
        // 正常状态
        return;
    } else if (isRuning == false) {
        if (!status.isStart()) {
            // 阻塞等待挂起
            permitMonitor.waitForChannelPermit();
        }
        MainstemMonitor mainstemMonitor = ArbitrateFactory.getInstance(pipelineId, MainstemMonitor.class);
        // 等待自己成为active
        mainstemMonitor.waitForActive();
        status = permitMonitor.getChannelPermit(false);
        if (status.isStart()) {
            return;
        } else {
            logger.info("pipelineId[{}] mainstem ignore by status[{}]", new Object[] { pipelineId, status });
            // 重新进行check一次
            await(pipelineId);
        }
    }
}
Also used : PermitMonitor(com.alibaba.otter.shared.arbitrate.impl.setl.monitor.PermitMonitor) MainStemEventData(com.alibaba.otter.shared.arbitrate.model.MainStemEventData) ChannelStatus(com.alibaba.otter.shared.common.model.config.channel.ChannelStatus) MainstemMonitor(com.alibaba.otter.shared.arbitrate.impl.setl.monitor.MainstemMonitor)

Example 14 with MainStemEventData

use of com.alibaba.otter.shared.arbitrate.model.MainStemEventData in project otter by alibaba.

the class SelectStageListener 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.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("SelectStageListener", 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 15 with MainStemEventData

use of com.alibaba.otter.shared.arbitrate.model.MainStemEventData in project otter by alibaba.

the class MainStemMonitorTest method switchWarmup.

/**
     * 手工触发一次主备切换
     */
private void switchWarmup(Long channelId, Long pipelineId) {
    String path = ManagePathUtils.getMainStem(channelId, pipelineId);
    try {
        while (true) {
            Stat stat = new Stat();
            byte[] bytes = zookeeper.readData(path, stat);
            MainStemEventData mainStemData = JsonUtils.unmarshalFromByte(bytes, MainStemEventData.class);
            mainStemData.setActive(false);
            try {
                zookeeper.writeData(path, JsonUtils.marshalToByte(mainStemData), stat.getVersion());
                break;
            } catch (ZkBadVersionException e) {
            // ignore , retrying
            }
        }
    } catch (ZkNoNodeException e) {
    // ignore
    } catch (ZkException e) {
        throw new ArbitrateException("releaseMainStem", pipelineId.toString(), e);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) ZkException(org.I0Itec.zkclient.exception.ZkException) MainStemEventData(com.alibaba.otter.shared.arbitrate.model.MainStemEventData) ZkBadVersionException(org.I0Itec.zkclient.exception.ZkBadVersionException) ArbitrateException(com.alibaba.otter.shared.arbitrate.exception.ArbitrateException)

Aggregations

MainStemEventData (com.alibaba.otter.shared.arbitrate.model.MainStemEventData)18 ZkException (org.I0Itec.zkclient.exception.ZkException)5 PermitMonitor (com.alibaba.otter.shared.arbitrate.impl.setl.monitor.PermitMonitor)4 ChannelStatus (com.alibaba.otter.shared.common.model.config.channel.ChannelStatus)4 Pipeline (com.alibaba.otter.shared.common.model.config.pipeline.Pipeline)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)3 ArbitrateException (com.alibaba.otter.shared.arbitrate.exception.ArbitrateException)2 PositionEventData (com.alibaba.otter.shared.arbitrate.model.PositionEventData)2 ProcessNodeEventData (com.alibaba.otter.shared.arbitrate.model.ProcessNodeEventData)2 Channel (com.alibaba.otter.shared.common.model.config.channel.Channel)2 ThroughputStat (com.alibaba.otter.shared.common.model.statistics.throughput.ThroughputStat)2 ZkBadVersionException (org.I0Itec.zkclient.exception.ZkBadVersionException)2 Stat (org.apache.zookeeper.data.Stat)2 TopDelayStat (com.alibaba.otter.manager.biz.statistics.delay.param.TopDelayStat)1 DataStat (com.alibaba.otter.manager.biz.statistics.delay.param.TopDelayStat.DataStat)1 ThroughputCondition (com.alibaba.otter.manager.biz.statistics.throughput.param.ThroughputCondition)1 BaseEventTest (com.alibaba.otter.shared.arbitrate.BaseEventTest)1 ChannelArbitrateEvent (com.alibaba.otter.shared.arbitrate.impl.manage.ChannelArbitrateEvent)1