Search in sources :

Example 1 with MainStemEventData

use of com.alibaba.otter.shared.arbitrate.model.MainStemEventData 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 2 with MainStemEventData

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

the class MainstemMonitor method check.

/**
     * 检查当前的状态
     */
public boolean check() {
    String path = StagePathUtils.getMainStem(getPipelineId());
    try {
        byte[] bytes = zookeeper.readData(path);
        Long nid = ArbitrateConfigUtils.getCurrentNid();
        MainStemEventData eventData = JsonUtils.unmarshalFromByte(bytes, MainStemEventData.class);
        // 更新下为最新值
        activeData = eventData;
        // 检查下nid是否为自己
        boolean result = nid.equals(eventData.getNid());
        if (!result) {
            logger.warn("mainstem is running in node[{}] , but not in node[{}]", eventData.getNid(), nid);
        }
        return result;
    } catch (ZkNoNodeException e) {
        logger.warn("mainstem is not run any in node");
        return false;
    } catch (ZkInterruptedException e) {
        logger.warn("mainstem check is interrupt");
        // 清除interrupt标记
        Thread.interrupted();
        return check();
    } catch (ZkException e) {
        logger.warn("mainstem check is failed");
        return false;
    }
}
Also used : ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) ZkException(org.I0Itec.zkclient.exception.ZkException) MainStemEventData(com.alibaba.otter.shared.arbitrate.model.MainStemEventData) ZkInterruptedException(org.I0Itec.zkclient.exception.ZkInterruptedException)

Example 3 with MainStemEventData

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

the class MainstemMonitor method initMainstem.

public void initMainstem() {
    if (isStop()) {
        return;
    }
    PermitMonitor permitMonitor = ArbitrateFactory.getInstance(getPipelineId(), PermitMonitor.class);
    ChannelStatus status = permitMonitor.getChannelPermit(true);
    if (status.isStop()) {
        // 如果已经关闭则退出
        return;
    }
    Long nid = ArbitrateConfigUtils.getCurrentNid();
    String path = StagePathUtils.getMainStem(getPipelineId());
    MainStemEventData data = new MainStemEventData();
    data.setStatus(MainStemEventData.Status.TAKEING);
    data.setPipelineId(getPipelineId());
    // 设置当前的nid
    data.setNid(nid);
    // 序列化
    byte[] bytes = JsonUtils.marshalToByte(data);
    try {
        mutex.set(false);
        zookeeper.create(path, bytes, CreateMode.EPHEMERAL);
        activeData = data;
        // 触发一下事件
        processActiveEnter();
        mutex.set(true);
    } catch (ZkNodeExistsException e) {
        bytes = zookeeper.readData(path, true);
        if (bytes == null) {
            // 如果不存在节点,立即尝试一次
            initMainstem();
        } else {
            activeData = JsonUtils.unmarshalFromByte(bytes, MainStemEventData.class);
            if (nid.equals(activeData.getNid())) {
                // reload时会重复创建,如果是自己就触发一下
                mutex.set(true);
            }
        }
    }
}
Also used : ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException) MainStemEventData(com.alibaba.otter.shared.arbitrate.model.MainStemEventData) ChannelStatus(com.alibaba.otter.shared.common.model.config.channel.ChannelStatus)

Example 4 with MainStemEventData

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

the class PermitMonitor method initOppositeMainStemStatus.

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

Example 5 with MainStemEventData

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

the class OtterDownStreamHandler method startDetecting.

private void startDetecting() {
    // 直接发送已追上的状态,保持和eromanga兼容处理
    MainStemEventData mainStemData = new MainStemEventData();
    mainStemData.setPipelineId(pipelineId);
    mainStemData.setStatus(MainStemEventData.Status.OVERTAKE);
    arbitrateEventService.mainStemEvent().single(mainStemData);
    // 启动异步线程定时监控,一定会有数据过来
    String schedulerName = String.format("pipelineId = %s , CanalDetecting", String.valueOf(pipelineId));
    scheduler = Executors.newScheduledThreadPool(1, new NamedThreadFactory(schedulerName));
    future = scheduler.scheduleAtFixedRate(new Runnable() {

        public void run() {
            try {
                MDC.put(OtterConstants.splitPipelineLogFileKey, String.valueOf(pipelineId));
                // (因为会有心跳包数据,理论上时间间隔会小于一定值)
                if (isDelayed(System.currentTimeMillis(), lastEventExecuteTime)) {
                    notifyFailed();
                } else {
                    notifySuccessed();
                }
            } catch (Exception e) {
                logger.error("heartbeat check failed!", e);
            } finally {
                MDC.remove(OtterConstants.splitPipelineLogFileKey);
            }
        }
    }, detectingIntervalInSeconds, detectingIntervalInSeconds, TimeUnit.SECONDS);
}
Also used : NamedThreadFactory(com.alibaba.otter.shared.common.utils.thread.NamedThreadFactory) MainStemEventData(com.alibaba.otter.shared.arbitrate.model.MainStemEventData)

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