Search in sources :

Example 11 with OFileClassic

use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.

the class ReadWriteCacheConcurrentTest method validateFileContent.

private void validateFileContent(byte version, int k) throws IOException {
    String path = storageLocal.getConfiguration().getDirectory() + "/readWriteCacheTest" + k + ".tst";
    OFileClassic fileClassic = new OFileClassic(path, "r");
    fileClassic.open();
    for (int i = 0; i < PAGE_COUNT; i++) {
        byte[] content = new byte[8];
        fileClassic.read(i * (8 + systemOffset) + systemOffset, content, 8);
        Assert.assertEquals(content, new byte[] { version, 2, 3, seed, 5, 6, (byte) k, (byte) (i & 0xFF) }, " i = " + i);
    }
    fileClassic.close();
}
Also used : OFileClassic(com.orientechnologies.orient.core.storage.fs.OFileClassic)

Example 12 with OFileClassic

use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.

the class OWOWCache method cacheFileContent.

private OCachePointer[] cacheFileContent(final int intId, final long startPageIndex, final int pageCount, final boolean addNewPages, OModifiableBoolean cacheHit) throws IOException, InterruptedException {
    final long fileId = composeFileId(id, intId);
    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 OLogSequenceNumber lastLsn;
        if (writeAheadLog != null)
            lastLsn = writeAheadLog.getFlushedLsn();
        else
            lastLsn = new OLogSequenceNumber(-1, -1);
        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;
            try {
                if (pageCount == 1) {
                    final ByteBuffer buffer = bufferPool.acquireDirect(false);
                    fileClassic.read(firstPageStartPosition, buffer);
                    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);
                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 if (addNewPages) {
            final int space = (int) (firstPageEndPosition - fileClassic.getFileSize());
            if (space > 0)
                fileClassic.allocateSpace(space);
            freeSpaceCheckAfterNewPageAdd();
            final ByteBuffer buffer = bufferPool.acquireDirect(true);
            final OCachePointer dataPointer = new OCachePointer(buffer, bufferPool, lastLsn, fileId, startPageIndex);
            cacheHit.setValue(true);
            return new OCachePointer[] { dataPointer };
        } else
            return new OCachePointer[0];
    } finally {
        files.release(entry);
    }
}
Also used : OLogSequenceNumber(com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber) AtomicLong(java.util.concurrent.atomic.AtomicLong) OSessionStoragePerformanceStatistic(com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic) ByteBuffer(java.nio.ByteBuffer) OCachePointer(com.orientechnologies.orient.core.storage.cache.OCachePointer) OFileClassic(com.orientechnologies.orient.core.storage.fs.OFileClassic)

Example 13 with OFileClassic

use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.

the class WOWCacheTest method testNoChecksumFailureIfNoChecksumProvided.

@Test
public void testNoChecksumFailureIfNoChecksumProvided() throws IOException {
    wowCache.setChecksumMode(OChecksumMode.Off);
    final long fileId = wowCache.addFile(fileName);
    OCachePointer cachePointer = wowCache.load(fileId, 0, 1, true, new OModifiableBoolean(), true)[0];
    cachePointer.acquireExclusiveLock();
    final ByteBuffer buffer = cachePointer.getSharedBuffer();
    buffer.position(systemOffset);
    buffer.put(new byte[buffer.remaining()]);
    cachePointer.releaseExclusiveLock();
    wowCache.store(fileId, 0, cachePointer);
    cachePointer.decrementReadersReferrer();
    wowCache.flush();
    final String path = storageLocal.getConfiguration().getDirectory() + File.separator + fileName;
    final OFileClassic file = new OFileClassic(path, "rw");
    file.open();
    file.writeByte(systemOffset, (byte) 1);
    file.close();
    wowCache.setChecksumMode(OChecksumMode.StoreAndThrow);
    cachePointer = wowCache.load(fileId, 0, 1, true, new OModifiableBoolean(), true)[0];
    cachePointer.decrementReadersReferrer();
}
Also used : OCachePointer(com.orientechnologies.orient.core.storage.cache.OCachePointer) OModifiableBoolean(com.orientechnologies.common.types.OModifiableBoolean) ByteBuffer(java.nio.ByteBuffer) OFileClassic(com.orientechnologies.orient.core.storage.fs.OFileClassic) Test(org.testng.annotations.Test) WriteAheadLogTest(com.orientechnologies.orient.core.storage.impl.local.paginated.wal.WriteAheadLogTest)

Example 14 with OFileClassic

use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.

the class WOWCacheTest method assertFile.

private void assertFile(long pageIndex, byte[] value, OLogSequenceNumber lsn) throws IOException {
    String path = storageLocal.getConfiguration().getDirectory() + File.separator + fileName;
    OFileClassic fileClassic = new OFileClassic(path, "r");
    fileClassic.open();
    byte[] content = new byte[8 + systemOffset];
    fileClassic.read(pageIndex * (8 + systemOffset), content, 8 + systemOffset);
    Assert.assertEquals(Arrays.copyOfRange(content, systemOffset, 8 + systemOffset), value);
    long magicNumber = OLongSerializer.INSTANCE.deserializeNative(content, 0);
    Assert.assertEquals(magicNumber, OWOWCache.MAGIC_NUMBER_WITH_CHECKSUM);
    CRC32 crc32 = new CRC32();
    crc32.update(content, OIntegerSerializer.INT_SIZE + OLongSerializer.LONG_SIZE, content.length - OIntegerSerializer.INT_SIZE - OLongSerializer.LONG_SIZE);
    int crc = OIntegerSerializer.INSTANCE.deserializeNative(content, OLongSerializer.LONG_SIZE);
    Assert.assertEquals(crc, (int) crc32.getValue());
    int segment = OIntegerSerializer.INSTANCE.deserializeNative(content, OLongSerializer.LONG_SIZE + OIntegerSerializer.INT_SIZE);
    long position = OLongSerializer.INSTANCE.deserializeNative(content, OLongSerializer.LONG_SIZE + 2 * OIntegerSerializer.INT_SIZE);
    OLogSequenceNumber readLsn = new OLogSequenceNumber(segment, position);
    Assert.assertEquals(readLsn, lsn);
    fileClassic.close();
}
Also used : OLogSequenceNumber(com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber) CRC32(java.util.zip.CRC32) OFileClassic(com.orientechnologies.orient.core.storage.fs.OFileClassic)

Example 15 with OFileClassic

use of com.orientechnologies.orient.core.storage.fs.OFileClassic in project orientdb by orientechnologies.

the class WOWCacheTest method testChecksumFailure.

@Test
public void testChecksumFailure() throws IOException {
    wowCache.setChecksumMode(OChecksumMode.StoreAndThrow);
    final long fileId = wowCache.addFile(fileName);
    final OCachePointer cachePointer = wowCache.load(fileId, 0, 1, true, new OModifiableBoolean(), true)[0];
    cachePointer.acquireExclusiveLock();
    final ByteBuffer buffer = cachePointer.getSharedBuffer();
    buffer.position(systemOffset);
    buffer.put(new byte[buffer.remaining()]);
    cachePointer.releaseExclusiveLock();
    wowCache.store(fileId, 0, cachePointer);
    cachePointer.decrementReadersReferrer();
    wowCache.flush();
    final String path = storageLocal.getConfiguration().getDirectory() + File.separator + fileName;
    final OFileClassic file = new OFileClassic(path, "rw");
    file.open();
    file.writeByte(systemOffset, (byte) 1);
    file.close();
    Assert.assertThrows(OStorageException.class, new Assert.ThrowingRunnable() {

        @Override
        public void run() throws Throwable {
            wowCache.load(fileId, 0, 1, true, new OModifiableBoolean(), true);
        }
    });
}
Also used : Assert(org.testng.Assert) OCachePointer(com.orientechnologies.orient.core.storage.cache.OCachePointer) OModifiableBoolean(com.orientechnologies.common.types.OModifiableBoolean) ByteBuffer(java.nio.ByteBuffer) OFileClassic(com.orientechnologies.orient.core.storage.fs.OFileClassic) Test(org.testng.annotations.Test) WriteAheadLogTest(com.orientechnologies.orient.core.storage.impl.local.paginated.wal.WriteAheadLogTest)

Aggregations

OFileClassic (com.orientechnologies.orient.core.storage.fs.OFileClassic)24 OCachePointer (com.orientechnologies.orient.core.storage.cache.OCachePointer)9 ByteBuffer (java.nio.ByteBuffer)9 OStorageException (com.orientechnologies.orient.core.exception.OStorageException)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)7 OModifiableBoolean (com.orientechnologies.common.types.OModifiableBoolean)6 OLogSequenceNumber (com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber)6 WriteAheadLogTest (com.orientechnologies.orient.core.storage.impl.local.paginated.wal.WriteAheadLogTest)6 Test (org.testng.annotations.Test)6 OSessionStoragePerformanceStatistic (com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)2 CRC32 (java.util.zip.CRC32)2 Assert (org.testng.Assert)2 OWriteCacheException (com.orientechnologies.orient.core.exception.OWriteCacheException)1 OPageDataVerificationError (com.orientechnologies.orient.core.storage.cache.OPageDataVerificationError)1 File (java.io.File)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 Lock (java.util.concurrent.locks.Lock)1