Search in sources :

Example 1 with LockToken

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

the class TestZKLocksService method testLocksAreReused.

public void testLocksAreReused() throws ServiceException, InterruptedException {
    String path = "a";
    ZKLocksService lockService = new ZKLocksService();
    try {
        lockService.init(Services.get());
        LockToken lock = lockService.getWriteLock(path, TestMemoryLocks.DEFAULT_LOCK_TIMEOUT);
        int oldHash = System.identityHashCode(lockService.getLocks().get(path));
        System.gc();
        lock.release();
        lock = lockService.getWriteLock(path, TestMemoryLocks.DEFAULT_LOCK_TIMEOUT);
        assertEquals(lockService.getLocks().size(), 1);
        int newHash = System.identityHashCode(lockService.getLocks().get(path));
        assertTrue(oldHash == newHash);
    } finally {
        lockService.destroy();
    }
}
Also used : LockToken(org.apache.oozie.lock.LockToken) ZKLockToken(org.apache.oozie.service.ZKLocksService.ZKLockToken)

Example 2 with LockToken

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

the class TestZKUtilsWithSecurity method testNewUsingACLs.

public void testNewUsingACLs() throws Exception {
    // We want to verify the ACLs on new 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 {
        Services.get().getConf().set("oozie.zookeeper.secure", "true");
        // Verify that the znodes don't already exist
        assertNull(getClient().getZookeeperClient().getZooKeeper().exists("/oozie", null));
        assertNull(getClient().checkExists().forPath("/locks"));
        assertNull(getClient().checkExists().forPath("/services"));
        // Check that new znodes will use the ACLs
        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("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)

Example 3 with LockToken

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

the class ZKUUIDService method resetSequence.

/**
 * Once sequence is reached limit, reset to 0.
 *
 * @throws Exception
 */
private void resetSequence() throws Exception {
    for (int i = 0; i < RETRY_COUNT; i++) {
        AtomicValue<Long> value = atomicIdGenerator.get();
        if (value.succeeded()) {
            if (value.postValue() < maxSequence) {
                return;
            }
        }
        // Acquire ZK lock, so that other host doesn't reset sequence.
        LockToken lock = null;
        try {
            lock = Services.get().get(MemoryLocksService.class).getWriteLock(ZKUUIDService.class.getName(), lockTimeout);
        } catch (InterruptedException e1) {
        // ignore
        }
        try {
            if (lock == null) {
                LOG.info("Lock is held by other system, will sleep and try again");
                Thread.sleep(1000);
                continue;
            } else {
                value = atomicIdGenerator.get();
                if (value.succeeded()) {
                    if (value.postValue() < maxSequence) {
                        return;
                    }
                }
                try {
                    atomicIdGenerator.forceSet(RESET_VALUE);
                } catch (Exception e) {
                    LOG.info("Exception while resetting sequence, will try again");
                    continue;
                }
                resetStartTime();
                return;
            }
        } finally {
            if (lock != null) {
                lock.release();
            }
        }
    }
    throw new Exception("Can't reset ID sequence in ZK. Retried " + RETRY_COUNT + " times");
}
Also used : DistributedAtomicLong(org.apache.curator.framework.recipes.atomic.DistributedAtomicLong) LockToken(org.apache.oozie.lock.LockToken)

Example 4 with LockToken

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

the class TestZKLocksService method testLocksAreGarbageCollected.

public void testLocksAreGarbageCollected() throws ServiceException, InterruptedException {
    String path = new String("a");
    String path1 = new String("a");
    ZKLocksService lockService = new ZKLocksService();
    try {
        lockService.init(Services.get());
        LockToken lock = lockService.getWriteLock(path, TestMemoryLocks.DEFAULT_LOCK_TIMEOUT);
        lock.release();
        assertEquals(lockService.getLocks().size(), 1);
        int oldHash = lockService.getLocks().get(path).hashCode();
        lock = lockService.getWriteLock(path1, TestMemoryLocks.DEFAULT_LOCK_TIMEOUT);
        int newHash = lockService.getLocks().get(path1).hashCode();
        assertTrue(oldHash == newHash);
        lock = null;
        System.gc();
        lock = lockService.getWriteLock(path, TestMemoryLocks.DEFAULT_LOCK_TIMEOUT);
        newHash = lockService.getLocks().get(path).hashCode();
        assertFalse(oldHash == newHash);
    } finally {
        lockService.destroy();
    }
}
Also used : LockToken(org.apache.oozie.lock.LockToken) ZKLockToken(org.apache.oozie.service.ZKLocksService.ZKLockToken)

Example 5 with LockToken

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

the class TestZKLocksService method testLockReaper.

public void testLockReaper() throws Exception {
    ConfigurationService.set(ZKLocksService.REAPING_THRESHOLD, "1");
    ZKLocksService zkls = new ZKLocksService();
    try {
        zkls.init(Services.get());
        for (int i = 0; i < 10; ++i) {
            LockToken l = zkls.getReadLock(String.valueOf(i), 1);
            l.release();
        }
        waitFor(10000, new Predicate() {

            @Override
            public boolean evaluate() throws Exception {
                Stat stat = getClient().checkExists().forPath(ZKLocksService.LOCKS_NODE);
                return stat.getNumChildren() == 0;
            }
        });
        Stat stat = getClient().checkExists().forPath(ZKLocksService.LOCKS_NODE);
        assertEquals(0, stat.getNumChildren());
    } finally {
        zkls.destroy();
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) LockToken(org.apache.oozie.lock.LockToken) ZKLockToken(org.apache.oozie.service.ZKLocksService.ZKLockToken) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException)

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