Search in sources :

Example 51 with ZooKeeperServer

use of org.apache.zookeeper.server.ZooKeeperServer 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 52 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);
    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);
    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 = ClientBase.getServer(factory);
        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 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);
    s2.start();
    QuorumPeer s3 = new QuorumPeer(peers, dataDir3, dataDir3, port3, 3, 3, tickTime, initLimit, syncLimit);
    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());
        Assert.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);
    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());
        Assert.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.Test)

Example 53 with ZooKeeperServer

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

the class ZooKeeperQuotaTest method testQuota.

@Test
public void testQuota() throws IOException, InterruptedException, KeeperException, Exception {
    final ZooKeeper zk = createClient();
    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);
    ZooKeeperMain.createQuota(zk, path, 5L, 10);
    // see if its set
    String absolutePath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode;
    byte[] data = zk.getData(absolutePath, false, new Stat());
    StatsTrack st = new StatsTrack(new String(data));
    Assert.assertTrue("bytes are set", st.getBytes() == 5L);
    Assert.assertTrue("num count is set", st.getCount() == 10);
    String statPath = Quotas.quotaZookeeper + path + "/" + Quotas.statNode;
    byte[] qdata = zk.getData(statPath, false, new Stat());
    StatsTrack qst = new StatsTrack(new String(qdata));
    Assert.assertTrue("bytes are set", qst.getBytes() == 8L);
    Assert.assertTrue("count is set", qst.getCount() == 2);
    // force server to restart and load from snapshot, not txn log
    stopServer();
    startServer();
    stopServer();
    startServer();
    ZooKeeperServer server = getServer(serverFactory);
    Assert.assertNotNull("Quota is still set", server.getZKDatabase().getDataTree().getMaxPrefixWithQuota(path) != null);
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) StatsTrack(org.apache.zookeeper.StatsTrack) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.Test)

Example 54 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 55 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)

Aggregations

ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)55 File (java.io.File)27 Test (org.junit.Test)24 ZooKeeper (org.apache.zookeeper.ZooKeeper)21 ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)20 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)19 InetSocketAddress (java.net.InetSocketAddress)18 IOException (java.io.IOException)17 Stat (org.apache.zookeeper.data.Stat)8 ArrayList (java.util.ArrayList)6 InterruptedIOException (java.io.InterruptedIOException)4 BindException (java.net.BindException)4 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)4 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)4 KeeperException (org.apache.zookeeper.KeeperException)3 ACL (org.apache.zookeeper.data.ACL)3 PrintWriter (java.io.PrintWriter)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 InvalidACLException (org.apache.zookeeper.KeeperException.InvalidACLException)2 Proposal (org.apache.zookeeper.server.quorum.Leader.Proposal)2