Search in sources :

Example 6 with ZkBadVersionException

use of org.I0Itec.zkclient.exception.ZkBadVersionException in project samza by apache.

the class ZkUtils method validateZkVersion.

// validate that Zk protocol currently used by the job is the same as in this participant
public void validateZkVersion() {
    // Version of the protocol is written into root znode. If root does not exist yet we need to create one.
    String rootPath = keyBuilder.getRootPath();
    if (!zkClient.exists(rootPath)) {
        try {
            // attempt to create the root with the correct version
            zkClient.createPersistent(rootPath, ZK_PROTOCOL_VERSION);
            LOG.info("Created zk root node: " + rootPath + " with zk version " + ZK_PROTOCOL_VERSION);
            return;
        } catch (ZkNodeExistsException e) {
            // ignoring
            LOG.warn("root path " + rootPath + " already exists.");
        }
    }
    // if exists, verify the version
    Stat stat = new Stat();
    String version = zkClient.readData(rootPath, stat);
    if (version == null) {
        // for backward compatibility, if no value - assume 1.0
        try {
            zkClient.writeData(rootPath, "1.0", stat.getVersion());
        } catch (ZkBadVersionException e) {
        // if the write failed with ZkBadVersionException it means someone else already wrote a version, so we can ignore it.
        }
        // re-read the updated version
        version = zkClient.readData(rootPath);
    }
    LOG.info("Current version for zk root node: " + rootPath + " is " + version + ", expected version is " + ZK_PROTOCOL_VERSION);
    if (!version.equals(ZK_PROTOCOL_VERSION)) {
        throw new SamzaException("ZK Protocol mismatch. Expected " + ZK_PROTOCOL_VERSION + "; found " + version);
    }
}
Also used : ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException) Stat(org.apache.zookeeper.data.Stat) ZkBadVersionException(org.I0Itec.zkclient.exception.ZkBadVersionException) SamzaException(org.apache.samza.SamzaException)

Example 7 with ZkBadVersionException

use of org.I0Itec.zkclient.exception.ZkBadVersionException 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 8 with ZkBadVersionException

use of org.I0Itec.zkclient.exception.ZkBadVersionException 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)

Example 9 with ZkBadVersionException

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

the class ZkClientx method updateDataSerialized.

/**
 * Updates data of an existing znode. The current content of the znode is passed to the {@link DataUpdater} that is
 * passed into this method, which returns the new content. The new content is only written back to ZooKeeper if
 * nobody has modified the given znode in between. If a concurrent change has been detected the new data of the
 * znode is passed to the updater once again until the new contents can be successfully written back to ZooKeeper.
 *
 * @param <T>
 * @param path The path of the znode.
 * @param updater Updater that creates the new contents.
 */
public <T extends Object> void updateDataSerialized(String path, DataUpdater<T> updater) {
    Stat stat = new Stat();
    boolean retry;
    do {
        retry = false;
        try {
            T oldData = (T) readData(path, stat);
            T newData = updater.update(oldData);
            writeData(path, newData, stat.getVersion());
        } catch (ZkBadVersionException e) {
            retry = true;
        }
    } while (retry);
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZkBadVersionException(org.I0Itec.zkclient.exception.ZkBadVersionException)

Aggregations

ZkBadVersionException (org.I0Itec.zkclient.exception.ZkBadVersionException)9 Stat (org.apache.zookeeper.data.Stat)9 ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)5 ZkException (org.I0Itec.zkclient.exception.ZkException)4 ZkNodeExistsException (org.I0Itec.zkclient.exception.ZkNodeExistsException)4 ArbitrateException (com.alibaba.otter.shared.arbitrate.exception.ArbitrateException)2 MainStemEventData (com.alibaba.otter.shared.arbitrate.model.MainStemEventData)2 HelixException (org.apache.helix.HelixException)2 HelixMetaDataAccessException (org.apache.helix.api.exceptions.HelixMetaDataAccessException)2 CreateMode (org.apache.zookeeper.CreateMode)2 ArrayList (java.util.ArrayList)1 ZNRecord (org.apache.helix.ZNRecord)1 PropertyStoreException (org.apache.helix.store.PropertyStoreException)1 SamzaException (org.apache.samza.SamzaException)1