Search in sources :

Example 36 with CountdownWatcher

use of org.apache.zookeeper.test.ClientBase.CountdownWatcher in project zookeeper by apache.

the class ZooKeeperServerStartupTest method testClientConnectionRequestDuringStartupWithNIOServerCnxn.

/**
 * Test case for
 * https://issues.apache.org/jira/browse/ZOOKEEPER-2383
 */
@Test
@Timeout(value = 30)
public void testClientConnectionRequestDuringStartupWithNIOServerCnxn() throws Exception {
    tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    startSimpleZKServer(startupDelayLatch);
    SimpleZooKeeperServer simplezks = (SimpleZooKeeperServer) zks;
    assertTrue(simplezks.waitForStartupInvocation(10), "Failed to invoke zks#startup() method during server startup");
    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zkClient = new ZooKeeper(HOSTPORT, ClientBase.CONNECTION_TIMEOUT, watcher);
    assertFalse(simplezks.waitForSessionCreation(5), "Since server is not fully started, zks#createSession() shouldn't be invoked");
    LOG.info("Decrements the count of the latch, so that server will proceed with startup");
    startupDelayLatch.countDown();
    assertTrue(ClientBase.waitForServerUp(HOSTPORT, ClientBase.CONNECTION_TIMEOUT), "waiting for server being up ");
    assertTrue(simplezks.waitForSessionCreation(5), "Failed to invoke zks#createSession() method during client session creation");
    watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
    zkClient.close();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 37 with CountdownWatcher

use of org.apache.zookeeper.test.ClientBase.CountdownWatcher in project zookeeper by apache.

the class ZxidRolloverTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    System.setProperty("zookeeper.admin.enableServer", "false");
    // set the snap count to something low so that we force log rollover
    // and verify that is working as part of the epoch rollover.
    SyncRequestProcessor.setSnapCount(7);
    qu = new QuorumUtil(1);
    startAll();
    for (int i = 0; i < zkClients.length; i++) {
        zkClientWatchers[i] = new CountdownWatcher();
        PeerStruct peer = qu.getPeer(i + 1);
        zkClients[i] = new ZooKeeper("127.0.0.1:" + peer.clientPort, ClientTest.CONNECTION_TIMEOUT, zkClientWatchers[i]);
    }
    waitForClientsConnected();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) QuorumUtil(org.apache.zookeeper.test.QuorumUtil) PeerStruct(org.apache.zookeeper.test.QuorumUtil.PeerStruct) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 38 with CountdownWatcher

use of org.apache.zookeeper.test.ClientBase.CountdownWatcher in project zookeeper by apache.

the class ZooKeeperServerMaxCnxnsTest method testMaxZooKeeperClients.

private void testMaxZooKeeperClients(String serverCnxnFactory) throws Exception {
    final int[] clientPorts = new int[SERVER_COUNT];
    int maxCnxns = 2;
    StringBuilder sb = new StringBuilder();
    sb.append("maxCnxns=" + maxCnxns + "\n");
    sb.append("serverCnxnFactory=" + serverCnxnFactory + "\n");
    String server;
    for (int i = 0; i < SERVER_COUNT; i++) {
        clientPorts[i] = PortAssignment.unique();
        server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant;127.0.0.1:" + clientPorts[i];
        sb.append(server + "\n");
    }
    String currentQuorumCfgSection = sb.toString();
    MainThread[] mt = new MainThread[SERVER_COUNT];
    // start 3 servers
    for (int i = 0; i < SERVER_COUNT; i++) {
        mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false);
        mt[i].start();
    }
    // ensure all servers started
    for (int i = 0; i < SERVER_COUNT; i++) {
        assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT), "waiting for server " + i + " being up");
    }
    int maxAllowedConnection = maxCnxns * SERVER_COUNT;
    String cxnString = getCxnString(clientPorts);
    final CountDownLatch countDownLatch = new CountDownLatch(maxAllowedConnection);
    ZooKeeper[] clients = new ZooKeeper[maxAllowedConnection];
    Watcher watcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getState() == Event.KeeperState.SyncConnected) {
                countDownLatch.countDown();
            }
        }
    };
    for (int i = 0; i < maxAllowedConnection; i++) {
        clients[i] = new ZooKeeper(cxnString, ClientBase.CONNECTION_TIMEOUT, watcher);
        Thread.sleep(100);
    }
    countDownLatch.await();
    // reaching this point indicates that all maxAllowedConnection connected
    // No more client to be allowed to connect now as we have reached the
    // max connections
    CountdownWatcher cdw = new CountdownWatcher();
    ZooKeeper extraClient = new ZooKeeper(cxnString, ClientBase.CONNECTION_TIMEOUT, cdw);
    try {
        cdw.waitForConnected(ClientBase.CONNECTION_TIMEOUT / 2);
        fail("Client is not supposed to get connected as max connection already reached.");
    } catch (TimeoutException e) {
        extraClient.close();
    }
    // lets close one already connected client
    clients[0].close();
    // Now extra client must automatically get connected
    cdw = new CountdownWatcher();
    extraClient = new ZooKeeper(cxnString, ClientBase.CONNECTION_TIMEOUT, cdw);
    cdw.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
    // verify some basic operation
    String create = extraClient.create("/test", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    assertEquals("/test", create);
    // cleanup
    extraClient.close();
}
Also used : CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) Watcher(org.apache.zookeeper.Watcher) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) CountDownLatch(java.util.concurrent.CountDownLatch) WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) TimeoutException(java.util.concurrent.TimeoutException)

Example 39 with CountdownWatcher

use of org.apache.zookeeper.test.ClientBase.CountdownWatcher in project zookeeper by apache.

the class QuorumTest method testMultiToFollower.

// skip superhammer and clientcleanup as they are too expensive for quorum
/**
 * Tests if a multiop submitted to a non-leader propagates to the leader properly
 * (see ZOOKEEPER-1124).
 *
 * The test works as follows. It has a client connect to a follower and submit a multiop
 * to the follower. It then verifies that the multiop successfully gets committed by the leader.
 *
 * Without the fix in ZOOKEEPER-1124, this fails with a ConnectionLoss KeeperException.
 */
@Test
public void testMultiToFollower() throws Exception {
    qu = new QuorumUtil(1);
    CountdownWatcher watcher = new CountdownWatcher();
    qu.startQuorum();
    int index = 1;
    while (qu.getPeer(index).peer.leader == null) {
        index++;
    }
    ZooKeeper zk = new ZooKeeper("127.0.0.1:" + qu.getPeer((index == 1) ? 2 : 1).peer.getClientPort(), ClientBase.CONNECTION_TIMEOUT, watcher);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    zk.multi(Arrays.asList(Op.create("/multi0", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), Op.create("/multi1", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), Op.create("/multi2", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)));
    zk.getData("/multi0", false, null);
    zk.getData("/multi1", false, null);
    zk.getData("/multi2", false, null);
    zk.close();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) Test(org.junit.jupiter.api.Test)

Example 40 with CountdownWatcher

use of org.apache.zookeeper.test.ClientBase.CountdownWatcher in project zookeeper by apache.

the class QuorumTest method testFollowersStartAfterLeader.

/**
 * See ZOOKEEPER-790 for details
 */
@Test
public void testFollowersStartAfterLeader() throws Exception {
    qu = new QuorumUtil(1);
    CountdownWatcher watcher = new CountdownWatcher();
    qu.startQuorum();
    int index = 1;
    while (qu.getPeer(index).peer.leader == null) {
        index++;
    }
    // break the quorum
    qu.shutdown(index);
    // try to reestablish the quorum
    qu.start(index);
    // Connect the client after services are restarted (otherwise we would get
    // SessionExpiredException as the previous local session was not persisted).
    ZooKeeper zk = new ZooKeeper("127.0.0.1:" + qu.getPeer((index == 1) ? 2 : 1).peer.getClientPort(), ClientBase.CONNECTION_TIMEOUT, watcher);
    try {
        watcher.waitForConnected(CONNECTION_TIMEOUT);
    } catch (TimeoutException e) {
        fail("client could not connect to reestablished quorum: giving up after 30+ seconds.");
    }
    zk.close();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Aggregations

CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)43 ZooKeeper (org.apache.zookeeper.ZooKeeper)40 Test (org.junit.jupiter.api.Test)33 Timeout (org.junit.jupiter.api.Timeout)26 HashMap (java.util.HashMap)14 KeeperException (org.apache.zookeeper.KeeperException)9 TimeoutException (java.util.concurrent.TimeoutException)7 Stat (org.apache.zookeeper.data.Stat)5 ClientTest (org.apache.zookeeper.test.ClientTest)5 IOException (java.io.IOException)4 File (java.io.File)3 TestableZooKeeper (org.apache.zookeeper.TestableZooKeeper)3 WatchedEvent (org.apache.zookeeper.WatchedEvent)3 ZooKeeperAdmin (org.apache.zookeeper.admin.ZooKeeperAdmin)3 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)3 Collection (java.util.Collection)2 Set (java.util.Set)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 Semaphore (java.util.concurrent.Semaphore)2 TimeUnit (java.util.concurrent.TimeUnit)2