Search in sources :

Example 1 with Stat

use of org.apache.zookeeper.data.Stat in project storm by apache.

the class Zookeeper method getVersion.

public static Integer getVersion(CuratorFramework zk, String path, boolean watch) throws Exception {
    String npath = normalizePath(path);
    Stat stat = null;
    if (existsNode(zk, npath, watch)) {
        if (watch) {
            stat = zk.checkExists().watched().forPath(npath);
        } else {
            stat = zk.checkExists().forPath(npath);
        }
    }
    return stat == null ? null : Integer.valueOf(stat.getVersion());
}
Also used : Stat(org.apache.zookeeper.data.Stat)

Example 2 with Stat

use of org.apache.zookeeper.data.Stat in project hbase by apache.

the class MasterAddressTracker method deleteIfEquals.

/**
   * delete the master znode if its content is same as the parameter
   * @param zkw must not be null
   * @param content must not be null
   */
public static boolean deleteIfEquals(ZooKeeperWatcher zkw, final String content) {
    if (content == null) {
        throw new IllegalArgumentException("Content must not be null");
    }
    try {
        Stat stat = new Stat();
        byte[] data = ZKUtil.getDataNoWatch(zkw, zkw.znodePaths.masterAddressZNode, stat);
        ServerName sn = ProtobufUtil.parseServerNameFrom(data);
        if (sn != null && content.equals(sn.toString())) {
            return (ZKUtil.deleteNode(zkw, zkw.znodePaths.masterAddressZNode, stat.getVersion()));
        }
    } catch (KeeperException e) {
        LOG.warn("Can't get or delete the master znode", e);
    } catch (DeserializationException e) {
        LOG.warn("Can't get or delete the master znode", e);
    }
    return false;
}
Also used : Stat(org.apache.zookeeper.data.Stat) ServerName(org.apache.hadoop.hbase.ServerName) KeeperException(org.apache.zookeeper.KeeperException) DeserializationException(org.apache.hadoop.hbase.exceptions.DeserializationException)

Example 3 with Stat

use of org.apache.zookeeper.data.Stat in project hbase by apache.

the class ZKUtil method watchAndCheckExists.

//
// Existence checks and watches
//
/**
   * Watch the specified znode for delete/create/change events.  The watcher is
   * set whether or not the node exists.  If the node already exists, the method
   * returns true.  If the node does not exist, the method returns false.
   *
   * @param zkw zk reference
   * @param znode path of node to watch
   * @return true if znode exists, false if does not exist or error
   * @throws KeeperException if unexpected zookeeper exception
   */
public static boolean watchAndCheckExists(ZooKeeperWatcher zkw, String znode) throws KeeperException {
    try {
        Stat s = zkw.getRecoverableZooKeeper().exists(znode, zkw);
        boolean exists = s != null ? true : false;
        if (exists) {
            LOG.debug(zkw.prefix("Set watcher on existing znode=" + znode));
        } else {
            LOG.debug(zkw.prefix("Set watcher on znode that does not yet exist, " + znode));
        }
        return exists;
    } catch (KeeperException e) {
        LOG.warn(zkw.prefix("Unable to set watcher on znode " + znode), e);
        zkw.keeperException(e);
        return false;
    } catch (InterruptedException e) {
        LOG.warn(zkw.prefix("Unable to set watcher on znode " + znode), e);
        zkw.interruptedException(e);
        return false;
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) KeeperException(org.apache.zookeeper.KeeperException)

Example 4 with Stat

use of org.apache.zookeeper.data.Stat in project hbase by apache.

the class ZKUtil method createAndWatch.

/**
   * Creates the specified node with the specified data and watches it.
   *
   * <p>Throws an exception if the node already exists.
   *
   * <p>The node created is persistent and open access.
   *
   * <p>Returns the version number of the created node if successful.
   *
   * @param zkw zk reference
   * @param znode path of node to create
   * @param data data of node to create
   * @return version of node created
   * @throws KeeperException if unexpected zookeeper exception
   * @throws KeeperException.NodeExistsException if node already exists
   */
public static int createAndWatch(ZooKeeperWatcher zkw, String znode, byte[] data) throws KeeperException, KeeperException.NodeExistsException {
    try {
        zkw.getRecoverableZooKeeper().create(znode, data, createACL(zkw, znode), CreateMode.PERSISTENT);
        Stat stat = zkw.getRecoverableZooKeeper().exists(znode, zkw);
        if (stat == null) {
            // Likely a race condition. Someone deleted the znode.
            throw KeeperException.create(KeeperException.Code.SYSTEMERROR, "ZK.exists returned null (i.e.: znode does not exist) for znode=" + znode);
        }
        return stat.getVersion();
    } catch (InterruptedException e) {
        zkw.interruptedException(e);
        return -1;
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat)

Example 5 with Stat

use of org.apache.zookeeper.data.Stat in project zookeeper by apache.

the class QuorumRequestPipelineTest method testExists.

@Test
public void testExists() throws Exception {
    Stat stat = create2EmptyNode(zkClient, PARENT_PATH);
    Assert.assertEquals(String.format("%s Exists returns correct node stat", serverState), stat, zkClient.exists(PARENT_PATH, false));
}
Also used : Stat(org.apache.zookeeper.data.Stat) Test(org.junit.Test)

Aggregations

Stat (org.apache.zookeeper.data.Stat)778 KeeperException (org.apache.zookeeper.KeeperException)261 Test (org.junit.Test)124 IOException (java.io.IOException)109 ZooKeeper (org.apache.zookeeper.ZooKeeper)84 ArrayList (java.util.ArrayList)59 Test (org.testng.annotations.Test)58 Test (org.junit.jupiter.api.Test)51 ACL (org.apache.zookeeper.data.ACL)49 AsyncCallback (org.apache.zookeeper.AsyncCallback)48 Watcher (org.apache.zookeeper.Watcher)47 List (java.util.List)43 CountDownLatch (java.util.concurrent.CountDownLatch)43 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)38 CuratorFramework (org.apache.curator.framework.CuratorFramework)37 WatchedEvent (org.apache.zookeeper.WatchedEvent)36 Map (java.util.Map)32 HashMap (java.util.HashMap)30 WebApplicationException (javax.ws.rs.WebApplicationException)29 ExecutionException (java.util.concurrent.ExecutionException)27