Search in sources :

Example 11 with ZooKeeper

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

the class NIOServerCnxnTest method testOperationsAfterCnxnClose.

/**
     * Test operations on ServerCnxn after socket closure.
     */
@Test(timeout = 60000)
public void testOperationsAfterCnxnClose() throws IOException, InterruptedException, KeeperException {
    final ZooKeeper zk = createClient();
    final String path = "/a";
    try {
        // make sure zkclient works
        zk.create(path, "test".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        Assert.assertNotNull("Didn't create znode:" + path, zk.exists(path, false));
        // Defaults ServerCnxnFactory would be instantiated with
        // NIOServerCnxnFactory
        Assert.assertTrue("Didn't instantiate ServerCnxnFactory with NIOServerCnxnFactory!", serverFactory instanceof NIOServerCnxnFactory);
        Iterable<ServerCnxn> connections = serverFactory.getConnections();
        for (ServerCnxn serverCnxn : connections) {
            serverCnxn.close();
            try {
                serverCnxn.toString();
            } catch (Exception e) {
                LOG.error("Exception while getting connection details!", e);
                Assert.fail("Shouldn't throw exception while " + "getting connection details!");
            }
        }
    } finally {
        zk.close();
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with ZooKeeper

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

the class ZooKeeperServerMainTest method testStandalone.

/**
     * Verify the ability to start a standalone server instance.
     */
@Test
public void testStandalone() throws Exception {
    ClientBase.setupTestEnv();
    final int CLIENT_PORT = PortAssignment.unique();
    MainThread main = new MainThread(CLIENT_PORT, true, null);
    main.start();
    Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT));
    clientConnected = new CountDownLatch(1);
    ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT, this);
    Assert.assertTrue("Failed to establish zkclient connection!", clientConnected.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS));
    zk.create("/foo", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Assert.assertEquals(new String(zk.getData("/foo", null, null)), "foobar");
    zk.close();
    main.shutdown();
    main.join();
    main.deleteDirs();
    Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT));
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 13 with ZooKeeper

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

the class ZooKeeperServerMainTest method verifySessionTimeOut.

private void verifySessionTimeOut(int sessionTimeout, int expectedSessionTimeout, String HOSTPORT) throws IOException, KeeperException, InterruptedException {
    clientConnected = new CountDownLatch(1);
    ZooKeeper zk = new ZooKeeper(HOSTPORT, sessionTimeout, this);
    Assert.assertTrue("Failed to establish zkclient connection!", clientConnected.await(sessionTimeout, TimeUnit.MILLISECONDS));
    Assert.assertEquals("Not able to configure the sessionTimeout values", expectedSessionTimeout, zk.getSessionTimeout());
    zk.close();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 14 with ZooKeeper

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

the class ZooKeeperServerMainTest method testNonRecoverableError.

/**
     * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2247.
     * Test to verify that even after non recoverable error (error while
     * writing transaction log), ZooKeeper is still available.
     */
@Test(timeout = 30000)
public void testNonRecoverableError() throws Exception {
    ClientBase.setupTestEnv();
    final int CLIENT_PORT = PortAssignment.unique();
    MainThread main = new MainThread(CLIENT_PORT, true, null);
    main.start();
    Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT));
    ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT, this);
    zk.create("/foo1", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    Assert.assertEquals(new String(zk.getData("/foo1", null, null)), "foobar");
    // inject problem in server
    ZooKeeperServer zooKeeperServer = main.getCnxnFactory().getZooKeeperServer();
    FileTxnSnapLog snapLog = zooKeeperServer.getTxnLogFactory();
    FileTxnSnapLog fileTxnSnapLogWithError = new FileTxnSnapLog(snapLog.getDataDir(), snapLog.getSnapDir()) {

        @Override
        public void commit() throws IOException {
            throw new IOException("Input/output error");
        }
    };
    ZKDatabase newDB = new ZKDatabase(fileTxnSnapLogWithError);
    zooKeeperServer.setZKDatabase(newDB);
    try {
        // do create operation, so that injected IOException is thrown
        zk.create("/foo2", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        fail("IOException is expected as error is injected in transaction log commit funtionality");
    } catch (Exception e) {
    // do nothing
    }
    zk.close();
    Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT));
    fileTxnSnapLogWithError.close();
    main.shutdown();
    main.deleteDirs();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) IOException(java.io.IOException) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) KeeperException(org.apache.zookeeper.KeeperException) ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException) IOException(java.io.IOException) Test(org.junit.Test)

Example 15 with ZooKeeper

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

the class ZooKeeperServerStartupTest method testClientConnectionRequestDuringStartupWithNettyServerCnxn.

/**
     * Test case for
     * {@link https://issues.apache.org/jira/browse/ZOOKEEPER-2383}.
     */
@Test(timeout = 30000)
public void testClientConnectionRequestDuringStartupWithNettyServerCnxn() throws Exception {
    tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    String originalServerCnxnFactory = System.getProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY);
    try {
        System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, NettyServerCnxnFactory.class.getName());
        startSimpleZKServer(startupDelayLatch);
        SimpleZooKeeperServer simplezks = (SimpleZooKeeperServer) zks;
        Assert.assertTrue("Failed to invoke zks#startup() method during server startup", simplezks.waitForStartupInvocation(10));
        CountdownWatcher watcher = new CountdownWatcher();
        ZooKeeper zkClient = new ZooKeeper(HOSTPORT, ClientBase.CONNECTION_TIMEOUT, watcher);
        Assert.assertFalse("Since server is not fully started, zks#createSession() shouldn't be invoked", simplezks.waitForSessionCreation(5));
        LOG.info("Decrements the count of the latch, so that server will proceed with startup");
        startupDelayLatch.countDown();
        Assert.assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, ClientBase.CONNECTION_TIMEOUT));
        Assert.assertTrue("Failed to invoke zks#createSession() method during client session creation", simplezks.waitForSessionCreation(5));
        watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
        zkClient.close();
    } finally {
        // reset cnxn factory
        if (originalServerCnxnFactory == null) {
            System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY);
            return;
        }
        System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, originalServerCnxnFactory);
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) Test(org.junit.Test)

Aggregations

ZooKeeper (org.apache.zookeeper.ZooKeeper)311 Test (org.junit.Test)172 KeeperException (org.apache.zookeeper.KeeperException)89 Stat (org.apache.zookeeper.data.Stat)43 WatchedEvent (org.apache.zookeeper.WatchedEvent)36 ArrayList (java.util.ArrayList)35 TestableZooKeeper (org.apache.zookeeper.TestableZooKeeper)33 IOException (java.io.IOException)32 CountDownLatch (java.util.concurrent.CountDownLatch)30 Watcher (org.apache.zookeeper.Watcher)25 File (java.io.File)23 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)21 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)21 ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)20 ReconfigTest (org.apache.zookeeper.test.ReconfigTest)16 ZooKeeperAdmin (org.apache.zookeeper.admin.ZooKeeperAdmin)15 TimeoutException (java.util.concurrent.TimeoutException)8 AsyncCallback (org.apache.zookeeper.AsyncCallback)8 ACL (org.apache.zookeeper.data.ACL)8 Id (org.apache.zookeeper.data.Id)8