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