Search in sources :

Example 31 with ZooKeeper

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

the class ChrootClientTest method setUp.

@Override
public void setUp() throws Exception {
    String hp = hostPort;
    hostPort = hostPort + "/chrootclienttest";
    System.out.println(hostPort);
    super.setUp();
    LOG.info("STARTING " + getTestName());
    ZooKeeper zk = createClient(hp);
    try {
        zk.create("/chrootclienttest", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } finally {
        zk.close();
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper)

Example 32 with ZooKeeper

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

the class ClientBase method verifyUnexpectedBeans.

private void verifyUnexpectedBeans(Set<ObjectName> children) {
    if (allClients != null) {
        for (ZooKeeper zkc : allClients) {
            Iterator<ObjectName> childItr = children.iterator();
            while (childItr.hasNext()) {
                ObjectName clientBean = childItr.next();
                if (clientBean.toString().contains(getHexSessionId(zkc.getSessionId()))) {
                    LOG.info("found name:" + zkc.getSessionId() + " client bean:" + clientBean.toString());
                    childItr.remove();
                }
            }
        }
    }
    for (ObjectName bean : children) {
        LOG.info("unexpected:" + bean.toString());
    }
    Assert.assertEquals("Unexpected bean exists!", 0, children.size());
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) ObjectName(javax.management.ObjectName)

Example 33 with ZooKeeper

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

the class ClientHammerTest method runHammer.

public void runHammer(final int threadCount, final int childCount) throws Throwable {
    try {
        HammerThread[] threads = new HammerThread[threadCount];
        long start = Time.currentElapsedTime();
        for (int i = 0; i < threads.length; i++) {
            ZooKeeper zk = createClient();
            String prefix = "/test-" + i;
            zk.create(prefix, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            prefix += "/";
            HammerThread thread = new BasicHammerThread("BasicHammerThread-" + i, zk, prefix, childCount);
            thread.start();
            threads[i] = thread;
        }
        verifyHammer(start, threads, childCount);
    } catch (Throwable t) {
        LOG.error("test Assert.failed", t);
        throw t;
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper)

Example 34 with ZooKeeper

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

the class DisconnectedWatcherTest method testDefaultWatcherAutoResetWithChroot.

@Test
public void testDefaultWatcherAutoResetWithChroot() throws Exception {
    ZooKeeper zk1 = createClient();
    zk1.create("/ch1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    MyWatcher watcher = new MyWatcher();
    ZooKeeper zk2 = createClient(watcher, hostPort + "/ch1");
    zk2.getChildren("/", true);
    // this call shouldn't trigger any error or watch
    zk1.create("/youdontmatter1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // this should trigger the watch
    zk1.create("/ch1/youshouldmatter1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    WatchedEvent e = watcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS);
    Assert.assertNotNull(e);
    Assert.assertEquals(EventType.NodeChildrenChanged, e.getType());
    Assert.assertEquals("/", e.getPath());
    zk2.getChildren("/", true);
    stopServer();
    watcher.waitForDisconnected(3000);
    startServer();
    watcher.waitForConnected(3000);
    // this should trigger the watch
    zk1.create("/ch1/youshouldmatter2", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    e = watcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS);
    Assert.assertNotNull(e);
    Assert.assertEquals(EventType.NodeChildrenChanged, e.getType());
    Assert.assertEquals("/", e.getPath());
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Test(org.junit.Test)

Example 35 with ZooKeeper

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

the class EmptiedSnapshotRecoveryTest method runTest.

public void runTest(boolean leaveEmptyFile) throws Exception {
    File tmpSnapDir = ClientBase.createTmpDir();
    File tmpLogDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpSnapDir, tmpLogDir, 3000);
    SyncRequestProcessor.setSnapCount(SNAP_COUNT);
    final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
    f.startup(zks);
    Assert.assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
    ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this);
    try {
        for (int i = 0; i < N_TRANSACTIONS; i++) {
            zk.create("/node-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
    } finally {
        zk.close();
    }
    f.shutdown();
    zks.shutdown();
    Assert.assertTrue("waiting for server to shutdown", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
    // start server again with intact database
    zks = new ZooKeeperServer(tmpSnapDir, tmpLogDir, 3000);
    zks.startdata();
    long zxid = zks.getZKDatabase().getDataTreeLastProcessedZxid();
    LOG.info("After clean restart, zxid = " + zxid);
    Assert.assertTrue("zxid > 0", zxid > 0);
    zks.shutdown();
    // Make all snapshots empty
    FileTxnSnapLog txnLogFactory = zks.getTxnLogFactory();
    List<File> snapshots = txnLogFactory.findNRecentSnapshots(10);
    Assert.assertTrue("We have a snapshot to corrupt", snapshots.size() > 0);
    for (File file : snapshots) {
        if (leaveEmptyFile) {
            new PrintWriter(file).close();
        } else {
            file.delete();
        }
    }
    // start server again with corrupted database
    zks = new ZooKeeperServer(tmpSnapDir, tmpLogDir, 3000);
    try {
        zks.startdata();
        zxid = zks.getZKDatabase().loadDataBase();
        Assert.fail("Should have gotten exception for corrupted database");
    } catch (IOException e) {
    // expected behavior
    }
    zks.shutdown();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) IOException(java.io.IOException) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) PrintWriter(java.io.PrintWriter)

Aggregations

ZooKeeper (org.apache.zookeeper.ZooKeeper)330 Test (org.junit.Test)189 KeeperException (org.apache.zookeeper.KeeperException)90 Stat (org.apache.zookeeper.data.Stat)44 ArrayList (java.util.ArrayList)35 WatchedEvent (org.apache.zookeeper.WatchedEvent)35 TestableZooKeeper (org.apache.zookeeper.TestableZooKeeper)34 IOException (java.io.IOException)32 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)32 CountDownLatch (java.util.concurrent.CountDownLatch)31 Watcher (org.apache.zookeeper.Watcher)24 File (java.io.File)22 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)21 HashMap (java.util.HashMap)17 ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)17 ReconfigTest (org.apache.zookeeper.test.ReconfigTest)16 ZooKeeperAdmin (org.apache.zookeeper.admin.ZooKeeperAdmin)15 ACL (org.apache.zookeeper.data.ACL)10 ClientTest (org.apache.zookeeper.test.ClientTest)10 TimeoutException (java.util.concurrent.TimeoutException)9