Search in sources :

Example 6 with OFileClassic

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

the class OWOWCache method loadFile.

public long loadFile(final String fileName) throws IOException {
    filesLock.acquireWriteLock();
    try {
        Integer fileId = nameIdMap.get(fileName);
        OFileClassic fileClassic;
        // check that file is already registered
        if (!(fileId == null || fileId < 0)) {
            final long externalId = composeFileId(id, fileId);
            fileClassic = files.get(externalId);
            if (fileClassic != null)
                return externalId;
            else
                throw new OStorageException("File with given name " + fileName + " only partially registered in storage");
        }
        fileClassic = createFileInstance(fileName);
        if (!fileClassic.exists())
            throw new OStorageException("File with name " + fileName + " does not exist in storage " + storageLocal.getName());
        else {
            // REGISTER THE FILE
            OLogManager.instance().debug(this, "File '" + fileName + "' is not registered in 'file name - id' map, but exists in file system. Registering it");
            if (fileId == null) {
                ++fileCounter;
                fileId = fileCounter;
            } else
                fileId = -fileId;
            long externalId = composeFileId(id, fileId);
            while (files.get(externalId) != null) {
                ++fileCounter;
                fileId = fileCounter;
                externalId = composeFileId(id, fileId);
            }
            openFile(fileClassic);
            files.add(externalId, fileClassic);
            nameIdMap.put(fileName, fileId);
            writeNameIdEntry(new NameFileIdEntry(fileName, fileId), true);
            return externalId;
        }
    } catch (InterruptedException e) {
        throw OException.wrapException(new OStorageException("Thread was interrupted"), e);
    } finally {
        filesLock.releaseWriteLock();
    }
}
Also used : OStorageException(com.orientechnologies.orient.core.exception.OStorageException) OFileClassic(com.orientechnologies.orient.core.storage.fs.OFileClassic)

Example 7 with OFileClassic

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

the class OWOWCache method checkStoredPages.

public OPageDataVerificationError[] checkStoredPages(OCommandOutputListener commandOutputListener) {
    final int notificationTimeOut = 5000;
    final List<OPageDataVerificationError> errors = new ArrayList<OPageDataVerificationError>();
    filesLock.acquireWriteLock();
    try {
        for (Integer intId : nameIdMap.values()) {
            if (intId < 0)
                continue;
            boolean fileIsCorrect;
            final long externalId = composeFileId(id, intId);
            final OClosableEntry<Long, OFileClassic> entry = files.acquire(externalId);
            final OFileClassic fileClassic = entry.get();
            try {
                if (commandOutputListener != null)
                    commandOutputListener.onMessage("Flashing file " + fileClassic.getName() + "... ");
                flush(intId);
                if (commandOutputListener != null)
                    commandOutputListener.onMessage("Start verification of content of " + fileClassic.getName() + "file ...");
                long time = System.currentTimeMillis();
                long filledUpTo = fileClassic.getFileSize();
                fileIsCorrect = true;
                for (long pos = 0; pos < filledUpTo; pos += pageSize) {
                    boolean checkSumIncorrect = false;
                    boolean magicNumberIncorrect = false;
                    byte[] data = new byte[pageSize];
                    fileClassic.read(pos, data, data.length);
                    long magicNumber = OLongSerializer.INSTANCE.deserializeNative(data, MAGIC_NUMBER_OFFSET);
                    if (magicNumber != MAGIC_NUMBER_WITH_CHECKSUM && magicNumber != MAGIC_NUMBER_WITHOUT_CHECKSUM) {
                        magicNumberIncorrect = true;
                        if (commandOutputListener != null)
                            commandOutputListener.onMessage("Error: Magic number for page " + (pos / pageSize) + " in file " + fileClassic.getName() + " does not match !!!");
                        fileIsCorrect = false;
                    }
                    if (magicNumber != MAGIC_NUMBER_WITHOUT_CHECKSUM) {
                        final int storedCRC32 = OIntegerSerializer.INSTANCE.deserializeNative(data, CHECKSUM_OFFSET);
                        final int calculatedCRC32 = calculatePageCrc(data);
                        if (storedCRC32 != calculatedCRC32) {
                            checkSumIncorrect = true;
                            if (commandOutputListener != null)
                                commandOutputListener.onMessage("Error: Checksum for page " + (pos / pageSize) + " in file " + fileClassic.getName() + " is incorrect !!!");
                            fileIsCorrect = false;
                        }
                    }
                    if (magicNumberIncorrect || checkSumIncorrect)
                        errors.add(new OPageDataVerificationError(magicNumberIncorrect, checkSumIncorrect, pos / pageSize, fileClassic.getName()));
                    if (commandOutputListener != null && System.currentTimeMillis() - time > notificationTimeOut) {
                        time = notificationTimeOut;
                        commandOutputListener.onMessage((pos / pageSize) + " pages were processed ...");
                    }
                }
            } catch (IOException ioe) {
                if (commandOutputListener != null)
                    commandOutputListener.onMessage("Error: Error during processing of file " + fileClassic.getName() + ". " + ioe.getMessage());
                fileIsCorrect = false;
            } finally {
                files.release(entry);
            }
            if (!fileIsCorrect) {
                if (commandOutputListener != null)
                    commandOutputListener.onMessage("Verification of file " + fileClassic.getName() + " is finished with errors.");
            } else {
                if (commandOutputListener != null)
                    commandOutputListener.onMessage("Verification of file " + fileClassic.getName() + " is successfully finished.");
            }
        }
        return errors.toArray(new OPageDataVerificationError[errors.size()]);
    } catch (InterruptedException e) {
        throw OException.wrapException(new OStorageException("Thread was interrupted"), e);
    } finally {
        filesLock.releaseWriteLock();
    }
}
Also used : OPageDataVerificationError(com.orientechnologies.orient.core.storage.cache.OPageDataVerificationError) IOException(java.io.IOException) OFileClassic(com.orientechnologies.orient.core.storage.fs.OFileClassic) OStorageException(com.orientechnologies.orient.core.exception.OStorageException) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 8 with OFileClassic

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

the class WOWCacheTest method testNoChecksumVerificationIfNotRequested.

@Test
public void testNoChecksumVerificationIfNotRequested() throws IOException {
    wowCache.setChecksumMode(OChecksumMode.StoreAndThrow);
    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();
    cachePointer = wowCache.load(fileId, 0, 1, true, new OModifiableBoolean(), false)[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 9 with OFileClassic

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

the class WOWCacheTest method testNoChecksumFailureIfVerificationTurnedOffOnLoad.

@Test
public void testNoChecksumFailureIfVerificationTurnedOffOnLoad() throws IOException {
    wowCache.setChecksumMode(OChecksumMode.Store);
    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();
    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 10 with OFileClassic

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

the class WOWCacheTest method testNoChecksumFailureIfVerificationTurnedOff.

@Test
public void testNoChecksumFailureIfVerificationTurnedOff() 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();
    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)

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