Search in sources :

Example 1 with InterProcessReadWriteLock

use of org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock in project coprhd-controller by CoprHD.

the class CoordinatorClientImpl method getReadWriteLock.

@Override
public InterProcessReadWriteLock getReadWriteLock(String name) throws CoordinatorException {
    EnsurePath path = new EnsurePath(ZkPath.MUTEX.toString());
    try {
        path.ensure(_zkConnection.curator().getZookeeperClient());
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw CoordinatorException.fatals.unableToGetLock(name, e);
    } catch (Exception e) {
        throw CoordinatorException.fatals.unableToGetLock(name, e);
    }
    String lockPath = ZKPaths.makePath(ZkPath.MUTEX.toString(), name);
    return new InterProcessReadWriteLock(_zkConnection.curator(), lockPath);
}
Also used : EnsurePath(org.apache.curator.utils.EnsurePath) PropertyInfoMapper.decodeFromString(com.emc.storageos.coordinator.mapper.PropertyInfoMapper.decodeFromString) CoordinatorException(com.emc.storageos.coordinator.exceptions.CoordinatorException) RetryableCoordinatorException(com.emc.storageos.coordinator.exceptions.RetryableCoordinatorException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InterProcessReadWriteLock(org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock)

Example 2 with InterProcessReadWriteLock

use of org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock in project oozie by apache.

the class ZKLocksService method acquireLock.

private LockToken acquireLock(final String resource, final Type type, final long wait) throws InterruptedException {
    LOG.debug("Acquiring ZooKeeper lock. [resource={};type={};wait={}]", resource, type, wait);
    InterProcessReadWriteLock lockEntry;
    final String zkPath = LOCKS_NODE + "/" + resource;
    LOG.debug("Checking existing Curator lock or creating new one. [zkPath={}]", zkPath);
    // Creating a Curator InterProcessReadWriteLock is lightweight - only calling acquire() costs real ZooKeeper calls
    final InterProcessReadWriteLock newLockEntry = new InterProcessReadWriteLock(zk.getClient(), zkPath);
    final InterProcessReadWriteLock existingLockEntry = zkLocks.putIfAbsent(resource, newLockEntry);
    if (existingLockEntry == null) {
        lockEntry = newLockEntry;
        LOG.debug("No existing Curator lock present, new one created successfully. [zkPath={}]", zkPath);
    } else {
        // We can't destoy newLockEntry and we don't have to - it's taken care of by Curator and JVM GC
        lockEntry = existingLockEntry;
        LOG.debug("Reusing existing Curator lock. [zkPath={}]", zkPath);
    }
    ZKLockToken token = null;
    try {
        LOG.debug("Calling Curator to acquire ZooKeeper lock. [resource={};type={};wait={}]", resource, type, wait);
        final InterProcessMutex lock = (type.equals(Type.READ)) ? lockEntry.readLock() : lockEntry.writeLock();
        if (wait == -1) {
            lock.acquire();
            token = new ZKLockToken(lockEntry, type);
            LOG.debug("ZooKeeper lock acquired successfully. [resource={};type={}]", resource, type);
        } else if (lock.acquire(wait, TimeUnit.MILLISECONDS)) {
            token = new ZKLockToken(lockEntry, type);
            LOG.debug("ZooKeeper lock acquired successfully waiting. [resource={};type={};wait={}]", resource, type, wait);
        } else {
            LOG.warn("Could not acquire ZooKeeper lock, timed out. [resource={};type={};wait={}]", resource, type, wait);
        }
    } catch (final Exception ex) {
        // Not throwing exception. Should return null, so that command can be requeued
        LOG.warn("Could not acquire lock due to a ZooKeeper error. " + "[ex={};resource={};type={};wait={}]", ex, resource, type, wait);
        LOG.error("Error while acquiring lock", ex);
    }
    return token;
}
Also used : InterProcessReadWriteLock(org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 3 with InterProcessReadWriteLock

use of org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock 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)

Aggregations

InterProcessReadWriteLock (org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock)3 IOException (java.io.IOException)2 InterProcessMutex (org.apache.curator.framework.recipes.locks.InterProcessMutex)2 KeeperException (org.apache.zookeeper.KeeperException)2 CoordinatorException (com.emc.storageos.coordinator.exceptions.CoordinatorException)1 RetryableCoordinatorException (com.emc.storageos.coordinator.exceptions.RetryableCoordinatorException)1 PropertyInfoMapper.decodeFromString (com.emc.storageos.coordinator.mapper.PropertyInfoMapper.decodeFromString)1 UnknownHostException (java.net.UnknownHostException)1 EnsurePath (org.apache.curator.utils.EnsurePath)1 LockToken (org.apache.oozie.lock.LockToken)1 ZKLockToken (org.apache.oozie.service.ZKLocksService.ZKLockToken)1 ConnectionLossException (org.apache.zookeeper.KeeperException.ConnectionLossException)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1