Search in sources :

Example 31 with CreateMode

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

the class ZKUtil method createOrReplace.

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

                    @Override
                    public Object update(Object 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 createOrReplace " + path + " Exception:" + e.getMessage() + ". Will retry.");
        }
    }
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) DataUpdater(org.I0Itec.zkclient.DataUpdater) HelixException(org.apache.helix.HelixException)

Example 32 with CreateMode

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

the class ZkBaseDataAccessor method doUpdate.

/**
 * sync update
 */
public AccessResult doUpdate(String path, DataUpdater<T> updater, int options) {
    AccessResult result = new AccessResult();
    CreateMode mode = AccessOption.getMode(options);
    if (mode == null) {
        LOG.error("Invalid update mode. options: " + options);
        result._retCode = RetCode.ERROR;
        return result;
    }
    boolean retry;
    T updatedData = null;
    do {
        retry = false;
        try {
            Stat readStat = new Stat();
            T oldData = (T) _zkClient.readData(path, readStat);
            T newData = updater.update(oldData);
            if (newData != null) {
                Stat setStat = _zkClient.writeDataGetStat(path, newData, readStat.getVersion());
                DataTree.copyStat(setStat, result._stat);
            }
            updatedData = newData;
        } catch (ZkBadVersionException e) {
            retry = true;
        } catch (ZkNoNodeException e) {
            // node not exist, try create, pass null to updater
            try {
                T newData = updater.update(null);
                RetCode rc;
                if (newData != null) {
                    AccessResult res = doCreate(path, newData, options);
                    result._pathCreated.addAll(res._pathCreated);
                    rc = res._retCode;
                } else {
                    // If update returns null, no need to create.
                    rc = RetCode.OK;
                }
                switch(rc) {
                    case OK:
                        updatedData = newData;
                        break;
                    case NODE_EXISTS:
                        retry = true;
                        break;
                    default:
                        LOG.error("Fail to update path by creating: " + path);
                        result._retCode = RetCode.ERROR;
                        return result;
                }
            } catch (Exception e1) {
                LOG.error("Exception while updating path by creating: " + path, e1);
                result._retCode = RetCode.ERROR;
                return result;
            }
        } catch (Exception e) {
            LOG.error("Exception while updating path: " + path, e);
            result._retCode = RetCode.ERROR;
            return result;
        }
    } while (retry);
    result._retCode = RetCode.OK;
    result._updatedValue = updatedData;
    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 33 with CreateMode

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

the class ZkBaseDataAccessor method doCreate.

/**
 * sync create
 */
public AccessResult doCreate(String path, T record, int options) {
    AccessResult result = new AccessResult();
    CreateMode mode = AccessOption.getMode(options);
    if (mode == null) {
        LOG.error("Invalid create mode. options: " + options);
        result._retCode = RetCode.ERROR;
        return result;
    }
    boolean retry;
    do {
        retry = false;
        try {
            _zkClient.create(path, record, mode);
            result._pathCreated.add(path);
            result._retCode = RetCode.OK;
            return result;
        } catch (ZkNoNodeException e) {
            // this will happen if parent node does not exist
            String parentPath = HelixUtil.getZkParentPath(path);
            try {
                AccessResult res = doCreate(parentPath, null, AccessOption.PERSISTENT);
                result._pathCreated.addAll(res._pathCreated);
                RetCode rc = res._retCode;
                if (rc == RetCode.OK || rc == RetCode.NODE_EXISTS) {
                    // if parent node created/exists, retry
                    retry = true;
                }
            } catch (Exception e1) {
                LOG.error("Exception while creating path: " + parentPath, e1);
                result._retCode = RetCode.ERROR;
                return result;
            }
        } catch (ZkNodeExistsException e) {
            LOG.warn("Node already exists. path: " + path);
            result._retCode = RetCode.NODE_EXISTS;
            return result;
        } catch (Exception e) {
            LOG.error("Exception while creating path: " + path, e);
            result._retCode = RetCode.ERROR;
            return result;
        }
    } while (retry);
    result._retCode = RetCode.OK;
    return result;
}
Also used : ZkNodeExistsException(org.I0Itec.zkclient.exception.ZkNodeExistsException) ZkNoNodeException(org.I0Itec.zkclient.exception.ZkNoNodeException) CreateMode(org.apache.zookeeper.CreateMode) 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 34 with CreateMode

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

the class ZkBaseDataAccessor method set.

/**
 * async set, give up on error other than NoNode
 */
boolean[] set(List<String> paths, List<T> records, List<List<String>> pathsCreated, List<Stat> stats, int options) {
    if (paths == null || paths.size() == 0) {
        return new boolean[0];
    }
    if ((records != null && records.size() != paths.size()) || (pathsCreated != null && pathsCreated.size() != paths.size())) {
        throw new IllegalArgumentException("paths, records, and pathsCreated should be of same size");
    }
    boolean[] success = new boolean[paths.size()];
    CreateMode mode = AccessOption.getMode(options);
    if (mode == null) {
        LOG.error("Invalid async set mode. options: " + options);
        return success;
    }
    List<Stat> setStats = new ArrayList<Stat>(Collections.<Stat>nCopies(paths.size(), null));
    SetDataCallbackHandler[] cbList = new SetDataCallbackHandler[paths.size()];
    CreateCallbackHandler[] createCbList = null;
    boolean[] needSet = new boolean[paths.size()];
    Arrays.fill(needSet, true);
    long startT = System.nanoTime();
    try {
        boolean retry;
        do {
            retry = false;
            for (int i = 0; i < paths.size(); i++) {
                if (!needSet[i])
                    continue;
                String path = paths.get(i);
                T record = records.get(i);
                cbList[i] = new SetDataCallbackHandler();
                _zkClient.asyncSetData(path, record, -1, cbList[i]);
            }
            boolean failOnNoNode = false;
            for (int i = 0; i < cbList.length; i++) {
                SetDataCallbackHandler cb = cbList[i];
                cb.waitForSuccess();
                Code rc = Code.get(cb.getRc());
                switch(rc) {
                    case OK:
                        setStats.set(i, cb.getStat());
                        needSet[i] = false;
                        break;
                    case NONODE:
                        // if fail on NoNode, try create the node
                        failOnNoNode = true;
                        break;
                    default:
                        // if fail on error other than NoNode, give up
                        needSet[i] = false;
                        break;
                }
            }
            // if failOnNoNode, try create
            if (failOnNoNode) {
                boolean[] needCreate = Arrays.copyOf(needSet, needSet.length);
                createCbList = create(paths, records, needCreate, pathsCreated, options);
                for (int i = 0; i < createCbList.length; i++) {
                    CreateCallbackHandler createCb = createCbList[i];
                    if (createCb == null) {
                        continue;
                    }
                    Code rc = Code.get(createCb.getRc());
                    switch(rc) {
                        case OK:
                            setStats.set(i, ZNode.ZERO_STAT);
                            needSet[i] = false;
                            break;
                        case NODEEXISTS:
                            retry = true;
                            break;
                        default:
                            // if creation fails on error other than NodeExists
                            // no need to retry set
                            needSet[i] = false;
                            break;
                    }
                }
            }
        } while (retry);
        // construct return results
        for (int i = 0; i < cbList.length; i++) {
            SetDataCallbackHandler cb = cbList[i];
            Code rc = Code.get(cb.getRc());
            if (rc == Code.OK) {
                success[i] = true;
            } else if (rc == Code.NONODE) {
                CreateCallbackHandler createCb = createCbList[i];
                if (Code.get(createCb.getRc()) == Code.OK) {
                    success[i] = true;
                }
            }
        }
        if (stats != null) {
            stats.clear();
            stats.addAll(setStats);
        }
        return success;
    } 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) Code(org.apache.zookeeper.KeeperException.Code) SetDataCallbackHandler(org.apache.helix.manager.zk.ZkAsyncCallbacks.SetDataCallbackHandler) Stat(org.apache.zookeeper.data.Stat) CreateMode(org.apache.zookeeper.CreateMode)

Example 35 with CreateMode

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

the class ZookeeperClient method makePath.

public void makePath(String path, byte[] data, CreateMode createMode) throws Exception {
    boolean failOnExists = true;
    boolean retry = true;
    if (path.startsWith("/")) {
        path = path.substring(1, path.length());
    }
    String[] paths = path.split("/");
    StringBuilder sbPath = new StringBuilder();
    for (int i = 0; i < paths.length; i++) {
        byte[] bytes = null;
        String pathPiece = paths[i];
        sbPath.append("/" + pathPiece);
        final String currentPath = sbPath.toString();
        Object exists = zkClient.exists(currentPath, null);
        if (exists == null || ((i == paths.length - 1) && failOnExists)) {
            CreateMode mode = CreateMode.PERSISTENT;
            if (i == paths.length - 1) {
                mode = createMode;
                bytes = data;
            }
            try {
                zkClient.create(currentPath, bytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, mode);
            } catch (NodeExistsException e) {
                if (!failOnExists) {
                    // TODO:
                    zkClient.setData(currentPath, data, -1);
                    zkClient.exists(currentPath, null);
                    return;
                }
                if (i == paths.length - 1) {
                    throw e;
                }
            }
            if (i == paths.length - 1) {
                zkClient.exists(currentPath, null);
            }
        } else if (i == paths.length - 1) {
            zkClient.setData(currentPath, data, -1);
            zkClient.exists(currentPath, null);
        }
    }
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException)

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