public void testBasicLocking()
throws Throwable {
try {
LN ln = new LN(new byte[0]);
/*
* Make a null txn that will lock. Take a lock and then end the
* operation.
*/
EnvironmentImpl envImpl = DbInternal.envGetEnvironmentImpl(env);
MemoryBudget mb = envImpl.getMemoryBudget();
long beforeLock = mb.getCacheMemoryUsage();
Locker nullTxn = new BasicLocker(envImpl);
LockGrantType lockGrant = nullTxn.lock
(ln.getNodeId(), LockType.READ,
DbInternal.dbGetDatabaseImpl(db)).
getLockGrant();
assertEquals(LockGrantType.NEW, lockGrant);
long afterLock = mb.getCacheMemoryUsage();
checkHeldLocks(nullTxn, 1, 0);
nullTxn.operationEnd();
long afterRelease = mb.getCacheMemoryUsage();
checkHeldLocks(nullTxn, 0, 0);
checkCacheUsage(beforeLock, afterLock, afterRelease,
LockManager.TOTAL_LOCK_OVERHEAD +
MemoryBudget.LOCKINFO_OVERHEAD);
// Take a lock, release it.
beforeLock = mb.getCacheMemoryUsage();
lockGrant = nullTxn.lock
(ln.getNodeId(), LockType.READ,
DbInternal.dbGetDatabaseImpl(db)).
getLockGrant();
afterLock = mb.getCacheMemoryUsage();
assertEquals(LockGrantType.NEW, lockGrant);
checkHeldLocks(nullTxn, 1, 0);
nullTxn.releaseLock(ln.getNodeId());
checkHeldLocks(nullTxn, 0, 0);
afterRelease = mb.getCacheMemoryUsage();
checkCacheUsage(beforeLock, afterLock, afterRelease,
LockManager.TOTAL_LOCK_OVERHEAD +
MemoryBudget.LOCKINFO_OVERHEAD);
/*
* Make a user transaction, check lock and release.
*/
beforeLock = mb.getCacheMemoryUsage();
Txn userTxn = new Txn(envImpl, new TransactionConfig());
lockGrant = userTxn.lock
(ln.getNodeId(), LockType.READ,
DbInternal.dbGetDatabaseImpl(db)).
getLockGrant();
afterLock = mb.getCacheMemoryUsage();
assertEquals(LockGrantType.NEW, lockGrant);
checkHeldLocks(userTxn, 1, 0);
// Try demoting, nothing should happen.
try {
userTxn.demoteLock(ln.getNodeId());
fail("exception not thrown on phoney demoteLock");
} catch (AssertionError e){
}
checkHeldLocks(userTxn, 1, 0);
long afterDemotion = mb.getCacheMemoryUsage();
assertEquals(afterLock, afterDemotion);
// Make it a write lock, then demote.
lockGrant = userTxn.lock
(ln.getNodeId(), LockType.WRITE,
DbInternal.dbGetDatabaseImpl(db)).
getLockGrant();
assertEquals(LockGrantType.PROMOTION, lockGrant);
long afterWriteLock = mb.getCacheMemoryUsage();
assertTrue(afterWriteLock > afterLock);
assertTrue(afterLock > beforeLock);
checkHeldLocks(userTxn, 0, 1);
userTxn.demoteLock(ln.getNodeId());
checkHeldLocks(userTxn, 1, 0);
// Shouldn't release at operation end
userTxn.operationEnd();
checkHeldLocks(userTxn, 1, 0);
userTxn.releaseLock(ln.getNodeId());
checkHeldLocks(userTxn, 0, 0);
userTxn.commit(Txn.TXN_SYNC);
afterRelease = mb.getCacheMemoryUsage();
assertEquals(beforeLock, afterRelease);
} catch (Throwable t) {