Search in sources :

Example 96 with OLogSequenceNumber

use of com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber in project orientdb by orientechnologies.

the class OWOWCache method flushPage.

private void flushPage(final int fileId, final long pageIndex, final ByteBuffer buffer) throws IOException, InterruptedException {
    if (writeAheadLog != null) {
        final OLogSequenceNumber lsn = ODurablePage.getLogSequenceNumberFromPage(buffer);
        final OLogSequenceNumber flushedLSN = writeAheadLog.getFlushedLsn();
        if (flushedLSN == null || flushedLSN.compareTo(lsn) < 0)
            writeAheadLog.flush();
    }
    final byte[] content = new byte[pageSize];
    buffer.position(0);
    buffer.get(content);
    OLongSerializer.INSTANCE.serializeNative(checksumMode == OChecksumMode.Off ? MAGIC_NUMBER_WITHOUT_CHECKSUM : MAGIC_NUMBER_WITH_CHECKSUM, content, MAGIC_NUMBER_OFFSET);
    if (checksumMode != OChecksumMode.Off) {
        final int crc32 = calculatePageCrc(content);
        OIntegerSerializer.INSTANCE.serializeNative(crc32, content, CHECKSUM_OFFSET);
    }
    final long externalId = composeFileId(id, fileId);
    final OClosableEntry<Long, OFileClassic> entry = files.acquire(externalId);
    try {
        final OFileClassic fileClassic = entry.get();
        fileClassic.write(pageIndex * pageSize, content);
        if (syncOnPageFlush)
            fileClassic.synch();
    } finally {
        files.release(entry);
    }
}
Also used : OLogSequenceNumber(com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber) AtomicLong(java.util.concurrent.atomic.AtomicLong) OFileClassic(com.orientechnologies.orient.core.storage.fs.OFileClassic)

Example 97 with OLogSequenceNumber

use of com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber in project orientdb by orientechnologies.

the class OWOWCache method loadFileContent.

private OCachePointer[] loadFileContent(final int intId, final long startPageIndex, final int pageCount, boolean verifyChecksums) throws IOException {
    final long fileId = composeFileId(id, intId);
    try {
        final OClosableEntry<Long, OFileClassic> entry = files.acquire(fileId);
        try {
            final OFileClassic fileClassic = entry.get();
            if (fileClassic == null)
                throw new IllegalArgumentException("File with id " + intId + " not found in WOW Cache");
            final long firstPageStartPosition = startPageIndex * pageSize;
            final long firstPageEndPosition = firstPageStartPosition + pageSize;
            if (fileClassic.getFileSize() >= firstPageEndPosition) {
                final OSessionStoragePerformanceStatistic sessionStoragePerformanceStatistic = performanceStatisticManager.getSessionPerformanceStatistic();
                if (sessionStoragePerformanceStatistic != null) {
                    sessionStoragePerformanceStatistic.startPageReadFromFileTimer();
                }
                int pagesRead = 0;
                final OLogSequenceNumber lastLsn = writeAheadLog == null ? new OLogSequenceNumber(-1, -1) : writeAheadLog.getFlushedLsn();
                try {
                    if (pageCount == 1) {
                        final ByteBuffer buffer = bufferPool.acquireDirect(false);
                        assert buffer.position() == 0;
                        fileClassic.read(firstPageStartPosition, buffer, false);
                        if (verifyChecksums && (checksumMode == OChecksumMode.StoreAndVerify || checksumMode == OChecksumMode.StoreAndThrow))
                            verifyChecksum(buffer, fileId, startPageIndex, null);
                        buffer.position(0);
                        final OCachePointer dataPointer = new OCachePointer(buffer, bufferPool, lastLsn, fileId, startPageIndex);
                        pagesRead = 1;
                        return new OCachePointer[] { dataPointer };
                    }
                    final long maxPageCount = (fileClassic.getFileSize() - firstPageStartPosition) / pageSize;
                    final int realPageCount = Math.min((int) maxPageCount, pageCount);
                    final ByteBuffer[] buffers = new ByteBuffer[realPageCount];
                    for (int i = 0; i < buffers.length; i++) {
                        buffers[i] = bufferPool.acquireDirect(false);
                        assert buffers[i].position() == 0;
                    }
                    fileClassic.read(firstPageStartPosition, buffers, false);
                    if (verifyChecksums && (checksumMode == OChecksumMode.StoreAndVerify || checksumMode == OChecksumMode.StoreAndThrow))
                        for (int i = 0; i < buffers.length; ++i) verifyChecksum(buffers[i], fileId, startPageIndex + i, buffers);
                    final OCachePointer[] dataPointers = new OCachePointer[buffers.length];
                    for (int n = 0; n < buffers.length; n++) {
                        buffers[n].position(0);
                        dataPointers[n] = new OCachePointer(buffers[n], bufferPool, lastLsn, fileId, startPageIndex + n);
                    }
                    pagesRead = dataPointers.length;
                    return dataPointers;
                } finally {
                    if (sessionStoragePerformanceStatistic != null) {
                        sessionStoragePerformanceStatistic.stopPageReadFromFileTimer(pagesRead);
                    }
                }
            } else
                return null;
        } finally {
            files.release(entry);
        }
    } catch (InterruptedException e) {
        throw OException.wrapException(new OStorageException("Data load was interrupted"), e);
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) OFileClassic(com.orientechnologies.orient.core.storage.fs.OFileClassic) OLogSequenceNumber(com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber) OStorageException(com.orientechnologies.orient.core.exception.OStorageException) AtomicLong(java.util.concurrent.atomic.AtomicLong) OSessionStoragePerformanceStatistic(com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic) OCachePointer(com.orientechnologies.orient.core.storage.cache.OCachePointer)

Aggregations

OLogSequenceNumber (com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber)97 ByteBuffer (java.nio.ByteBuffer)78 OCacheEntry (com.orientechnologies.orient.core.storage.cache.OCacheEntry)74 OByteBufferPool (com.orientechnologies.common.directmemory.OByteBufferPool)69 OCachePointer (com.orientechnologies.orient.core.storage.cache.OCachePointer)68 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)20 OWALChangesTree (com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OWALChangesTree)19 Test (org.testng.annotations.Test)16 ORecordId (com.orientechnologies.orient.core.id.ORecordId)10 Random (java.util.Random)10 HashMap (java.util.HashMap)8 Map (java.util.Map)8 TreeSet (java.util.TreeSet)8 OFileClassic (com.orientechnologies.orient.core.storage.fs.OFileClassic)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 OModifiableBoolean (com.orientechnologies.common.types.OModifiableBoolean)3 OStorageException (com.orientechnologies.orient.core.exception.OStorageException)3 HazelcastException (com.hazelcast.core.HazelcastException)2 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)2 OOfflineNodeException (com.orientechnologies.common.concur.OOfflineNodeException)2