Search in sources :

Example 1 with ZkBadVersionException

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

the class TestExecutor method compareAndSetZnode.

private static boolean compareAndSetZnode(ZnodeValue expect, ZnodeOpArg arg, ZkClient zkClient, ZNRecord diff) {
    String path = arg._znodePath;
    ZnodePropertyType type = arg._propertyType;
    String key = arg._key;
    boolean success = true;
    // retry 3 times in case there are write conflicts
    // ms
    long backoffTime = 20;
    for (int i = 0; i < 3; i++) {
        try {
            Stat stat = new Stat();
            ZNRecord record = zkClient.<ZNRecord>readDataAndStat(path, stat, true);
            if (isValueExpected(record, type, key, expect, diff)) {
                if (arg._operation.compareTo("+") == 0) {
                    if (record == null) {
                        record = new ZNRecord("default");
                    }
                    ZnodeModValueType valueType = getValueType(arg._propertyType, arg._key);
                    switch(valueType) {
                        case SINGLE_VALUE:
                            setSingleValue(record, arg._propertyType, arg._key, arg._updateValue._singleValue);
                            break;
                        case LIST_VALUE:
                            setListValue(record, arg._key, arg._updateValue._listValue);
                            break;
                        case MAP_VALUE:
                            setMapValue(record, arg._key, arg._updateValue._mapValue);
                            break;
                        case ZNODE_VALUE:
                            // deep copy
                            record = ZNRECORD_SERIALIZER.deserialize(ZNRECORD_SERIALIZER.serialize(arg._updateValue._znodeValue));
                            break;
                        case INVALID:
                            break;
                        default:
                            break;
                    }
                } else if (arg._operation.compareTo("-") == 0) {
                    ZnodeModValueType valueType = getValueType(arg._propertyType, arg._key);
                    switch(valueType) {
                        case SINGLE_VALUE:
                            removeSingleValue(record, arg._propertyType, arg._key);
                            break;
                        case LIST_VALUE:
                            removeListValue(record, arg._key);
                            break;
                        case MAP_VALUE:
                            removeMapValue(record, arg._key);
                            break;
                        case ZNODE_VALUE:
                            record = null;
                            break;
                        case INVALID:
                            break;
                        default:
                            break;
                    }
                } else {
                    logger.warn("fail to execute (unsupport operation): " + arg._operation);
                    success = false;
                }
                if (success == true) {
                    if (record == null) {
                        zkClient.delete(path);
                    } else {
                        try {
                            zkClient.createPersistent(path, true);
                        } catch (ZkNodeExistsException e) {
                        // OK
                        }
                        zkClient.writeData(path, record, stat.getVersion());
                    }
                    return true;
                } else {
                    return false;
                }
            }
        } catch (ZkBadVersionException e) {
        // e.printStackTrace();
        } catch (PropertyStoreException e) {
        // e.printStackTrace();
        }
        try {
            Thread.sleep(backoffTime);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        backoffTime *= 2;
    }
    return false;
}
Also used : ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException) Stat(org.apache.zookeeper.data.Stat) PropertyStoreException(org.apache.helix.store.PropertyStoreException) ZkBadVersionException(org.I0Itec.zkclient.exception.ZkBadVersionException) ZNRecord(org.apache.helix.ZNRecord)

Example 2 with ZkBadVersionException

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

the class ZkBaseDataAccessor method doSet.

/**
 * sync set
 */
public AccessResult doSet(String path, T record, int expectVersion, int options) {
    AccessResult result = new AccessResult();
    CreateMode mode = AccessOption.getMode(options);
    if (mode == null) {
        LOG.error("Invalid set mode. options: " + options);
        result._retCode = RetCode.ERROR;
        return result;
    }
    boolean retry;
    do {
        retry = false;
        try {
            Stat stat = _zkClient.writeDataGetStat(path, record, expectVersion);
            DataTree.copyStat(stat, result._stat);
        } catch (ZkNoNodeException e) {
            // node not exists, try create if expectedVersion == -1; in this case, stat will not be set
            if (expectVersion != -1) {
                LOG.error("Could not create node if expectVersion != -1, was " + expectVersion);
                result._retCode = RetCode.ERROR;
                return result;
            }
            try {
                // may create recursively
                AccessResult res = doCreate(path, record, options);
                result._pathCreated.addAll(res._pathCreated);
                RetCode rc = res._retCode;
                switch(rc) {
                    case OK:
                        // not set stat if node is created (instead of set)
                        break;
                    case NODE_EXISTS:
                        retry = true;
                        break;
                    default:
                        LOG.error("Fail to set path by creating: " + path);
                        result._retCode = RetCode.ERROR;
                        return result;
                }
            } catch (Exception e1) {
                LOG.error("Exception while setting path by creating: " + path, e);
                result._retCode = RetCode.ERROR;
                return result;
            }
        } catch (ZkBadVersionException e) {
            LOG.debug("Exception while setting path: " + path, e);
            throw e;
        } catch (Exception e) {
            LOG.error("Exception while setting path: " + path, e);
            result._retCode = RetCode.ERROR;
            return result;
        }
    } while (retry);
    result._retCode = RetCode.OK;
    return result;
}
Also used : Stat(org.apache.zookeeper.data.Stat) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) CreateMode(org.apache.zookeeper.CreateMode) ZkBadVersionException(org.I0Itec.zkclient.exception.ZkBadVersionException) ZkException(org.I0Itec.zkclient.exception.ZkException) HelixMetaDataAccessException(org.apache.helix.api.exceptions.HelixMetaDataAccessException) HelixException(org.apache.helix.HelixException) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException) ZkBadVersionException(org.I0Itec.zkclient.exception.ZkBadVersionException)

Example 3 with ZkBadVersionException

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

the class ZkClient 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.
 */
@SuppressWarnings("unchecked")
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)

Example 4 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)

Example 5 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)

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