use of org.apache.ignite.internal.processors.cache.persistence.freelist.io.PagesListNodeIO in project ignite by apache.
the class PagesList method merge.
/**
* @param pageId Page ID.
* @param page Page pointer.
* @param nextId Next page ID.
* @param bucket Bucket index.
* @return Page ID to recycle.
* @throws IgniteCheckedException If failed.
*/
private long merge(final long pageId, final long page, long nextId, int bucket) throws IgniteCheckedException {
// We should do mergeNoNext then.
assert nextId != 0;
// Lock all the pages in correct order (from next to previous) and do the merge in retry loop.
for (; ; ) {
final long curId = nextId;
final long curPage = curId == 0L ? 0L : acquirePage(curId);
try {
boolean write = false;
// Explicit check.
final long curAddr = curPage == 0L ? 0L : writeLock(curId, curPage);
// Explicit check.
final long pageAddr = writeLock(pageId, page);
if (pageAddr == 0L) {
if (// Unlock next page if needed.
curAddr != 0L)
writeUnlock(curId, curPage, curAddr, false);
// Someone has merged or taken our empty page concurrently. Nothing to do here.
return 0L;
}
try {
PagesListNodeIO io = PagesListNodeIO.VERSIONS.forPage(pageAddr);
if (!io.isEmpty(pageAddr))
// No need to merge anymore.
return 0L;
// Check if we see a consistent state of the world.
if (io.getNextId(pageAddr) == curId && (curId == 0L) == (curAddr == 0L)) {
long recycleId = doMerge(pageId, page, pageAddr, io, curId, curPage, curAddr, bucket);
write = true;
// Done.
return recycleId;
}
// Reread next page ID and go for retry.
nextId = io.getNextId(pageAddr);
} finally {
if (curAddr != 0L)
writeUnlock(curId, curPage, curAddr, write);
writeUnlock(pageId, page, pageAddr, write);
}
} finally {
if (curPage != 0L)
releasePage(curId, curPage);
}
}
}
use of org.apache.ignite.internal.processors.cache.persistence.freelist.io.PagesListNodeIO in project ignite by apache.
the class PagesList method takeEmptyPage.
/**
* @param bucket Bucket index.
* @param initIoVers Optional IO to initialize page.
* @return Removed page ID.
* @throws IgniteCheckedException If failed.
*/
protected final long takeEmptyPage(int bucket, @Nullable IOVersions initIoVers) throws IgniteCheckedException {
for (int lockAttempt = 0; ; ) {
Stripe stripe = getPageForTake(bucket);
if (stripe == null)
return 0L;
final long tailId = stripe.tailId;
final long tailPage = acquirePage(tailId);
try {
// Explicit check.
long tailAddr = writeLockPage(tailId, tailPage, bucket, lockAttempt++, null);
if (tailAddr == 0L)
continue;
if (stripe.empty) {
// Another thread took the last page.
writeUnlock(tailId, tailPage, tailAddr, false);
if (bucketsSize[bucket].get() > 0) {
// Ignore current attempt.
lockAttempt--;
continue;
} else
return 0L;
}
assert PageIO.getPageId(tailAddr) == tailId : "tailId = " + tailId + ", tailPageId = " + PageIO.getPageId(tailAddr);
assert PageIO.getType(tailAddr) == PageIO.T_PAGE_LIST_NODE;
boolean dirty = false;
long dataPageId;
long recycleId = 0L;
try {
PagesListNodeIO io = PagesListNodeIO.VERSIONS.forPage(tailAddr);
if (io.getNextId(tailAddr) != 0) {
// It is not a tail anymore, retry.
continue;
}
long pageId = io.takeAnyPage(tailAddr);
if (pageId != 0L) {
decrementBucketSize(bucket);
if (needWalDeltaRecord(tailId, tailPage, null))
wal.log(new PagesListRemovePageRecord(grpId, tailId, pageId));
dirty = true;
dataPageId = pageId;
if (io.isEmpty(tailAddr)) {
long prevId = io.getPreviousId(tailAddr);
// to prevent empty page leak to data pages.
if (!isReuseBucket(bucket)) {
if (prevId != 0L) {
Boolean ok = write(prevId, cutTail, null, bucket, FALSE);
assert ok == TRUE : ok;
recycleId = recyclePage(tailId, tailPage, tailAddr, null);
} else
stripe.empty = true;
} else
stripe.empty = prevId == 0L;
}
} else {
// a previous page, so, the current page can be collected
assert isReuseBucket(bucket);
long prevId = io.getPreviousId(tailAddr);
assert prevId != 0L;
Boolean ok = write(prevId, cutTail, bucket, FALSE);
assert ok == TRUE : ok;
decrementBucketSize(bucket);
if (initIoVers != null) {
dataPageId = PageIdUtils.changeType(tailId, FLAG_DATA);
PageIO initIo = initIoVers.latest();
initIo.initNewPage(tailAddr, dataPageId, pageSize());
if (needWalDeltaRecord(tailId, tailPage, null)) {
wal.log(new InitNewPageRecord(grpId, tailId, initIo.getType(), initIo.getVersion(), dataPageId));
}
} else
dataPageId = recyclePage(tailId, tailPage, tailAddr, null);
dirty = true;
}
// If we do not have a previous page (we are at head), then we still can return
// current page but we have to drop the whole stripe. Since it is a reuse bucket,
// we will not do that, but just return 0L, because this may produce contention on
// meta page.
} finally {
writeUnlock(tailId, tailPage, tailAddr, dirty);
}
// Put recycled page (if any) to the reuse bucket after tail is unlocked.
if (recycleId != 0L) {
assert !isReuseBucket(bucket);
reuseList.addForRecycle(new SingletonReuseBag(recycleId));
}
return dataPageId;
} finally {
releasePage(tailId, tailPage);
}
}
}
use of org.apache.ignite.internal.processors.cache.persistence.freelist.io.PagesListNodeIO in project ignite by apache.
the class PagesListAddPageRecord method applyDelta.
/**
* {@inheritDoc}
*/
@Override
public void applyDelta(PageMemory pageMem, long pageAddr) throws IgniteCheckedException {
PagesListNodeIO io = PagesListNodeIO.VERSIONS.forPage(pageAddr);
int cnt = io.addPage(pageAddr, dataPageId, pageMem.pageSize());
assert cnt >= 0 : cnt;
}
use of org.apache.ignite.internal.processors.cache.persistence.freelist.io.PagesListNodeIO in project ignite by apache.
the class PagesListRemovePageRecord method applyDelta.
/**
* {@inheritDoc}
*/
@Override
public void applyDelta(PageMemory pageMem, long pageAddr) throws IgniteCheckedException {
PagesListNodeIO io = PagesListNodeIO.VERSIONS.forPage(pageAddr);
boolean rmvd = io.removePage(pageAddr, rmvdPageId);
assert rmvd;
}
use of org.apache.ignite.internal.processors.cache.persistence.freelist.io.PagesListNodeIO in project ignite by apache.
the class PagesListSetPreviousRecord method applyDelta.
/**
* {@inheritDoc}
*/
@Override
public void applyDelta(PageMemory pageMem, long pageAddr) throws IgniteCheckedException {
PagesListNodeIO io = PagesListNodeIO.VERSIONS.forPage(pageAddr);
io.setPreviousId(pageAddr, prevPageId);
}
Aggregations