Example usage for org.apache.zookeeper CreateMode PERSISTENT_SEQUENTIAL

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

Introduction

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

Prototype

CreateMode PERSISTENT_SEQUENTIAL

To view the source code for org.apache.zookeeper CreateMode PERSISTENT_SEQUENTIAL.

Click Source Link

Document

The znode will not be automatically deleted upon client's disconnect, and its name will be appended with a monotonically increasing number.

Usage

From source file:akka.cluster.zookeeper.DistributedQueue.java

License:Apache License

/**
 * Inserts data into queue.//from  w w  w . j  a  va2 s.c o m
 * @param data
 * @return true if data was successfully added
 */
public boolean offer(byte[] data) throws KeeperException, InterruptedException {
    for (;;) {
        try {
            zookeeper.create(dir + "/" + prefix, data, acl, CreateMode.PERSISTENT_SEQUENTIAL);
            return true;
        } catch (KeeperException.NoNodeException e) {
            zookeeper.create(dir, new byte[0], acl, CreateMode.PERSISTENT);
        }
    }

}

From source file:com.alibaba.otter.shared.arbitrate.impl.setl.rpc.monitor.SelectProcessListener.java

License:Apache License

public void processChanged(List<Long> processIds) {
    super.processChanged(processIds);
    // add by ljh at 2012-09-13,zookeeper ConnectionLoss
    for (Long processId : processIds) {
        if (!replyProcessIds.contains(processId)) {
            logger.warn("process is not in order, please check processId:{}", processId);
            addReply(processId);/* w  ww  . j a  v  a 2 s . com*/
        }
    }

    try {
        String path = StagePathUtils.getProcessRoot(getPipelineId());
        // ?
        int size = ArbitrateConfigUtils.getParallelism(getPipelineId()) - processIds.size();
        if (size > 0) {// 
            PermitMonitor permit = ArbitrateFactory.getInstance(getPipelineId(), PermitMonitor.class);
            if (permit.isPermit() == false) { // ?????
                return;
            }

            String mainStemPath = StagePathUtils.getMainStem(getPipelineId());
            byte[] bytes = zookeeper.readData(mainStemPath, true);
            if (bytes == null) {
                return;
            }

            MainStemEventData eventData = JsonUtils.unmarshalFromByte(bytes, MainStemEventData.class);
            if (eventData.getNid().equals(ArbitrateConfigUtils.getCurrentNid()) == false) {
                return;// ?mainStem,???
            }

            // ?select????????????
            // DistributedLock lock = new DistributedLock(PathUtils.getSelectLock(getPipelineId()));
            // try {
            // lock.lock();
            // //process
            // } finally {
            // lock.unlock();
            // }

            synchronized (this) {
                // ???, dobble-check
                List<String> currentProcesses = zookeeper.getChildren(path);
                size = ArbitrateConfigUtils.getParallelism(getPipelineId()) - currentProcesses.size();
                if (size > 0) {// 
                    ProcessNodeEventData nodeData = new ProcessNodeEventData();
                    nodeData.setStatus(ProcessNodeEventData.Status.UNUSED);// 
                    nodeData.setMode(ArbitrateMode.RPC);
                    nodeData.setNid(ArbitrateConfigUtils.getCurrentNid());
                    byte[] nodeBytes = JsonUtils.marshalToByte(nodeData);
                    String processPath = zookeeper.create(path + "/", nodeBytes,
                            CreateMode.PERSISTENT_SEQUENTIAL);
                    // ?
                    String processNode = StringUtils.substringAfterLast(processPath, "/");
                    Long processId = StagePathUtils.getProcessId(processNode);// ?process
                    addReply(processId);
                }
            }

        }
    } catch (ZkException e) {
        recovery(getPipelineId());// ?recovery??ConnectionLosscreate?
        logger.error("add process error!", e);
    }

}

From source file:com.alibaba.otter.shared.arbitrate.impl.setl.zookeeper.monitor.SelectStageListener.java

License:Apache License

public void processChanged(List<Long> processIds) {
    super.processChanged(processIds);
    // add by ljh at 2012-09-13,zookeeper ConnectionLoss
    for (Long processId : processIds) {
        if (!replyProcessIds.contains(processId)) {
            logger.warn("process is not in order, please check processId:{}", processId);
            addReply(processId);/* w w w.j a v a  2s.c o m*/
        }
    }

    try {
        String path = StagePathUtils.getProcessRoot(getPipelineId());
        // ?
        int size = ArbitrateConfigUtils.getParallelism(getPipelineId()) - processIds.size();
        if (size > 0) {// 
            PermitMonitor permit = ArbitrateFactory.getInstance(getPipelineId(), PermitMonitor.class);
            if (permit.isPermit() == false) { // ?????
                return;
            }

            String mainStemPath = StagePathUtils.getMainStem(getPipelineId());
            byte[] bytes = zookeeper.readData(mainStemPath, true);
            if (bytes == null) {
                return;
            }

            MainStemEventData eventData = JsonUtils.unmarshalFromByte(bytes, MainStemEventData.class);
            if (eventData.getNid().equals(ArbitrateConfigUtils.getCurrentNid()) == false) {
                return;// ?mainStem,???
            }

            // ?select????????????
            // DistributedLock lock = new DistributedLock(PathUtils.getSelectLock(getPipelineId()));
            // try {
            // lock.lock();
            // //process
            // } finally {
            // lock.unlock();
            // }

            synchronized (this) {
                // ???, dobble-check
                List<String> currentProcesses = zookeeper.getChildren(path);
                size = ArbitrateConfigUtils.getParallelism(getPipelineId()) - currentProcesses.size();
                if (size > 0) {// 
                    ProcessNodeEventData nodeData = new ProcessNodeEventData();
                    nodeData.setStatus(ProcessNodeEventData.Status.UNUSED);// 
                    nodeData.setNid(ArbitrateConfigUtils.getCurrentNid());
                    byte[] nodeBytes = JsonUtils.marshalToByte(nodeData);
                    String processPath = zookeeper.create(path + "/", nodeBytes,
                            CreateMode.PERSISTENT_SEQUENTIAL);
                    // ?
                    String processNode = StringUtils.substringAfterLast(processPath, "/");
                    Long processId = StagePathUtils.getProcessId(processNode);// ?process
                    addReply(processId);
                }
            }

        }
    } catch (ZkException e) {
        recovery(getPipelineId());// ?recovery??ConnectionLosscreate?
        logger.error("SelectStageListener", e);
    }

}

From source file:com.alibaba.otter.shared.arbitrate.setl.BaseStageTest.java

License:Apache License

protected Long initProcess() {
    String path = zookeeper.create(processPath + "/", new byte[0], CreateMode.PERSISTENT_SEQUENTIAL);

    // ?/*  w  w  w  . j a  v  a 2 s  . com*/
    String processNode = StringUtils.substringAfterLast(path, "/");
    return StagePathUtils.getProcessId(processNode);// ?process
}

From source file:com.alibaba.otter.shared.common.utils.zookeeper.ZkClientx.java

License:Apache License

/**
 * Create a persistent Sequential node.// w ww  .  j  ava  2s.  c o  m
 * 
 * @param path
 * @param createParents if true all parent dirs are created as well and no {@link ZkNodeExistsException} is thrown
 * in case the path already exists
 * @throws ZkInterruptedException if operation was interrupted, or a required reconnection got interrupted
 * @throws IllegalArgumentException if called from anything except the ZooKeeper event thread
 * @throws ZkException if any ZooKeeper exception occurred
 * @throws RuntimeException if any other exception occurs
 */
public String createPersistentSequential(String path, boolean createParents)
        throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException {
    try {
        return create(path, null, CreateMode.PERSISTENT_SEQUENTIAL);
    } catch (ZkNoNodeException e) {
        if (!createParents) {
            throw e;
        }
        String parentDir = path.substring(0, path.lastIndexOf('/'));
        createPersistent(parentDir, createParents);
        return createPersistentSequential(path, createParents);
    }
}

From source file:com.alibaba.otter.shared.common.utils.zookeeper.ZkClientx.java

License:Apache License

/**
 * Create a persistent Sequential node.// w  w  w. j a v  a  2 s  .  c  o  m
 * 
 * @param path
 * @param data
 * @param createParents if true all parent dirs are created as well and no {@link ZkNodeExistsException} is thrown
 * in case the path already exists
 * @throws ZkInterruptedException if operation was interrupted, or a required reconnection got interrupted
 * @throws IllegalArgumentException if called from anything except the ZooKeeper event thread
 * @throws ZkException if any ZooKeeper exception occurred
 * @throws RuntimeException if any other exception occurs
 */
public String createPersistentSequential(String path, Object data, boolean createParents)
        throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException {
    try {
        return create(path, data, CreateMode.PERSISTENT_SEQUENTIAL);
    } catch (ZkNoNodeException e) {
        if (!createParents) {
            throw e;
        }
        String parentDir = path.substring(0, path.lastIndexOf('/'));
        createPersistent(parentDir, createParents);
        return createPersistentSequential(path, data, createParents);
    }
}

From source file:com.alibaba.otter.shared.common.utils.zookeeper.ZkClientx.java

License:Apache License

/**
 * Create a persistent, sequental node.//from  ww  w .  j a  v  a2s . c  om
 * 
 * @param path
 * @param data
 * @return create node's path
 * @throws ZkInterruptedException if operation was interrupted, or a required reconnection got interrupted
 * @throws IllegalArgumentException if called from anything except the ZooKeeper event thread
 * @throws ZkException if any ZooKeeper exception occurred
 * @throws RuntimeException if any other exception occurs
 */
public String createPersistentSequential(String path, Object data)
        throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException {
    return create(path, data, CreateMode.PERSISTENT_SEQUENTIAL);
}

From source file:com.andyadc.menagerie.collections.ZkHashMap.java

License:Apache License

/**
 * Constructs a new HashMap with the specified ZooKeeper client and serializer,
 * located at the specifed base znode, with the specified concurrency level and ZooKeeper privileges.
 * <p>//from w w w .  j a v  a 2  s .c  o m
 * Note: {@code mapNode} <em>must </em> exist in zookeeper before calling this constructor, or else an exception
 * will be thrown when first attempting to use this map.
 *
 * @param mapNode          the znode to use
 * @param zkSessionManager the session manager for the ZooKeeper instance
 * @param privileges       the ZooKeeper privileges to use
 * @param serializer       the serializer to use
 * @param concurrency      the estimated number of concurrently updating parties.
 */
public ZkHashMap(String mapNode, ZkSessionManager zkSessionManager, List<ACL> privileges,
        Serializer<Entry<K, V>> serializer, int concurrency) {
    /*
    Shamelessly stolen from the implementation of ConcurrentHashMap
    */
    int sshift = 0;
    int ssize = 1;
    while (ssize < concurrency) {
        ++sshift;
        ssize <<= 1;
    }
    segmentShift = 32 - sshift;
    segmentMask = ssize - 1;
    this.segments = ZkSegment.newArray(ssize);

    //need to build the map in a single, synchronized activity across all members
    Lock mapLock = new ReentrantZkLock(mapNode, zkSessionManager, privileges);
    mapLock.lock();
    try {
        //need to read the data out of zookeeper
        ZooKeeper zk = zkSessionManager.getZooKeeper();
        for (int i = 0; i < segments.length; i++) {
            try {
                //attach the first segments to any segments which are already created
                List<String> zkBuckets = ZkUtils.filterByPrefix(zk.getChildren(mapNode, false), "bucket");
                ZkUtils.sortBySequence(zkBuckets, '-');
                int numBucketsBuilt = 0;
                for (int bucketIndex = 0; bucketIndex < zkBuckets.size()
                        && bucketIndex < segments.length; bucketIndex++) {
                    numBucketsBuilt++;
                    segments[bucketIndex] = new ZkSegment<K, V>(mapNode + "/" + zkBuckets.get(bucketIndex),
                            serializer, zkSessionManager, privileges);
                }

                //create any additional segments as needed
                while (numBucketsBuilt < segments.length) {
                    String bucketNode = ZkUtils.safeCreate(zk, mapNode + "/bucket-", new byte[] {}, privileges,
                            CreateMode.PERSISTENT_SEQUENTIAL);
                    segments[numBucketsBuilt] = new ZkSegment<K, V>(bucketNode, serializer, zkSessionManager,
                            privileges);
                    numBucketsBuilt++;
                }
            } catch (KeeperException e) {
                throw new RuntimeException(e);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    } finally {
        mapLock.unlock();
    }
}

From source file:com.andyadc.menagerie.collections.ZkListSet.java

License:Apache License

@Override
public boolean add(T t) {
    acquireWriteLock();/*from w w  w .  j  a va 2 s  . co  m*/
    try {
        ZooKeeper zk = sessionManager.getZooKeeper();
        List<String> children = ZkUtils.filterByPrefix(zk.getChildren(baseNode, false), prefix());
        for (String child : children) {
            byte[] data = ZkUtils.safeGetData(zk, baseNode + "/" + child, false, new Stat());
            if (data.length > 0) {
                if (serializer.deserialize(data).equals(t))
                    return false;
            }
        }
        zk.create(baseNode + "/" + prefix() + delimiter(), serializer.serialize(t), privileges,
                CreateMode.PERSISTENT_SEQUENTIAL);
        return true;
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (KeeperException e) {
        throw new RuntimeException(e);
    } finally {
        releaseWriteLock();
    }
}

From source file:com.andyadc.menagerie.latches.ZkCountDownLatch.java

License:Apache License

/**
 * Creates a new CountDownLatch on the specified latchNode, or joins a CountDownLatch which has been
 * previously created by another node/thread on the same latchNode.
 * <p>//  ww w .j a v a  2s .  co m
 * When this constructor returns, the Latch is guaranteed to be in a cluster- and thread-safe state which
 * is ready to be used.
 * <p>
 * This constructor creates a CountDownLatch where the caller can choose between node-fault tolerance and algorithmic
 * certainty. If {@code tolerateFailures} is set to true, then once a party has counted down against this latch, it
 * will remain counted down, even if that party subsequently fails. To require that all parties remain alive until
 * the latch has been reached, set {@code tolerateFailures} to false.
 *
 * @param total            the number of elements which must countDown before threads may proceed.
 * @param latchNode        the node to execute the latch under
 * @param zkSessionManager the ZkSessionManager to use
 * @param privileges       the privileges for this latch
 * @param tolerateFailures set to {@code true} to ensure that nodes can progress once all parties have reported once;
 *                         set to false to require <i>all</i> parties to remain connected until the Latch has been filled.
 * @throws RuntimeException wrapping:
 *                          <ul>
 *                          <li> {@link KeeperException} if the ZooKeeper Server has trouble with the requests
 *                          <li> {@link InterruptedException} if the ZooKeeper client has trouble communicating with the ZooKeeper service
 *                          </ul>
 */
public ZkCountDownLatch(long total, String latchNode, ZkSessionManager zkSessionManager, List<ACL> privileges,
        boolean tolerateFailures) {
    super(total, latchNode, zkSessionManager, privileges);
    this.countDownMode = tolerateFailures ? CreateMode.PERSISTENT_SEQUENTIAL : CreateMode.EPHEMERAL_SEQUENTIAL;
    ensureState();
}