Search in sources :

Example 16 with CountdownWatcher

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

the class QuorumKerberosHostBasedAuthTest method testValidCredentials.

/**
 * Test to verify that server is able to start with valid credentials
 */
@Test
@Timeout(value = 120)
public void testValidCredentials() throws Exception {
    String serverPrincipal = hostServerPrincipal.substring(0, hostServerPrincipal.lastIndexOf("@"));
    Map<String, String> authConfigs = new HashMap<String, String>();
    authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true");
    authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true");
    authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true");
    authConfigs.put(QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL, serverPrincipal);
    String connectStr = startQuorum(3, authConfigs, 3);
    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher);
    watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
    for (int i = 0; i < 10; i++) {
        zk.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    zk.close();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) HashMap(java.util.HashMap) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 17 with CountdownWatcher

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

the class ZooKeeperServerStartupTest method testClientConnectionRequestDuringStartupWithNettyServerCnxn.

/**
 * Test case for
 * https://issues.apache.org/jira/browse/ZOOKEEPER-2383
 */
@Test
@Timeout(value = 30)
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;
        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();
    } 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.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 18 with CountdownWatcher

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

the class ReconfigDuringLeaderSyncTest method testDuringLeaderSync.

/**
 * <pre>
 * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2172.
 * Cluster crashes when reconfig a new node as a participant.
 * </pre>
 *
 * This issue occurs when reconfig's PROPOSAL and COMMITANDACTIVATE come in
 * between the snapshot and the UPTODATE. In this case processReconfig was
 * not invoked on the newly added node, and zoo.cfg.dynamic.next wasn't
 * deleted.
 */
@Test
public void testDuringLeaderSync() throws Exception {
    final int[] clientPorts = new int[SERVER_COUNT + 1];
    StringBuilder sb = new StringBuilder();
    String[] serverConfig = new String[SERVER_COUNT + 1];
    for (int i = 0; i < SERVER_COUNT; i++) {
        clientPorts[i] = PortAssignment.unique();
        serverConfig[i] = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant;127.0.0.1:" + clientPorts[i];
        sb.append(serverConfig[i] + "\n");
    }
    String currentQuorumCfgSection = sb.toString();
    mt = new MainThread[SERVER_COUNT + 1];
    // 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++) {
        Assert.assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT));
    }
    CountdownWatcher watch = new CountdownWatcher();
    ZooKeeperAdmin preReconfigClient = new ZooKeeperAdmin("127.0.0.1:" + clientPorts[0], ClientBase.CONNECTION_TIMEOUT, watch);
    preReconfigClient.addAuthInfo("digest", "super:test".getBytes());
    watch.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
    // new server joining
    int joinerId = SERVER_COUNT;
    clientPorts[joinerId] = PortAssignment.unique();
    serverConfig[joinerId] = "server." + joinerId + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant;127.0.0.1:" + clientPorts[joinerId];
    // Find leader id.
    int leaderId = -1;
    for (int i = 0; i < SERVER_COUNT; i++) {
        if (mt[i].main.quorumPeer.leader != null) {
            leaderId = i;
            break;
        }
    }
    assertFalse(leaderId == -1);
    // Joiner initial config consists of itself and the leader.
    sb = new StringBuilder();
    sb.append(serverConfig[leaderId] + "\n").append(serverConfig[joinerId] + "\n");
    /**
     * This server will delay the response to a NEWLEADER message, and run
     * reconfig command so that message at this processed in bellow order
     *
     * <pre>
     * NEWLEADER
     * reconfig's PROPOSAL
     * reconfig's COMMITANDACTIVATE
     * UPTODATE
     * </pre>
     */
    mt[joinerId] = new MainThread(joinerId, clientPorts[joinerId], sb.toString(), false) {

        @Override
        public TestQPMain getTestQPMain() {
            return new MockTestQPMain();
        }
    };
    mt[joinerId].start();
    CustomQuorumPeer qp = getCustomQuorumPeer(mt[joinerId]);
    // delete any already existing .next file
    String nextDynamicConfigFilename = qp.getNextDynamicConfigFilename();
    File nextDynaFile = new File(nextDynamicConfigFilename);
    nextDynaFile.delete();
    // Leader.NEWLEADER
    while (true) {
        if (qp.isNewLeaderMessage()) {
            preReconfigClient.reconfigure(serverConfig[joinerId], null, null, -1, null, null);
            break;
        } else {
            // sleep for 10 millisecond and then again check
            Thread.sleep(10);
        }
    }
    watch = new CountdownWatcher();
    ZooKeeper postReconfigClient = new ZooKeeper("127.0.0.1:" + clientPorts[joinerId], ClientBase.CONNECTION_TIMEOUT, watch);
    watch.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
    // do one successful operation on the newly added node
    postReconfigClient.create("/reconfigIssue", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    assertFalse("zoo.cfg.dynamic.next is not deleted.", nextDynaFile.exists());
    // verify that joiner has up-to-date config, including all four servers.
    for (long j = 0; j <= SERVER_COUNT; j++) {
        assertNotNull("server " + j + " is not present in the new quorum", qp.getQuorumVerifier().getVotingMembers().get(j));
    }
    // close clients
    preReconfigClient.close();
    postReconfigClient.close();
}
Also used : CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin) ZooKeeper(org.apache.zookeeper.ZooKeeper) File(java.io.File) Test(org.junit.Test)

Example 19 with CountdownWatcher

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

the class ZKAuditLoggerPerformance method main.

public static void main(String[] args) {
    if (args.length != 3) {
        System.err.println("USAGE: ZKAuditLoggerPerformance connectionString parentPath numberOfRecords");
        System.exit(1);
    }
    String cxnString = args[0];
    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zkClient = null;
    try {
        zkClient = new ZooKeeper(cxnString, 60000, watcher);
        watcher.waitForConnected(30000);
    } catch (InterruptedException | TimeoutException | IOException e) {
        String msg = "ZooKeeper client can not connect to " + cxnString;
        logErrorAndExit(e, msg);
    }
    String parentPath = args[1];
    try {
        Stat exists = zkClient.exists(parentPath, false);
        if (exists == null) {
            System.err.println("Parent path '" + parentPath + "' must exist.");
            System.exit(1);
        }
    } catch (KeeperException | InterruptedException e1) {
        String msg = "Error while checking the existence of parent path";
        logErrorAndExit(e1, msg);
    }
    int recordCount = 0;
    try {
        recordCount = Integer.parseInt(args[2]);
    } catch (NumberFormatException e) {
        String msg = "Failed to parse '" + args[2] + "' to integer";
        LOG.error(msg, e);
        System.err.println(msg);
        System.exit(1);
    }
    ZKAuditLoggerPerformance auditLoggingPerf = new ZKAuditLoggerPerformance(zkClient, parentPath, recordCount);
    AuditLogPerfReading doOperations = null;
    try {
        doOperations = auditLoggingPerf.doOperations();
    } catch (Exception e) {
        String msg = "Error while doing operations.";
        LOG.error(msg, e);
        System.err.println(msg);
        System.exit(1);
    }
    System.out.println("Time taken for " + recordCount + " operations are:");
    System.out.println(doOperations.report());
    System.exit(0);
}
Also used : CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) IOException(java.io.IOException) KeeperException(org.apache.zookeeper.KeeperException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) KeeperException(org.apache.zookeeper.KeeperException) TimeoutException(java.util.concurrent.TimeoutException)

Example 20 with CountdownWatcher

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

the class FollowerResyncConcurrencyTest method testLaggingFollowerResyncsUnderNewEpoch.

/**
 * See ZOOKEEPER-1319 - verify that a lagging follwer resyncs correctly
 *
 * 1) start with down quorum
 * 2) start leader/follower1, add some data
 * 3) restart leader/follower1
 * 4) start follower2
 * 5) verify data consistency across the ensemble
 *
 * @throws Exception
 */
@Test
public void testLaggingFollowerResyncsUnderNewEpoch() throws Exception {
    CountdownWatcher watcher1 = new CountdownWatcher();
    CountdownWatcher watcher2 = new CountdownWatcher();
    CountdownWatcher watcher3 = new CountdownWatcher();
    QuorumUtil qu = new QuorumUtil(1);
    qu.shutdownAll();
    qu.start(1);
    qu.start(2);
    assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + qu.getPeer(1).clientPort, ClientBase.CONNECTION_TIMEOUT), "Waiting for server up");
    assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + qu.getPeer(2).clientPort, ClientBase.CONNECTION_TIMEOUT), "Waiting for server up");
    ZooKeeper zk1 = createClient(qu.getPeer(1).peer.getClientPort(), watcher1);
    LOG.info("zk1 has session id 0x{}", Long.toHexString(zk1.getSessionId()));
    final String resyncPath = "/resyncundernewepoch";
    zk1.create(resyncPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk1.close();
    qu.shutdown(1);
    qu.shutdown(2);
    assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + qu.getPeer(1).clientPort, ClientBase.CONNECTION_TIMEOUT), "Waiting for server down");
    assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + qu.getPeer(2).clientPort, ClientBase.CONNECTION_TIMEOUT), "Waiting for server down");
    qu.start(1);
    qu.start(2);
    assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + qu.getPeer(1).clientPort, ClientBase.CONNECTION_TIMEOUT), "Waiting for server up");
    assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + qu.getPeer(2).clientPort, ClientBase.CONNECTION_TIMEOUT), "Waiting for server up");
    qu.start(3);
    assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + qu.getPeer(3).clientPort, ClientBase.CONNECTION_TIMEOUT), "Waiting for server up");
    zk1 = createClient(qu.getPeer(1).peer.getClientPort(), watcher1);
    LOG.info("zk1 has session id 0x{}", Long.toHexString(zk1.getSessionId()));
    assertNotNull(zk1.exists(resyncPath, false), "zk1 has data");
    final ZooKeeper zk2 = createClient(qu.getPeer(2).peer.getClientPort(), watcher2);
    LOG.info("zk2 has session id 0x{}", Long.toHexString(zk2.getSessionId()));
    assertNotNull(zk2.exists(resyncPath, false), "zk2 has data");
    final ZooKeeper zk3 = createClient(qu.getPeer(3).peer.getClientPort(), watcher3);
    LOG.info("zk3 has session id 0x{}", Long.toHexString(zk3.getSessionId()));
    assertNotNull(zk3.exists(resyncPath, false), "zk3 has data");
    zk1.close();
    zk2.close();
    zk3.close();
    qu.shutdownAll();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) 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