Search in sources :

Example 41 with EphemeralFileSystemAbstraction

use of org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction in project neo4j by neo4j.

the class DurableStateStorageTest method shouldClearFileOnFirstUse.

@Test
public void shouldClearFileOnFirstUse() throws Throwable {
    // given
    EphemeralFileSystemAbstraction fsa = fileSystemRule.get();
    fsa.mkdir(testDir.directory());
    int rotationCount = 10;
    DurableStateStorage<AtomicInteger> storage = new DurableStateStorage<>(fsa, testDir.directory(), "state", new AtomicIntegerMarshal(), rotationCount, NullLogProvider.getInstance());
    int largestValueWritten = 0;
    try (Lifespan lifespan = new Lifespan(storage)) {
        for (; largestValueWritten < rotationCount * 2; largestValueWritten++) {
            storage.persistStoreData(new AtomicInteger(largestValueWritten));
        }
    }
    // now both files are full. We reopen, then write some more.
    storage = lifeRule.add(new DurableStateStorage<>(fsa, testDir.directory(), "state", new AtomicIntegerMarshal(), rotationCount, NullLogProvider.getInstance()));
    storage.persistStoreData(new AtomicInteger(largestValueWritten++));
    storage.persistStoreData(new AtomicInteger(largestValueWritten++));
    storage.persistStoreData(new AtomicInteger(largestValueWritten));
    /*
         * We have written stuff in fileA but not gotten to the end (resulting in rotation). The largestValueWritten
         * should nevertheless be correct
         */
    ByteBuffer forReadingBackIn = ByteBuffer.allocate(10_000);
    StoreChannel lastWrittenTo = fsa.open(stateFileA(), "r");
    lastWrittenTo.read(forReadingBackIn);
    forReadingBackIn.flip();
    AtomicInteger lastRead = null;
    while (true) {
        try {
            lastRead = new AtomicInteger(forReadingBackIn.getInt());
        } catch (BufferUnderflowException e) {
            break;
        }
    }
    // then
    assertNotNull(lastRead);
    assertEquals(largestValueWritten, lastRead.get());
}
Also used : EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StoreChannel(org.neo4j.io.fs.StoreChannel) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException) Test(org.junit.Test)

Example 42 with EphemeralFileSystemAbstraction

use of org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction in project neo4j by neo4j.

the class DurableStateStorageTest method shouldRotateBackToFirstStoreFileAfterSufficientEntries.

@Test
public void shouldRotateBackToFirstStoreFileAfterSufficientEntries() throws Exception {
    // given
    EphemeralFileSystemAbstraction fsa = fileSystemRule.get();
    fsa.mkdir(testDir.directory());
    final int numberOfEntriesBeforeRotation = 100;
    DurableStateStorage<AtomicInteger> storage = lifeRule.add(new DurableStateStorage<>(fsa, testDir.directory(), "state", new AtomicIntegerMarshal(), numberOfEntriesBeforeRotation, NullLogProvider.getInstance()));
    // when
    for (int i = 0; i < numberOfEntriesBeforeRotation * 2; i++) {
        storage.persistStoreData(new AtomicInteger(i));
    }
    // Force the rotation back to the first store
    storage.persistStoreData(new AtomicInteger(9999));
    // then
    assertEquals(4, fsa.getFileSize(stateFileA()));
    assertEquals(numberOfEntriesBeforeRotation * 4, fsa.getFileSize(stateFileB()));
}
Also used : EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 43 with EphemeralFileSystemAbstraction

use of org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction in project neo4j by neo4j.

the class ReadAheadChannelTest method shouldReturnPositionWithinBufferedStream.

@Test
public void shouldReturnPositionWithinBufferedStream() throws Exception {
    // given
    EphemeralFileSystemAbstraction fsa = fileSystemRule.get();
    File file = new File("foo.txt");
    int readAheadSize = 512;
    int fileSize = readAheadSize * 8;
    createFile(fsa, file, fileSize);
    ReadAheadChannel<StoreChannel> bufferedReader = new ReadAheadChannel<>(fsa.open(file, "r"), readAheadSize);
    // when
    for (int i = 0; i < fileSize / Long.BYTES; i++) {
        assertEquals(Long.BYTES * i, bufferedReader.position());
        bufferedReader.getLong();
    }
    assertEquals(fileSize, bufferedReader.position());
    try {
        bufferedReader.getLong();
        fail();
    } catch (ReadPastEndException e) {
    // expected
    }
    assertEquals(fileSize, bufferedReader.position());
}
Also used : EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) ReadPastEndException(org.neo4j.storageengine.api.ReadPastEndException) Test(org.junit.Test)

Example 44 with EphemeralFileSystemAbstraction

use of org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction in project neo4j by neo4j.

the class TestTxEntries method testStartEntryWrittenOnceOnRollback.

/*
     * Starts a JVM, executes a tx that fails on prepare and rollbacks,
     * triggering a bug where an extra start entry for that tx is written
     * in the xa log.
     */
@Test
public void testStartEntryWrittenOnceOnRollback() throws Exception {
    final GraphDatabaseService db = new TestGraphDatabaseFactory().setFileSystem(fs.get()).newImpermanentDatabase(storeDir);
    createSomeTransactions(db);
    EphemeralFileSystemAbstraction snapshot = fs.snapshot(() -> db.shutdown());
    new TestGraphDatabaseFactory().setFileSystem(snapshot).newImpermanentDatabase(storeDir).shutdown();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) Test(org.junit.Test)

Example 45 with EphemeralFileSystemAbstraction

use of org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction in project neo4j by neo4j.

the class TestEphemeralFileChannel method absoluteVersusRelative.

@Test
public void absoluteVersusRelative() throws Exception {
    // GIVEN
    File file = new File("myfile");
    EphemeralFileSystemAbstraction fs = fileSystemRule.get();
    StoreChannel channel = fs.open(file, "rw");
    byte[] bytes = "test".getBytes();
    channel.write(ByteBuffer.wrap(bytes));
    channel.close();
    // WHEN
    channel = fs.open(new File(file.getAbsolutePath()), "r");
    byte[] readBytes = new byte[bytes.length];
    int nrOfReadBytes = channel.read(ByteBuffer.wrap(readBytes));
    // THEN
    assertEquals(bytes.length, nrOfReadBytes);
    assertTrue(Arrays.equals(bytes, readBytes));
    fs.close();
}
Also used : EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) Test(org.junit.Test)

Aggregations

EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)54 Test (org.junit.Test)37 File (java.io.File)27 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)10 StoreChannel (org.neo4j.io.fs.StoreChannel)8 IOException (java.io.IOException)7 AdversarialFileSystemAbstraction (org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction)7 Before (org.junit.Before)6 CountingAdversary (org.neo4j.adversaries.CountingAdversary)6 Transaction (org.neo4j.graphdb.Transaction)6 ByteBuffer (java.nio.ByteBuffer)5 MethodGuardedAdversary (org.neo4j.adversaries.MethodGuardedAdversary)5 StateRecoveryManager (org.neo4j.causalclustering.core.state.StateRecoveryManager)5 EndOfStreamException (org.neo4j.causalclustering.messaging.EndOfStreamException)5 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Node (org.neo4j.graphdb.Node)4 SelectiveFileSystemAbstraction (org.neo4j.graphdb.mockfs.SelectiveFileSystemAbstraction)4 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)4 NodeRecord (org.neo4j.kernel.impl.store.record.NodeRecord)4