Search in sources :

Example 11 with OModifiableBoolean

use of com.orientechnologies.common.types.OModifiableBoolean in project orientdb by orientechnologies.

the class O2QCache method doLoad.

private UpdateCacheResult doLoad(long fileId, long pageIndex, boolean checkPinnedPages, boolean addNewPages, OWriteCache writeCache, final int pageCount, final OSessionStoragePerformanceStatistic sessionStoragePerformanceStatistic) throws IOException {
    if (pageCount < 1)
        throw new IllegalArgumentException("Amount of pages to load from cache should be not less than 1 but passed value is " + pageCount);
    boolean removeColdPages = false;
    OCacheEntry cacheEntry = null;
    Lock fileLock;
    Lock[] pageLocks;
    final OModifiableBoolean cacheHit = new OModifiableBoolean(false);
    cacheLock.acquireReadLock();
    try {
        fileLock = fileLockManager.acquireSharedLock(fileId);
        try {
            final PageKey[] pageKeys = new PageKey[pageCount];
            for (int i = 0; i < pageKeys.length; i++) {
                pageKeys[i] = new PageKey(fileId, pageIndex + i);
            }
            pageLocks = pageLockManager.acquireExclusiveLocksInBatch(pageKeys);
            try {
                if (checkPinnedPages)
                    cacheEntry = pinnedPages.get(new PinnedPage(fileId, pageIndex));
                if (cacheEntry == null) {
                    UpdateCacheResult cacheResult = updateCache(fileId, pageIndex, addNewPages, writeCache, pageCount, cacheHit);
                    if (cacheResult == null)
                        return null;
                    cacheEntry = cacheResult.cacheEntry;
                    removeColdPages = cacheResult.removeColdPages;
                } else {
                    cacheHit.setValue(true);
                }
                cacheEntry.incrementUsages();
            } finally {
                for (Lock pageLock : pageLocks) {
                    pageLock.unlock();
                }
            }
        } finally {
            fileLockManager.releaseSharedLock(fileId);
        }
    } finally {
        cacheLock.releaseReadLock();
    }
    if (sessionStoragePerformanceStatistic != null)
        sessionStoragePerformanceStatistic.incrementPageAccessOnCacheLevel(cacheHit.getValue());
    return new UpdateCacheResult(removeColdPages, cacheEntry);
}
Also used : OModifiableBoolean(com.orientechnologies.common.types.OModifiableBoolean) Lock(java.util.concurrent.locks.Lock)

Example 12 with OModifiableBoolean

use of com.orientechnologies.common.types.OModifiableBoolean in project orientdb by orientechnologies.

the class O2QCache method restoreQueueWithoutPageLoad.

/**
   * Restores queues state if it is NOT needed to load cache page from disk to cache.
   * <p>
   * Following format is used to store queue state:
   * <p>
   * <ol>
   * <li>File id or -1 if end of queue is reached (int)</li>
   * <li>Page index (long), is absent if end of the queue is reached</li>
   * </ol>
   *
   * @param dataInputStream Stream of file which contains state of the cache.
   * @param queue           Queue, state of which should be restored.
   * @param writeCache      Write cache is used to load data from disk if needed.
   */
private void restoreQueueWithoutPageLoad(OWriteCache writeCache, LRUList queue, DataInputStream dataInputStream) throws IOException {
    // used only for statistics, and there is passed merely as stub
    final OModifiableBoolean cacheHit = new OModifiableBoolean();
    int internalFileId = dataInputStream.readInt();
    while (internalFileId >= 0) {
        final long pageIndex = dataInputStream.readLong();
        try {
            final long fileId = writeCache.externalFileId(internalFileId);
            final OCacheEntry cacheEntry = new OCacheEntry(fileId, pageIndex, null, false);
            Set<Long> pages = filePages.get(fileId);
            if (pages == null) {
                pages = new HashSet<Long>();
                Set<Long> op = filePages.putIfAbsent(fileId, pages);
                if (op != null) {
                    pages = op;
                }
            }
            queue.putToMRU(cacheEntry);
            pages.add(cacheEntry.getPageIndex());
        } finally {
            internalFileId = dataInputStream.readInt();
        }
    }
}
Also used : OModifiableBoolean(com.orientechnologies.common.types.OModifiableBoolean)

Example 13 with OModifiableBoolean

use of com.orientechnologies.common.types.OModifiableBoolean in project orientdb by orientechnologies.

the class O2QCache method restoreQueueWithPageLoad.

/**
   * Restores queues state if it is needed to load cache page from disk to cache.
   * <p>
   * Following format is used to store queue state:
   * <p>
   * <ol>
   * <li>File id or -1 if end of queue is reached (int)</li>
   * <li>Page index (long), is absent if end of the queue is reached</li>
   * </ol>
   *
   * @param dataInputStream Stream of file which contains state of the cache.
   * @param queue           Queue, state of which should be restored.
   * @param writeCache      Write cache is used to load data from disk if needed.
   */
private void restoreQueueWithPageLoad(OWriteCache writeCache, LRUList queue, DataInputStream dataInputStream) throws IOException {
    // used only for statistics, and there is passed merely as stub
    final OModifiableBoolean cacheHit = new OModifiableBoolean();
    // first step, we will create two tree maps to sort data by position in file and load them with maximum speed and
    // then to put data into the queue to restore position of entries in LRU list.
    final TreeMap<PageKey, OPair<Long, OCacheEntry>> filePositionMap = new TreeMap<PageKey, OPair<Long, OCacheEntry>>();
    final TreeMap<Long, OCacheEntry> queuePositionMap = new TreeMap<Long, OCacheEntry>();
    long position = 0;
    int internalFileId = dataInputStream.readInt();
    while (internalFileId >= 0) {
        final long pageIndex = dataInputStream.readLong();
        try {
            final long fileId = writeCache.externalFileId(internalFileId);
            final OCacheEntry entry = new OCacheEntry(fileId, pageIndex, null, false);
            filePositionMap.put(new PageKey(fileId, pageIndex), new OPair<Long, OCacheEntry>(position, entry));
            queuePositionMap.put(position, entry);
            position++;
        } finally {
            internalFileId = dataInputStream.readInt();
        }
    }
    // second step: load pages sorted by position in file
    for (final Map.Entry<PageKey, OPair<Long, OCacheEntry>> entry : filePositionMap.entrySet()) {
        final PageKey pageKey = entry.getKey();
        final OPair<Long, OCacheEntry> pair = entry.getValue();
        final OCachePointer[] pointers = writeCache.load(pageKey.fileId, pageKey.pageIndex, 1, false, cacheHit);
        if (pointers.length == 0) {
            queuePositionMap.remove(pair.key);
            continue;
        }
        final OCacheEntry cacheEntry = pair.value;
        cacheEntry.setCachePointer(pointers[0]);
    }
    // third step: add pages according to their order in LRU queue
    for (final OCacheEntry cacheEntry : queuePositionMap.values()) {
        final long fileId = cacheEntry.getFileId();
        Set<Long> pages = filePages.get(fileId);
        if (pages == null) {
            pages = new HashSet<Long>();
            Set<Long> op = filePages.putIfAbsent(fileId, pages);
            if (op != null) {
                pages = op;
            }
        }
        queue.putToMRU(cacheEntry);
        pages.add(cacheEntry.getPageIndex());
    }
}
Also used : OPair(com.orientechnologies.common.util.OPair) OModifiableBoolean(com.orientechnologies.common.types.OModifiableBoolean) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 14 with OModifiableBoolean

use of com.orientechnologies.common.types.OModifiableBoolean in project orientdb by orientechnologies.

the class WOWCacheTest method testFlushAllContentEventually.

public void testFlushAllContentEventually() throws Exception {
    Random random = new Random();
    byte[][] pageData = new byte[200][];
    long fileId = wowCache.addFile(fileName);
    for (int i = 0; i < pageData.length; i++) {
        byte[] data = new byte[8];
        random.nextBytes(data);
        pageData[i] = data;
        final OCachePointer cachePointer = wowCache.load(fileId, i, 1, true, new OModifiableBoolean())[0];
        cachePointer.acquireExclusiveLock();
        ByteBuffer buffer = cachePointer.getSharedBuffer();
        buffer.position(systemOffset);
        buffer.put(data);
        cachePointer.releaseExclusiveLock();
        wowCache.store(fileId, i, cachePointer);
        cachePointer.decrementReadersReferrer();
    }
    for (int i = 0; i < pageData.length; i++) {
        byte[] dataOne = pageData[i];
        OCachePointer cachePointer = wowCache.load(fileId, i, 1, false, new OModifiableBoolean())[0];
        byte[] dataTwo = new byte[8];
        ByteBuffer buffer = cachePointer.getSharedBuffer();
        buffer.position(systemOffset);
        buffer.get(dataTwo);
        cachePointer.decrementReadersReferrer();
        Assert.assertEquals(dataTwo, dataOne);
    }
    final long start = System.currentTimeMillis();
    while (wowCache.getWriteCacheSize() != 0) {
        Thread.sleep(1000);
        //wait no more than 10 min
        if (((System.currentTimeMillis() - start) / 1000) > 10 * 60) {
            Assert.assertEquals(wowCache.getWriteCacheSize(), 0);
        }
    }
    for (int i = 0; i < pageData.length; i++) {
        byte[] dataContent = pageData[i];
        assertFile(i, dataContent, new OLogSequenceNumber(0, 0));
    }
}
Also used : OLogSequenceNumber(com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber) OCachePointer(com.orientechnologies.orient.core.storage.cache.OCachePointer) OModifiableBoolean(com.orientechnologies.common.types.OModifiableBoolean) ByteBuffer(java.nio.ByteBuffer)

Example 15 with OModifiableBoolean

use of com.orientechnologies.common.types.OModifiableBoolean in project orientdb by orientechnologies.

the class WOWCacheTest method testLoadStore.

public void testLoadStore() throws IOException {
    Random random = new Random();
    byte[][] pageData = new byte[200][];
    long fileId = wowCache.addFile(fileName);
    for (int i = 0; i < pageData.length; i++) {
        byte[] data = new byte[8];
        random.nextBytes(data);
        pageData[i] = data;
        final OCachePointer cachePointer = wowCache.load(fileId, i, 1, true, new OModifiableBoolean())[0];
        cachePointer.acquireExclusiveLock();
        ByteBuffer buffer = cachePointer.getSharedBuffer();
        buffer.position(systemOffset);
        buffer.put(data);
        cachePointer.releaseExclusiveLock();
        wowCache.store(fileId, i, cachePointer);
        cachePointer.decrementReadersReferrer();
    }
    for (int i = 0; i < pageData.length; i++) {
        byte[] dataOne = pageData[i];
        OCachePointer cachePointer = wowCache.load(fileId, i, 1, false, new OModifiableBoolean())[0];
        byte[] dataTwo = new byte[8];
        ByteBuffer buffer = cachePointer.getSharedBuffer();
        buffer.position(systemOffset);
        buffer.get(dataTwo);
        cachePointer.decrementReadersReferrer();
        Assert.assertEquals(dataTwo, dataOne);
    }
    wowCache.flush();
    for (int i = 0; i < pageData.length; i++) {
        byte[] dataContent = pageData[i];
        assertFile(i, dataContent, new OLogSequenceNumber(0, 0));
    }
}
Also used : OLogSequenceNumber(com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber) OCachePointer(com.orientechnologies.orient.core.storage.cache.OCachePointer) OModifiableBoolean(com.orientechnologies.common.types.OModifiableBoolean) ByteBuffer(java.nio.ByteBuffer)

Aggregations

OModifiableBoolean (com.orientechnologies.common.types.OModifiableBoolean)15 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)4 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)3 OCachePointer (com.orientechnologies.orient.core.storage.cache.OCachePointer)3 OLogSequenceNumber (com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber)3 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)3 ByteBuffer (java.nio.ByteBuffer)3 ODatabase (com.orientechnologies.orient.core.db.ODatabase)2 OInvalidIndexEngineIdException (com.orientechnologies.orient.core.exception.OInvalidIndexEngineIdException)2 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)2 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)2 ArrayList (java.util.ArrayList)2 OPair (com.orientechnologies.common.util.OPair)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 ORID (com.orientechnologies.orient.core.id.ORID)1 ORecordId (com.orientechnologies.orient.core.id.ORecordId)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 OCommandParameters (com.orientechnologies.orient.core.sql.OCommandParameters)1