Search in sources :

Example 81 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class StateRecoveryManagerTest method shouldReturnTheFullFileAsPreviouslyInactiveWhenActiveContainsCorruptEntry.

@Test
public void shouldReturnTheFullFileAsPreviouslyInactiveWhenActiveContainsCorruptEntry() throws Exception {
    // given
    EphemeralFileSystemAbstraction fsa = fileSystemRule.get();
    fsa.mkdir(testDir.directory());
    File fileA = fileA();
    StoreChannel channel = fsa.create(fileA);
    ByteBuffer buffer = writeLong(42);
    channel.writeAll(buffer);
    channel.force(false);
    buffer.clear();
    // extraneous bytes
    buffer.putLong(101);
    buffer.flip();
    channel.writeAll(buffer);
    channel.force(false);
    File fileB = fileB();
    fsa.create(fileB);
    StateRecoveryManager<Long> manager = new StateRecoveryManager<>(fsa, new LongMarshal());
    // when
    final StateRecoveryManager.RecoveryStatus recoveryStatus = manager.recover(fileA, fileB);
    // then
    assertEquals(fileB, recoveryStatus.activeFile());
}
Also used : EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) StoreChannel(org.neo4j.io.fs.StoreChannel) StateRecoveryManager(org.neo4j.causalclustering.core.state.StateRecoveryManager) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 82 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class StateRecoveryManagerTest method writeSomeLongsIn.

private void writeSomeLongsIn(EphemeralFileSystemAbstraction fsa, File file, long... longs) throws IOException {
    final StoreChannel channel = fsa.open(file, "rw");
    ByteBuffer buffer = ByteBuffer.allocate(longs.length * 8);
    for (long aLong : longs) {
        buffer.putLong(aLong);
    }
    buffer.flip();
    channel.writeAll(buffer);
    channel.force(false);
    channel.close();
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) ByteBuffer(java.nio.ByteBuffer)

Example 83 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class SegmentFile method getOrCreateWriter.

private synchronized PhysicalFlushableChannel getOrCreateWriter() throws IOException {
    if (bufferedWriter == null) {
        if (!refCount.increase()) {
            throw new IOException("Writer has been closed");
        }
        StoreChannel channel = fileSystem.open(file, "rw");
        channel.position(channel.size());
        bufferedWriter = new PhysicalFlushableChannel(channel);
    }
    return bufferedWriter;
}
Also used : PhysicalFlushableChannel(org.neo4j.kernel.impl.transaction.log.PhysicalFlushableChannel) StoreChannel(org.neo4j.io.fs.StoreChannel) IOException(java.io.IOException)

Example 84 with StoreChannel

use of org.neo4j.io.fs.StoreChannel 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 85 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class ReadAheadChannelTest method shouldReturnValueIfSufficientBytesAreBufferedEvenIfEOFHasBeenEncountered.

@Test
public void shouldReturnValueIfSufficientBytesAreBufferedEvenIfEOFHasBeenEncountered() throws Exception {
    // Given
    FileSystemAbstraction fileSystem = fileSystemRule.get();
    StoreChannel storeChannel = fileSystem.open(new File("foo.txt"), "rw");
    ByteBuffer buffer = ByteBuffer.allocate(1);
    buffer.put((byte) 1);
    buffer.flip();
    storeChannel.writeAll(buffer);
    storeChannel.force(false);
    storeChannel.close();
    storeChannel = fileSystem.open(new File("foo.txt"), "r");
    ReadAheadChannel<StoreChannel> channel = new ReadAheadChannel<>(storeChannel);
    try {
        channel.getShort();
        fail("Should have thrown exception signalling end of file reached");
    } catch (ReadPastEndException endOfFile) {
    // outstanding
    }
    assertEquals((byte) 1, channel.get());
    try {
        channel.get();
        fail("Should have thrown exception signalling end of file reached");
    } catch (ReadPastEndException endOfFile) {
    // outstanding
    }
}
Also used : EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) ReadPastEndException(org.neo4j.storageengine.api.ReadPastEndException) Test(org.junit.Test)

Aggregations

StoreChannel (org.neo4j.io.fs.StoreChannel)113 Test (org.junit.Test)71 File (java.io.File)65 ByteBuffer (java.nio.ByteBuffer)48 IOException (java.io.IOException)20 DelegatingStoreChannel (org.neo4j.graphdb.mockfs.DelegatingStoreChannel)17 PhysicalLogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel)15 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)13 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)11 LogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel)10 ReadAheadLogChannel (org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel)7 LogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeader)7 LogHeaderReader.readLogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader)7 InputStream (java.io.InputStream)6 PageSwapperFactory (org.neo4j.io.pagecache.PageSwapperFactory)6 PageSwapperTest (org.neo4j.io.pagecache.PageSwapperTest)6 HashSet (java.util.HashSet)5 AdversarialPagedFile (org.neo4j.adversaries.pagecache.AdversarialPagedFile)5 PageSwapper (org.neo4j.io.pagecache.PageSwapper)5 PagedFile (org.neo4j.io.pagecache.PagedFile)5