Search in sources :

Example 6 with CreateMode

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.CreateMode in project helix by apache.

the class ZKUtil method createOrUpdate.

public static void createOrUpdate(ZkClient client, String path, final ZNRecord record, final boolean persistent, final boolean mergeOnUpdate) {
    int retryCount = 0;
    while (retryCount < RETRYLIMIT) {
        try {
            if (client.exists(path)) {
                DataUpdater<ZNRecord> updater = new DataUpdater<ZNRecord>() {

                    @Override
                    public ZNRecord update(ZNRecord currentData) {
                        if (currentData != null && mergeOnUpdate) {
                            currentData.update(record);
                            return currentData;
                        }
                        return record;
                    }
                };
                client.updateDataSerialized(path, updater);
            } else {
                CreateMode mode = (persistent) ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL;
                client.create(path, record, mode);
            }
            break;
        } catch (Exception e) {
            retryCount = retryCount + 1;
            logger.warn("Exception trying to update " + path + " Exception:" + e.getMessage() + ". Will retry.");
        }
    }
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) DataUpdater(org.I0Itec.zkclient.DataUpdater) ZNRecord(org.apache.helix.ZNRecord) HelixException(org.apache.helix.HelixException)

Example 7 with CreateMode

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.CreateMode in project helix by apache.

the class ZKUtil method asyncCreateOrMerge.

public static void asyncCreateOrMerge(ZkClient client, String path, final ZNRecord record, final boolean persistent, final boolean mergeOnUpdate) {
    try {
        if (client.exists(path)) {
            if (mergeOnUpdate) {
                ZNRecord curRecord = client.readData(path);
                if (curRecord != null) {
                    curRecord.merge(record);
                    client.asyncSetData(path, curRecord, -1, null);
                } else {
                    client.asyncSetData(path, record, -1, null);
                }
            } else {
                client.asyncSetData(path, record, -1, null);
            }
        } else {
            CreateMode mode = (persistent) ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL;
            if (record.getDeltaList().size() > 0) {
                ZNRecord newRecord = new ZNRecord(record.getId());
                newRecord.merge(record);
                client.create(path, null, mode);
                client.asyncSetData(path, newRecord, -1, null);
            } else {
                client.create(path, null, mode);
                client.asyncSetData(path, record, -1, null);
            }
        }
    } catch (Exception e) {
        logger.error("Exception in async create or update " + path + ". Exception: " + e.getMessage() + ". Give up.");
    }
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) ZNRecord(org.apache.helix.ZNRecord) HelixException(org.apache.helix.HelixException)

Example 8 with CreateMode

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.CreateMode in project helix by apache.

the class ZKUtil method createOrMerge.

public static void createOrMerge(ZkClient client, String path, final ZNRecord record, final boolean persistent, final boolean mergeOnUpdate) {
    int retryCount = 0;
    while (retryCount < RETRYLIMIT) {
        try {
            if (client.exists(path)) {
                DataUpdater<ZNRecord> updater = new DataUpdater<ZNRecord>() {

                    @Override
                    public ZNRecord update(ZNRecord currentData) {
                        if (currentData != null && mergeOnUpdate) {
                            currentData.merge(record);
                            return currentData;
                        }
                        return record;
                    }
                };
                client.updateDataSerialized(path, updater);
            } else {
                CreateMode mode = (persistent) ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL;
                if (record.getDeltaList().size() > 0) {
                    ZNRecord value = new ZNRecord(record.getId());
                    value.merge(record);
                    client.create(path, value, mode);
                } else {
                    client.create(path, record, mode);
                }
            }
            break;
        } catch (Exception e) {
            retryCount = retryCount + 1;
            logger.warn("Exception trying to update " + path + " Exception:" + e.getMessage() + ". Will retry.");
        }
    }
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) DataUpdater(org.I0Itec.zkclient.DataUpdater) ZNRecord(org.apache.helix.ZNRecord) HelixException(org.apache.helix.HelixException)

Example 9 with CreateMode

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.CreateMode in project helix by apache.

the class ZkBaseDataAccessor method update.

/**
 * async update
 * return: updatedData on success or null on fail
 */
List<T> update(List<String> paths, List<DataUpdater<T>> updaters, List<List<String>> pathsCreated, List<Stat> stats, int options) {
    if (paths == null || paths.size() == 0) {
        LOG.error("paths is null or empty");
        return Collections.emptyList();
    }
    if (updaters.size() != paths.size() || (pathsCreated != null && pathsCreated.size() != paths.size())) {
        throw new IllegalArgumentException("paths, updaters, and pathsCreated should be of same size");
    }
    List<Stat> setStats = new ArrayList<Stat>(Collections.<Stat>nCopies(paths.size(), null));
    List<T> updateData = new ArrayList<T>(Collections.<T>nCopies(paths.size(), null));
    CreateMode mode = AccessOption.getMode(options);
    if (mode == null) {
        LOG.error("Invalid update mode. options: " + options);
        return updateData;
    }
    SetDataCallbackHandler[] cbList = new SetDataCallbackHandler[paths.size()];
    CreateCallbackHandler[] createCbList = null;
    boolean[] needUpdate = new boolean[paths.size()];
    Arrays.fill(needUpdate, true);
    long startT = System.nanoTime();
    try {
        boolean retry;
        do {
            retry = false;
            // init'ed with false
            boolean[] needCreate = new boolean[paths.size()];
            boolean failOnNoNode = false;
            // asycn read all data
            List<Stat> curStats = new ArrayList<Stat>();
            List<T> curDataList = get(paths, curStats, Arrays.copyOf(needUpdate, needUpdate.length), false);
            // async update
            List<T> newDataList = new ArrayList<T>();
            for (int i = 0; i < paths.size(); i++) {
                if (!needUpdate[i]) {
                    newDataList.add(null);
                    continue;
                }
                String path = paths.get(i);
                DataUpdater<T> updater = updaters.get(i);
                T newData = updater.update(curDataList.get(i));
                newDataList.add(newData);
                if (newData == null) {
                    // No need to create or update if the updater does not return a new version
                    continue;
                }
                Stat curStat = curStats.get(i);
                if (curStat == null) {
                    // node not exists
                    failOnNoNode = true;
                    needCreate[i] = true;
                } else {
                    cbList[i] = new SetDataCallbackHandler();
                    _zkClient.asyncSetData(path, newData, curStat.getVersion(), cbList[i]);
                }
            }
            // wait for completion
            boolean failOnBadVersion = false;
            for (int i = 0; i < paths.size(); i++) {
                SetDataCallbackHandler cb = cbList[i];
                if (cb == null)
                    continue;
                cb.waitForSuccess();
                switch(Code.get(cb.getRc())) {
                    case OK:
                        updateData.set(i, newDataList.get(i));
                        setStats.set(i, cb.getStat());
                        needUpdate[i] = false;
                        break;
                    case NONODE:
                        failOnNoNode = true;
                        needCreate[i] = true;
                        break;
                    case BADVERSION:
                        failOnBadVersion = true;
                        break;
                    default:
                        // if fail on error other than NoNode or BadVersion
                        // will not retry
                        needUpdate[i] = false;
                        break;
                }
            }
            // if failOnNoNode, try create
            if (failOnNoNode) {
                createCbList = create(paths, newDataList, needCreate, pathsCreated, options);
                for (int i = 0; i < paths.size(); i++) {
                    CreateCallbackHandler createCb = createCbList[i];
                    if (createCb == null) {
                        continue;
                    }
                    switch(Code.get(createCb.getRc())) {
                        case OK:
                            needUpdate[i] = false;
                            updateData.set(i, newDataList.get(i));
                            setStats.set(i, ZNode.ZERO_STAT);
                            break;
                        case NODEEXISTS:
                            retry = true;
                            break;
                        default:
                            // if fail on error other than NodeExists
                            // will not retry
                            needUpdate[i] = false;
                            break;
                    }
                }
            }
            // if failOnBadVersion, retry
            if (failOnBadVersion) {
                retry = true;
            }
        } while (retry);
        if (stats != null) {
            stats.clear();
            stats.addAll(setStats);
        }
        return updateData;
    } finally {
        long endT = System.nanoTime();
        if (LOG.isTraceEnabled()) {
            LOG.trace("setData_async, size: " + paths.size() + ", paths: " + paths.get(0) + ",... time: " + (endT - startT) + " ns");
        }
    }
}
Also used : ArrayList(java.util.ArrayList) CreateCallbackHandler(org.apache.helix.manager.zk.ZkAsyncCallbacks.CreateCallbackHandler) SetDataCallbackHandler(org.apache.helix.manager.zk.ZkAsyncCallbacks.SetDataCallbackHandler) Stat(org.apache.zookeeper.data.Stat) CreateMode(org.apache.zookeeper.CreateMode)

Example 10 with CreateMode

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.CreateMode in project helix by apache.

the class ZkBaseDataAccessor method create.

/**
 * async create. give up on error other than NONODE
 */
CreateCallbackHandler[] create(List<String> paths, List<T> records, boolean[] needCreate, List<List<String>> pathsCreated, int options) {
    if ((records != null && records.size() != paths.size()) || needCreate.length != paths.size() || (pathsCreated != null && pathsCreated.size() != paths.size())) {
        throw new IllegalArgumentException("paths, records, needCreate, and pathsCreated should be of same size");
    }
    CreateCallbackHandler[] cbList = new CreateCallbackHandler[paths.size()];
    CreateMode mode = AccessOption.getMode(options);
    if (mode == null) {
        LOG.error("Invalid async set mode. options: " + options);
        return cbList;
    }
    boolean retry;
    do {
        retry = false;
        for (int i = 0; i < paths.size(); i++) {
            if (!needCreate[i])
                continue;
            String path = paths.get(i);
            T record = records == null ? null : records.get(i);
            cbList[i] = new CreateCallbackHandler();
            _zkClient.asyncCreate(path, record, mode, cbList[i]);
        }
        List<String> parentPaths = new ArrayList<String>(Collections.<String>nCopies(paths.size(), null));
        boolean failOnNoNode = false;
        for (int i = 0; i < paths.size(); i++) {
            if (!needCreate[i])
                continue;
            CreateCallbackHandler cb = cbList[i];
            cb.waitForSuccess();
            String path = paths.get(i);
            if (Code.get(cb.getRc()) == Code.NONODE) {
                String parentPath = HelixUtil.getZkParentPath(path);
                parentPaths.set(i, parentPath);
                failOnNoNode = true;
            } else {
                // if create succeed or fail on error other than NONODE,
                // give up
                needCreate[i] = false;
                // if succeeds, record what paths we've created
                if (Code.get(cb.getRc()) == Code.OK && pathsCreated != null) {
                    if (pathsCreated.get(i) == null) {
                        pathsCreated.set(i, new ArrayList<String>());
                    }
                    pathsCreated.get(i).add(path);
                }
            }
        }
        if (failOnNoNode) {
            boolean[] needCreateParent = Arrays.copyOf(needCreate, needCreate.length);
            CreateCallbackHandler[] parentCbList = create(parentPaths, null, needCreateParent, pathsCreated, AccessOption.PERSISTENT);
            for (int i = 0; i < parentCbList.length; i++) {
                CreateCallbackHandler parentCb = parentCbList[i];
                if (parentCb == null)
                    continue;
                Code rc = Code.get(parentCb.getRc());
                // if parent is created, retry create child
                if (rc == Code.OK || rc == Code.NODEEXISTS) {
                    retry = true;
                    break;
                }
            }
        }
    } while (retry);
    return cbList;
}
Also used : ArrayList(java.util.ArrayList) CreateCallbackHandler(org.apache.helix.manager.zk.ZkAsyncCallbacks.CreateCallbackHandler) Code(org.apache.zookeeper.KeeperException.Code) CreateMode(org.apache.zookeeper.CreateMode)

Aggregations

CreateMode (org.apache.zookeeper.CreateMode)35 KeeperException (org.apache.zookeeper.KeeperException)8 HelixException (org.apache.helix.HelixException)7 Stat (org.apache.zookeeper.data.Stat)7 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Op (org.apache.zookeeper.Op)4 ACL (org.apache.zookeeper.data.ACL)4 DataUpdater (org.I0Itec.zkclient.DataUpdater)3 ZkBadVersionException (org.I0Itec.zkclient.exception.ZkBadVersionException)3 ZkException (org.I0Itec.zkclient.exception.ZkException)3 ZkNoNodeException (org.I0Itec.zkclient.exception.ZkNoNodeException)3 ZkNodeExistsException (org.I0Itec.zkclient.exception.ZkNodeExistsException)3 ZNRecord (org.apache.helix.ZNRecord)3 HelixMetaDataAccessException (org.apache.helix.api.exceptions.HelixMetaDataAccessException)3 CreateCallbackHandler (org.apache.helix.manager.zk.ZkAsyncCallbacks.CreateCallbackHandler)3 Code (org.apache.zookeeper.KeeperException.Code)3 CreateRequest (org.apache.zookeeper.proto.CreateRequest)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 URI (java.net.URI)2