Search in sources :

Example 21 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 22 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)

Example 23 with ServerCnxnFactory

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

the class RestoreCommittedLogTest method testRestoreCommittedLog.

/**
     * test the purge
     * @throws Exception an exception might be thrown here
     */
@Test
public void testRestoreCommittedLog() throws Exception {
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    SyncRequestProcessor.setSnapCount(100);
    final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
    f.startup(zks);
    Assert.assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
    ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
    try {
        for (int i = 0; i < 2000; i++) {
            zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
    } finally {
        zk.close();
    }
    f.shutdown();
    zks.shutdown();
    Assert.assertTrue("waiting for server to shutdown", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
    // start server again
    zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    zks.startdata();
    List<Proposal> committedLog = zks.getZKDatabase().getCommittedLog();
    int logsize = committedLog.size();
    LOG.info("committedLog size = {}", logsize);
    Assert.assertTrue("log size != 0", (logsize != 0));
    zks.shutdown();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Proposal(org.apache.zookeeper.server.quorum.Leader.Proposal) Test(org.junit.Test)

Example 24 with ServerCnxnFactory

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

the class QuorumPeerMain method runFromConfig.

public void runFromConfig(QuorumPeerConfig config) throws IOException, AdminServerException {
    try {
        ManagedUtil.registerLog4jMBeans();
    } catch (JMException e) {
        LOG.warn("Unable to register log4j JMX control", e);
    }
    LOG.info("Starting quorum peer");
    try {
        ServerCnxnFactory cnxnFactory = null;
        ServerCnxnFactory secureCnxnFactory = null;
        if (config.getClientPortAddress() != null) {
            cnxnFactory = ServerCnxnFactory.createFactory();
            cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns(), false);
        }
        if (config.getSecureClientPortAddress() != null) {
            secureCnxnFactory = ServerCnxnFactory.createFactory();
            secureCnxnFactory.configure(config.getSecureClientPortAddress(), config.getMaxClientCnxns(), true);
        }
        quorumPeer = getQuorumPeer();
        quorumPeer.setTxnFactory(new FileTxnSnapLog(config.getDataLogDir(), config.getDataDir()));
        quorumPeer.enableLocalSessions(config.areLocalSessionsEnabled());
        quorumPeer.enableLocalSessionsUpgrading(config.isLocalSessionsUpgradingEnabled());
        //quorumPeer.setQuorumPeers(config.getAllMembers());
        quorumPeer.setElectionType(config.getElectionAlg());
        quorumPeer.setMyid(config.getServerId());
        quorumPeer.setTickTime(config.getTickTime());
        quorumPeer.setMinSessionTimeout(config.getMinSessionTimeout());
        quorumPeer.setMaxSessionTimeout(config.getMaxSessionTimeout());
        quorumPeer.setInitLimit(config.getInitLimit());
        quorumPeer.setSyncLimit(config.getSyncLimit());
        quorumPeer.setConfigFileName(config.getConfigFilename());
        quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory()));
        quorumPeer.setQuorumVerifier(config.getQuorumVerifier(), false);
        if (config.getLastSeenQuorumVerifier() != null) {
            quorumPeer.setLastSeenQuorumVerifier(config.getLastSeenQuorumVerifier(), false);
        }
        quorumPeer.initConfigInZKDatabase();
        quorumPeer.setCnxnFactory(cnxnFactory);
        quorumPeer.setSecureCnxnFactory(secureCnxnFactory);
        quorumPeer.setLearnerType(config.getPeerType());
        quorumPeer.setSyncEnabled(config.getSyncEnabled());
        quorumPeer.setQuorumListenOnAllIPs(config.getQuorumListenOnAllIPs());
        quorumPeer.start();
        quorumPeer.join();
    } catch (InterruptedException e) {
        // warn, but generally this is ok
        LOG.warn("Quorum Peer interrupted", e);
    }
}
Also used : JMException(javax.management.JMException) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog)

Aggregations

ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)24 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)22 Test (org.junit.Test)22 ZooKeeper (org.apache.zookeeper.ZooKeeper)20 File (java.io.File)19 Stat (org.apache.zookeeper.data.Stat)7 ArrayList (java.util.ArrayList)5 IOException (java.io.IOException)4 InetSocketAddress (java.net.InetSocketAddress)3 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)3 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)3 InetAddress (java.net.InetAddress)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 KeeperException (org.apache.zookeeper.KeeperException)2 ACL (org.apache.zookeeper.data.ACL)2 FileTxnLog (org.apache.zookeeper.server.persistence.FileTxnLog)2 FileTxnIterator (org.apache.zookeeper.server.persistence.FileTxnLog.FileTxnIterator)2 TxnIterator (org.apache.zookeeper.server.persistence.TxnLog.TxnIterator)2 Proposal (org.apache.zookeeper.server.quorum.Leader.Proposal)2 TxnHeader (org.apache.zookeeper.txn.TxnHeader)2