Example usage for org.apache.zookeeper CreateMode isSequential

List of usage examples for org.apache.zookeeper CreateMode isSequential

Introduction

In this page you can find the example usage for org.apache.zookeeper CreateMode isSequential.

Prototype

public boolean isSequential() 

Source Link

Usage

From source file:com.alibaba.otter.shared.arbitrate.impl.zookeeper.ZooKeeperx.java

License:Apache License

/**
 * <pre>/*from  w w w . j  ava 2s . com*/
 * 1. zookeeperConnectionLosscreate/setData/delete???zookeeper server
 * 2. SelectStageListenerprocessId?processId?id. retry??processId??
 * </pre>
 * 
 * @see org.apache.zookeeper.ZooKeeper#create(String path, byte[] path, List acl, CreateMode mode)
 */
public String create(final String path, final byte[] data, final CreateMode mode)
        throws KeeperException, InterruptedException {
    if (mode.isSequential()) {
        return zookeeper.create(path, data, acl, mode);
    } else {
        return retryOperation(new ZooKeeperOperation<String>() {

            public String execute() throws KeeperException, InterruptedException {
                return zookeeper.create(path, data, acl, mode);
            }
        });
    }

}

From source file:com.linkedin.d2.discovery.stores.zk.RetryZooKeeper.java

License:Apache License

public String createUniqueSequential(final String path, final byte[] data, final List<ACL> acl,
        final CreateMode createMode, final AsyncCallback.StringCallback cb, final Object ctx) {
    if (!createMode.isSequential()) {
        create(path, data, acl, createMode, cb, ctx);
        return path;
    }//from   www .ja va 2s .  c  o  m

    final String retryPath = path + "-" + _uuid.toString() + "-";
    final RetryCallback callback = new RetryCallback() {
        @Override
        protected void retry() {
            final String parentPath = path.substring(0, path.lastIndexOf('/'));
            final StringCallback stringCallback = this;

            final ChildrenCallback childrenCallback = new ChildrenCallback() {
                @Override
                public void processResult(int ccRC, String ccPath, Object ccCtx, List<String> ccChildren) {
                    KeeperException.Code code = KeeperException.Code.get(ccRC);
                    // we don't have to handle CONNECTIONLOSS here; it would be handled by the retry version of getChildren
                    switch (code) {
                    case OK:
                        List<String> ourChildren = new ArrayList<String>();
                        for (final String child : ccChildren) {
                            if (child.contains(_uuid.toString())) {
                                ourChildren.add(child);
                            }
                        }
                        if (ourChildren.size() > 0) {
                            ChildrenInspector inspector = new ChildrenInspector(ourChildren.size());
                            for (final String ourChild : ourChildren) {
                                // user retry version of getData here
                                getData(parentPath + "/" + ourChild, false, inspector, null);
                            }
                        } else {
                            // no children belong to us found, retry create directly
                            _log.info("Retry create operation: path = " + retryPath + "data length: "
                                    + data.length);
                            zkCreate(retryPath, data, acl, createMode, stringCallback, ctx);
                        }
                        break;
                    default:
                        _log.error("Retry create aborted in getChildren. KeeperException code: " + code);
                        break;
                    }
                }

                class ChildrenInspector implements DataCallback {
                    private int _count;

                    ChildrenInspector(int count) {
                        _count = count;
                    }

                    @Override
                    public void processResult(int dcRC, String dcPath, Object dcCtx, byte[] dcData,
                            Stat dcStat) {
                        KeeperException.Code code = KeeperException.Code.get(dcRC);
                        // we don't have to handle CONNECTIONLOSS here
                        switch (code) {
                        case OK:
                            if (Arrays.equals(data, dcData)) {
                                // we find the data we wanted to create
                                // do not decrement _count
                                // retry create won't be triggered as a result
                            } else {
                                // this is not the data we wanted to create
                                _count--;
                                if (_count == 0) {
                                    // this is the last child to be inspected
                                    // all previous children do not have the data we wanted to create
                                    // trigger retry create
                                    _log.info("Retry create operation: path = " + retryPath + "data length: "
                                            + data.length);
                                    zkCreate(retryPath, data, acl, createMode, stringCallback, ctx);
                                }
                            }
                            break;
                        default:
                            _log.error("Retry create stopped in getData. KeeperException code: " + code);
                            break;
                        }
                    }
                }
            };
            // use retry version of getChildren
            getChildren(parentPath, false, childrenCallback, null);
        }

        @Override
        protected void processStringResult(int cbRC, String cbPath, Object cbCtx, String cbName) {
            cb.processResult(cbRC, cbPath, cbCtx, cbName);
        }
    };

    zkCreate(retryPath, data, acl, createMode, callback, ctx);
    return retryPath;
}

From source file:com.linkedin.d2.discovery.stores.zk.RetryZooKeeper.java

License:Apache License

@Override
public void create(final String path, final byte[] data, final List<ACL> acl, final CreateMode createMode,
        final AsyncCallback.StringCallback cb, final Object ctx) {
    final RetryCallback callback = new RetryCallback() {
        @Override//from   w w  w .  j ava 2 s. c  om
        protected void retry() {
            if (!createMode.isSequential()) {
                // it's always safe to retry create for non-sequential names
                _log.info("Retry create operation: path = " + path + " data length " + data.length);
                zkCreate(path, data, acl, createMode, this, ctx);
            } else {
                _log.error("Connection lost during create operation of sequential node. "
                        + "Consider using createUniqueSequential() instead");
            }
        }

        @Override
        protected void processStringResult(int cbRC, String cbPath, Object cbCtx, String cbName) {
            cb.processResult(cbRC, cbPath, cbCtx, cbName);
        }
    };

    zkCreate(path, data, acl, createMode, callback, ctx);
}

From source file:com.yihaodian.architecture.zkclient.InMemoryConnection.java

License:Apache License

@Override
public String create(String path, byte[] data, CreateMode mode) throws KeeperException, InterruptedException {
    _lock.lock();//  ww  w  .  ja v a  2s . c  o  m
    try {

        if (mode.isSequential()) {
            final int newSequence = sequence.getAndIncrement();
            path = path + ZkPathUtil.leadingZeros(newSequence, 10);
        }

        if (exists(path, false)) {
            throw new KeeperException.NodeExistsException();
        }
        _data.put(path, new DataAndVersion(data, 0));
        _creationTime.put(path, System.currentTimeMillis());
        checkWatch(_nodeWatches, path, EventType.NodeCreated);
        // we also need to send a child change event for the parent
        String parentPath = getParentPath(path);
        if (parentPath != null) {
            checkWatch(_nodeWatches, parentPath, EventType.NodeChildrenChanged);
        }
        return path;
    } finally {
        _lock.unlock();
    }
}

From source file:com.zookeeper.zkclient.InMemoryConnection.java

License:Apache License

@Override
public String create(String path, byte[] data, List<ACL> acl, CreateMode mode)
        throws KeeperException, InterruptedException {
    _lock.lock();/*from  w w  w  . java2  s .c o m*/
    try {

        if (mode.isSequential()) {
            final int newSequence = sequence.getAndIncrement();
            path = path + ZkPathUtil.leadingZeros(newSequence, 10);
        }

        if (exists(path, false)) {
            throw new KeeperException.NodeExistsException();
        }
        String parentPath = getParentPath(path);
        checkACL(parentPath, ZooDefs.Perms.CREATE);

        _data.put(path, new DataAndVersion(data, 0, acl));
        _creationTime.put(path, System.currentTimeMillis());
        checkWatch(_nodeWatches, path, EventType.NodeCreated);
        // we also need to send a child change event for the parent
        if (parentPath != null) {
            checkWatch(_nodeWatches, parentPath, EventType.NodeChildrenChanged);
        }
        return path;
    } finally {
        _lock.unlock();
    }
}

From source file:io.reign.util.ZkClientUtil.java

License:Apache License

/**
 * /*from  w  ww.  jav  a  2s. c  o  m*/
 * @param zkClient
 * @param pathScheme
 * @param path
 * @param leafData
 * @param aclList
 * @param createMode
 * @param leafDataVersion
 * @param statRef
 *            to return Stat on update of data nodes
 * @return
 * @throws KeeperException
 */
public String updatePath(final ZkClient zkClient, final PathScheme pathScheme, final String path,
        final byte[] leafData, final List<ACL> aclList, final CreateMode createMode, int leafDataVersion,
        AtomicReference<Stat> statRef) throws KeeperException {

    /***** if there is leaf data, try updating first to save on ZK ops *****/
    if (!createMode.isSequential()) {
        try {
            if (statRef != null) {
                statRef.set(zkClient.setData(path, leafData, leafDataVersion));
            } else {
                zkClient.setData(path, leafData, leafDataVersion);
            }
            return path;
        } catch (KeeperException e) {
            if (e.code() == KeeperException.Code.NONODE) {
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            "updatePath():  path does not exist for data update:  " + e + ":  path=" + path);
                }
            } else {
                logger.error("updatePath():  " + e, e);
                throw e;
            }
        } catch (InterruptedException e) {
            logger.warn("Interrupted in updatePath():  " + e, e);

        } // try/catch
    } // if

    /***** try to build path without building parents to save on calls to ZK *****/
    try {

        String pathCreated = zkClient.create(path, leafData, aclList, createMode);

        if (logger.isDebugEnabled()) {
            logger.debug("Created path directly:  pathCreated={}", pathCreated);
        }

        return pathCreated;
    } catch (KeeperException e) {
        if (e.code() == KeeperException.Code.NODEEXISTS) {
            if (logger.isDebugEnabled()) {
                logger.debug("Path already exists:  " + e + ": path=" + path);
            }

            return path;
        } else if (e.code() == KeeperException.Code.NONODE) {
            if (logger.isDebugEnabled()) {
                logger.debug("Parent path does not exist:  " + e + ":  path=" + path);
            }
        } else {
            logger.error("Error while building path:  " + e + "; path=" + path, e);
            throw e;
        } // if

    } catch (InterruptedException e) {
        logger.error(e + "", e);
        logger.warn("Interrupted in updatePath():  " + e, e);
    } // try/catch

    /***** build path by building parent nodes first *****/
    String[] tokens = pathScheme.tokenizePath(path);

    String pathCreated = "";
    String pathToCreate = null;
    for (int i = 0; i < tokens.length; i++) {
        String token = tokens[i];
        if ("".equals(token)) {
            // we should never get here, and if we are getting here, log as
            // error
            logger.warn("updatePath():  token is empty string!:  path='{}'; i={}; pathCreated='{}'",
                    new Object[] { path, i, pathCreated });

        } else {
            try {
                pathToCreate = pathCreated + "/" + token;

                if (logger.isDebugEnabled()) {
                    logger.debug("Creating node:  pathToCreate={}; token={}", pathToCreate, token);
                }

                // default to persistent mode until leaf node, then we use
                // the preferred create mode of caller
                CreateMode currentCreateMode = CreateMode.PERSISTENT;
                byte[] nodeData = null;
                if (i == tokens.length - 1) {
                    currentCreateMode = createMode;
                    nodeData = leafData;
                }

                pathCreated = zkClient.create(pathToCreate, nodeData, aclList, currentCreateMode);

            } catch (KeeperException e) {
                if (e.code() == KeeperException.Code.NODEEXISTS) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Path already exists:  path={}", pathToCreate);
                    }

                    pathCreated = pathToCreate;
                } else {
                    logger.error("Error while building path:  " + e + ":  path=" + pathToCreate, e);
                    throw e;
                } // if

            } catch (InterruptedException e) {
                logger.error(e + "", e);
                logger.warn("Interrupted in updatePath():  " + e, e);
            } // try/catch
        } // if
    } // for

    if (logger.isDebugEnabled()) {
        logger.debug("Created path by building parent nodes:  pathCreated={}", pathCreated);
    }
    return pathCreated;

}

From source file:org.apache.curator.framework.recipes.locks.InterProcessExpiringSemaphore.java

License:Apache License

private InterProcessExpiringSemaphore(CuratorFramework client, String path, int maxLeases,
        SharedCountReader count, SharedCountReader timeToLive, CreateMode createMode) {

    this.client = client;
    this.currentTimeProvider = new ZooCurrentTimeProvider(client, ZKPaths.makePath(path, TIME_PARENT));
    path = PathUtils.validatePath(path);
    this.lock = new InterProcessMutex(client, ZKPaths.makePath(path, LOCK_PARENT));
    this.maxLeases = (count != null) ? count.getCount() : maxLeases;
    // if time to live is null, do not try to purse expired leases
    this.createMode = createMode != null && createMode.isSequential() ? createMode
            : CreateMode.EPHEMERAL_SEQUENTIAL;
    this.timeToLive = Preconditions.checkNotNull(timeToLive, "Time to live is required");

    this.leasesPath = ZKPaths.makePath(path, LEASE_PARENT);

    if (count != null) {
        count.addListener(new SharedCountListener() {
            @Override//from w  w w  .ja v a2 s  . c om
            public void countHasChanged(SharedCountReader sharedCount, int newCount) throws Exception {
                InterProcessExpiringSemaphore.this.maxLeases = newCount;
                notifyFromWatcher();
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                // no need to handle this here - clients should set their own connection state listener
            }
        });
    }
}

From source file:org.apache.curator.framework.schema.Schema.java

License:Apache License

/**
 * Validate that this schema's create mode setting matches and that the data is valid
 *
 * @param mode CreateMode being used//  www . j av  a2s.c  o  m
 * @param path the znode full path
 * @param data data being set
 * @param acl the creation acls
 * @throws SchemaViolation if schema's create mode setting does not match or data is invalid
 */
public void validateCreate(CreateMode mode, String path, byte[] data, List<ACL> acl) {
    if (mode.isEphemeral() && (ephemeral == Allowance.CANNOT)) {
        throw new SchemaViolation(this, new SchemaViolation.ViolatorData(path, data, acl),
                "Cannot be ephemeral");
    }

    if (!mode.isEphemeral() && (ephemeral == Allowance.MUST)) {
        throw new SchemaViolation(this, new SchemaViolation.ViolatorData(path, data, acl), "Must be ephemeral");
    }

    if (mode.isSequential() && (sequential == Allowance.CANNOT)) {
        throw new SchemaViolation(this, new SchemaViolation.ViolatorData(path, data, acl),
                "Cannot be sequential");
    }

    if (!mode.isSequential() && (sequential == Allowance.MUST)) {
        throw new SchemaViolation(this, new SchemaViolation.ViolatorData(path, data, acl),
                "Must be sequential");
    }

    validateGeneral(path, data, acl);
}

From source file:org.apache.flink.runtime.zookeeper.ZooKeeperStateHandleStoreITCase.java

License:Apache License

/**
 * Tests that {@link CreateMode} is respected.
 *///from   w  ww. j  a  v  a 2  s. c  om
@Test
public void testAddWithCreateMode() throws Exception {
    LongStateStorage longStateStorage = new LongStateStorage();
    ZooKeeperStateHandleStore<Long> store = new ZooKeeperStateHandleStore<Long>(ZooKeeper.getClient(),
            longStateStorage);

    // Config
    Long state = 3457347234L;

    CreateMode[] modes = CreateMode.values();
    for (int i = 0; i < modes.length; i++) {
        CreateMode mode = modes[i];
        state += i;

        String pathInZooKeeper = "/testAddWithCreateMode" + mode.name();

        // Test
        store.add(pathInZooKeeper, state, mode);

        if (mode.isSequential()) {
            // Figure out the sequential ID
            List<String> paths = ZooKeeper.getClient().getChildren().forPath("/");
            for (String p : paths) {
                if (p.startsWith("testAddWithCreateMode" + mode.name())) {
                    pathInZooKeeper = "/" + p;
                    break;
                }
            }
        }

        // Verify
        // State handle created
        assertEquals(i + 1, store.getAll().size());
        assertEquals(state, longStateStorage.getStateHandles().get(i).getState(null));

        // Path created
        Stat stat = ZooKeeper.getClient().checkExists().forPath(pathInZooKeeper);

        assertNotNull(stat);

        // Is ephemeral or persistent
        if (mode.isEphemeral()) {
            assertTrue(stat.getEphemeralOwner() != 0);
        } else {
            assertEquals(0, stat.getEphemeralOwner());
        }

        // Data is equal
        @SuppressWarnings("unchecked")
        Long actual = ((StateHandle<Long>) InstantiationUtil.deserializeObject(
                ZooKeeper.getClient().getData().forPath(pathInZooKeeper), ClassLoader.getSystemClassLoader()))
                        .getState(null);

        assertEquals(state, actual);
    }
}

From source file:org.I0Itec.zkclient.InMemoryConnection.java

License:Apache License

@Override
public String create(String path, byte[] data, CreateMode mode) throws KeeperException, InterruptedException {
    _lock.lock();// w  w  w . j  a v a 2s.c o  m
    try {

        if (mode.isSequential()) {
            final int newSequence = sequence.getAndIncrement();
            path = path + ZkPathUtil.leadingZeros(newSequence, 10);
        }

        if (exists(path, false)) {
            throw new KeeperException.NodeExistsException();
        }
        _data.put(path, data);
        _creationTime.put(path, System.currentTimeMillis());
        checkWatch(_nodeWatches, path, EventType.NodeCreated);
        // we also need to send a child change event for the parent
        String parentPath = getParentPath(path);
        if (parentPath != null) {
            checkWatch(_nodeWatches, parentPath, EventType.NodeChildrenChanged);
        }
        return path;
    } finally {
        _lock.unlock();
    }
}