Search in sources :

Example 6 with LockToken

use of org.apache.oozie.lock.LockToken in project oozie by apache.

the class TestZKLocksService method testRetriableRelease.

public void testRetriableRelease() throws Exception {
    final String path = UUID.randomUUID().toString();
    ZKLocksService zkls = new ZKLocksService();
    try {
        zkls.init(Services.get());
        InterProcessReadWriteLock lockEntry = Mockito.mock(InterProcessReadWriteLock.class);
        final InterProcessMutex writeLock = Mockito.mock(InterProcessMutex.class);
        Mockito.when(lockEntry.writeLock()).thenReturn(writeLock);
        Mockito.doThrow(new ConnectionLossException()).when(writeLock).release();
        Mockito.doNothing().when(writeLock).acquire();
        // put mocked lockEntry
        zkls.getLocks().putIfAbsent(path, lockEntry);
        LockToken lock = zkls.getWriteLock(path, -1);
        final boolean[] lockReleased = new boolean[] { false };
        Runnable exceptionStopper = new Runnable() {

            @Override
            public void run() {
                try {
                    // Stop the exception on release() after some time in other thread
                    Thread.sleep(TimeUnit.SECONDS.toMillis(13));
                    Mockito.doAnswer(new Answer() {

                        @Override
                        public Object answer(InvocationOnMock invocation) throws Throwable {
                            lockReleased[0] = true;
                            return null;
                        }
                    }).when(writeLock).release();
                } catch (Exception e) {
                    log.error(e);
                    fail("Test case failed due to " + e.getMessage());
                }
            }
        };
        new Thread(exceptionStopper).start();
        // Try to release the lock
        lock.release();
        assertEquals("Failing the test case. The lock should have been released", true, lockReleased[0]);
    } finally {
        zkls.destroy();
    }
}
Also used : LockToken(org.apache.oozie.lock.LockToken) ZKLockToken(org.apache.oozie.service.ZKLocksService.ZKLockToken) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) InterProcessReadWriteLock(org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 7 with LockToken

use of org.apache.oozie.lock.LockToken in project oozie by apache.

the class TestZKUtilsWithSecurity method testCheckAndSetACLs.

public void testCheckAndSetACLs() throws Exception {
    // We want to verify the ACLs on locks and the service discovery; ZKUtils does the service discovery and starting
    // ZKLocksService will use ZKUtils which will start advertising on the service discovery.  We can also acquire a lock so
    // it will create a lock znode.
    ZKLocksService zkls = new ZKLocksService();
    try {
        zkls.init(Services.get());
        LockToken lock = zkls.getWriteLock("foo", 3);
        lock.release();
        List<ACL> acls = getClient().getZookeeperClient().getZooKeeper().getACL("/oozie", new Stat());
        assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
        assertEquals("world", acls.get(0).getId().getScheme());
        assertEquals("anyone", acls.get(0).getId().getId());
        acls = getClient().getACL().forPath("/locks");
        assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
        assertEquals("world", acls.get(0).getId().getScheme());
        assertEquals("anyone", acls.get(0).getId().getId());
        acls = getClient().getACL().forPath("/locks/foo");
        assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
        assertEquals("world", acls.get(0).getId().getScheme());
        assertEquals("anyone", acls.get(0).getId().getId());
        acls = getClient().getACL().forPath("/services");
        assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
        assertEquals("world", acls.get(0).getId().getScheme());
        assertEquals("anyone", acls.get(0).getId().getId());
        acls = getClient().getACL().forPath("/services/servers");
        assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
        assertEquals("world", acls.get(0).getId().getScheme());
        assertEquals("anyone", acls.get(0).getId().getId());
        acls = getClient().getACL().forPath("/services/servers/" + ZK_ID);
        assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
        assertEquals("world", acls.get(0).getId().getScheme());
        assertEquals("anyone", acls.get(0).getId().getId());
    } finally {
        // unregistering all users of ZKUtils (i.e. ZKLocksService) will cause it to disconnect so when we set
        // "oozie.zookeeper.secure" to true, it will again connect but using SASL/Kerberos
        zkls.destroy();
    }
    // Verify that the expected paths created above still exist with the "world" ACLs
    List<ACL> acls = getClient().getZookeeperClient().getZooKeeper().getACL("/oozie", new Stat());
    assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
    assertEquals("world", acls.get(0).getId().getScheme());
    assertEquals("anyone", acls.get(0).getId().getId());
    acls = getClient().getACL().forPath("/locks");
    assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
    assertEquals("world", acls.get(0).getId().getScheme());
    assertEquals("anyone", acls.get(0).getId().getId());
    acls = getClient().getACL().forPath("/locks/foo");
    assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
    assertEquals("world", acls.get(0).getId().getScheme());
    assertEquals("anyone", acls.get(0).getId().getId());
    acls = getClient().getACL().forPath("/services");
    assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
    assertEquals("world", acls.get(0).getId().getScheme());
    assertEquals("anyone", acls.get(0).getId().getId());
    acls = getClient().getACL().forPath("/services/servers");
    assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
    assertEquals("world", acls.get(0).getId().getScheme());
    assertEquals("anyone", acls.get(0).getId().getId());
    zkls = new ZKLocksService();
    try {
        Services.get().getConf().set("oozie.zookeeper.secure", "true");
        // Now that security is enabled, it will trigger the checkAndSetACLs() code to go through and set all of the previously
        // created znodes to have "sasl" ACLs
        zkls.init(Services.get());
        acls = getClient().getZookeeperClient().getZooKeeper().getACL("/oozie", new Stat());
        assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
        assertEquals("sasl", acls.get(0).getId().getScheme());
        assertEquals(PRIMARY_PRINCIPAL, acls.get(0).getId().getId());
        acls = getClient().getACL().forPath("/locks");
        assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
        assertEquals("sasl", acls.get(0).getId().getScheme());
        assertEquals(PRIMARY_PRINCIPAL, acls.get(0).getId().getId());
        acls = getClient().getACL().forPath("/locks/foo");
        assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
        assertEquals("sasl", acls.get(0).getId().getScheme());
        assertEquals(PRIMARY_PRINCIPAL, acls.get(0).getId().getId());
        acls = getClient().getACL().forPath("/services");
        assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
        assertEquals("sasl", acls.get(0).getId().getScheme());
        assertEquals(PRIMARY_PRINCIPAL, acls.get(0).getId().getId());
        acls = getClient().getACL().forPath("/services/servers");
        assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
        assertEquals("sasl", acls.get(0).getId().getScheme());
        assertEquals(PRIMARY_PRINCIPAL, acls.get(0).getId().getId());
        acls = getClient().getACL().forPath("/services/servers/" + ZK_ID);
        assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms());
        assertEquals("sasl", acls.get(0).getId().getScheme());
        assertEquals(PRIMARY_PRINCIPAL, acls.get(0).getId().getId());
    } finally {
        zkls.destroy();
        Services.get().getConf().set("oozie.zookeeper.secure", "false");
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) LockToken(org.apache.oozie.lock.LockToken) ZKLocksService(org.apache.oozie.service.ZKLocksService) ACL(org.apache.zookeeper.data.ACL)

Aggregations

LockToken (org.apache.oozie.lock.LockToken)7 ZKLockToken (org.apache.oozie.service.ZKLocksService.ZKLockToken)4 Stat (org.apache.zookeeper.data.Stat)3 ZKLocksService (org.apache.oozie.service.ZKLocksService)2 ConnectionLossException (org.apache.zookeeper.KeeperException.ConnectionLossException)2 ACL (org.apache.zookeeper.data.ACL)2 DistributedAtomicLong (org.apache.curator.framework.recipes.atomic.DistributedAtomicLong)1 InterProcessMutex (org.apache.curator.framework.recipes.locks.InterProcessMutex)1 InterProcessReadWriteLock (org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1