Search in sources :

Example 86 with PageCache

use of org.neo4j.io.pagecache.PageCache in project neo4j by neo4j.

the class GBPTreeRecoveryTest method doShouldRecoverFromAnything.

private void doShouldRecoverFromAnything(boolean replayRecoveryExactlyFromCheckpoint) throws Exception {
    assertInitialized();
    // GIVEN
    // a tree which has had random updates and checkpoints in it, load generated with specific seed
    File file = directory.file("index");
    List<Action> load = generateLoad();
    int lastCheckPointIndex = indexOfLastCheckpoint(load);
    {
        // _,_,_,_,_,_,_,c,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,c,_,_,_,_,_,_,_,_,_,_,_
        //                                                 ^             ^
        //                                                 |             |------------ crash flush index
        //                                                 |-------------------------- last checkpoint index
        //
        PageCache pageCache = createPageCache();
        GBPTree<MutableLong, MutableLong> index = createIndex(pageCache, file);
        // Execute all actions up to and including last checkpoint ...
        execute(load.subList(0, lastCheckPointIndex + 1), index);
        // ... a random amount of the remaining "unsafe" actions ...
        int numberOfRemainingActions = load.size() - lastCheckPointIndex - 1;
        int crashFlushIndex = lastCheckPointIndex + random.nextInt(numberOfRemainingActions) + 1;
        execute(load.subList(lastCheckPointIndex + 1, crashFlushIndex), index);
        // ... flush ...
        pageCache.flushAndForce();
        // ... execute the remaining actions
        execute(load.subList(crashFlushIndex, load.size()), index);
        // ... and finally crash
        fs.snapshot(throwing(() -> {
            index.close();
            pageCache.close();
        }));
    }
    // WHEN doing recovery
    List<Action> recoveryActions;
    if (replayRecoveryExactlyFromCheckpoint) {
        recoveryActions = recoveryActions(load, lastCheckPointIndex + 1);
    } else {
        recoveryActions = recoveryActions(load, random.nextInt(lastCheckPointIndex + 1));
    }
    // first crashing during recovery
    int numberOfCrashesDuringRecovery = random.intBetween(0, 3);
    for (int i = 0; i < numberOfCrashesDuringRecovery; i++) {
        PageCache pageCache = createPageCache();
        GBPTree<MutableLong, MutableLong> index = createIndex(pageCache, file);
        int numberOfActionsToRecoverBeforeCrashing = random.intBetween(1, recoveryActions.size());
        recover(recoveryActions.subList(0, numberOfActionsToRecoverBeforeCrashing), index);
        pageCache.flushAndForce();
        fs.snapshot(throwing(() -> {
            index.close();
            pageCache.close();
        }));
    }
    // to finally apply all actions after last checkpoint and verify tree
    try (PageCache pageCache = createPageCache();
        GBPTree<MutableLong, MutableLong> index = createIndex(pageCache, file)) {
        recover(recoveryActions, index);
        index.finishRecovery();
        // THEN
        // we should end up with a consistent index containing all the stuff load says
        index.consistencyCheck();
        long[] /*key,value,key,value...*/
        aggregate = expectedSortedAggregatedDataFromGeneratedLoad(load);
        try (RawCursor<Hit<MutableLong, MutableLong>, IOException> cursor = index.seek(new MutableLong(Long.MIN_VALUE), new MutableLong(Long.MAX_VALUE))) {
            for (int i = 0; i < aggregate.length; ) {
                assertTrue(cursor.next());
                Hit<MutableLong, MutableLong> hit = cursor.get();
                assertEquals(aggregate[i++], hit.key().longValue());
                assertEquals(aggregate[i++], hit.value().longValue());
            }
            assertFalse(cursor.next());
        }
    }
}
Also used : IOException(java.io.IOException) MutableLong(org.apache.commons.lang3.mutable.MutableLong) File(java.io.File) PageCache(org.neo4j.io.pagecache.PageCache)

Example 87 with PageCache

use of org.neo4j.io.pagecache.PageCache in project neo4j by neo4j.

the class GBPTreeRecoveryTest method shouldRecoverFromCrashBeforeFirstCheckpoint.

@Test
public void shouldRecoverFromCrashBeforeFirstCheckpoint() throws Exception {
    // GIVEN
    // a tree with only small amount of data that has not yet seen checkpoint from outside
    File file = directory.file("index");
    {
        PageCache pageCache = createPageCache();
        GBPTree<MutableLong, MutableLong> index = createIndex(pageCache, file);
        Writer<MutableLong, MutableLong> writer = index.writer();
        key.setValue(1L);
        value.setValue(10L);
        writer.put(key, value);
        pageCache.flushAndForce();
        fs.snapshot(throwing(() -> {
            writer.close();
            index.close();
            pageCache.close();
        }));
    }
    // WHEN
    try (PageCache pageCache = createPageCache();
        GBPTree<MutableLong, MutableLong> index = createIndex(pageCache, file)) {
        // this is the mimic:ed recovery
        index.prepareForRecovery();
        index.finishRecovery();
        try (Writer<MutableLong, MutableLong> writer = index.writer()) {
            writer.put(key, value);
        }
        // THEN
        // we should end up with a consistent index
        index.consistencyCheck();
        // ... containing all the stuff load says
        try (RawCursor<Hit<MutableLong, MutableLong>, IOException> cursor = index.seek(new MutableLong(Long.MIN_VALUE), new MutableLong(Long.MAX_VALUE))) {
            assertTrue(cursor.next());
            Hit<MutableLong, MutableLong> hit = cursor.get();
            assertEquals(key.getValue(), hit.key().getValue());
            assertEquals(value.getValue(), hit.value().getValue());
        }
    }
}
Also used : MutableLong(org.apache.commons.lang3.mutable.MutableLong) IOException(java.io.IOException) File(java.io.File) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.Test)

Example 88 with PageCache

use of org.neo4j.io.pagecache.PageCache in project neo4j by neo4j.

the class CrashGenerationCleanerTest method setupPagedFile.

@Before
public void setupPagedFile() throws IOException {
    PageCache pageCache = pageCacheRule.getPageCache(fileSystemRule.get(), config().withPageSize(PAGE_SIZE).withAccessChecks(true));
    pagedFile = pageCache.map(testDirectory.file(FILE_NAME), PAGE_SIZE, CREATE, DELETE_ON_CLOSE);
}
Also used : PageCache(org.neo4j.io.pagecache.PageCache) Before(org.junit.Before)

Example 89 with PageCache

use of org.neo4j.io.pagecache.PageCache in project neo4j by neo4j.

the class AccessCheckingPageCacheTest method getPageCursor.

@Before
public void getPageCursor() throws IOException {
    PageCache mockedPageCache = mock(PageCache.class);
    PagedFile mockedPagedFile = mock(PagedFile.class);
    PageCursor mockedCursor = mock(PageCursor.class);
    when(mockedPagedFile.io(anyLong(), anyInt())).thenReturn(mockedCursor);
    when(mockedPageCache.map(any(File.class), anyInt(), anyVararg())).thenReturn(mockedPagedFile);
    pageCache = new AccessCheckingPageCache(mockedPageCache);
    PagedFile file = pageCache.map(new File("some file"), 512);
    cursor = file.io(0, PagedFile.PF_SHARED_READ_LOCK);
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) PagedFile(org.neo4j.io.pagecache.PagedFile) File(java.io.File) PageCache(org.neo4j.io.pagecache.PageCache) PageCursor(org.neo4j.io.pagecache.PageCursor) Before(org.junit.Before)

Example 90 with PageCache

use of org.neo4j.io.pagecache.PageCache in project neo4j by neo4j.

the class CoreBootstrapperTest method shouldSetAllCoreState.

@Test
public void shouldSetAllCoreState() throws Exception {
    // given
    int nodeCount = 100;
    FileSystemAbstraction fileSystem = fileSystemRule.get();
    File classicNeo4jStore = RestoreClusterUtils.createClassicNeo4jStore(testDirectory.directory(), fileSystem, nodeCount, Standard.LATEST_NAME);
    PageCache pageCache = pageCacheRule.getPageCache(fileSystem);
    CoreBootstrapper bootstrapper = new CoreBootstrapper(classicNeo4jStore, pageCache, fileSystem, Config.defaults(), NullLogProvider.getInstance());
    // when
    Set<MemberId> membership = asSet(randomMember(), randomMember(), randomMember());
    CoreSnapshot snapshot = bootstrapper.bootstrap(membership);
    // then
    assertEquals(nodeCount, ((IdAllocationState) snapshot.get(CoreStateType.ID_ALLOCATION)).firstUnallocated(IdType.NODE));
    /* Bootstrapped state is created in RAFT land at index -1 and term -1. */
    assertEquals(0, snapshot.prevIndex());
    assertEquals(0, snapshot.prevTerm());
    /* Lock is initially not taken. */
    assertEquals(new ReplicatedLockTokenState(), snapshot.get(CoreStateType.LOCK_TOKEN));
    /* Raft has the bootstrapped set of members initially. */
    assertEquals(membership, ((RaftCoreState) snapshot.get(CoreStateType.RAFT_CORE_STATE)).committed().members());
    /* The session state is initially empty. */
    assertEquals(new GlobalSessionTrackerState(), snapshot.get(CoreStateType.SESSION_TRACKER));
    LastCommittedIndexFinder lastCommittedIndexFinder = new LastCommittedIndexFinder(new ReadOnlyTransactionIdStore(pageCache, classicNeo4jStore), new ReadOnlyTransactionStore(pageCache, fileSystem, classicNeo4jStore, new Monitors()), NullLogProvider.getInstance());
    long lastCommittedIndex = lastCommittedIndexFinder.getLastCommittedIndex();
    assertEquals(-1, lastCommittedIndex);
}
Also used : FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) LastCommittedIndexFinder(org.neo4j.causalclustering.core.state.machines.tx.LastCommittedIndexFinder) ReadOnlyTransactionStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore) GlobalSessionTrackerState(org.neo4j.causalclustering.core.replication.session.GlobalSessionTrackerState) MemberId(org.neo4j.causalclustering.identity.MemberId) CoreSnapshot(org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot) ReadOnlyTransactionIdStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionIdStore) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) Monitors(org.neo4j.kernel.monitoring.Monitors) ReplicatedLockTokenState(org.neo4j.causalclustering.core.state.machines.locks.ReplicatedLockTokenState) File(java.io.File) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.Test)

Aggregations

PageCache (org.neo4j.io.pagecache.PageCache)134 Test (org.junit.Test)90 File (java.io.File)74 Config (org.neo4j.kernel.configuration.Config)39 IOException (java.io.IOException)32 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)31 StoreVersionCheck (org.neo4j.kernel.impl.storemigration.StoreVersionCheck)19 LegacyStoreVersionCheck (org.neo4j.kernel.impl.storemigration.legacystore.LegacyStoreVersionCheck)17 UpgradableDatabase (org.neo4j.kernel.impl.storemigration.UpgradableDatabase)16 PagedFile (org.neo4j.io.pagecache.PagedFile)15 LogService (org.neo4j.kernel.impl.logging.LogService)13 DefaultFileSystemAbstraction (org.neo4j.io.fs.DefaultFileSystemAbstraction)12 NullLogService (org.neo4j.kernel.impl.logging.NullLogService)12 StoreFactory (org.neo4j.kernel.impl.store.StoreFactory)12 NeoStores (org.neo4j.kernel.impl.store.NeoStores)11 RecordFormatSelector.selectForConfig (org.neo4j.kernel.impl.store.format.RecordFormatSelector.selectForConfig)11 RecordFormatSelector.selectForStoreOrConfig (org.neo4j.kernel.impl.store.format.RecordFormatSelector.selectForStoreOrConfig)11 Before (org.junit.Before)10 TransactionId (org.neo4j.kernel.impl.store.TransactionId)10 StoreCopyClient (org.neo4j.com.storecopy.StoreCopyClient)9