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);
}
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;
}
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();
}
}
Aggregations