Search in sources :

Example 6 with ODurablePage

use of com.orientechnologies.orient.core.storage.impl.local.paginated.base.ODurablePage in project orientdb by orientechnologies.

the class LocalPaginatedClusterWithWALTest method restoreClusterFromWAL.

private void restoreClusterFromWAL() throws IOException {
    ODiskWriteAheadLog log = new ODiskWriteAheadLog(4, -1, 10 * 1024L * OWALPage.PAGE_SIZE, null, true, storage, 10);
    OLogSequenceNumber lsn = log.begin();
    List<OWALRecord> atomicUnit = new ArrayList<OWALRecord>();
    boolean atomicChangeIsProcessed = false;
    while (lsn != null) {
        OWALRecord walRecord = log.read(lsn);
        if (walRecord instanceof OOperationUnitRecord)
            atomicUnit.add(walRecord);
        if (!atomicChangeIsProcessed) {
            if (walRecord instanceof OAtomicUnitStartRecord)
                atomicChangeIsProcessed = true;
        } else if (walRecord instanceof OAtomicUnitEndRecord) {
            atomicChangeIsProcessed = false;
            for (OWALRecord restoreRecord : atomicUnit) {
                if (restoreRecord instanceof OAtomicUnitStartRecord || restoreRecord instanceof OAtomicUnitEndRecord || restoreRecord instanceof ONonTxOperationPerformedWALRecord)
                    continue;
                if (restoreRecord instanceof OFileCreatedWALRecord) {
                    final OFileCreatedWALRecord fileCreatedCreatedRecord = (OFileCreatedWALRecord) restoreRecord;
                    final String fileName = fileCreatedCreatedRecord.getFileName().replace("actualPaginatedClusterWithWALTest", "expectedPaginatedClusterWithWALTest");
                    if (!expectedWriteCache.exists(fileName))
                        expectedReadCache.addFile(fileName, fileCreatedCreatedRecord.getFileId(), expectedWriteCache);
                } else {
                    final OUpdatePageRecord updatePageRecord = (OUpdatePageRecord) restoreRecord;
                    final long fileId = updatePageRecord.getFileId();
                    final long pageIndex = updatePageRecord.getPageIndex();
                    OCacheEntry cacheEntry = expectedReadCache.load(fileId, pageIndex, true, expectedWriteCache, 1);
                    if (cacheEntry == null) {
                        do {
                            if (cacheEntry != null)
                                readCache.release(cacheEntry, expectedWriteCache);
                            cacheEntry = expectedReadCache.allocateNewPage(fileId, expectedWriteCache);
                        } while (cacheEntry.getPageIndex() != pageIndex);
                    }
                    cacheEntry.acquireExclusiveLock();
                    try {
                        ODurablePage durablePage = new ODurablePage(cacheEntry, null);
                        durablePage.restoreChanges(updatePageRecord.getChanges());
                        durablePage.setLsn(updatePageRecord.getLsn());
                        cacheEntry.markDirty();
                    } finally {
                        cacheEntry.releaseExclusiveLock();
                        expectedReadCache.release(cacheEntry, expectedWriteCache);
                    }
                }
            }
            atomicUnit.clear();
        } else {
            Assert.assertTrue(walRecord instanceof OUpdatePageRecord || walRecord instanceof OFileCreatedWALRecord || walRecord instanceof ONonTxOperationPerformedWALRecord);
        }
        lsn = log.next(lsn);
    }
    Assert.assertTrue(atomicUnit.isEmpty());
    log.close();
}
Also used : ArrayList(java.util.ArrayList) ODurablePage(com.orientechnologies.orient.core.storage.impl.local.paginated.base.ODurablePage) OCacheEntry(com.orientechnologies.orient.core.storage.cache.OCacheEntry)

Aggregations

ODurablePage (com.orientechnologies.orient.core.storage.impl.local.paginated.base.ODurablePage)6 OCacheEntry (com.orientechnologies.orient.core.storage.cache.OCacheEntry)5 ArrayList (java.util.ArrayList)3 OReadCache (com.orientechnologies.orient.core.storage.cache.OReadCache)1 OWriteCache (com.orientechnologies.orient.core.storage.cache.OWriteCache)1 OAbstractPaginatedStorage (com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage)1 OSessionStoragePerformanceStatistic (com.orientechnologies.orient.core.storage.impl.local.statistic.OSessionStoragePerformanceStatistic)1