Search in sources :

Example 1 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project hbase by apache.

the class HBaseTestingUtility method expireSession.

/**
   * Expire a ZooKeeper session as recommended in ZooKeeper documentation
   * http://hbase.apache.org/book.html#trouble.zookeeper
   * There are issues when doing this:
   * [1] http://www.mail-archive.com/dev@zookeeper.apache.org/msg01942.html
   * [2] https://issues.apache.org/jira/browse/ZOOKEEPER-1105
   *
   * @param nodeZK - the ZK watcher to expire
   * @param checkStatus - true to check if we can create an HTable with the
   *                    current configuration.
   */
public void expireSession(ZooKeeperWatcher nodeZK, boolean checkStatus) throws Exception {
    Configuration c = new Configuration(this.conf);
    String quorumServers = ZKConfig.getZKQuorumServersString(c);
    ZooKeeper zk = nodeZK.getRecoverableZooKeeper().getZooKeeper();
    byte[] password = zk.getSessionPasswd();
    long sessionID = zk.getSessionId();
    // Expiry seems to be asynchronous (see comment from P. Hunt in [1]),
    //  so we create a first watcher to be sure that the
    //  event was sent. We expect that if our watcher receives the event
    //  other watchers on the same machine will get is as well.
    // When we ask to close the connection, ZK does not close it before
    //  we receive all the events, so don't have to capture the event, just
    //  closing the connection should be enough.
    ZooKeeper monitor = new ZooKeeper(quorumServers, 1000, new org.apache.zookeeper.Watcher() {

        @Override
        public void process(WatchedEvent watchedEvent) {
            LOG.info("Monitor ZKW received event=" + watchedEvent);
        }
    }, sessionID, password);
    // Making it expire
    ZooKeeper newZK = new ZooKeeper(quorumServers, 1000, EmptyWatcher.instance, sessionID, password);
    //ensure that we have connection to the server before closing down, otherwise
    //the close session event will be eaten out before we start CONNECTING state
    long start = System.currentTimeMillis();
    while (newZK.getState() != States.CONNECTED && System.currentTimeMillis() - start < 1000) {
        Thread.sleep(1);
    }
    newZK.close();
    LOG.info("ZK Closed Session 0x" + Long.toHexString(sessionID));
    // Now closing & waiting to be sure that the clients get it.
    monitor.close();
    if (checkStatus) {
        getConnection().getTable(TableName.META_TABLE_NAME).close();
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Configuration(org.apache.hadoop.conf.Configuration)

Example 2 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project hbase by apache.

the class ZkAclReset method resetAcls.

private static void resetAcls(final ZooKeeperWatcher zkw, final String znode, final boolean eraseAcls) throws Exception {
    List<String> children = ZKUtil.listChildrenNoWatch(zkw, znode);
    if (children != null) {
        for (String child : children) {
            resetAcls(zkw, ZKUtil.joinZNode(znode, child), eraseAcls);
        }
    }
    ZooKeeper zk = zkw.getRecoverableZooKeeper().getZooKeeper();
    if (eraseAcls) {
        LOG.info(" - erase ACLs for " + znode);
        zk.setACL(znode, ZooDefs.Ids.OPEN_ACL_UNSAFE, -1);
    } else {
        LOG.info(" - set ACLs for " + znode);
        zk.setACL(znode, ZKUtil.createACL(zkw, znode, true), -1);
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper)

Example 3 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.

the class QuorumPeerMainTest method waitForAll.

private void waitForAll(ZooKeeper[] zks, States state) throws InterruptedException {
    int iterations = ClientBase.CONNECTION_TIMEOUT / 1000;
    boolean someoneNotConnected = true;
    while (someoneNotConnected) {
        if (iterations-- == 0) {
            ClientBase.logAllStackTraces();
            throw new RuntimeException("Waiting too long");
        }
        someoneNotConnected = false;
        for (ZooKeeper zk : zks) {
            if (zk.getState() != state) {
                someoneNotConnected = true;
                break;
            }
        }
        Thread.sleep(1000);
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper)

Example 4 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.

the class SaslAuthTest method testValidSaslIds.

@Test
public void testValidSaslIds() throws Exception {
    ZooKeeper zk = createClient();
    List<String> validIds = new ArrayList<String>();
    validIds.add("user");
    validIds.add("service/host.name.com");
    validIds.add("user@KERB.REALM");
    validIds.add("service/host.name.com@KERB.REALM");
    int i = 0;
    for (String validId : validIds) {
        List<ACL> aclList = new ArrayList<ACL>();
        ACL acl = new ACL(0, new Id("sasl", validId));
        aclList.add(acl);
        zk.create("/valid" + i, null, aclList, CreateMode.PERSISTENT);
        i++;
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) ArrayList(java.util.ArrayList) ACL(org.apache.zookeeper.data.ACL) Id(org.apache.zookeeper.data.Id) Test(org.junit.Test)

Example 5 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.

the class WatchEventWhenAutoResetTest method testNodeChildrenChanged.

@Test
public void testNodeChildrenChanged() throws Exception {
    QuorumUtil qu = new QuorumUtil(1);
    qu.startAll();
    EventsWatcher watcher = new EventsWatcher();
    ZooKeeper zk1 = createClient(qu, 1, watcher);
    ZooKeeper zk2 = createClient(qu, 2);
    String path = "/test-children-changed";
    zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk1.getChildren(path, watcher);
    qu.shutdown(1);
    zk2.create(path + "/children-1", new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    qu.start(1);
    watcher.waitForConnected(TIMEOUT * 1000L);
    watcher.assertEvent(TIMEOUT, EventType.NodeChildrenChanged);
    qu.shutdownAll();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) Test(org.junit.Test)

Aggregations

ZooKeeper (org.apache.zookeeper.ZooKeeper)605 Test (org.junit.jupiter.api.Test)190 KeeperException (org.apache.zookeeper.KeeperException)153 Test (org.junit.Test)100 Stat (org.apache.zookeeper.data.Stat)86 IOException (java.io.IOException)83 CountDownLatch (java.util.concurrent.CountDownLatch)80 WatchedEvent (org.apache.zookeeper.WatchedEvent)70 Watcher (org.apache.zookeeper.Watcher)59 ArrayList (java.util.ArrayList)47 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)41 TestableZooKeeper (org.apache.zookeeper.TestableZooKeeper)38 Timeout (org.junit.jupiter.api.Timeout)32 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)31 File (java.io.File)28 HashMap (java.util.HashMap)26 CompletableFuture (java.util.concurrent.CompletableFuture)22 Test (org.testng.annotations.Test)21 AsyncCallback (org.apache.zookeeper.AsyncCallback)19 ACL (org.apache.zookeeper.data.ACL)19