Search in sources :

Example 6 with ServerCnxnFactory

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

the class ACLTest method testAcls.

/**
     * Verify that acl optimization of storing just
     * a few acls and there references in the data
     * node is actually working.
     */
@Test
public void testAcls() throws Exception {
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    SyncRequestProcessor.setSnapCount(1000);
    final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
    f.startup(zks);
    ZooKeeper zk;
    String path;
    try {
        LOG.info("starting up the zookeeper server .. waiting");
        Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
        zk = ClientBase.createZKClient(HOSTPORT);
        LOG.info("starting creating acls");
        for (int i = 0; i < 100; i++) {
            path = "/" + i;
            zk.create(path, path.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        int size = zks.getZKDatabase().getAclSize();
        Assert.assertTrue("size of the acl map ", (2 == zks.getZKDatabase().getAclSize()));
        for (int j = 100; j < 200; j++) {
            path = "/" + j;
            ACL acl = new ACL();
            acl.setPerms(0);
            Id id = new Id();
            id.setId("1.1.1." + j);
            id.setScheme("ip");
            acl.setId(id);
            ArrayList<ACL> list = new ArrayList<ACL>();
            list.add(acl);
            zk.create(path, path.getBytes(), list, CreateMode.PERSISTENT);
        }
        Assert.assertTrue("size of the acl map ", (102 == zks.getZKDatabase().getAclSize()));
    } finally {
        // now shutdown the server and restart it
        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);
    f.startup(zks);
    try {
        Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
        zk = ClientBase.createZKClient(HOSTPORT);
        Assert.assertTrue("acl map ", (102 == zks.getZKDatabase().getAclSize()));
        for (int j = 200; j < 205; j++) {
            path = "/" + j;
            ACL acl = new ACL();
            acl.setPerms(0);
            Id id = new Id();
            id.setId("1.1.1." + j);
            id.setScheme("ip");
            acl.setId(id);
            ArrayList<ACL> list = new ArrayList<ACL>();
            list.add(acl);
            zk.create(path, path.getBytes(), list, CreateMode.PERSISTENT);
        }
        Assert.assertTrue("acl map ", (107 == zks.getZKDatabase().getAclSize()));
        zk.close();
    } finally {
        f.shutdown();
        zks.shutdown();
        Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT));
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ArrayList(java.util.ArrayList) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) ACL(org.apache.zookeeper.data.ACL) Id(org.apache.zookeeper.data.Id) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.Test)

Example 7 with ServerCnxnFactory

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

the class LoadFromLogTest method testRestoreWithTransactionErrors.

/**
     * Test we can restore a snapshot that has errors and data ahead of the zxid
     * of the snapshot file.
     */
@Test
public void testRestoreWithTransactionErrors() throws Exception {
    final String hostPort = HOST + PortAssignment.unique();
    // setup a single server cluster
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    SyncRequestProcessor.setSnapCount(10000);
    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 = getConnectedZkClient(hostPort);
    // generate some transactions
    try {
        for (int i = 0; i < NUM_MESSAGES; i++) {
            try {
                zk.create("/invaliddir/test-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
            } catch (NoNodeException e) {
            //Expected
            }
        }
    } finally {
        zk.close();
    }
    // force the zxid to be behind the content
    zks.getZKDatabase().setlastProcessedZxid(zks.getZKDatabase().getDataTreeLastProcessedZxid() - 10);
    LOG.info("Set lastProcessedZxid to " + zks.getZKDatabase().getDataTreeLastProcessedZxid());
    // Force snapshot and restore
    zks.takeSnapshot();
    zks.shutdown();
    f.shutdown();
    zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    SyncRequestProcessor.setSnapCount(10000);
    f = ServerCnxnFactory.createFactory(PORT, -1);
    f.startup(zks);
    Assert.assertTrue("waiting for server being up ", ClientBase.waitForServerUp(hostPort, CONNECTION_TIMEOUT));
    f.shutdown();
    zks.shutdown();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.Test)

Example 8 with ServerCnxnFactory

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

the class LoadFromLogTest method testLoad.

/**
     * test that all transactions from the Log are loaded, and only once
     * @throws Exception an exception might be thrown here
     */
@Test
public void testLoad() throws Exception {
    final String hostPort = HOST + PortAssignment.unique();
    // setup a single server cluster
    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);
    // generate some transactions that will get logged
    try {
        for (int i = 0; i < NUM_MESSAGES; i++) {
            zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
    } finally {
        zk.close();
    }
    f.shutdown();
    Assert.assertTrue("waiting for server to shutdown", ClientBase.waitForServerDown(hostPort, CONNECTION_TIMEOUT));
    // now verify that the FileTxnLog reads every transaction only once
    File logDir = new File(tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION);
    FileTxnLog txnLog = new FileTxnLog(logDir);
    TxnIterator itr = txnLog.read(0);
    // Check that storage space return some value
    FileTxnIterator fileItr = (FileTxnIterator) itr;
    long storageSize = fileItr.getStorageSize();
    LOG.info("Txnlog size: " + storageSize + " bytes");
    Assert.assertTrue("Storage size is greater than zero ", (storageSize > 0));
    long expectedZxid = 0;
    long lastZxid = 0;
    TxnHeader hdr;
    do {
        hdr = itr.getHeader();
        expectedZxid++;
        Assert.assertTrue("not the same transaction. lastZxid=" + lastZxid + ", zxid=" + hdr.getZxid(), lastZxid != hdr.getZxid());
        Assert.assertTrue("excepting next transaction. expected=" + expectedZxid + ", retreived=" + hdr.getZxid(), (hdr.getZxid() == expectedZxid));
        lastZxid = hdr.getZxid();
    } while (itr.next());
    Assert.assertTrue("processed all transactions. " + expectedZxid + " == " + TOTAL_TRANSACTIONS, (expectedZxid == TOTAL_TRANSACTIONS));
    zks.shutdown();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) FileTxnIterator(org.apache.zookeeper.server.persistence.FileTxnLog.FileTxnIterator) FileTxnLog(org.apache.zookeeper.server.persistence.FileTxnLog) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) TxnIterator(org.apache.zookeeper.server.persistence.TxnLog.TxnIterator) FileTxnIterator(org.apache.zookeeper.server.persistence.FileTxnLog.FileTxnIterator) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.Test)

Example 9 with ServerCnxnFactory

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

the class LoadFromLogTest method testDatadirAutocreate.

/**
     * Verify snap/log dir create with/without autocreate enabled.
     */
@Test
public void testDatadirAutocreate() throws Exception {
    ClientBase.setupTestEnv();
    final String hostPort = HOST + PortAssignment.unique();
    // first verify the default (autocreate on) works
    File tmpDir = ClientBase.createTmpDir();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    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));
    zks.shutdown();
    f.shutdown();
    Assert.assertTrue("waiting for server being down ", ClientBase.waitForServerDown(hostPort, CONNECTION_TIMEOUT));
    try {
        // now verify autocreate off works
        System.setProperty(FileTxnSnapLog.ZOOKEEPER_DATADIR_AUTOCREATE, "false");
        tmpDir = ClientBase.createTmpDir();
        zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
        f = ServerCnxnFactory.createFactory(PORT, -1);
        f.startup(zks);
        Assert.assertTrue("waiting for server being up ", ClientBase.waitForServerUp(hostPort, CONNECTION_TIMEOUT));
        Assert.fail("Server should not have started without datadir");
    } catch (IOException e) {
        LOG.info("Server failed to start - correct behavior " + e);
    } finally {
        System.setProperty(FileTxnSnapLog.ZOOKEEPER_DATADIR_AUTOCREATE, FileTxnSnapLog.ZOOKEEPER_DATADIR_AUTOCREATE_DEFAULT);
    }
}
Also used : ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) IOException(java.io.IOException) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.Test)

Example 10 with ServerCnxnFactory

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

the class LoadFromLogTest method testLoadFailure.

/**
     * test that we fail to load txnlog of a request zxid that is older
     * than what exist on disk
     * @throws Exception an exception might be thrown here
     */
@Test
public void testLoadFailure() throws Exception {
    final String hostPort = HOST + PortAssignment.unique();
    // setup a single server cluster
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    // So we have at least 4 logs
    SyncRequestProcessor.setSnapCount(50);
    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);
    // generate some transactions that will get logged
    try {
        for (int i = 0; i < NUM_MESSAGES; i++) {
            zk.create("/data-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
        }
    } finally {
        zk.close();
    }
    f.shutdown();
    Assert.assertTrue("waiting for server to shutdown", ClientBase.waitForServerDown(hostPort, CONNECTION_TIMEOUT));
    File logDir = new File(tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION);
    File[] logFiles = FileTxnLog.getLogFiles(logDir.listFiles(), 0);
    // Verify that we have at least 4 txnlog
    Assert.assertTrue(logFiles.length > 4);
    // Delete the first log file, so we will fail to read it back from disk
    Assert.assertTrue("delete the first log file", logFiles[0].delete());
    // Find zxid for the second log
    long secondStartZxid = Util.getZxidFromName(logFiles[1].getName(), "log");
    FileTxnLog txnLog = new FileTxnLog(logDir);
    TxnIterator itr = txnLog.read(1, false);
    // Oldest log is already remove, so this should point to the start of
    // of zxid on the second log
    Assert.assertEquals(secondStartZxid, itr.getHeader().getZxid());
    itr = txnLog.read(secondStartZxid, false);
    Assert.assertEquals(secondStartZxid, itr.getHeader().getZxid());
    Assert.assertTrue(itr.next());
    // Trying to get a second txn on second txnlog give us the
    // the start of second log, since the first one is removed
    long nextZxid = itr.getHeader().getZxid();
    itr = txnLog.read(nextZxid, false);
    Assert.assertEquals(secondStartZxid, itr.getHeader().getZxid());
    // Trying to get a first txn on the third give us the
    // the start of second log, since the first one is removed
    long thirdStartZxid = Util.getZxidFromName(logFiles[2].getName(), "log");
    itr = txnLog.read(thirdStartZxid, false);
    Assert.assertEquals(secondStartZxid, itr.getHeader().getZxid());
    Assert.assertTrue(itr.next());
    nextZxid = itr.getHeader().getZxid();
    itr = txnLog.read(nextZxid, false);
    Assert.assertEquals(secondStartZxid, itr.getHeader().getZxid());
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) FileTxnLog(org.apache.zookeeper.server.persistence.FileTxnLog) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) TxnIterator(org.apache.zookeeper.server.persistence.TxnLog.TxnIterator) FileTxnIterator(org.apache.zookeeper.server.persistence.FileTxnLog.FileTxnIterator) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.Test)

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