Search in sources :

Example 16 with ICachedPage

use of org.apache.hyracks.storage.common.buffercache.ICachedPage in project asterixdb by apache.

the class LinkedMetaDataPageManager method releasePage.

@Override
public void releasePage(ITreeIndexMetadataFrame metaFrame, int freePageNum) throws HyracksDataException {
    // Get the metadata node
    ICachedPage metaPage = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, getMetadataPageId()), false);
    metaPage.acquireWriteLatch();
    try {
        metaFrame.setPage(metaPage);
        if (metaFrame.getSpace() > Integer.BYTES) {
            metaFrame.addFreePage(freePageNum);
        } else {
            // allocate a new page in the chain of meta pages
            int newPageNum = metaFrame.getFreePage();
            if (newPageNum < 0) {
                throw new HyracksDataException("Inconsistent Meta Page State. It has no space, but it also has no entries.");
            }
            ICachedPage newNode = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, newPageNum), false);
            newNode.acquireWriteLatch();
            try {
                int metaMaxPage = metaFrame.getMaxPage();
                // copy metaDataPage to newNode
                System.arraycopy(metaPage.getBuffer().array(), 0, newNode.getBuffer().array(), 0, metaPage.getBuffer().capacity());
                metaFrame.init();
                metaFrame.setNextMetadataPage(newPageNum);
                metaFrame.setMaxPage(metaMaxPage);
                metaFrame.addFreePage(freePageNum);
            } finally {
                newNode.releaseWriteLatch(true);
                bufferCache.unpin(newNode);
            }
        }
    } finally {
        metaPage.releaseWriteLatch(true);
        bufferCache.unpin(metaPage);
    }
}
Also used : ICachedPage(org.apache.hyracks.storage.common.buffercache.ICachedPage) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 17 with ICachedPage

use of org.apache.hyracks.storage.common.buffercache.ICachedPage in project asterixdb by apache.

the class LinkedMetaDataPageManager method getFileOffset.

@Override
public long getFileOffset(ITreeIndexMetadataFrame frame, IValueReference key) throws HyracksDataException {
    int metadataPageNum = getMetadataPageId();
    if (metadataPageNum != IBufferCache.INVALID_PAGEID) {
        ICachedPage metaNode = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, getMetadataPageId()), false);
        metaNode.acquireReadLatch();
        try {
            frame.setPage(metaNode);
            return ((long) metadataPageNum * bufferCache.getPageSizeWithHeader()) + frame.getOffset(key);
        } finally {
            metaNode.releaseReadLatch();
            bufferCache.unpin(metaNode);
        }
    }
    return -1;
}
Also used : ICachedPage(org.apache.hyracks.storage.common.buffercache.ICachedPage)

Example 18 with ICachedPage

use of org.apache.hyracks.storage.common.buffercache.ICachedPage in project asterixdb by apache.

the class LinkedMetaDataPageManager method getRootPageId.

@Override
public int getRootPageId() throws HyracksDataException {
    ICachedPage metaNode;
    metaNode = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, getMetadataPageId()), false);
    ITreeIndexMetadataFrame metaFrame = frameFactory.createFrame();
    metaNode.acquireReadLatch();
    try {
        metaFrame.setPage(metaNode);
        return metaFrame.getRootPageId();
    } finally {
        metaNode.releaseReadLatch();
        bufferCache.unpin(metaNode);
    }
}
Also used : ICachedPage(org.apache.hyracks.storage.common.buffercache.ICachedPage) ITreeIndexMetadataFrame(org.apache.hyracks.storage.am.common.api.ITreeIndexMetadataFrame)

Example 19 with ICachedPage

use of org.apache.hyracks.storage.common.buffercache.ICachedPage in project asterixdb by apache.

the class LinkedMetaDataPageManager method takePage.

@Override
public int takePage(ITreeIndexMetadataFrame metaFrame) throws HyracksDataException {
    ICachedPage metaNode = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, getMetadataPageId()), false);
    metaNode.acquireWriteLatch();
    int freePage = IBufferCache.INVALID_PAGEID;
    try {
        metaFrame.setPage(metaNode);
        freePage = metaFrame.getFreePage();
        if (freePage < 0) {
            // no free page entry on this page
            int nextPage = metaFrame.getNextMetadataPage();
            if (nextPage > 0) {
                // sibling may have free pages
                ICachedPage nextNode = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, nextPage), false);
                nextNode.acquireWriteLatch();
                // of nextpage
                try {
                    // remember entries that remain unchanged
                    int maxPage = metaFrame.getMaxPage();
                    // copy entire page (including sibling pointer, free
                    // page entries, and all other info)
                    // after this copy nextPage is considered a free page
                    System.arraycopy(nextNode.getBuffer().array(), 0, metaNode.getBuffer().array(), 0, nextNode.getBuffer().capacity());
                    // reset unchanged entry
                    metaFrame.setMaxPage(maxPage);
                    freePage = metaFrame.getFreePage();
                    // happen, but we deal with it anyway just to be safe
                    if (freePage < 0) {
                        freePage = nextPage;
                    } else {
                        metaFrame.addFreePage(nextPage);
                    }
                } finally {
                    nextNode.releaseWriteLatch(true);
                    bufferCache.unpin(nextNode);
                }
            } else {
                freePage = metaFrame.getMaxPage();
                freePage++;
                metaFrame.setMaxPage(freePage);
            }
        }
    } finally {
        metaNode.releaseWriteLatch(true);
        bufferCache.unpin(metaNode);
    }
    return freePage;
}
Also used : ICachedPage(org.apache.hyracks.storage.common.buffercache.ICachedPage)

Example 20 with ICachedPage

use of org.apache.hyracks.storage.common.buffercache.ICachedPage in project asterixdb by apache.

the class BufferCacheTest method simpleOpenPinCloseTest.

@Test
public void simpleOpenPinCloseTest() throws HyracksException {
    TestStorageManagerComponentHolder.init(PAGE_SIZE, NUM_PAGES, MAX_OPEN_FILES);
    IBufferCache bufferCache = TestStorageManagerComponentHolder.getBufferCache(ctx.getJobletContext().getServiceContext());
    IFileMapProvider fmp = TestStorageManagerComponentHolder.getFileMapProvider();
    IIOManager ioManager = TestStorageManagerComponentHolder.getIOManager();
    String fileName = getFileName();
    FileReference file = ioManager.resolve(fileName);
    bufferCache.createFile(file);
    int fileId = fmp.lookupFileId(file);
    int num = 10;
    int testPageId = 0;
    bufferCache.openFile(fileId);
    ICachedPage page = null;
    // tryPin should fail
    page = bufferCache.tryPin(BufferedFileHandle.getDiskPageId(fileId, testPageId));
    Assert.assertNull(page);
    // pin page should succeed
    page = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, testPageId), true);
    page.acquireWriteLatch();
    try {
        for (int i = 0; i < num; i++) {
            page.getBuffer().putInt(i * 4, i);
        }
        // try pin should succeed
        ICachedPage page2 = bufferCache.tryPin(BufferedFileHandle.getDiskPageId(fileId, testPageId));
        Assert.assertNotNull(page2);
        bufferCache.unpin(page2);
    } finally {
        page.releaseWriteLatch(true);
        bufferCache.unpin(page);
    }
    bufferCache.closeFile(fileId);
    // This code is commented because the method pinSanityCheck in the BufferCache is commented.
    /*boolean exceptionThrown = false;

        // tryPin should fail since file is not open
        try {
            page = bufferCache.tryPin(BufferedFileHandle.getDiskPageId(fileId, testPageId));
        } catch (HyracksDataException e) {
            exceptionThrown = true;
        }
        Assert.assertTrue(exceptionThrown);

        // pin should fail since file is not open
        exceptionThrown = false;
        try {
            page = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, testPageId), false);
        } catch (HyracksDataException e) {
            exceptionThrown = true;
        }
        Assert.assertTrue(exceptionThrown);*/
    // open file again
    bufferCache.openFile(fileId);
    // tryPin should succeed because page should still be cached
    page = bufferCache.tryPin(BufferedFileHandle.getDiskPageId(fileId, testPageId));
    Assert.assertNotNull(page);
    page.acquireReadLatch();
    try {
        // verify contents of page
        for (int i = 0; i < num; i++) {
            Assert.assertEquals(page.getBuffer().getInt(i * 4), i);
        }
    } finally {
        page.releaseReadLatch();
        bufferCache.unpin(page);
    }
    bufferCache.closeFile(fileId);
    bufferCache.close();
}
Also used : IFileMapProvider(org.apache.hyracks.storage.common.file.IFileMapProvider) ICachedPage(org.apache.hyracks.storage.common.buffercache.ICachedPage) FileReference(org.apache.hyracks.api.io.FileReference) IIOManager(org.apache.hyracks.api.io.IIOManager) IBufferCache(org.apache.hyracks.storage.common.buffercache.IBufferCache) Test(org.junit.Test)

Aggregations

ICachedPage (org.apache.hyracks.storage.common.buffercache.ICachedPage)51 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)13 ITreeIndexMetadataFrame (org.apache.hyracks.storage.am.common.api.ITreeIndexMetadataFrame)7 IBufferCache (org.apache.hyracks.storage.common.buffercache.IBufferCache)5 IFileMapProvider (org.apache.hyracks.storage.common.file.IFileMapProvider)4 FileReference (org.apache.hyracks.api.io.FileReference)3 Test (org.junit.Test)3 ByteBuffer (java.nio.ByteBuffer)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IIOManager (org.apache.hyracks.api.io.IIOManager)2 ITupleReference (org.apache.hyracks.dataflow.common.data.accessors.ITupleReference)2 BTreeNSMInteriorFrame (org.apache.hyracks.storage.am.btree.frames.BTreeNSMInteriorFrame)2 ITreeIndexFrame (org.apache.hyracks.storage.am.common.api.ITreeIndexFrame)2 FrameOpSpaceStatus (org.apache.hyracks.storage.am.common.frames.FrameOpSpaceStatus)2 TreeIndexDiskOrderScanCursor (org.apache.hyracks.storage.am.common.impls.TreeIndexDiskOrderScanCursor)2 MultiComparator (org.apache.hyracks.storage.common.MultiComparator)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)1