Search in sources :

Example 1 with CountdownWatcher

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

the class ReadOnlyModeTest method testConnectionEvents.

/**
 * Ensures that upon connection to a read-only server client receives
 * ConnectedReadOnly state notification.
 */
@Test
@Timeout(value = 90)
public void testConnectionEvents() throws Exception {
    qu.enableLocalSession(true);
    qu.startQuorum();
    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true);
    boolean success = false;
    for (int i = 0; i < 30; i++) {
        try {
            zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            success = true;
            break;
        } catch (KeeperException.ConnectionLossException e) {
            Thread.sleep(1000);
        }
    }
    assertTrue(success, "Did not succeed in connecting in 30s");
    assertFalse(watcher.readOnlyConnected, "The connection should not be read-only yet");
    // kill peer and wait no more than 5 seconds for read-only server
    // to be started (which should take one tickTime (2 seconds))
    qu.shutdown(2);
    // Re-connect the client (in case we were connected to the shut down
    // server and the local session was not persisted).
    zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true);
    long start = Time.currentElapsedTime();
    while (!(zk.getState() == States.CONNECTEDREADONLY)) {
        Thread.sleep(200);
        // TODO this was originally 5 seconds, but realistically, on random/slow/virt hosts, there is no way to guarantee this
        assertTrue(Time.currentElapsedTime() - start < 30000, "Can't connect to the server");
    }
    watcher.waitForReadOnlyConnected(5000);
    zk.close();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 2 with CountdownWatcher

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

the class ReadOnlyModeTest method testSessionEstablishment.

/**
 * Tests a situation when client firstly connects to a read-only server and
 * then connects to a majority server. Transition should be transparent for
 * the user.
 */
@Test
@Timeout(value = 90)
public void testSessionEstablishment() throws Exception {
    qu.enableLocalSession(true);
    qu.startQuorum();
    qu.shutdown(2);
    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    assertSame(States.CONNECTEDREADONLY, zk.getState(), "should be in r/o mode");
    long fakeId = zk.getSessionId();
    LOG.info("Connected as r/o mode with state {} and session id {}", zk.getState(), fakeId);
    watcher.reset();
    qu.start(2);
    assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT), "waiting for server up");
    LOG.info("Server 127.0.0.1:{} is up", qu.getPeer(2).clientPort);
    // ZOOKEEPER-2722: wait until we can connect to a read-write server after the quorum
    // is formed. Otherwise, it is possible that client first connects to a read-only server,
    // then drops the connection because of shutting down of the read-only server caused
    // by leader election / quorum forming between the read-only server and the newly started
    // server. If we happen to execute the zk.create after the read-only server is shutdown and
    // before the quorum is formed, we will get a ConnectLossException.
    watcher.waitForSyncConnected(CONNECTION_TIMEOUT);
    assertEquals(States.CONNECTED, zk.getState(), "Should be in read-write mode");
    LOG.info("Connected as rw mode with state {} and session id {}", zk.getState(), zk.getSessionId());
    zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    assertFalse(zk.getSessionId() == fakeId, "fake session and real session have same id");
    zk.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 3 with CountdownWatcher

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

the class ReadOnlyModeTest method testGlobalSessionInRO.

@Test
@Timeout(value = 90)
public void testGlobalSessionInRO() throws Exception {
    qu.startQuorum();
    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    LOG.info("global session created 0x{}", Long.toHexString(zk.getSessionId()));
    watcher.reset();
    qu.shutdown(2);
    try {
        watcher.waitForConnected(CONNECTION_TIMEOUT);
        fail("Should not be able to renew a global session");
    } catch (TimeoutException e) {
    }
    zk.close();
    watcher.reset();
    zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true);
    try {
        watcher.waitForConnected(CONNECTION_TIMEOUT);
        fail("Should not be able to create a global session");
    } catch (TimeoutException e) {
    }
    zk.close();
    qu.getPeer(1).peer.enableLocalSessions(true);
    zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true);
    try {
        watcher.waitForConnected(CONNECTION_TIMEOUT);
    } catch (TimeoutException e) {
        fail("Should be able to create a local session");
    }
    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) Timeout(org.junit.jupiter.api.Timeout)

Example 4 with CountdownWatcher

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

the class ReadOnlyModeTest method testSeekForRwServer.

/**
 * Ensures that client seeks for r/w servers while it's connected to r/o
 * server.
 */
@SuppressWarnings("deprecation")
@Test
@Timeout(value = 90)
public void testSeekForRwServer() throws Exception {
    qu.enableLocalSession(true);
    qu.startQuorum();
    try (LoggerTestTool loggerTestTool = new LoggerTestTool("org.apache.zookeeper")) {
        ByteArrayOutputStream os = loggerTestTool.getOutputStream();
        qu.shutdown(2);
        CountdownWatcher watcher = new CountdownWatcher();
        ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true);
        watcher.waitForConnected(CONNECTION_TIMEOUT);
        // if we don't suspend a peer it will rejoin a quorum
        qu.getPeer(1).peer.suspend();
        // start two servers to form a quorum; client should detect this and
        // connect to one of them
        watcher.reset();
        qu.start(2);
        qu.start(3);
        ClientBase.waitForServerUp(qu.getConnString(), 2000);
        watcher.waitForConnected(CONNECTION_TIMEOUT);
        zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        // resume poor fellow
        qu.getPeer(1).peer.resume();
        String log = os.toString();
        assertFalse(StringUtils.isEmpty(log), "OutputStream doesn't have any log messages");
        LineNumberReader r = new LineNumberReader(new StringReader(log));
        String line;
        Pattern p = Pattern.compile(".*Majority server found.*");
        boolean found = false;
        while ((line = r.readLine()) != null) {
            if (p.matcher(line).matches()) {
                found = true;
                break;
            }
        }
        assertTrue(found, "Majority server wasn't found while connected to r/o server");
    }
}
Also used : Pattern(java.util.regex.Pattern) ZooKeeper(org.apache.zookeeper.ZooKeeper) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LineNumberReader(java.io.LineNumberReader) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 5 with CountdownWatcher

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

the class StandaloneTest method testStandaloneReconfigFails.

/**
 * Verify that reconfiguration in standalone mode fails with
 * KeeperException.UnimplementedException.
 */
@Test
public void testStandaloneReconfigFails() throws Exception {
    ClientBase.setupTestEnv();
    final int CLIENT_PORT = PortAssignment.unique();
    final String HOSTPORT = "127.0.0.1:" + CLIENT_PORT;
    File tmpDir = ClientBase.createTmpDir();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(CLIENT_PORT, -1);
    f.startup(zks);
    assertTrue(ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server being up ");
    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, watcher);
    ZooKeeperAdmin zkAdmin = new ZooKeeperAdmin(HOSTPORT, CONNECTION_TIMEOUT, watcher);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    List<String> joiners = new ArrayList<String>();
    joiners.add("server.2=localhost:1234:1235;1236");
    // generate some transactions that will get logged
    try {
        zkAdmin.addAuthInfo("digest", "super:test".getBytes());
        zkAdmin.reconfigure(joiners, null, null, -1, new Stat());
        fail("Reconfiguration in standalone should trigger " + "UnimplementedException");
    } catch (KeeperException.UnimplementedException ex) {
    // expected
    }
    zk.close();
    zks.shutdown();
    f.shutdown();
    assertTrue(ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server being down ");
}
Also used : CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) ArrayList(java.util.ArrayList) ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) KeeperException(org.apache.zookeeper.KeeperException) 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