Search in sources :

Example 21 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.

the class ACLCountTest method testAclCount.

/**
     *
     * Create a node and add 4 ACL values to it, but there are only 2 unique ACL values,
     * and each is repeated once:
     *
     *   ACL(ZooDefs.Perms.READ,ZooDefs.Ids.ANYONE_ID_UNSAFE);
     *   ACL(ZooDefs.Perms.ALL,ZooDefs.Ids.AUTH_IDS);
     *   ACL(ZooDefs.Perms.READ,ZooDefs.Ids.ANYONE_ID_UNSAFE);
     *   ACL(ZooDefs.Perms.ALL,ZooDefs.Ids.AUTH_IDS);
     *
     * Even though we've added 4 ACL values, there should only be 2 ACLs for that node,
     * since there are only 2 *unique* ACL values.
     */
@Test
public void testAclCount() 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;
    final ArrayList<ACL> CREATOR_ALL_AND_WORLD_READABLE = new ArrayList<ACL>() {

        {
            add(new ACL(ZooDefs.Perms.READ, ZooDefs.Ids.ANYONE_ID_UNSAFE));
            add(new ACL(ZooDefs.Perms.ALL, ZooDefs.Ids.AUTH_IDS));
            add(new ACL(ZooDefs.Perms.READ, ZooDefs.Ids.ANYONE_ID_UNSAFE));
            add(new ACL(ZooDefs.Perms.ALL, ZooDefs.Ids.AUTH_IDS));
        }
    };
    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);
        zk.addAuthInfo("digest", "pat:test".getBytes());
        zk.setACL("/", Ids.CREATOR_ALL_ACL, -1);
        String path = "/path";
        try {
            Assert.assertEquals(4, CREATOR_ALL_AND_WORLD_READABLE.size());
        } catch (Exception e) {
            LOG.error("Something is fundamentally wrong with ArrayList's add() method. add()ing four times to an empty ArrayList should result in an ArrayList with 4 members.");
            throw e;
        }
        zk.create(path, path.getBytes(), CREATOR_ALL_AND_WORLD_READABLE, CreateMode.PERSISTENT);
        List<ACL> acls = zk.getACL("/path", new Stat());
        Assert.assertEquals(2, acls.size());
    } catch (Exception e) {
        // test failed somehow.
        Assert.assertTrue(false);
    }
    f.shutdown();
    zks.shutdown();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) ArrayList(java.util.ArrayList) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) ACL(org.apache.zookeeper.data.ACL) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.Test)

Example 22 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.

the class ACLRootTest method testRootAcl.

@Test
public void testRootAcl() throws Exception {
    ZooKeeper zk = createClient();
    try {
        // set auth using digest
        zk.addAuthInfo("digest", "pat:test".getBytes());
        zk.setACL("/", Ids.CREATOR_ALL_ACL, -1);
        zk.getData("/", false, null);
        zk.close();
        // verify no access
        zk = createClient();
        try {
            zk.getData("/", false, null);
            Assert.fail("validate auth");
        } catch (KeeperException.NoAuthException e) {
        // expected
        }
        try {
            zk.create("/apps", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
            Assert.fail("validate auth");
        } catch (KeeperException.InvalidACLException e) {
        // expected
        }
        zk.addAuthInfo("digest", "world:anyone".getBytes());
        try {
            zk.create("/apps", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
            Assert.fail("validate auth");
        } catch (KeeperException.NoAuthException e) {
        // expected
        }
        zk.close();
        // verify access using original auth
        zk = createClient();
        zk.addAuthInfo("digest", "pat:test".getBytes());
        zk.getData("/", false, null);
        zk.create("/apps", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
        zk.delete("/apps", -1);
        // reset acl (back to open) and verify accessible again
        zk.setACL("/", Ids.OPEN_ACL_UNSAFE, -1);
        zk.close();
        zk = createClient();
        zk.getData("/", false, null);
        zk.create("/apps", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        try {
            zk.create("/apps", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
            Assert.fail("validate auth");
        } catch (KeeperException.InvalidACLException e) {
        // expected
        }
        zk.delete("/apps", -1);
        zk.addAuthInfo("digest", "world:anyone".getBytes());
        zk.create("/apps", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
        zk.close();
        zk = createClient();
        zk.delete("/apps", -1);
    } finally {
        zk.close();
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 23 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.

the class ACLTest method testDisconnectedAddAuth.

@Test
public void testDisconnectedAddAuth() 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);
    try {
        LOG.info("starting up the zookeeper server .. waiting");
        Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
        ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
        try {
            zk.addAuthInfo("digest", "pat:test".getBytes());
            zk.setACL("/", Ids.CREATOR_ALL_ACL, -1);
        } finally {
            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) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.Test)

Example 24 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper 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 25 with ZooKeeper

use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.

the class QuorumPeerMainTest method testEarlyLeaderAbandonment.

/**
     * Test early leader abandonment.
     */
@Test
public void testEarlyLeaderAbandonment() throws Exception {
    ClientBase.setupTestEnv();
    final int SERVER_COUNT = 3;
    final int[] clientPorts = new int[SERVER_COUNT];
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < SERVER_COUNT; i++) {
        clientPorts[i] = PortAssignment.unique();
        sb.append("server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + clientPorts[i] + "\n");
    }
    String quorumCfgSection = sb.toString();
    MainThread[] mt = new MainThread[SERVER_COUNT];
    ZooKeeper[] zk = new ZooKeeper[SERVER_COUNT];
    for (int i = 0; i < SERVER_COUNT; i++) {
        mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection);
        mt[i].start();
        zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this);
    }
    waitForAll(zk, States.CONNECTED);
    // that is rather innocuous.
    for (int i = 0; i < SERVER_COUNT; i++) {
        mt[i].shutdown();
    }
    waitForAll(zk, States.CONNECTING);
    for (int i = 0; i < SERVER_COUNT; i++) {
        mt[i].start();
        // Recreate a client session since the previous session was not persisted.
        zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this);
    }
    waitForAll(zk, States.CONNECTED);
    // ok lets find the leader and kill everything else, we have a few
    // seconds, so it should be plenty of time
    int leader = -1;
    Map<Long, Proposal> outstanding = null;
    for (int i = 0; i < SERVER_COUNT; i++) {
        if (mt[i].main.quorumPeer.leader == null) {
            mt[i].shutdown();
        } else {
            leader = i;
            outstanding = mt[leader].main.quorumPeer.leader.outstandingProposals;
        }
    }
    try {
        zk[leader].create("/zk" + leader, "zk".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        Assert.fail("create /zk" + leader + " should have failed");
    } catch (KeeperException e) {
    }
    // just make sure that we actually did get it in process at the
    // leader
    Assert.assertTrue(outstanding.size() == 1);
    Assert.assertTrue(((Proposal) outstanding.values().iterator().next()).request.getHdr().getType() == OpCode.create);
    // make sure it has a chance to write it to disk
    Thread.sleep(1000);
    mt[leader].shutdown();
    waitForAll(zk, States.CONNECTING);
    for (int i = 0; i < SERVER_COUNT; i++) {
        if (i != leader) {
            mt[i].start();
        }
    }
    for (int i = 0; i < SERVER_COUNT; i++) {
        if (i != leader) {
            // Recreate a client session since the previous session was not persisted.
            zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this);
            waitForOne(zk[i], States.CONNECTED);
            zk[i].create("/zk" + i, "zk".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
    }
    mt[leader].start();
    waitForAll(zk, States.CONNECTED);
    // make sure everything is consistent
    for (int i = 0; i < SERVER_COUNT; i++) {
        for (int j = 0; j < SERVER_COUNT; j++) {
            if (i == leader) {
                Assert.assertTrue((j == leader ? ("Leader (" + leader + ")") : ("Follower " + j)) + " should not have /zk" + i, zk[j].exists("/zk" + i, false) == null);
            } else {
                Assert.assertTrue((j == leader ? ("Leader (" + leader + ")") : ("Follower " + j)) + " does not have /zk" + i, zk[j].exists("/zk" + i, false) != null);
            }
        }
    }
    for (int i = 0; i < SERVER_COUNT; i++) {
        zk[i].close();
    }
    for (int i = 0; i < SERVER_COUNT; i++) {
        mt[i].shutdown();
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) Proposal(org.apache.zookeeper.server.quorum.Leader.Proposal) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Aggregations

ZooKeeper (org.apache.zookeeper.ZooKeeper)311 Test (org.junit.Test)172 KeeperException (org.apache.zookeeper.KeeperException)89 Stat (org.apache.zookeeper.data.Stat)43 WatchedEvent (org.apache.zookeeper.WatchedEvent)36 ArrayList (java.util.ArrayList)35 TestableZooKeeper (org.apache.zookeeper.TestableZooKeeper)33 IOException (java.io.IOException)32 CountDownLatch (java.util.concurrent.CountDownLatch)30 Watcher (org.apache.zookeeper.Watcher)25 File (java.io.File)23 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)21 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)21 ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)20 ReconfigTest (org.apache.zookeeper.test.ReconfigTest)16 ZooKeeperAdmin (org.apache.zookeeper.admin.ZooKeeperAdmin)15 TimeoutException (java.util.concurrent.TimeoutException)8 AsyncCallback (org.apache.zookeeper.AsyncCallback)8 ACL (org.apache.zookeeper.data.ACL)8 Id (org.apache.zookeeper.data.Id)8