Search in sources :

Example 16 with ServerCnxnFactory

use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.

the class StatCommandTest method setUp.

@Before
public void setUp() throws IOException {
    outputWriter = new StringWriter();
    ServerCnxn serverCnxnMock = mock(ServerCnxn.class);
    LeaderZooKeeperServer zks = mock(LeaderZooKeeperServer.class);
    when(zks.isRunning()).thenReturn(true);
    providerMock = mock(ServerStats.Provider.class);
    when(zks.serverStats()).thenReturn(new ServerStats(providerMock));
    ZKDatabase zkDatabaseMock = mock(ZKDatabase.class);
    when(zks.getZKDatabase()).thenReturn(zkDatabaseMock);
    Leader leaderMock = mock(Leader.class);
    when(leaderMock.getProposalStats()).thenReturn(new ProposalStats());
    when(zks.getLeader()).thenReturn(leaderMock);
    ServerCnxnFactory serverCnxnFactory = mock(ServerCnxnFactory.class);
    ServerCnxn serverCnxn = mock(ServerCnxn.class);
    List<ServerCnxn> connections = new ArrayList<>();
    connections.add(serverCnxn);
    when(serverCnxnFactory.getConnections()).thenReturn(connections);
    statCommand = new StatCommand(new PrintWriter(outputWriter), serverCnxnMock, FourLetterCommands.statCmd);
    statCommand.setZkServer(zks);
    statCommand.setFactory(serverCnxnFactory);
}
Also used : ArrayList(java.util.ArrayList) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) ServerCnxn(org.apache.zookeeper.server.ServerCnxn) StringWriter(java.io.StringWriter) ServerStats(org.apache.zookeeper.server.ServerStats) StatCommand(org.apache.zookeeper.server.command.StatCommand) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) PrintWriter(java.io.PrintWriter) Before(org.junit.Before)

Example 17 with ServerCnxnFactory

use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.

the class ACLTest method testNullACL.

@Test
public void testNullACL() throws Exception {
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
    f.startup(zks);
    ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
    try {
        // case 1 : null ACL with create
        try {
            zk.create("/foo", "foo".getBytes(), null, CreateMode.PERSISTENT);
            Assert.fail("Expected InvalidACLException for null ACL parameter");
        } catch (InvalidACLException e) {
        // Expected. Do nothing
        }
        // case 2 : null ACL with other create API
        try {
            zk.create("/foo", "foo".getBytes(), null, CreateMode.PERSISTENT, null);
            Assert.fail("Expected InvalidACLException for null ACL parameter");
        } catch (InvalidACLException e) {
        // Expected. Do nothing
        }
        // case 3 : null ACL with setACL
        try {
            zk.setACL("/foo", null, 0);
            Assert.fail("Expected InvalidACLException for null ACL parameter");
        } catch (InvalidACLException e) {
        // Expected. Do nothing
        }
    } finally {
        zk.close();
        f.shutdown();
        zks.shutdown();
        Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT));
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) InvalidACLException(org.apache.zookeeper.KeeperException.InvalidACLException) Test(org.junit.Test)

Example 18 with ServerCnxnFactory

use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.

the class ClientPortBindTest method testBindByAddress.

/**
 * Verify that the server binds to the specified address
 */
@Test
public void testBindByAddress() throws Exception {
    String bindAddress = null;
    Enumeration<NetworkInterface> intfs = NetworkInterface.getNetworkInterfaces();
    // if we have a loopback and it has an address use it
    while (intfs.hasMoreElements()) {
        NetworkInterface i = intfs.nextElement();
        try {
            if (i.isLoopback()) {
                Enumeration<InetAddress> addrs = i.getInetAddresses();
                while (addrs.hasMoreElements()) {
                    InetAddress a = addrs.nextElement();
                    if (a.isLoopbackAddress()) {
                        bindAddress = a.getHostAddress();
                        break;
                    }
                }
            }
        } catch (SocketException se) {
            LOG.warn("Couldn't find  loopback interface: " + se.getMessage());
        }
    }
    if (bindAddress == null) {
        LOG.warn("Unable to determine loop back address, skipping test");
        return;
    }
    final int PORT = PortAssignment.unique();
    LOG.info("Using " + bindAddress + " as the bind address");
    final String HOSTPORT = bindAddress + ":" + PORT;
    LOG.info("Using " + HOSTPORT + " as the host/port");
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(new InetSocketAddress(bindAddress, PORT), -1);
    f.startup(zks);
    LOG.info("starting up the the server, waiting");
    Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
    ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
    try {
        zk.close();
    } finally {
        f.shutdown();
        zks.shutdown();
        Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
    }
}
Also used : SocketException(java.net.SocketException) ZooKeeper(org.apache.zookeeper.ZooKeeper) InetSocketAddress(java.net.InetSocketAddress) NetworkInterface(java.net.NetworkInterface) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) InetAddress(java.net.InetAddress) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.Test)

Example 19 with ServerCnxnFactory

use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.

the class RecoveryTest method testRecovery.

/**
 * Verify that if a server goes down that clients will reconnect
 * automatically after the server is restarted. Note that this requires the
 * server to restart within the connection timeout period.
 *
 * Also note that the client latches are used to eliminate any chance
 * of spurrious connectionloss exceptions on the read ops. Specifically
 * a sync operation will throw this exception if the server goes down
 * (as recognized by the client) during the operation. If the operation
 * occurs after the server is down, but before the client recognizes
 * that the server is down (ping) then the op will throw connectionloss.
 */
@Test
public void testRecovery() throws Exception {
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    int oldSnapCount = SyncRequestProcessor.getSnapCount();
    SyncRequestProcessor.setSnapCount(1000);
    try {
        final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
        ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
        f.startup(zks);
        LOG.info("starting up the the server, waiting");
        Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
        startSignal = new CountDownLatch(1);
        ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this);
        startSignal.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
        Assert.assertTrue("count == 0", startSignal.getCount() == 0);
        String path;
        LOG.info("starting creating nodes");
        for (int i = 0; i < 10; i++) {
            path = "/" + i;
            zk.create(path, (path + "!").getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            for (int j = 0; j < 10; j++) {
                String subpath = path + "/" + j;
                zk.create(subpath, (subpath + "!").getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                for (int k = 0; k < 20; k++) {
                    String subsubpath = subpath + "/" + k;
                    zk.create(subsubpath, (subsubpath + "!").getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                }
            }
        }
        f.shutdown();
        zks.shutdown();
        Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
        zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
        f = ServerCnxnFactory.createFactory(PORT, -1);
        startSignal = new CountDownLatch(1);
        f.startup(zks);
        Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
        startSignal.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
        Assert.assertTrue("count == 0", startSignal.getCount() == 0);
        Stat stat = new Stat();
        for (int i = 0; i < 10; i++) {
            path = "/" + i;
            LOG.info("Checking " + path);
            Assert.assertEquals(new String(zk.getData(path, false, stat)), path + "!");
            for (int j = 0; j < 10; j++) {
                String subpath = path + "/" + j;
                Assert.assertEquals(new String(zk.getData(subpath, false, stat)), subpath + "!");
                for (int k = 0; k < 20; k++) {
                    String subsubpath = subpath + "/" + k;
                    Assert.assertEquals(new String(zk.getData(subsubpath, false, stat)), subsubpath + "!");
                }
            }
        }
        f.shutdown();
        zks.shutdown();
        Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT));
        zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
        f = ServerCnxnFactory.createFactory(PORT, -1);
        startSignal = new CountDownLatch(1);
        f.startup(zks);
        Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
        startSignal.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
        Assert.assertTrue("count == 0", startSignal.getCount() == 0);
        stat = new Stat();
        LOG.info("Check 2");
        for (int i = 0; i < 10; i++) {
            path = "/" + i;
            Assert.assertEquals(new String(zk.getData(path, false, stat)), path + "!");
            for (int j = 0; j < 10; j++) {
                String subpath = path + "/" + j;
                Assert.assertEquals(new String(zk.getData(subpath, false, stat)), subpath + "!");
                for (int k = 0; k < 20; k++) {
                    String subsubpath = subpath + "/" + k;
                    Assert.assertEquals(new String(zk.getData(subsubpath, false, stat)), subsubpath + "!");
                }
            }
        }
        zk.close();
        f.shutdown();
        zks.shutdown();
        Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
    } finally {
        SyncRequestProcessor.setSnapCount(oldSnapCount);
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.Test)

Example 20 with ServerCnxnFactory

use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.

the class RepeatStartupTest method testFail.

/**
 * bring up 5 quorum peers and then shut them down
 * and then bring one of the nodes as server
 *
 * @throws Exception might be thrown here
 */
@Test
public void testFail() throws Exception {
    QuorumBase qb = new QuorumBase();
    qb.setUp();
    System.out.println("Comment: the servers are at " + qb.hostPort);
    ZooKeeper zk = qb.createClient();
    zk.create("/test", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.close();
    QuorumBase.shutdown(qb.s1);
    QuorumBase.shutdown(qb.s2);
    QuorumBase.shutdown(qb.s3);
    QuorumBase.shutdown(qb.s4);
    QuorumBase.shutdown(qb.s5);
    String hp = qb.hostPort.split(",")[0];
    ZooKeeperServer zks = new ZooKeeperServer(qb.s1.getTxnFactory().getSnapDir(), qb.s1.getTxnFactory().getDataDir(), 3000);
    final int PORT = Integer.parseInt(hp.split(":")[1]);
    ServerCnxnFactory factory = ServerCnxnFactory.createFactory(PORT, -1);
    factory.startup(zks);
    System.out.println("Comment: starting factory");
    Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp("127.0.0.1:" + PORT, QuorumTest.CONNECTION_TIMEOUT));
    factory.shutdown();
    zks.shutdown();
    Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + PORT, QuorumTest.CONNECTION_TIMEOUT));
    System.out.println("Comment: shutting down standalone");
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.Test)

Aggregations

ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)23 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)20 Test (org.junit.Test)19 ZooKeeper (org.apache.zookeeper.ZooKeeper)17 File (java.io.File)16 ArrayList (java.util.ArrayList)7 Stat (org.apache.zookeeper.data.Stat)6 IOException (java.io.IOException)4 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)4 InetSocketAddress (java.net.InetSocketAddress)3 ACL (org.apache.zookeeper.data.ACL)3 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)3 PrintWriter (java.io.PrintWriter)2 InetAddress (java.net.InetAddress)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 KeeperException (org.apache.zookeeper.KeeperException)2 InvalidACLException (org.apache.zookeeper.KeeperException.InvalidACLException)2 Proposal (org.apache.zookeeper.server.quorum.Leader.Proposal)2 StringWriter (java.io.StringWriter)1 NetworkInterface (java.net.NetworkInterface)1