Search in sources :

Example 81 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project apex-malhar by apache.

the class HBaseTestHelper method startZooKeeperServer.

private static void startZooKeeperServer() throws IOException, InterruptedException {
    String zooLocation = System.getProperty("java.io.tmpdir");
    File zooFile = new File(zooLocation, "zookeeper-malhartest");
    ZooKeeperServer zooKeeper = new ZooKeeperServer(zooFile, zooFile, 2000);
    NIOServerCnxnFactory serverFactory = new NIOServerCnxnFactory();
    serverFactory.configure(new InetSocketAddress(2181), 10);
    serverFactory.startup(zooKeeper);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 82 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer in project apex-malhar by apache.

the class KafkaOperatorTestBase method stopZookeeper.

public static void stopZookeeper() {
    for (ZooKeeperServer zs : zkServer) {
        if (zs != null) {
            zs.shutdown();
        }
    }
    for (ServerCnxnFactory zkf : zkFactory) {
        if (zkf != null) {
            zkf.closeAll();
            zkf.shutdown();
        }
    }
    zkServer = new ZooKeeperServer[2];
    zkFactory = new ServerCnxnFactory[2];
}
Also used : NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 83 with ZooKeeperServer

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

the class TruncateTest method testTruncate.

@Test
public void testTruncate() throws Exception {
    // Prime the server that is going to come in late with 50 txns
    String hostPort = "127.0.0.1:" + PortAssignment.unique();
    int maxCnxns = 100;
    ServerCnxnFactory factory = ClientBase.createNewServerInstance(null, hostPort, maxCnxns);
    ClientBase.startServerInstance(dataDir1, factory, hostPort, 1);
    ClientBase.shutdownServerInstance(factory, hostPort);
    // standalone starts with 0 epoch while quorum starts with 1
    File origfile = new File(new File(dataDir1, "version-2"), "snapshot.0");
    File newfile = new File(new File(dataDir1, "version-2"), "snapshot.100000000");
    origfile.renameTo(newfile);
    factory = ClientBase.createNewServerInstance(null, hostPort, maxCnxns);
    ClientBase.startServerInstance(dataDir1, factory, hostPort, 1);
    ZooKeeper zk = ClientBase.createZKClient(hostPort, 15000);
    for (int i = 0; i < 50; i++) {
        zk.create("/" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    zk.close();
    ZKDatabase zkDb;
    {
        ZooKeeperServer zs = factory.getZooKeeperServer();
        zkDb = zs.getZKDatabase();
    }
    factory.shutdown();
    try {
        zkDb.close();
    } catch (IOException ie) {
        LOG.warn("Error closing logs ", ie);
    }
    int tickTime = 2000;
    int initLimit = 3;
    int syncLimit = 3;
    int connectToLearnerMasterLimit = 3;
    int port1 = PortAssignment.unique();
    int port2 = PortAssignment.unique();
    int port3 = PortAssignment.unique();
    // Start up two of the quorum and add 10 txns
    Map<Long, QuorumServer> peers = new HashMap<Long, QuorumServer>();
    peers.put(Long.valueOf(1), new QuorumServer(1, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port1)));
    peers.put(Long.valueOf(2), new QuorumServer(2, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port2)));
    peers.put(Long.valueOf(3), new QuorumServer(3, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port3)));
    QuorumPeer s2 = new QuorumPeer(peers, dataDir2, dataDir2, port2, 3, 2, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit);
    s2.start();
    QuorumPeer s3 = new QuorumPeer(peers, dataDir3, dataDir3, port3, 3, 3, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit);
    s3.start();
    zk = ClientBase.createZKClient("127.0.0.1:" + port2, 15000);
    for (int i = 0; i < 10; i++) {
        zk.create("/" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    zk.close();
    final ZooKeeper zk2 = ClientBase.createZKClient("127.0.0.1:" + port2, 15000);
    zk2.getData("/9", false, new Stat());
    try {
        zk2.getData("/10", false, new Stat());
        fail("Should have gotten an error");
    } catch (KeeperException.NoNodeException e) {
    // this is what we want
    }
    QuorumPeer s1 = new QuorumPeer(peers, dataDir1, dataDir1, port1, 3, 1, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit);
    s1.start();
    ZooKeeper zk1 = ClientBase.createZKClient("127.0.0.1:" + port1, 15000);
    zk1.getData("/9", false, new Stat());
    try {
        // /10 wont work because the session expiration
        // will match the zxid for /10 and so we wont
        // actually truncate the zxid for /10 creation
        // due to an artifact of switching the xid of the standalone
        // /11 is the last entry in the log for the xid
        // as a result /12 is the first of the truncated znodes to check for
        zk1.getData("/12", false, new Stat());
        fail("Should have gotten an error");
    } catch (KeeperException.NoNodeException e) {
    // this is what we want
    }
    zk1.close();
    QuorumBase.shutdown(s1);
    QuorumBase.shutdown(s2);
    QuorumBase.shutdown(s3);
}
Also used : HashMap(java.util.HashMap) QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) 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)

Example 84 with ZooKeeperServer

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

the class ZooKeeperQuotaTest method testQuota.

@Test
public void testQuota() throws Exception {
    final String path = "/a/b/v";
    // making sure setdata works on /
    zk.setData("/", "some".getBytes(), -1);
    zk.create("/a", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create("/a/b", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create("/a/b/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create("/a/b/v/d", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    StatsTrack quota = new StatsTrack();
    quota.setCount(4);
    quota.setCountHardLimit(4);
    quota.setBytes(9L);
    quota.setByteHardLimit(15L);
    SetQuotaCommand.createQuota(zk, path, quota);
    // see if its set
    String absolutePath = Quotas.limitPath(path);
    byte[] data = zk.getData(absolutePath, false, new Stat());
    StatsTrack st = new StatsTrack(data);
    assertTrue(st.getBytes() == 9L, "bytes are set");
    assertTrue(st.getByteHardLimit() == 15L, "byte hard limit is set");
    assertTrue(st.getCount() == 4, "num count is set");
    assertTrue(st.getCountHardLimit() == 4, "count hard limit is set");
    // check quota node readable by old servers
    OldStatsTrack ost = new OldStatsTrack(new String(data));
    assertTrue(ost.getBytes() == 9L, "bytes are set");
    assertTrue(ost.getCount() == 4, "num count is set");
    String statPath = Quotas.statPath(path);
    byte[] qdata = zk.getData(statPath, false, new Stat());
    StatsTrack qst = new StatsTrack(qdata);
    assertTrue(qst.getBytes() == 8L, "bytes are set");
    assertTrue(qst.getCount() == 2, "count is set");
    // force server to restart and load from snapshot, not txn log
    stopServer();
    startServer();
    stopServer();
    startServer();
    ZooKeeperServer server = serverFactory.getZooKeeperServer();
    assertNotNull(server.getZKDatabase().getDataTree().getMaxPrefixWithQuota(path) != null, "Quota is still set");
}
Also used : Stat(org.apache.zookeeper.data.Stat) OldStatsTrack(org.apache.zookeeper.test.StatsTrackTest.OldStatsTrack) StatsTrack(org.apache.zookeeper.StatsTrack) OldStatsTrack(org.apache.zookeeper.test.StatsTrackTest.OldStatsTrack) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.jupiter.api.Test)

Example 85 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer 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");
        assertTrue(ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server up");
        startSignal = new CountDownLatch(1);
        ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this);
        startSignal.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
        assertTrue(startSignal.getCount() == 0, "count == 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();
        assertTrue(ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server down");
        zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
        f = ServerCnxnFactory.createFactory(PORT, -1);
        startSignal = new CountDownLatch(1);
        f.startup(zks);
        assertTrue(ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server up");
        startSignal.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
        assertTrue(startSignal.getCount() == 0, "count == 0");
        Stat stat = new Stat();
        for (int i = 0; i < 10; i++) {
            path = "/" + i;
            LOG.info("Checking " + path);
            assertEquals(new String(zk.getData(path, false, stat)), path + "!");
            for (int j = 0; j < 10; j++) {
                String subpath = path + "/" + j;
                assertEquals(new String(zk.getData(subpath, false, stat)), subpath + "!");
                for (int k = 0; k < 20; k++) {
                    String subsubpath = subpath + "/" + k;
                    assertEquals(new String(zk.getData(subsubpath, false, stat)), subsubpath + "!");
                }
            }
        }
        f.shutdown();
        zks.shutdown();
        assertTrue(ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT), "waiting for server down");
        zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
        f = ServerCnxnFactory.createFactory(PORT, -1);
        startSignal = new CountDownLatch(1);
        f.startup(zks);
        assertTrue(ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server up");
        startSignal.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
        assertTrue(startSignal.getCount() == 0, "count == 0");
        stat = new Stat();
        LOG.info("Check 2");
        for (int i = 0; i < 10; i++) {
            path = "/" + i;
            assertEquals(new String(zk.getData(path, false, stat)), path + "!");
            for (int j = 0; j < 10; j++) {
                String subpath = path + "/" + j;
                assertEquals(new String(zk.getData(subpath, false, stat)), subpath + "!");
                for (int k = 0; k < 20; k++) {
                    String subsubpath = subpath + "/" + k;
                    assertEquals(new String(zk.getData(subsubpath, false, stat)), subsubpath + "!");
                }
            }
        }
        zk.close();
        f.shutdown();
        zks.shutdown();
        assertTrue(ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT), "waiting for server down");
    } 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.jupiter.api.Test)

Aggregations

ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)96 File (java.io.File)39 Test (org.junit.jupiter.api.Test)33 ZooKeeper (org.apache.zookeeper.ZooKeeper)31 InetSocketAddress (java.net.InetSocketAddress)28 IOException (java.io.IOException)27 ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)26 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)25 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)10 Stat (org.apache.zookeeper.data.Stat)9 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)8 ArrayList (java.util.ArrayList)6 ServerConfig (org.apache.zookeeper.server.ServerConfig)6 InterruptedIOException (java.io.InterruptedIOException)4 BindException (java.net.BindException)4 KeeperException (org.apache.zookeeper.KeeperException)4 Test (org.junit.Test)4 Field (java.lang.reflect.Field)3 ACL (org.apache.zookeeper.data.ACL)3 Proposal (org.apache.zookeeper.server.quorum.Leader.Proposal)3