use of org.olat.core.commons.services.lock.pessimistic.PLock in project OpenOLAT by OpenOLAT.
the class PLockTest method testSingleRowLockingSupported.
@Test
public void testSingleRowLockingSupported() {
log.info("testing if one lock only locks the given row and not the complete table (test whether the database supports rowlocking)");
// make sure both row entries for the locks are created, otherwise the system-wide locking
// applied on lock-row-creation cannot support row-level-locking by definition.
PLock pc1 = pessimisticLockManager.findOrPersistPLock("blabla");
Assert.assertNotNull(pc1);
PLock pc2 = pessimisticLockManager.findOrPersistPLock("blublu");
Assert.assertNotNull(pc2);
dbInstance.closeSession();
final List<Long> holder = new ArrayList<Long>(1);
// first thread acquires the lock and waits and continues holding the lock for some time.
PLock p1 = pessimisticLockManager.findOrPersistPLock("blabla");
Assert.assertNotNull(p1);
new Thread(new Runnable() {
public void run() {
PLock p2 = pessimisticLockManager.findOrPersistPLock("blublu");
assertNotNull(p2);
long p2Acquired = System.nanoTime();
holder.add(new Long(p2Acquired));
dbInstance.closeSession();
}
}).start();
sleep(500);
long p1AboutToRelease = System.nanoTime();
dbInstance.closeSession();
// if row locking is not supported, then the timestamp when p2 has been acquired will be shortly -after- p1 has been released
boolean singleRowLockingOk = holder.size() > 0 && holder.get(0).longValue() < p1AboutToRelease;
assertTrue("the database does not seem to support row locking when executing 'select for update', critical for performance!, ", singleRowLockingOk);
}
use of org.olat.core.commons.services.lock.pessimistic.PLock in project openolat by klemens.
the class PLockTest method testSync.
@Test
public void testSync() {
log.info("testing enrollment");
// ------------------ now check with lock -------------------
// create a group
// create users
final List<Identity> identities = new ArrayList<Identity>();
for (int i = 0; i < MAX_COUNT + MAX_USERS_MORE; i++) {
Identity id = JunitTestHelper.createAndPersistIdentityAsUser("u-" + i + "-" + UUID.randomUUID().toString());
identities.add(id);
log.info("testSync: Identity=" + id.getName() + " created");
}
dbInstance.closeSession();
final SecurityGroup group2 = BaseSecurityManager.getInstance().createAndPersistSecurityGroup();
// make sure the lock has been written to the disk (tests for createOrFind see other methods)
dbInstance.closeSession();
// prepare threads
int numOfThreads = MAX_COUNT + MAX_USERS_MORE;
final CountDownLatch finishCount = new CountDownLatch(numOfThreads);
// try to enrol all in the same group
for (int i = 0; i < numOfThreads; i++) {
final int j = i;
new Thread(new Runnable() {
public void run() {
try {
log.info("testSync: thread started j=" + j);
Identity id = identities.get(j);
//
PLock p2 = pessimisticLockManager.findOrPersistPLock("befinsert");
assertNotNull(p2);
doNoLockingEnrol(id, group2);
dbInstance.commit();
dbInstance.closeSession();
} catch (Exception e) {
e.printStackTrace();
} finally {
finishCount.countDown();
}
}
}).start();
}
try {
finishCount.await(120, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.error("", e);
}
// now count
dbInstance.closeSession();
int cnt2 = BaseSecurityManager.getInstance().countIdentitiesOfSecurityGroup(group2);
assertTrue("cnt should be smaller or eq than allowed since synced with select for update. cnt:" + cnt2 + ", max " + MAX_COUNT, cnt2 <= MAX_COUNT);
assertTrue("cnt should be eq to allowed since synced with select for update. cnt:" + cnt2 + ", max " + MAX_COUNT, cnt2 == MAX_COUNT);
log.info("cnt lock " + cnt2);
}
use of org.olat.core.commons.services.lock.pessimistic.PLock in project openolat by klemens.
the class PLockTest method testNestedLockingSupported.
@Test
public void testNestedLockingSupported() {
log.info("testing if nested locking is supported");
// make sure all three row entries for the locks are created, otherwise the system-wide locking
// applied on lock-row-creation cannot support row-level-locking by definition.
PLock pc1 = pessimisticLockManager.findOrPersistPLock("blabla");
assertNotNull(pc1);
PLock pc2 = pessimisticLockManager.findOrPersistPLock("blublu");
assertNotNull(pc2);
PLock pc3 = pessimisticLockManager.findOrPersistPLock("blibli");
assertNotNull(pc3);
dbInstance.closeSession();
final List<Long> holder = new ArrayList<Long>(1);
// first thread acquires the two locks and waits and continues holding the lock for some time.
PLock p1 = pessimisticLockManager.findOrPersistPLock("blabla");
assertNotNull(p1);
PLock p3 = pessimisticLockManager.findOrPersistPLock("blibli");
assertNotNull(p3);
new Thread(new Runnable() {
public void run() {
PLock p2 = pessimisticLockManager.findOrPersistPLock("blibli");
assertNotNull(p2);
long p2Acquired = System.nanoTime();
holder.add(new Long(p2Acquired));
dbInstance.closeSession();
}
}).start();
sleep(500);
boolean acOk = holder.size() == 0;
// the commit will drop the lock on blibli d
dbInstance.closeSession();
sleep(500);
boolean acNowOk = holder.size() == 1;
// if row locking is not supported, then the timestamp when p2 has been acquired will be shortly -after- p1 has been released
assertTrue("since holding the blabla lock, no other may acquire it", acOk);
assertTrue("after having released the blabla lock, a next waiting thread must have acquired it after some time", acNowOk);
}
use of org.olat.core.commons.services.lock.pessimistic.PLock in project openolat by klemens.
the class PLockTest method testDeadLockTimeout.
@Test
public void testDeadLockTimeout() {
log.info("testing if deadlock detection and handling is supported");
// make sure all three row entries for the locks are created, otherwise the system-wide locking
// applied on lock-row-creation cannot support row-level-locking by definition.
PLock pc1 = pessimisticLockManager.findOrPersistPLock("blabla");
assertNotNull(pc1);
PLock pc2 = pessimisticLockManager.findOrPersistPLock("blublu");
assertNotNull(pc2);
PLock pc3 = pessimisticLockManager.findOrPersistPLock("blibli");
assertNotNull(pc3);
dbInstance.closeSession();
/**
* t1 t2
* bla bli
* .. ..
* .. ..
* .. ..
* bli ..
* ..
* ..
* bla
* -> deadlock! t2 waits on bla (already acquired by t1, but t1 waits on bli, already acquired by t2)
*/
final List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>(1));
final CountDownLatch finishCount = new CountDownLatch(2);
// t1
new Thread(new Runnable() {
public void run() {
try {
PLock p1 = pessimisticLockManager.findOrPersistPLock("blabla");
assertNotNull(p1);
sleep(250);
// now try to acquire blibli but that fails, since blibli is already locked by thread 2.
// but thread 2 cannot continue either, since it is waiting for lock blabla, which is already hold by thread 1
// -> deadlock
PLock p3 = pessimisticLockManager.findOrPersistPLock("blibli");
assertNotNull(p3);
} catch (Exception e) {
exceptionHolder.add(e);
} finally {
try {
dbInstance.closeSession();
} catch (Exception e) {
// ignore
}
finishCount.countDown();
}
}
}).start();
// t2
new Thread(new Runnable() {
public void run() {
try {
PLock p2 = pessimisticLockManager.findOrPersistPLock("blibli");
assertNotNull(p2);
sleep(500);
PLock p3 = pessimisticLockManager.findOrPersistPLock("blabla");
assertNotNull(p3);
} catch (Exception e) {
exceptionHolder.add(e);
} finally {
try {
dbInstance.closeSession();
} catch (Exception e) {
// ignore
}
finishCount.countDown();
}
}
}).start();
// sleep until t1 and t2 should have terminated/excepted
try {
finishCount.await(8, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Assert.fail("Takes too long (more than 8sec)");
}
// if not -> they are in deadlock and the db did not detect it
for (Exception exception : exceptionHolder) {
log.error("exception: ", exception);
}
assertTrue("expected a deadlock exception, but got none", exceptionHolder.size() > 0);
}
Aggregations