Search in sources :

Example 1 with PortForwarder

use of org.apache.zookeeper.server.util.PortForwarder in project zookeeper by apache.

the class ObserverMasterTestBase method setUp.

protected PortForwarder setUp(final int omProxyPort, final Boolean testObserverMaster) throws IOException {
    ClientBase.setupTestEnv();
    final int PORT_QP1 = PortAssignment.unique();
    final int PORT_QP2 = PortAssignment.unique();
    final int PORT_OBS = PortAssignment.unique();
    final int PORT_QP_LE1 = PortAssignment.unique();
    final int PORT_QP_LE2 = PortAssignment.unique();
    final int PORT_OBS_LE = PortAssignment.unique();
    CLIENT_PORT_QP1 = PortAssignment.unique();
    CLIENT_PORT_QP2 = PortAssignment.unique();
    CLIENT_PORT_OBS = PortAssignment.unique();
    OM_PORT = PortAssignment.unique();
    String quorumCfgSection = "server.1=127.0.0.1:" + (PORT_QP1) + ":" + (PORT_QP_LE1) + ";" + CLIENT_PORT_QP1 + "\nserver.2=127.0.0.1:" + (PORT_QP2) + ":" + (PORT_QP_LE2) + ";" + CLIENT_PORT_QP2 + "\nserver.3=127.0.0.1:" + (PORT_OBS) + ":" + (PORT_OBS_LE) + ":observer" + ";" + CLIENT_PORT_OBS;
    String extraCfgs = testObserverMaster ? String.format("observerMasterPort=%d%n", OM_PORT) : "";
    String extraCfgsObs = testObserverMaster ? String.format("observerMasterPort=%d%n", omProxyPort <= 0 ? OM_PORT : omProxyPort) : "";
    PortForwarder forwarder = null;
    if (testObserverMaster && omProxyPort >= 0) {
        forwarder = new PortForwarder(omProxyPort, OM_PORT);
    }
    q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection, extraCfgs);
    q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection, extraCfgs);
    q3 = new MainThread(3, CLIENT_PORT_OBS, quorumCfgSection, extraCfgsObs);
    q1.start();
    q2.start();
    assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT), "waiting for server 1 being up");
    assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT), "waiting for server 2 being up");
    return forwarder;
}
Also used : PortForwarder(org.apache.zookeeper.server.util.PortForwarder)

Example 2 with PortForwarder

use of org.apache.zookeeper.server.util.PortForwarder in project zookeeper by apache.

the class ObserverMasterTest method testRevalidation.

@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testRevalidation(boolean testObserverMaster) throws Exception {
    setUp(-1, testObserverMaster);
    q3.start();
    assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_OBS, CONNECTION_TIMEOUT), "waiting for server 3 being up");
    final int leaderProxyPort = PortAssignment.unique();
    final int obsProxyPort = PortAssignment.unique();
    int leaderPort = q1.getQuorumPeer().leader == null ? CLIENT_PORT_QP2 : CLIENT_PORT_QP1;
    PortForwarder leaderPF = new PortForwarder(leaderProxyPort, leaderPort);
    latch = new CountDownLatch(1);
    zk = new ZooKeeper(String.format("127.0.0.1:%d,127.0.0.1:%d", leaderProxyPort, obsProxyPort), ClientBase.CONNECTION_TIMEOUT, this);
    latch.await();
    zk.create("/revalidtest", "test".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    assertNotNull(zk.exists("/revalidtest", null), "Read-after write failed");
    latch = new CountDownLatch(2);
    PortForwarder obsPF = new PortForwarder(obsProxyPort, CLIENT_PORT_OBS);
    try {
        leaderPF.shutdown();
    } catch (Exception e) {
    // ignore?
    }
    latch.await();
    assertEquals(new String(zk.getData("/revalidtest", null, null)), "test");
    obsPF.shutdown();
    shutdown();
}
Also used : PortForwarder(org.apache.zookeeper.server.util.PortForwarder) ZooKeeper(org.apache.zookeeper.ZooKeeper) CountDownLatch(java.util.concurrent.CountDownLatch) AttributeNotFoundException(javax.management.AttributeNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) KeeperException(org.apache.zookeeper.KeeperException) RuntimeMBeanException(javax.management.RuntimeMBeanException) IOException(java.io.IOException) MalformedObjectNameException(javax.management.MalformedObjectNameException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with PortForwarder

use of org.apache.zookeeper.server.util.PortForwarder in project zookeeper by apache.

the class ObserverMasterTest method testObserver.

/**
 * This test ensures two things:
 * 1. That Observers can successfully proxy requests to the ensemble.
 * 2. That Observers don't participate in leader elections.
 * The second is tested by constructing an ensemble where a leader would
 * be elected if and only if an Observer voted.
 */
@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testObserver(boolean testObserverMaster) throws Exception {
    // We expect two notifications before we want to continue
    latch = new CountDownLatch(2);
    setUp(-1, testObserverMaster);
    q3.start();
    assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_OBS, CONNECTION_TIMEOUT), "waiting for server 3 being up");
    validateObserverSyncTimeMetrics();
    if (testObserverMaster) {
        int masterPort = q3.getQuorumPeer().observer.getSocket().getPort();
        LOG.info("port {} {}", masterPort, OM_PORT);
        assertEquals(masterPort, OM_PORT, "observer failed to connect to observer master");
    }
    zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT_OBS, ClientBase.CONNECTION_TIMEOUT, this);
    zk.create("/obstest", "test".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // Assert that commands are getting forwarded correctly
    assertEquals(new String(zk.getData("/obstest", null, null)), "test");
    // Now check that other commands don't blow everything up
    zk.sync("/", null, null);
    zk.setData("/obstest", "test2".getBytes(), -1);
    zk.getChildren("/", false);
    assertEquals(zk.getState(), States.CONNECTED);
    LOG.info("Shutting down server 2");
    // Now kill one of the other real servers
    q2.shutdown();
    assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT), "Waiting for server 2 to shut down");
    LOG.info("Server 2 down");
    // Now the resulting ensemble shouldn't be quorate
    latch.await();
    assertNotSame(KeeperState.SyncConnected, lastEvent.getState(), "Client is still connected to non-quorate cluster");
    LOG.info("Latch returned");
    try {
        assertNotEquals("Shouldn't get a response when cluster not quorate!", "test", new String(zk.getData("/obstest", null, null)));
    } catch (ConnectionLossException c) {
        LOG.info("Connection loss exception caught - ensemble not quorate (this is expected)");
    }
    latch = new CountDownLatch(1);
    LOG.info("Restarting server 2");
    // Bring it back
    // q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection, extraCfgs);
    q2.start();
    LOG.info("Waiting for server 2 to come up");
    assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT), "waiting for server 2 being up");
    LOG.info("Server 2 started, waiting for latch");
    latch.await();
    // It's possible our session expired - but this is ok, shows we
    // were able to talk to the ensemble
    assertTrue((KeeperState.SyncConnected == lastEvent.getState() || KeeperState.Expired == lastEvent.getState()), "Client didn't reconnect to quorate ensemble (state was" + lastEvent.getState() + ")");
    LOG.info("perform a revalidation test");
    int leaderProxyPort = PortAssignment.unique();
    int obsProxyPort = PortAssignment.unique();
    int leaderPort = q1.getQuorumPeer().leader == null ? CLIENT_PORT_QP2 : CLIENT_PORT_QP1;
    PortForwarder leaderPF = new PortForwarder(leaderProxyPort, leaderPort);
    latch = new CountDownLatch(1);
    ZooKeeper client = new ZooKeeper(String.format("127.0.0.1:%d,127.0.0.1:%d", leaderProxyPort, obsProxyPort), ClientBase.CONNECTION_TIMEOUT, this);
    latch.await();
    client.create("/revalidtest", "test".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    assertNotNull(client.exists("/revalidtest", null), "Read-after write failed");
    latch = new CountDownLatch(2);
    PortForwarder obsPF = new PortForwarder(obsProxyPort, CLIENT_PORT_OBS);
    try {
        leaderPF.shutdown();
    } catch (Exception e) {
    // ignore?
    }
    latch.await();
    assertEquals(new String(client.getData("/revalidtest", null, null)), "test");
    client.close();
    obsPF.shutdown();
    shutdown();
}
Also used : PortForwarder(org.apache.zookeeper.server.util.PortForwarder) ZooKeeper(org.apache.zookeeper.ZooKeeper) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) AttributeNotFoundException(javax.management.AttributeNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) KeeperException(org.apache.zookeeper.KeeperException) RuntimeMBeanException(javax.management.RuntimeMBeanException) IOException(java.io.IOException) MalformedObjectNameException(javax.management.MalformedObjectNameException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

PortForwarder (org.apache.zookeeper.server.util.PortForwarder)3 IOException (java.io.IOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AttributeNotFoundException (javax.management.AttributeNotFoundException)2 InstanceNotFoundException (javax.management.InstanceNotFoundException)2 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)2 MBeanException (javax.management.MBeanException)2 MalformedObjectNameException (javax.management.MalformedObjectNameException)2 ReflectionException (javax.management.ReflectionException)2 RuntimeMBeanException (javax.management.RuntimeMBeanException)2 KeeperException (org.apache.zookeeper.KeeperException)2 ConnectionLossException (org.apache.zookeeper.KeeperException.ConnectionLossException)2 ZooKeeper (org.apache.zookeeper.ZooKeeper)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ValueSource (org.junit.jupiter.params.provider.ValueSource)2