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;
}
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();
}
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();
}
Aggregations