Search in sources :

Example 21 with ZkException

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

the class ZooKeeperx method connect.

@Override
public void connect(Watcher watcher) {
    ReflectionUtils.makeAccessible(zookeeperLockField);
    ReflectionUtils.makeAccessible(zookeeperFiled);
    Lock _zookeeperLock = (ReentrantLock) ReflectionUtils.getField(zookeeperLockField, this);
    ZooKeeper _zk = (ZooKeeper) ReflectionUtils.getField(zookeeperFiled, this);
    _zookeeperLock.lock();
    try {
        if (_zk != null) {
            throw new IllegalStateException("zk client has already been started");
        }
        String zkServers = _serversList.get(0);
        try {
            logger.debug("Creating new ZookKeeper instance to connect to " + zkServers + ".");
            _zk = new ZooKeeper(zkServers, _sessionTimeOut, watcher);
            configMutliCluster(_zk);
            ReflectionUtils.setField(zookeeperFiled, this, _zk);
        } catch (IOException e) {
            throw new ZkException("Unable to connect to " + zkServers, e);
        }
    } finally {
        _zookeeperLock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ZooKeeper(org.apache.zookeeper.ZooKeeper) ZkException(org.I0Itec.zkclient.exception.ZkException) IOException(java.io.IOException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 22 with ZkException

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

the class ZooKeeperx method configMutliCluster.

// ===============================
public void configMutliCluster(ZooKeeper zk) {
    if (_serversList.size() == 1) {
        return;
    }
    String cluster1 = _serversList.get(0);
    try {
        if (_serversList.size() > 1) {
            // 强制的声明accessible
            ReflectionUtils.makeAccessible(clientCnxnField);
            ReflectionUtils.makeAccessible(hostProviderField);
            ReflectionUtils.makeAccessible(serverAddressesField);
            // 添加第二组集群列表
            for (int i = 1; i < _serversList.size(); i++) {
                String cluster = _serversList.get(i);
                // 强制获取zk中的地址信息
                ClientCnxn cnxn = (ClientCnxn) ReflectionUtils.getField(clientCnxnField, zk);
                HostProvider hostProvider = (HostProvider) ReflectionUtils.getField(hostProviderField, cnxn);
                List<InetSocketAddress> serverAddrs = (List<InetSocketAddress>) ReflectionUtils.getField(serverAddressesField, hostProvider);
                // 添加第二组集群列表
                serverAddrs.addAll(new ConnectStringParser(cluster).getServerAddresses());
            }
        }
    } catch (Exception e) {
        try {
            if (zk != null) {
                zk.close();
            }
        } catch (InterruptedException ie) {
        // ignore interrupt
        }
        throw new ZkException("zookeeper_create_error, serveraddrs=" + cluster1, e);
    }
}
Also used : ConnectStringParser(org.apache.zookeeper.client.ConnectStringParser) ZkException(org.I0Itec.zkclient.exception.ZkException) InetSocketAddress(java.net.InetSocketAddress) StaticHostProvider(org.apache.zookeeper.client.StaticHostProvider) HostProvider(org.apache.zookeeper.client.HostProvider) List(java.util.List) ClientCnxn(org.apache.zookeeper.ClientCnxn) ZkException(org.I0Itec.zkclient.exception.ZkException) IOException(java.io.IOException)

Example 23 with ZkException

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

the class ExtractZooKeeperArbitrateEvent method await.

// private TerminArbitrateEvent terminEvent;
/**
 * <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();
    ExtractStageListener extractStageListener = ArbitrateFactory.getInstance(pipelineId, ExtractStageListener.class);
    // 符合条件的processId
    Long processId = extractStageListener.waitForProcess();
    ChannelStatus status = permitMonitor.getChannelPermit();
    if (status.isStart()) {
        // 即时查询一下当前的状态,状态随时可能会变
        // 根据pipelineId+processId构造对应的path
        String path = StagePathUtils.getSelectStage(pipelineId, processId);
        try {
            byte[] data = zookeeper.readData(path);
            EtlEventData eventData = JsonUtils.unmarshalFromByte(data, EtlEventData.class);
            // 获取下一个处理节点信息
            Node node = LoadBalanceFactory.getNextTransformNode(pipelineId);
            if (node == null) {
                // terminEvent.single(termin);
                throw new ArbitrateException("Extract_single", "no next node");
            } else {
                eventData.setNextNid(node.getId());
                // 只有这一条路返回
                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("Extract_await", e.getMessage(), e);
        }
    } else {
        logger.warn("pipelineId[{}] extract 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 : ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) ZkException(org.I0Itec.zkclient.exception.ZkException) PermitMonitor(com.alibaba.otter.shared.arbitrate.impl.setl.monitor.PermitMonitor) Node(com.alibaba.otter.shared.common.model.config.node.Node) ArbitrateException(com.alibaba.otter.shared.arbitrate.exception.ArbitrateException) ChannelStatus(com.alibaba.otter.shared.common.model.config.channel.ChannelStatus) ExtractStageListener(com.alibaba.otter.shared.arbitrate.impl.setl.zookeeper.monitor.ExtractStageListener) EtlEventData(com.alibaba.otter.shared.arbitrate.model.EtlEventData)

Example 24 with ZkException

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

the class SelectProcessListener method recovery.

/**
 * 尝试载入一下上一次未使用的processId,可能发生mainstem切换,新的S模块需要感知前S模块已创建但未使用的process,不然就是一个死锁。而针对已经使用的processId会由e/t/l节点进行处理
 */
private void recovery(Long pipelineId) {
    List<Long> currentProcessIds = processMonitor.getCurrentProcessIds(false);
    for (Long processId : currentProcessIds) {
        String path = StagePathUtils.getProcess(pipelineId, processId);
        try {
            byte[] bytes = zookeeper.readData(path);
            ProcessNodeEventData nodeData = JsonUtils.unmarshalFromByte(bytes, ProcessNodeEventData.class);
            if (nodeData.getStatus().isUnUsed()) {
                // 加入未使用的processId
                addReply(processId);
            }
        } catch (ZkException e) {
            logger.error("recovery error!", e);
        }
    }
}
Also used : ZkException(org.I0Itec.zkclient.exception.ZkException) ProcessNodeEventData(com.alibaba.otter.shared.arbitrate.model.ProcessNodeEventData)

Example 25 with ZkException

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

the class LoadZooKeeperArbitrateEvent method await.

// private Map<Long, DistributedLock> locks = new ConcurrentHashMap<Long,
// DistributedLock>();
/**
 * <pre>
 * 算法:
 * 1. 检查当前的Permit,阻塞等待其授权(解决Channel的pause状态处理)
 * 2. 开始阻塞获取符合条件的processId
 * 3. 检查当前的即时Permit状态 (在阻塞获取processId过程会出现一些error信号,process节点会被删除)
 * 4. 获取Select传递的EventData数据,添加next node信息后直接返回
 * </pre>
 */
public EtlEventData await(Long pipelineId) throws InterruptedException {
    Assert.notNull(pipelineId);
    PermitMonitor permitMonitor = ArbitrateFactory.getInstance(pipelineId, PermitMonitor.class);
    // 阻塞等待授权
    permitMonitor.waitForPermit();
    LoadStageListener loadStageListener = ArbitrateFactory.getInstance(pipelineId, LoadStageListener.class);
    // 符合条件的processId
    Long processId = loadStageListener.waitForProcess();
    // DistributedLock lock = getLock(pipelineId);
    try {
        // 使用锁的理由:
        // 1. 针对双向同步时,其中一个方向出现了异常,需要发起另一端的关闭,此时对方正好在执行某个process的load
        // 2. 单向同步时,如果出现node节点异常,此时正常的节点正在执行某个process的load
        // 为避免因load无法中端引起的数据重复录入,所以针对load阶段添加分布式锁。在有process
        // load过程中不允许进行pipeline关闭操作
        // lock.lock();
        ChannelStatus status = permitMonitor.getChannelPermit();
        if (status.isStart()) {
            // 即时查询一下当前的状态,状态随时可能会变
            // 根据pipelineId+processId构造对应的path
            String path = StagePathUtils.getTransformStage(pipelineId, processId);
            try {
                byte[] data = zookeeper.readData(path);
                // 反序列化并返回
                return JsonUtils.unmarshalFromByte(data, EtlEventData.class);
            } catch (ZkNoNodeException e) {
                logger.error("pipeline[{}] processId[{}] is invalid , retry again", pipelineId, processId);
                // /出现节点不存在,说明出现了error情况,递归调用重新获取一次
                return await(pipelineId);
            } catch (ZkException e) {
                throw e;
            }
        } else {
            logger.warn("pipelineId[{}] load ignore processId[{}] by status[{}]", new Object[] { pipelineId, processId, status });
            // try {
            // lock.unlock();// 出现任何异常解除lock
            // } catch (KeeperException e) {
            // // ignore
            // }
            // 释放下processId,因为load是等待processId最小值完成Tranform才继续,如果这里不释放,会一直卡死等待
            String path = StagePathUtils.getProcess(pipelineId, processId);
            zookeeper.delete(path);
            // 出现rollback情况,递归调用重新获取一次,当前的processId可丢弃
            return await(pipelineId);
        }
    } catch (InterruptedException e) {
        throw e;
    } catch (Exception e) {
        throw new ArbitrateException(e);
    }
}
Also used : ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) LoadStageListener(com.alibaba.otter.shared.arbitrate.impl.setl.zookeeper.monitor.LoadStageListener) 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) ArbitrateException(com.alibaba.otter.shared.arbitrate.exception.ArbitrateException) ZkException(org.I0Itec.zkclient.exception.ZkException) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException)

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