Example usage for org.apache.zookeeper CreateMode PERSISTENT

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

Introduction

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

Prototype

CreateMode PERSISTENT

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

Click Source Link

Document

The znode will not be automatically deleted upon client's disconnect.

Usage

From source file:ZooKeeperSetAcls.java

License:Apache License

public ZooKeeperSetAcls(String args[]) {
    if (!cl.parseOptions(args)) {
        System.exit(1);/*from   w w w.  j  a v a  2s.c o  m*/
    }

    Watcher watcher = new MyWatcher();
    System.out.println("Connecting to " + cl.getOption("server"));
    try {
        zk = new ZooKeeper(cl.getOption("server"), Integer.parseInt(cl.getOption("timeout")), watcher);
    } catch (IOException e) {
        System.err.println("IOError: " + e.getMessage());
        System.exit(2);
    }

    for (MyPathWithACLs pwa : cl.getPathsWithACLs()) {
        try {
            zk.setACL(pwa.getPath(), pwa.getACLs(), -1);
        } catch (NoNodeException e) {
            // if node doesn't exist yet, try to create it with the given
            // ACL
            try {
                zk.create(pwa.getPath(), new byte[0], pwa.getACLs(), CreateMode.PERSISTENT);
            } catch (InterruptedException f) {
                System.err.println("create interrupted: " + f.getMessage());
                System.exit(5);
            } catch (KeeperException f) {
                System.err.println("create operation error: " + f.getMessage());
                System.exit(6);
            }
        } catch (InterruptedException e) {
            System.err.println("setACL interrupted: " + e.getMessage());
            System.exit(3);
        } catch (KeeperException e) {
            System.err.println("setACL operation error: " + e.getMessage());
            System.exit(4);
        }
    }
}

From source file:akka.cluster.LocalBookKeeper.java

License:Open Source License

public void initializeZookeper() {
    //initialize the zk client with values
    try {//from   w  w w  .  j a  va  2s. co m
        zkc = new ZooKeeper("127.0.0.1", ZooKeeperDefaultPort, new emptyWatcher());
        zkc.create("/ledgers", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zkc.create("/ledgers/available", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        // No need to create an entry for each requested bookie anymore as the
        // BookieServers will register themselves with ZooKeeper on startup.
    } catch (KeeperException e) {
    } catch (InterruptedException e) {
    } catch (IOException e) {
    }
}

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

License:Apache License

/**
 * Removes the head of the queue and returns it, blocks until it succeeds.
 * @return The former head of the queue//w w w  .  j av a  2 s .  c om
 * @throws NoSuchElementException
 * @throws KeeperException
 * @throws InterruptedException
 */
public byte[] take() throws KeeperException, InterruptedException {
    TreeMap<Long, String> orderedChildren;
    // Same as for element.  Should refactor this.
    while (true) {
        LatchChildWatcher childWatcher = new LatchChildWatcher();
        try {
            orderedChildren = orderedChildren(childWatcher);
        } catch (KeeperException.NoNodeException e) {
            zookeeper.create(dir, new byte[0], acl, CreateMode.PERSISTENT);
            continue;
        }
        if (orderedChildren.size() == 0) {
            childWatcher.await();
            continue;
        }

        for (String headNode : orderedChildren.values()) {
            String path = dir + "/" + headNode;
            try {
                byte[] data = zookeeper.getData(path, false, null);
                zookeeper.delete(path, -1);
                return data;
            } catch (KeeperException.NoNodeException e) {
                // Another client deleted the node first.
            }
        }
    }
}

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

License:Apache License

/**
 * Inserts data into queue.//  w w  w. j a  va 2 s.  com
 * @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:at.salzburgresearch.kmt.zkconfig.ZookeeperConfiguration.java

License:Apache License

private void zkInit() throws IOException {
    sync.raiseBarrier();//from  ww  w. j  a  v a 2 s.c  om
    final CountDownLatch connected = new CountDownLatch(1);
    log.debug("zkInit - connecting");
    // if (zk != null) zk.close();
    zk = new ZooKeeper(zkConnectionString, zkTimeout, new ZKWatcher(connected, sync));

    log.info("zkInit - ensure root node exists");
    try {
        if (connected.await(zkTimeout, TimeUnit.MILLISECONDS)) {
            for (int i = zkRoot.indexOf('/', 1); i > 0; i = zkRoot.indexOf('/', i + 1)) {
                final String path = zkRoot.substring(0, i);
                log.trace("zkInit - checking existence of {}", path);
                if (zk.exists(path, false) == null) {
                    zk.create(path, new byte[] {}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                }
            }
            log.debug("zkInit - zkRoot {} exists", zkRoot);
        } else {
            throw new IOException("Timeout while establishing ZooKeeper connection");
        }
    } catch (InterruptedException e) {
        throw new IOException("Could not connect", e);
    } catch (KeeperException e) {
        throw new IOException("Initial Connection failed - is zookeeper available?", e);
    }
    log.info("zkInit - connected");
    sync.lowerBarrier();
}

From source file:at.salzburgresearch.kmt.zkconfig.ZookeeperConfiguration.java

License:Apache License

protected void setPropertyDirect(String key, Object value) {
    if (value == null) {
        clearPropertyDirect(key);/*from  w  w w.j  av a 2  s .c  om*/
    } else {
        final String path = toZookeeperPath(key);
        try {
            sync.await();
            final Stat stat = zk.exists(path, false);
            if (stat != null) {
                log.debug("{} already exists, overwrite", key);
                zk.setData(path, serialize(value), stat.getVersion());
            } else {
                log.debug("new key {}", key);
                zk.create(path, serialize(value), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            if (handleException(e)) {
                setPropertyDirect(key, value);
                return;
            }
            e.printStackTrace();
        } finally {
            cache.invalidate(path);
        }
    }
}

From source file:backtype.storm.blobstore.KeySequenceNumber.java

License:Apache License

public synchronized int getKeySequenceNumber(Map conf) {
    TreeSet<Integer> sequenceNumbers = new TreeSet<Integer>();
    CuratorFramework zkClient = BlobStoreUtils.createZKClient(conf);
    try {/*  w  ww. j  a  v a  2  s.  co m*/
        // Key has not been created yet and it is the first time it is being created
        if (zkClient.checkExists().forPath(BLOBSTORE_SUBTREE + "/" + key) == null) {
            zkClient.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT)
                    .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                    .forPath(BLOBSTORE_MAX_KEY_SEQUENCE_SUBTREE + "/" + key);
            zkClient.setData().forPath(BLOBSTORE_MAX_KEY_SEQUENCE_SUBTREE + "/" + key,
                    ByteBuffer.allocate(INT_CAPACITY).putInt(INITIAL_SEQUENCE_NUMBER).array());
            return INITIAL_SEQUENCE_NUMBER;
        }

        // When all nimbodes go down and one or few of them come up
        // Unfortunately there might not be an exact way to know which one contains the most updated blob,
        // if all go down which is unlikely. Hence there might be a need to update the blob if all go down.
        List<String> stateInfoList = zkClient.getChildren().forPath(BLOBSTORE_SUBTREE + "/" + key);
        LOG.debug("stateInfoList-size {} stateInfoList-data {}", stateInfoList.size(), stateInfoList);
        if (stateInfoList.isEmpty()) {
            return getMaxSequenceNumber(zkClient);
        }

        LOG.debug("stateInfoSize {}", stateInfoList.size());
        // In all other cases check for the latest update sequence of the blob on the nimbus
        // and assign the appropriate number. Check if all are have same sequence number,
        // if not assign the highest sequence number.
        for (String stateInfo : stateInfoList) {
            sequenceNumbers.add(Integer.parseInt(
                    BlobStoreUtils.normalizeNimbusHostPortSequenceNumberInfo(stateInfo).getSequenceNumber()));
        }

        // Update scenario 2 and 3 explain the code logic written here
        // especially when nimbus crashes and comes up after and before update
        // respectively.
        int currentSeqNumber = getMaxSequenceNumber(zkClient);
        if (!checkIfStateContainsCurrentNimbusHost(stateInfoList, nimbusInfo) && !nimbusInfo.isLeader()) {
            if (sequenceNumbers.last() < currentSeqNumber) {
                return currentSeqNumber;
            } else {
                return INITIAL_SEQUENCE_NUMBER - 1;
            }
        }

        // It covers scenarios expalined in scenario 3 when nimbus-1 holding the latest
        // update goes down before it is downloaded by nimbus-2. Nimbus-2 gets elected as a leader
        // after which nimbus-1 comes back up and a read or update is performed.
        if (!checkIfStateContainsCurrentNimbusHost(stateInfoList, nimbusInfo) && nimbusInfo.isLeader()) {
            incrementMaxSequenceNumber(zkClient, currentSeqNumber);
            return currentSeqNumber + 1;
        }

        // This code logic covers the update scenarios in 2 when the nimbus-1 goes down
        // before syncing the blob to nimbus-2 and an update happens.
        // If seq-num for nimbus-2 is 2 and max-seq-number is 3 then next sequence number is 4
        // (max-seq-number + 1).
        // Other scenario it covers is when max-seq-number and nimbus seq number are equal.
        if (sequenceNumbers.size() == 1) {
            if (sequenceNumbers.first() < currentSeqNumber) {
                incrementMaxSequenceNumber(zkClient, currentSeqNumber);
                return currentSeqNumber + 1;
            } else {
                incrementMaxSequenceNumber(zkClient, currentSeqNumber);
                return sequenceNumbers.first() + 1;
            }
        }
    } catch (Exception e) {
        LOG.error("Exception {}", e);
    } finally {
        if (zkClient != null) {
            zkClient.close();
        }
    }
    // Normal create update sync scenario returns the greatest sequence number in the set
    return sequenceNumbers.last();
}

From source file:backtype.storm.transactional.state.TransactionalState.java

License:Apache License

public void setData(String path, Object obj) {
    path = "/" + path;
    byte[] ser = _ser.serializeObject(obj);
    try {/*from   w w w .j  a v  a2 s .c o m*/
        if (_curator.checkExists().forPath(path) != null) {
            _curator.setData().forPath(path, ser);
        } else {
            TransactionalState.createNode(_curator, path, ser, _zkAcls, CreateMode.PERSISTENT);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:basestormkafka.ZkState.java

License:Apache License

public void writeBytes(String path, byte[] bytes) {
    try {//from w ww  .  ja v  a 2 s  . c o m
        if (_curator.checkExists().forPath(path) == null) {
            _curator.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, bytes);
        } else {
            _curator.setData().forPath(path, bytes);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:blazingcache.zookeeper.ZKClusterManager.java

License:Apache License

/**
 *
 * @throws Exception in case of issue on starting the cluster manager
 *///from  ww  w  . j a v  a  2s .  c o  m
public void start() throws Exception {
    try {
        if (this.zk.exists(basePath, false) == null) {
            LOGGER.log(Level.SEVERE, "creating base path " + basePath);
            try {
                this.zk.create(basePath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            } catch (KeeperException anyError) {
                throw new Exception("Could not init Zookeeper space at path " + basePath, anyError);
            }
        }
        if (this.zk.exists(discoverypath, false) == null) {
            LOGGER.log(Level.SEVERE, "creating discoverypath path " + discoverypath);
            try {
                this.zk.create(discoverypath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            } catch (KeeperException anyError) {
                throw new Exception("Could not init Zookeeper space at path " + discoverypath, anyError);
            }
        }
        String newPath = zk.create(discoverypath + "/brokers", localhostdata, ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL_SEQUENTIAL);
        LOGGER.log(Level.SEVERE, "my own discoverypath path is " + newPath);

    } catch (KeeperException error) {
        throw new Exception("Could not init Zookeeper space at path " + basePath, error);
    }
}