Search in sources :

Example 1 with OPageDataVerificationError

use of com.orientechnologies.orient.core.storage.cache.OPageDataVerificationError 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, 0);
                    if (magicNumber != MAGIC_NUMBER) {
                        magicNumberIncorrect = true;
                        if (commandOutputListener != null)
                            commandOutputListener.onMessage("Error: Magic number for page " + (pos / pageSize) + " in file " + fileClassic.getName() + " does not much !!!");
                        fileIsCorrect = false;
                    }
                    final int storedCRC32 = OIntegerSerializer.INSTANCE.deserializeNative(data, OLongSerializer.LONG_SIZE);
                    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 2 with OPageDataVerificationError

use of com.orientechnologies.orient.core.storage.cache.OPageDataVerificationError in project orientdb by orientechnologies.

the class ReadWriteDiskCacheTest method testCheckSumIsBroken.

public void testCheckSumIsBroken() throws Exception {
    long fileId = readBuffer.addFile(fileName, writeBuffer);
    OCacheEntry[] entries = new OCacheEntry[6];
    for (int i = 0; i < 6; i++) {
        entries[i] = readBuffer.load(fileId, i, false, writeBuffer, 1);
        if (entries[i] == null) {
            entries[i] = readBuffer.allocateNewPage(fileId, writeBuffer);
            Assert.assertEquals(entries[i].getPageIndex(), i);
        }
        entries[i].getCachePointer().acquireExclusiveLock();
        entries[i].markDirty();
        final ByteBuffer buffer = entries[i].getCachePointer().getSharedBuffer();
        buffer.position(systemOffset);
        buffer.put(new byte[] { (byte) i, 1, 2, seed, 4, 5, 6, 7 });
        entries[i].getCachePointer().releaseExclusiveLock();
        readBuffer.release(entries[i], writeBuffer);
    }
    writeBuffer.flush();
    byte[] brokenByte = new byte[1];
    brokenByte[0] = 13;
    updateFilePage(2, systemOffset + 2, brokenByte);
    updateFilePage(4, systemOffset + 4, brokenByte);
    OPageDataVerificationError[] pageErrors = writeBuffer.checkStoredPages(null);
    Assert.assertEquals(2, pageErrors.length);
    Assert.assertFalse(pageErrors[0].incorrectMagicNumber);
    Assert.assertTrue(pageErrors[0].incorrectCheckSum);
    Assert.assertEquals(2, pageErrors[0].pageIndex);
    Assert.assertEquals("readWriteDiskCacheTest.tst", pageErrors[0].fileName);
    Assert.assertFalse(pageErrors[1].incorrectMagicNumber);
    Assert.assertTrue(pageErrors[1].incorrectCheckSum);
    Assert.assertEquals(4, pageErrors[1].pageIndex);
    Assert.assertEquals("readWriteDiskCacheTest.tst", pageErrors[1].fileName);
}
Also used : OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) OPageDataVerificationError(com.orientechnologies.orient.core.storage.cache.OPageDataVerificationError) ByteBuffer(java.nio.ByteBuffer)

Example 3 with OPageDataVerificationError

use of com.orientechnologies.orient.core.storage.cache.OPageDataVerificationError in project orientdb by orientechnologies.

the class ReadWriteDiskCacheTest method testMagicNumberIsBroken.

public void testMagicNumberIsBroken() throws Exception {
    long fileId = readBuffer.addFile(fileName, writeBuffer);
    OCacheEntry[] entries = new OCacheEntry[6];
    for (int i = 0; i < 6; i++) {
        entries[i] = readBuffer.load(fileId, i, false, writeBuffer, 1);
        if (entries[i] == null) {
            entries[i] = readBuffer.allocateNewPage(fileId, writeBuffer);
            Assert.assertEquals(entries[i].getPageIndex(), i);
        }
        entries[i].getCachePointer().acquireExclusiveLock();
        entries[i].markDirty();
        final ByteBuffer buffer = entries[i].getCachePointer().getSharedBuffer();
        buffer.position(systemOffset);
        buffer.put(new byte[] { (byte) i, 1, 2, seed, 4, 5, 6, 7 });
        entries[i].getCachePointer().releaseExclusiveLock();
        readBuffer.release(entries[i], writeBuffer);
    }
    writeBuffer.flush();
    byte[] brokenMagicNumber = new byte[OIntegerSerializer.INT_SIZE];
    OIntegerSerializer.INSTANCE.serializeNative(23, brokenMagicNumber, 0);
    updateFilePage(2, 0, brokenMagicNumber);
    updateFilePage(4, 0, brokenMagicNumber);
    OPageDataVerificationError[] pageErrors = writeBuffer.checkStoredPages(null);
    Assert.assertEquals(2, pageErrors.length);
    Assert.assertTrue(pageErrors[0].incorrectMagicNumber);
    Assert.assertFalse(pageErrors[0].incorrectCheckSum);
    Assert.assertEquals(2, pageErrors[0].pageIndex);
    Assert.assertEquals("readWriteDiskCacheTest.tst", pageErrors[0].fileName);
    Assert.assertTrue(pageErrors[1].incorrectMagicNumber);
    Assert.assertFalse(pageErrors[1].incorrectCheckSum);
    Assert.assertEquals(4, pageErrors[1].pageIndex);
    Assert.assertEquals("readWriteDiskCacheTest.tst", pageErrors[1].fileName);
}
Also used : OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry) OPageDataVerificationError(com.orientechnologies.orient.core.storage.cache.OPageDataVerificationError) ByteBuffer(java.nio.ByteBuffer)

Aggregations

OPageDataVerificationError (com.orientechnologies.orient.core.storage.cache.OPageDataVerificationError)3 OCacheEntry (com.orientechnologies.orient.core.storage.cache.OCacheEntry)2 ByteBuffer (java.nio.ByteBuffer)2 OStorageException (com.orientechnologies.orient.core.exception.OStorageException)1 OFileClassic (com.orientechnologies.orient.core.storage.fs.OFileClassic)1 IOException (java.io.IOException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1