Search in sources :

Example 21 with CountdownWatcher

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

the class FollowerResyncConcurrencyTest method followerResyncCrashTest.

public void followerResyncCrashTest(boolean useTxnLogResync) throws Throwable {
    final Semaphore sem = new Semaphore(0);
    QuorumUtil qu = new QuorumUtil(1);
    qu.startAll();
    CountdownWatcher watcher1 = new CountdownWatcher();
    CountdownWatcher watcher2 = new CountdownWatcher();
    CountdownWatcher watcher3 = new CountdownWatcher();
    int index = 1;
    while (qu.getPeer(index).peer.leader == null) {
        index++;
    }
    Leader leader = qu.getPeer(index).peer.leader;
    assertNotNull(leader);
    if (useTxnLogResync) {
        // Set the factor to high value so that this test case always
        // resync using txnlog
        qu.getPeer(index).peer.getActiveServer().getZKDatabase().setSnapshotSizeFactor(1000);
    } else {
        // Disable sending DIFF using txnlog, so that this test still
        // testing the ZOOKEEPER-962 bug
        qu.getPeer(index).peer.getActiveServer().getZKDatabase().setSnapshotSizeFactor(-1);
    }
    /* Reusing the index variable to select a follower to connect to */
    index = (index == 1) ? 2 : 1;
    LOG.info("Connecting to follower: {}", index);
    qu.shutdown(index);
    final ZooKeeper zk3 = createClient(qu.getPeer(3).peer.getClientPort(), watcher3);
    LOG.info("zk3 has session id 0x{}", Long.toHexString(zk3.getSessionId()));
    zk3.create("/mybar", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
    qu.restart(index);
    final ZooKeeper zk1 = createClient(qu.getPeer(index).peer.getClientPort(), watcher1);
    LOG.info("zk1 has session id 0x{}", Long.toHexString(zk1.getSessionId()));
    final ZooKeeper zk2 = createClient(qu.getPeer(index).peer.getClientPort(), watcher2);
    LOG.info("zk2 has session id 0x{}", Long.toHexString(zk2.getSessionId()));
    zk1.create("/first", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // Prepare a thread that will create znodes.
    Thread mytestfooThread = new Thread(new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i < 3000; i++) {
                // Here we create 3000 znodes
                zk3.create("/mytestfoo", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL, (rc, path, ctx, name) -> {
                    pending.decrementAndGet();
                    counter.incrementAndGet();
                    if (rc != 0) {
                        errors.incrementAndGet();
                    }
                    if (counter.get() == 16200) {
                        sem.release();
                    }
                }, null);
                pending.incrementAndGet();
                if (i % 10 == 0) {
                    try {
                        Thread.sleep(100);
                    } catch (Exception e) {
                    }
                }
            }
        }
    });
    // initial data is written.
    for (int i = 0; i < 13000; i++) {
        // Here we create 13000 znodes
        zk3.create("/mybar", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL, (rc, path, ctx, name) -> {
            pending.decrementAndGet();
            counter.incrementAndGet();
            if (rc != 0) {
                errors.incrementAndGet();
            }
            if (counter.get() == 16200) {
                sem.release();
            }
        }, null);
        pending.incrementAndGet();
        if (i == 5000) {
            qu.shutdown(index);
            LOG.info("Shutting down s1");
        }
        if (i == 12000) {
            // Start the prepared thread so that it is writing znodes while
            // the follower is restarting. On the first restart, the follow
            // should use txnlog to catchup. For subsequent restart, the
            // follower should use a diff to catchup.
            mytestfooThread.start();
            LOG.info("Restarting follower: {}", index);
            qu.restart(index);
            Thread.sleep(300);
            LOG.info("Shutdown follower: {}", index);
            qu.shutdown(index);
            Thread.sleep(300);
            LOG.info("Restarting follower: {}", index);
            qu.restart(index);
            LOG.info("Setting up server: {}", index);
        }
        if ((i % 1000) == 0) {
            Thread.sleep(1000);
        }
        if (i % 50 == 0) {
            zk2.create("/newbaz", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL, (rc, path, ctx, name) -> {
                pending.decrementAndGet();
                counter.incrementAndGet();
                if (rc != 0) {
                    errors.incrementAndGet();
                }
                if (counter.get() == 16200) {
                    sem.release();
                }
            }, null);
            pending.incrementAndGet();
        }
    }
    // Wait until all updates return
    if (!sem.tryAcquire(ClientBase.CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) {
        LOG.warn("Did not aquire semaphore fast enough");
    }
    mytestfooThread.join(ClientBase.CONNECTION_TIMEOUT);
    if (mytestfooThread.isAlive()) {
        LOG.error("mytestfooThread is still alive");
    }
    assertTrue(waitForPendingRequests(60));
    assertTrue(waitForSync(qu, index, 10));
    verifyState(qu, index, leader);
    zk1.close();
    zk2.close();
    zk3.close();
    qu.shutdownAll();
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) CreateMode(org.apache.zookeeper.CreateMode) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) Ids(org.apache.zookeeper.ZooDefs.Ids) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ZooKeeper(org.apache.zookeeper.ZooKeeper) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) Semaphore(java.util.concurrent.Semaphore) Collection(java.util.Collection) Set(java.util.Set) ZKTestCase(org.apache.zookeeper.ZKTestCase) IOException(java.io.IOException) WatchedEvent(org.apache.zookeeper.WatchedEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Leader(org.apache.zookeeper.server.quorum.Leader) AfterEach(org.junit.jupiter.api.AfterEach) TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) ZooDefs(org.apache.zookeeper.ZooDefs) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) ZooKeeper(org.apache.zookeeper.ZooKeeper) TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) Leader(org.apache.zookeeper.server.quorum.Leader) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) Semaphore(java.util.concurrent.Semaphore) TimeoutException(java.util.concurrent.TimeoutException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 22 with CountdownWatcher

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

the class DuplicateLocalSessionUpgradeTest method testLocalSessionUpgrade.

private void testLocalSessionUpgrade(boolean testLeader) throws Exception {
    int leaderIdx = qb.getLeaderIndex();
    assertFalse(leaderIdx == -1, "No leader in quorum?");
    int followerIdx = (leaderIdx + 1) % 5;
    int testPeerIdx = testLeader ? leaderIdx : followerIdx;
    String[] hostPorts = qb.hostPort.split(",");
    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = qb.createClient(watcher, hostPorts[testPeerIdx], CONNECTION_TIMEOUT);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    final String firstPath = "/first";
    final String secondPath = "/ephemeral";
    // Just create some node so that we know the current zxid
    zk.create(firstPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // Now, try an ephemeral node. This will trigger session upgrade
    // so there will be createSession request inject into the pipeline
    // prior to this request
    zk.create(secondPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    Stat firstStat = zk.exists(firstPath, null);
    assertNotNull(firstStat);
    Stat secondStat = zk.exists(secondPath, null);
    assertNotNull(secondStat);
    long zxidDiff = secondStat.getCzxid() - firstStat.getCzxid();
    // If there is only one createSession request in between, zxid diff
    // will be exactly 2. The alternative way of checking is to actually
    // read txnlog but this should be sufficient
    assertEquals(2L, zxidDiff);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher)

Example 23 with CountdownWatcher

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

the class QuorumDigestAuthTest method testValidCredentials.

/**
 * Test to verify that server is able to start with valid credentials
 */
@Test
@Timeout(value = 30)
public void testValidCredentials() throws Exception {
    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");
    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 24 with CountdownWatcher

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

the class QuorumDigestAuthTest method testValidCredentialsWithMultiAddresses.

/**
 * Test to verify that server is able to start with valid credentials
 * when using multiple Quorum / Election addresses
 */
@Test
@Timeout(value = 30)
public void testValidCredentialsWithMultiAddresses() throws Exception {
    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");
    String connectStr = startMultiAddressQuorum(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 25 with CountdownWatcher

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

the class QuorumKerberosHostBasedAuthTest method testConnectBadServer.

/**
 * Test to verify that the bad server connection to the quorum should be rejected.
 */
@Test
@Timeout(value = 120)
public void testConnectBadServer() 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();
    String quorumCfgSection = mt.get(0).getQuorumCfgSection();
    StringBuilder sb = new StringBuilder();
    sb.append(quorumCfgSection);
    int myid = mt.size() + 1;
    final int clientPort = PortAssignment.unique();
    String server = String.format("server.%d=localhost:%d:%d:participant", myid, PortAssignment.unique(), PortAssignment.unique());
    sb.append(server + "\n");
    quorumCfgSection = sb.toString();
    authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_LOGIN_CONTEXT, "QuorumLearnerMyHost");
    MainThread badServer = new MainThread(myid, clientPort, quorumCfgSection, authConfigs);
    badServer.start();
    watcher = new CountdownWatcher();
    connectStr = "127.0.0.1:" + clientPort;
    zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher);
    try {
        watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT / 3);
        fail("Must throw exception as the myHost is not an authorized one!");
    } catch (TimeoutException e) {
    // expected
    } finally {
        zk.close();
        badServer.shutdown();
        badServer.deleteBaseDir();
    }
}
Also used : MainThread(org.apache.zookeeper.server.quorum.QuorumPeerTestBase.MainThread) ZooKeeper(org.apache.zookeeper.ZooKeeper) HashMap(java.util.HashMap) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

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