use of org.apache.derby.iapi.store.raw.ContainerHandle in project derby by apache.
the class ReclaimSpaceHelper method reclaimSpace.
/**
* Reclaim space based on work.
*/
public static int reclaimSpace(BaseDataFileFactory dataFactory, RawTransaction tran, ReclaimSpace work) throws StandardException {
if (work.reclaimWhat() == ReclaimSpace.CONTAINER)
return reclaimContainer(dataFactory, tran, work);
// Else, not reclaiming container. Get a no-wait shared lock on the
// container regardless of how the user transaction had the
// container opened.
LockingPolicy container_rlock = tran.newLockingPolicy(LockingPolicy.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE, true);
if (SanityManager.DEBUG)
SanityManager.ASSERT(container_rlock != null);
ContainerHandle containerHdl = openContainerNW(tran, container_rlock, work.getContainerId());
if (containerHdl == null) {
tran.abort();
if (SanityManager.DEBUG) {
if (SanityManager.DEBUG_ON(DaemonService.DaemonTrace)) {
SanityManager.DEBUG(DaemonService.DaemonTrace, " aborted " + work + " because container is locked or dropped");
}
}
if (// retry this for serveral times
work.incrAttempts() < 3) // it is however, unlikely that three tries will be
// enough because there is no delay between retries.
// See DERBY-4059 and DERBY-4055 for details.
{
return Serviceable.REQUEUE;
} else {
if (SanityManager.DEBUG) {
if (SanityManager.DEBUG_ON(DaemonService.DaemonTrace)) {
SanityManager.DEBUG(DaemonService.DaemonTrace, " gave up after 3 tries to get container lock " + work);
}
}
return Serviceable.DONE;
}
}
if (work.reclaimWhat() == ReclaimSpace.PAGE) {
// Reclaiming a page - called by undo of insert which purged the
// last row off an overflow page. It is safe to reclaim the page
// without first locking the head row because unlike post commit
// work, this is post abort work. Abort is guarenteed to happen
// and to happen only once, if at all.
Page p = containerHdl.getPageNoWait(work.getPageId().getPageNumber());
if (p != null)
containerHdl.removePage(p);
tran.commit();
return Serviceable.DONE;
}
// We are reclaiming row space or long column.
// First get an xlock on the head row piece.
RecordHandle headRecord = work.getHeadRowHandle();
if (!container_rlock.lockRecordForWrite(tran, headRecord, false, /* not insert */
false)) {
// cannot get the row lock, retry
tran.abort();
if (work.incrAttempts() < 3) {
return Serviceable.REQUEUE;
} else {
if (SanityManager.DEBUG) {
if (SanityManager.DEBUG_ON(DaemonService.DaemonTrace)) {
SanityManager.DEBUG(DaemonService.DaemonTrace, " gave up after 3 tries to get row lock " + work);
}
}
return Serviceable.DONE;
}
}
if (work.reclaimWhat() == ReclaimSpace.ROW_RESERVE) {
// This row may benefit from compaction.
containerHdl.compactRecord(headRecord);
// This work is being done - post commit, there is no user
// transaction that depends on the commit being sync'd. It is safe
// to commitNoSync() This do as one of 2 things will happen:
//
// 1) if any data page associated with this transaction is
// moved from cache to disk, then the transaction log
// must be sync'd to the log record for that change and
// all log records including the commit of this xact must
// be sync'd before returning.
//
// 2) if the data page is never written then the log record
// for the commit may never be written, and the xact will
// never make to disk. This is ok as no subsequent action
// depends on this operation being committed.
//
tran.commitNoSync(Transaction.RELEASE_LOCKS);
return Serviceable.DONE;
} else {
if (SanityManager.DEBUG)
SanityManager.ASSERT(work.reclaimWhat() == ReclaimSpace.COLUMN_CHAIN);
// Reclaiming a long column chain due to update. The long column
// chain being reclaimed is the before image of the update
// operation.
//
long headPageId = ((PageKey) headRecord.getPageId()).getPageNumber();
// DERBY-4050 - we wait for the page so we don't have to retry.
// prior to the 4050 fix, we called getPageNoWait and just
// retried 3 times. This left unreclaimed space if we were
// not successful after three tries.
StoredPage headRowPage = (StoredPage) containerHdl.getPage(headPageId);
if (headRowPage == null) {
if (SanityManager.DEBUG) {
if (SanityManager.DEBUG_ON(DaemonService.DaemonTrace)) {
SanityManager.DEBUG(DaemonService.DaemonTrace, "gave up because hadRowPage was null" + work);
}
}
tran.abort();
return Serviceable.DONE;
}
try {
headRowPage.removeOrphanedColumnChain(work, containerHdl);
} finally {
headRowPage.unlatch();
}
// This work is being done - post commit, there is no user
// transaction that depends on the commit being sync'd. It is safe
// to commitNoSync() This do as one of 2 things will happen:
//
// 1) if any data page associated with this transaction is
// moved from cache to disk, then the transaction log
// must be sync'd to the log record for that change and
// all log records including the commit of this xact must
// be sync'd before returning.
//
// 2) if the data page is never written then the log record
// for the commit may never be written, and the xact will
// never make to disk. This is ok as no subsequent action
// depends on this operation being committed.
//
tran.commitNoSync(Transaction.RELEASE_LOCKS);
return Serviceable.DONE;
}
}
use of org.apache.derby.iapi.store.raw.ContainerHandle in project derby by apache.
the class LogicalPageOperation method reclaimPrepareLocks.
/**
************************************************************************
* Public Methods of RePreparable Interface:
**************************************************************************
*/
/**
* reclaim locks associated with the changes in this log record.
* <p>
* @param locking_policy The locking policy to use to claim the locks.
*
* @exception StandardException Standard exception policy.
*/
public void reclaimPrepareLocks(Transaction t, LockingPolicy locking_policy) throws StandardException {
if (SanityManager.DEBUG) {
SanityManager.DEBUG_PRINT("", "reclaimPrepareLocks().");
SanityManager.ASSERT(getRecordHandle() != null);
}
ContainerHandle ch = t.openContainer(getPageId().getContainerId(), locking_policy, (ContainerHandle.MODE_FORUPDATE | ContainerHandle.MODE_OPEN_FOR_LOCK_ONLY | ContainerHandle.MODE_LOCK_NOWAIT));
if (SanityManager.DEBUG) {
SanityManager.ASSERT(ch != null);
}
if (ch != null)
ch.close();
/*
// get the intent lock on the container.
boolean lock_granted =
locking_policy.lockContainer(
t,
getContainer(),
false, // don't wait for the lock, it is bug if a
// lock has to wait while reclaiming locks
// during recovery.
true); // lock for update.
if (SanityManager.DEBUG)
{
SanityManager.ASSERT(lock_granted);
}
*/
// get the row lock on the c.
boolean lock_granted = locking_policy.lockRecordForWrite(t, getRecordHandle(), // default is not for insert.
false, // don't wait for the lock, it is bug if a
false);
// lock has to wait while reclaiming locks
// during recovery.
releaseResource(t);
if (SanityManager.DEBUG) {
SanityManager.ASSERT(lock_granted);
}
}
Aggregations