use of org.apache.derby.iapi.store.raw.PageKey in project derby by apache.
the class FileContainer method reCreatePageForRedoRecovery.
/**
* ReCreate a page for rollforward recovery.
* <p>
* During redo recovery it is possible for the system to try to redo
* the creation of a page (ie. going from non-existence to version 0).
* It first trys to read the page from disk, but a few different types
* of errors can occur:
* o the page does not exist at all on disk, this can happen during
* rollforward recovery applied to a backup where the file was
* copied and the page was added to the file during the time frame
* of the backup but after the physical file was copied.
* o space in the file exists, but it was never initalized. This
* can happen if you happen to crash at just the right moment during
* the allocation process. Also
* on some OS's it is possible to read from a part of the file that
* was not ever written - resulting in garbage from the store's
* point of view (often the result is all 0's).
*
* All these errors are easy to recover from as the system can easily
* create a version 0 from scratch and write it to disk.
*
* Because the system does not sync allocation of data pages, it is also
* possible at this point that whlie writing the version 0 to disk to
* create it we may encounter an out of disk space error (caught in this
* routine as a StandardException from the create() call. We can't
* recovery from this without help from outside, so the caught exception
* is nested and a new exception thrown which the recovery system will
* output to the user asking them to check their disk for space/errors.
*
* @exception StandardException Standard exception policy.
*/
protected BasePage reCreatePageForRedoRecovery(BaseContainerHandle handle, int pageFormat, long pageNumber, long pageOffset) throws StandardException {
// recreating a page should be done only if are in the middle of
// rollforward recovery or if derby.storage.patchInitPageRecoverError
// is set to true.
// check if we are in rollforward recovery
boolean rollForwardRecovery = ((RawTransaction) handle.getTransaction()).inRollForwardRecovery();
if (!rollForwardRecovery && !(PropertyUtil.getSystemBoolean(RawStoreFactory.PATCH_INITPAGE_RECOVER_ERROR))) {
return null;
}
// RESOLVE: first need to verify that the page is really NOT in the
// container!
// no address translation necessary
PageKey pkey = new PageKey(identity, pageNumber);
PageCreationArgs reCreatePageArgs;
if (pageFormat == StoredPage.FORMAT_NUMBER) {
reCreatePageArgs = new PageCreationArgs(pageFormat, CachedPage.WRITE_SYNC, pageSize, spareSpace, minimumRecordSize, 0);
} else if (pageFormat == AllocPage.FORMAT_NUMBER) {
// only the first allocation page have borrowed space for the
// container info
int containerInfoSize = 0;
if (pageNumber == FIRST_ALLOC_PAGE_NUMBER) {
containerInfoSize = CONTAINER_INFO_SIZE;
firstAllocPageNumber = pageNumber;
firstAllocPageOffset = pageOffset;
}
reCreatePageArgs = new PageCreationArgs(pageFormat, CachedPage.WRITE_SYNC, pageSize, // allocation page has no need for spare
0, minimumRecordSize, containerInfoSize);
} else {
throw StandardException.newException(SQLState.DATA_UNKNOWN_PAGE_FORMAT, pkey);
}
if (SanityManager.DEBUG) {
if (SanityManager.DEBUG_ON("LoadTran"))
SanityManager.DEBUG_PRINT("Trace", "recreating page " + pkey + " for load tran");
}
// Can't just call initPage because that wants to log an initPage
// operation, whereas we are here because of an initPage operation in
// the log already.
BasePage page = null;
boolean releasePage = true;
try {
try {
// a brand new page, initialize a new page in cache
page = (BasePage) pageCache.create(pkey, reCreatePageArgs);
} catch (StandardException se) {
throw StandardException.newException(SQLState.FILE_NEW_PAGE_DURING_RECOVERY, se, pkey);
}
if (page != null) {
releasePage = false;
page = latchPage(handle, page, false);
if (page == null) {
throw StandardException.newException(SQLState.FILE_NEW_PAGE_NOT_LATCHED, pkey);
}
} else {
throw StandardException.newException(SQLState.FILE_NEW_PAGE_DURING_RECOVERY, pkey);
}
} finally {
if (releasePage && page != null) {
// release the new page from cache if it errors out before
// the exclusive lock is set error in roll forward recovery.
// , we are doomed anyway
pageCache.release((Cacheable) page);
page = null;
}
}
return page;
}
use of org.apache.derby.iapi.store.raw.PageKey in project derby by apache.
the class FileContainer method makeAllocPage.
/*
Make a new alloc page, latch it with the passed in container handle.
*/
private AllocPage makeAllocPage(RawTransaction ntt, BaseContainerHandle handle, long pageNumber, long pageOffset, int containerInfoSize) throws StandardException {
if (SanityManager.DEBUG) {
if (containerInfoSize != 0 && containerInfoSize != CONTAINER_INFO_SIZE)
SanityManager.THROWASSERT("expect 0 or " + CONTAINER_INFO_SIZE + ", got " + containerInfoSize);
if (pageNumber != FIRST_ALLOC_PAGE_NUMBER && containerInfoSize != 0)
SanityManager.THROWASSERT("Not first alloc page but container info size " + containerInfoSize);
}
// argument to the create is an array of ints
// 0'th element is the page format
// 1'st element is whether or not to sync the page to disk
// 2'nd element is the pagesize
// 3'rd element is spareSpace
// 4'th element is number of bytes to reserve for the container header
// 5'th element is the minimumRecordSize
// NOTE: the arg list here must match the one in allocPage
// No I/O at all if this new page is requested as part of a create
// and load statement or this new alloc page is in a temporary
// container.
// In the former case, BaseContainer will allow the MODE_UNLOGGED
// bit to go thru to the nested top transaction alloc handle.
// In the later case, there is no nested top transaction and the
// alloc handle is the user handle, which is UNLOGGED.
boolean noIO = (handle.getMode() & ContainerHandle.MODE_UNLOGGED) == ContainerHandle.MODE_UNLOGGED;
PageCreationArgs createAllocPageArgs = new PageCreationArgs(AllocPage.FORMAT_NUMBER, noIO ? 0 : CachedPage.WRITE_SYNC, pageSize, // allocation page has no need for spare
0, minimumRecordSize, containerInfoSize);
if (SanityManager.DEBUG) {
if (SanityManager.DEBUG_ON(SPACE_TRACE)) {
SanityManager.DEBUG(SPACE_TRACE, "making new allocation page at " + pageNumber);
}
}
if (pageNumber == FIRST_ALLOC_PAGE_NUMBER) {
// RESOLVE: make sure the following is true
//
// firstAllocPageNumber and Offset can be set and access without
// synchronization since the first allocation page is
// created as part of the container create, this value is set
// before any other transaction has a chance to open the container.
// Once set, the first allocation page does not move or change
// position
firstAllocPageNumber = pageNumber;
firstAllocPageOffset = pageOffset;
}
PageKey pkey = new PageKey(identity, pageNumber);
// return a latched new alloc page
return (AllocPage) initPage(handle, pkey, createAllocPageArgs, pageOffset, false, /* not reuse */
false);
}
use of org.apache.derby.iapi.store.raw.PageKey in project derby by apache.
the class FileContainer method newPage.
/**
* Create a new page in the container.
*
* <BR> MT - thread aware - It is assumed that our caller (our super class)
* has already arranged a logical lock on page allocation to only allow a
* single thread through here.
*
* Adding a new page involves 2 transactions and 2 pages.
* The User Transaction (UT) initiated the addPage call and expects a
* latched page (owns by the UT) to be returned.
* The Nested Top Transaction (NTT) is the transaction started by RawStore
* inside an addPage call. This NTT is committed before the page is
* returned. The NTT is used to accessed high traffic data structure such
* as the AllocPage.
*
* This is outline of the algorithm used in adding a page:
* 1) find or make an allocPage which can handle the addding of a new page.
* Latch the allocPage with the NTT.
* 2) invalidate the allocation information cached by the container.
* Without the cache no page can be gotten from the container. Pages
* already in the page cache is not affected. Thus by latching the
* allocPage and invalidating the allocation cache, this NTT blocks out
* all page gets from this container until it commits.
* 3) the allocPage determines which page can be allocated, mark that in its
* data structure (the alloc extent) and returns the page number of the
* new page. This change is associated with the NTT.
* 4) the NTT gets or creates the new page in the page cache (bypassing the
* lookup of the allocPage since that is already latched by the NTT and
* will deadlock).
* 5) the NTT initializes the page (mark it is being a VALID page).
* 6) the page latch is transfered to the UT from the NTT.
* 7) the new page is returned, latched by UT
*
* If we use an NTT, the caller has to commit the NTT to release the
* allocPage latch. If we don't use an NTT, the allocPage latch is released
* as this routine returns.
*
* @param userHandle - the container handle opened by the user transaction,
* use this to latch the new user page
* @param ntt - the nested top transaction for the purpose of allocating the new page
* If ntt is null, use the user transaction for allocation.
* #param allocHandle - the container handle opened by the ntt,
* use this to latch the alloc page
*
* @exception StandardException Standard Derby error policy
*/
protected BasePage newPage(BaseContainerHandle userHandle, RawTransaction ntt, BaseContainerHandle allocHandle, boolean isOverflow) throws StandardException {
// NOTE: we are single threaded thru this method, see MT comment
boolean useNTT = (ntt != null);
// if ntt is null, use user transaction
if (!useNTT)
ntt = userHandle.getTransaction();
// last allocated page
long lastPage;
// last pre-allcated page
long lastPreallocPage;
long pageNumber = // init to appease compiler
ContainerHandle.INVALID_PAGE_NUMBER;
// the page number of the new page
// the identity of the new page
PageKey pkey;
// if true, we are trying to reuse a page
boolean reuse;
/* in case the page recommeded by allocPage is not committed yet, may
/* need to retry a couple of times */
boolean retry;
int numtries = 0;
int maxTries = InterruptStatus.MAX_INTERRUPT_RETRIES;
long startSearch = lastAllocatedPage;
// the alloc page
AllocPage allocPage = null;
// the new page
BasePage page = null;
try {
do {
// we don't expect we need to retry
retry = false;
synchronized (allocCache) {
if (SanityManager.DEBUG) {
SanityManager.ASSERT(ntt.getId().equals(allocHandle.getTransaction().getId()));
if (useNTT)
SanityManager.ASSERT(!ntt.getId().equals(userHandle.getTransaction().getId()));
}
/* find an allocation page that can handle adding a new
* page.
*
* allocPage is unlatched when the ntt commits. The new
* page is initialized by the ntt but the latch is
* transfered to the user transaction before the allocPage
* is unlatched. The allocPage latch prevents almost any
* other reader or writer from finding the new page until
* the ntt is committed and the new page is latched by the
* user transaction.
*
* (If the page is being reused, it is possible for another
* xact which kept a handle on the reused page to find the
* page during the transfer UT -> NTT. If this unlikely
* even occurs and the transfer fails [see code relating
* to transfer below], we retry from the beginning.)
*
* After the NTT commits a reader (getNextPageNumber) may
* get the page number of the newly allocated page and it
* will wait for the new page and latch it when the user
* transaction commits, aborts or unlatches the new page.
* Whether the user transaction commits or aborts, the new
* page stay allocated.
*
* RESOLVE: before NTT rolls back (or commits) the latch is
* released. To repopulate the allocation cache, need to
* get either the container lock on add page, or get a per
* allocation page lock.
*
* This blocks all page read (getPage) from accessing this
* alloc page in this container until the alloc page is
* unlatched. Those who already have a page handle into
* this container are unaffected.
*
* In other words, allocation blocks out reader (of any
* page that is managed by this alloc page) by the latch
* on the allocation page.
*
* Note that write page can proceed as usual.
*/
try {
allocPage = findAllocPageForAdd(allocHandle, ntt, startSearch);
} catch (InterruptDetectedException e) {
// hold. We release it when we do "continue" below.
if (--maxTries > 0) {
// Clear firstAllocPageNumber, i.e. undo side
// effect of makeAllocPage, so retry will work
firstAllocPageNumber = ContainerHandle.INVALID_PAGE_NUMBER;
retry = true;
// needs) and retry writeRAFHeader.
try {
Thread.sleep(InterruptStatus.INTERRUPT_RETRY_SLEEP);
} catch (InterruptedException ee) {
// This thread received an interrupt as
// well, make a note.
InterruptStatus.setInterrupted();
}
continue;
} else {
throw StandardException.newException(SQLState.FILE_IO_INTERRUPTED, e);
}
}
allocCache.invalidate(allocPage, allocPage.getPageNumber());
}
if (SanityManager.DEBUG) {
if (allocPage == null)
allocCache.dumpAllocationCache();
SanityManager.ASSERT(allocPage != null, "findAllocPageForAdd returned a null alloc page");
}
//
// get the next free page's number.
// for case 1, page number > lastPreallocPage
// for case 2, page number <= lastPage
// for case 3, lastPage < page number <= lastPreallocPage
//
pageNumber = allocPage.nextFreePageNumber(startSearch);
// need to distinguish between the following 3 cases:
// 1) the page has not been allocate or initalized.
// Create it in the page cache and sync it to disk.
// 2) the page is being re-allocated.
// We need to read it in to re-initialize it
// 3) the page has been preallocated.
// Create it in the page cache and don't sync it to disk
//
// first find out the current last initialized page and
// preallocated page before the new page is added
lastPage = allocPage.getLastPagenum();
lastPreallocPage = allocPage.getLastPreallocPagenum();
reuse = pageNumber <= lastPage;
// no address translation necessary
pkey = new PageKey(identity, pageNumber);
if (reuse) {
// if re-useing a page, make sure the deallocLock on the new
// page is not held. We only need a zero duration lock on
// the new page because the allocPage is latched and this
// is the only thread which can be looking at this
// pageNumber.
RecordHandle deallocLock = BasePage.MakeRecordHandle(pkey, RecordHandle.DEALLOCATE_PROTECTION_HANDLE);
if (!getDeallocLock(allocHandle, deallocLock, false, /* nowait */
true)) {
// until we get a brand new page.
if (numtries == 0) {
startSearch = ContainerHandle.INVALID_PAGE_NUMBER;
lastAllocatedPage = pageNumber;
} else
// continue from where we were
startSearch = pageNumber;
numtries++;
// We have to unlatch the allocPage so that if that
// transaction rolls back, it won't deadlock with this
// transaction.
allocPage.unlatch();
allocPage = null;
retry = true;
} else {
// we got the lock, next time start from there
lastAllocatedPage = pageNumber;
}
} else {
// deallocated pages
if (numtries > 0)
lastAllocatedPage = ContainerHandle.INVALID_PAGE_NUMBER;
else
lastAllocatedPage = pageNumber;
}
// Retry from the beginning if necessary.
if (retry)
continue;
// If we get past here must have (retry == false)
if (SanityManager.DEBUG) {
SanityManager.ASSERT(retry == false);
}
if (SanityManager.DEBUG) {
// ASSERT lastPage <= lastPreallocPage
if (lastPage > lastPreallocPage) {
SanityManager.THROWASSERT("last page " + lastPage + " > lastPreallocPage " + lastPreallocPage);
}
}
// No I/O at all if this new page is requested as part of a
// create and load statement or this new page is in a temporary
// container.
//
// In the former case, BaseContainer will allow the
// MODE_UNLOGGED bit to go thru to the nested top transaction
// alloc handle. In the later case, there is no nested top
// transaction and the alloc handle is the user handle, which
// is UNLOGGED.
boolean noIO = (allocHandle.getMode() & ContainerHandle.MODE_UNLOGGED) == ContainerHandle.MODE_UNLOGGED;
// or in a create container.
if (!noIO && (bulkIncreaseContainerSize || (pageNumber > lastPreallocPage && pageNumber > PreAllocThreshold))) {
allocPage.preAllocatePage(this, PreAllocThreshold, PreAllocSize);
}
// update last preAllocated Page, it may have been changed by
// the preAllocatePage call. We don't want to do the sync if
// preAllocatePage already took care of it.
lastPreallocPage = allocPage.getLastPreallocPagenum();
boolean prealloced = pageNumber <= lastPreallocPage;
// Argument to the create is an array of ints.
// The array is only used for new page creation or for creating
// a preallocated page, not for reuse.
// 0'th element is the page format
// 1'st element is whether or not to sync the page to disk
// 2'nd element is pagesize
// 3'rd element is spareSpace
PageCreationArgs createPageArgs = new PageCreationArgs(StoredPage.FORMAT_NUMBER, prealloced ? 0 : (noIO ? 0 : CachedPage.WRITE_SYNC), pageSize, spareSpace, minimumRecordSize, 0);
// RESOLVE: right now, there is no re-mapping of pages, so
// pageOffset = pageNumber*pageSize
long pageOffset = pageNumber * pageSize;
try {
page = initPage(allocHandle, pkey, createPageArgs, pageOffset, reuse, isOverflow);
} catch (StandardException se) {
if (SanityManager.DEBUG) {
SanityManager.DEBUG_PRINT("FileContainer", "got exception from initPage:" + "\nreuse = " + reuse + "\nsyncFlag = " + createPageArgs.syncFlag + "\nallocPage = " + allocPage);
}
allocCache.dumpAllocationCache();
throw se;
}
if (SanityManager.DEBUG) {
SanityManager.ASSERT(page != null, "initPage returns null page");
SanityManager.ASSERT(page.isLatched(), "initPage returns unlatched page");
}
// allocate the page in the allocation page bit map
allocPage.addPage(this, pageNumber, ntt, userHandle);
if (useNTT) {
// transfer the page latch from NTT to UT.
//
// after the page is unlatched by NTT, it is still
// protected from being found by almost everybody else
// because the alloc page is still latched and the alloc
// cache is invalidated.
//
// However it is possible for the page to be
// found by threads who specifically ask for this
// pagenumber (e.g. HeapPostCommit).
// We may find that such a thread has latched the page.
// We shouldn't wait for it because we have the alloc page
// latch, and this could cause deadlock (e.g.
// HeapPostCommit might call removePage and this would wait
// on the alloc page).
//
// We may instead find that we can latch the page, but that
// another thread has managed to get hold of it during the
// transfer and either deallocated it or otherwise change it
// (add rows, delete rows etc.)
//
// Since this doesn't happen very often, we retry in these
// 2 cases (we give up the alloc page and page and we start
// this method from scratch).
//
// If the lock manager were changed to allow latches to be
// transferred between transactions, wouldn't need to
// unlatch to do the transfer, and would avoid having to
// retry in these cases (DERBY-2337).
page.unlatch();
page = null;
// need to find it in the cache again since unlatch also
// unkept the page from the cache
page = (BasePage) pageCache.find(pkey);
page = latchPage(userHandle, page, false);
if (page == null || // rows (including deleted rows)
page.recordCount() != 0 || page.getPageStatus() != BasePage.VALID_PAGE) {
retry = true;
if (page != null) {
page.unlatch();
page = null;
}
allocPage.unlatch();
allocPage = null;
}
}
// if ntt is null, no need to transfer. Page is latched by user
// transaction already. Will be no need to retry.
// the alloc page is unlatched in the finally block.
} while (retry == true);
// At this point, should have a page suitable for returning
if (SanityManager.DEBUG)
SanityManager.ASSERT(page.isLatched());
} catch (StandardException se) {
if (page != null)
page.unlatch();
page = null;
// rethrow error
throw se;
} finally {
if (!useNTT && allocPage != null) {
allocPage.unlatch();
allocPage = null;
}
// NTT is committed by the caller
}
if (SanityManager.DEBUG)
SanityManager.ASSERT(page.isLatched());
// at a time in the future.
if (bulkIncreaseContainerSize) {
bulkIncreaseContainerSize = false;
PreAllocSize = DEFAULT_PRE_ALLOC_SIZE;
}
if (!isOverflow && page != null)
setLastInsertedPage(pageNumber);
// logging, this is an estimate only
if (estimatedPageCount >= 0)
estimatedPageCount++;
if (!this.identity.equals(page.getPageId().getContainerId())) {
if (SanityManager.DEBUG) {
SanityManager.THROWASSERT("just created a new page from a different container" + "\n this.identity = " + this.identity + "\n page.getPageId().getContainerId() = " + page.getPageId().getContainerId() + "\n userHandle is: " + userHandle + "\n allocHandle is: " + allocHandle + "\n this container is: " + this);
}
throw StandardException.newException(SQLState.DATA_DIFFERENT_CONTAINER, this.identity, page.getPageId().getContainerId());
}
// return the newly added page
return page;
}
use of org.apache.derby.iapi.store.raw.PageKey in project derby by apache.
the class FileContainer method getAllocPage.
/**
* Get an alloc page - only accessible to the raw store
* (container and recovery)
*
* @exception StandardException Derby Standard error policy
*/
protected BasePage getAllocPage(long pageNumber) throws StandardException {
if (// committed and dropped, cannot get a page
getCommittedDropState())
return null;
PageKey pageSearch = new PageKey(identity, pageNumber);
BasePage page = (BasePage) pageCache.find(pageSearch);
if (SanityManager.DEBUG) {
if (page == null)
SanityManager.THROWASSERT("getting a null alloc page page " + getIdentity() + pageNumber);
if (!(page instanceof AllocPage))
SanityManager.THROWASSERT("trying to get a user page as an alloc page " + getIdentity() + pageNumber);
}
// assuming that allocation page lives in the page cache...
return page;
}
use of org.apache.derby.iapi.store.raw.PageKey in project derby by apache.
the class D_RecordId method diag_detail.
/**
* Return a set of properties describing the the key used to lock container.
* <p>
* Used by debugging code to print the lock table on demand.
*
* @exception StandardException Standard exception policy.
*/
public void diag_detail(Properties prop) throws StandardException {
RecordId record_id = (RecordId) diag_object;
PageKey page_key = (PageKey) record_id.getPageId();
prop.put(RowLock.DIAG_CONTAINERID, Long.toString(page_key.getContainerId().getContainerId()));
prop.put(RowLock.DIAG_SEGMENTID, Long.toString(page_key.getContainerId().getSegmentId()));
prop.put(RowLock.DIAG_PAGENUM, Long.toString(record_id.getPageNumber()));
prop.put(RowLock.DIAG_RECID, Integer.toString(record_id.getId()));
return;
}
Aggregations