Search in sources :

Example 76 with StoreChannel

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

the class SingleFilePageSwapper method tryReopen.

/**
     * Reopens the channel if it has been closed and the close() method on
     * this swapper has not been called. In other words, if the channel has
     * been "accidentally" closed by an interrupt or the like.
     *
     * If the channel has been explicitly closed with the PageSwapper#close()
     * method, then this method will re-throw the passed-in exception.
     *
     * If the reopening of the file fails with an exception for some reason,
     * then that exception is added as a suppressed exception to the passed in
     * ClosedChannelException, and the CCE is then rethrown.
     */
private synchronized void tryReopen(long filePageId, ClosedChannelException closedException) throws ClosedChannelException {
    int stripe = stripe(filePageId);
    StoreChannel channel = channels[stripe];
    if (channel.isOpen()) {
        // Someone got ahead of us, presumably. Nothing to do.
        return;
    }
    if (closed) {
        // channel.
        throw closedException;
    }
    try {
        channels[stripe] = fs.open(file, "rw");
        if (stripe == tokenChannelStripe) {
            // The closing of a FileChannel also releases all associated file locks.
            acquireLock();
        }
    } catch (IOException e) {
        closedException.addSuppressed(e);
        throw closedException;
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) IOException(java.io.IOException)

Example 77 with StoreChannel

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

the class PageCacheTest method writesOfDifferentUnitsMustHaveCorrectEndianess.

@Test(timeout = SHORT_TIMEOUT_MILLIS)
public void writesOfDifferentUnitsMustHaveCorrectEndianess() throws Exception {
    configureStandardPageCache();
    PagedFile pagedFile = pageCache.map(file("a"), 20);
    try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK)) {
        assertTrue(cursor.next());
        byte[] data = { 42, 43, 44, 45, 46 };
        //  0+8 = 8
        cursor.putLong(41);
        //  8+4 = 12
        cursor.putInt(41);
        // 12+2 = 14
        cursor.putShort((short) 41);
        // 14+1 = 15
        cursor.putByte((byte) 41);
        // 15+5 = 20
        cursor.putBytes(data);
    }
    try (PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK)) {
        assertTrue(cursor.next());
        //  8
        long a = cursor.getLong();
        // 12
        int b = cursor.getInt();
        // 14
        short c = cursor.getShort();
        byte[] data = new byte[] { // 15
        cursor.getByte(), // 16
        cursor.getByte(), // 17
        cursor.getByte(), // 18
        cursor.getByte(), // 19
        cursor.getByte(), // 20
        cursor.getByte() };
        cursor.setOffset(0);
        cursor.putLong(1 + a);
        cursor.putInt(1 + b);
        cursor.putShort((short) (1 + c));
        for (byte d : data) {
            d++;
            cursor.putByte(d);
        }
    }
    pagedFile.close();
    StoreChannel channel = fs.open(file("a"), "r");
    ByteBuffer buf = ByteBuffer.allocate(20);
    channel.read(buf);
    buf.flip();
    assertThat(buf.getLong(), is(42L));
    assertThat(buf.getInt(), is(42));
    assertThat(buf.getShort(), is((short) 42));
    assertThat(buf.get(), is((byte) 42));
    assertThat(buf.get(), is((byte) 43));
    assertThat(buf.get(), is((byte) 44));
    assertThat(buf.get(), is((byte) 45));
    assertThat(buf.get(), is((byte) 46));
    assertThat(buf.get(), is((byte) 47));
}
Also used : AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 78 with StoreChannel

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

the class PageCacheTest method evictionMustFlushPagesToTheRightFiles.

@Test(timeout = SHORT_TIMEOUT_MILLIS)
public void evictionMustFlushPagesToTheRightFiles() throws IOException {
    generateFileWithRecords(file("a"), recordCount, recordSize);
    // diff. page size just to be difficult
    int filePageSize2 = filePageSize - 3;
    long maxPageIdCursor1 = recordCount / recordsPerFilePage;
    File file2 = file("b");
    OutputStream outputStream = fs.openAsOutputStream(file2, false);
    long file2sizeBytes = (maxPageIdCursor1 + 17) * filePageSize2;
    for (int i = 0; i < file2sizeBytes; i++) {
        // We will ues the page cache to change these 'a's into 'b's.
        outputStream.write('a');
    }
    outputStream.flush();
    outputStream.close();
    configureStandardPageCache();
    PagedFile pagedFile1 = pageCache.map(file("a"), filePageSize);
    PagedFile pagedFile2 = pageCache.map(file2, filePageSize2);
    long pageId1 = 0;
    long pageId2 = 0;
    boolean moreWorkToDo;
    do {
        boolean cursorReady1;
        boolean cursorReady2;
        try (PageCursor cursor = pagedFile1.io(pageId1, PF_SHARED_WRITE_LOCK)) {
            cursorReady1 = cursor.next() && cursor.getCurrentPageId() < maxPageIdCursor1;
            if (cursorReady1) {
                writeRecords(cursor);
                pageId1++;
            }
        }
        try (PageCursor cursor = pagedFile2.io(pageId2, PF_SHARED_WRITE_LOCK | PF_NO_GROW)) {
            cursorReady2 = cursor.next();
            if (cursorReady2) {
                for (int i = 0; i < filePageSize2; i++) {
                    cursor.putByte((byte) 'b');
                }
                assertFalse(cursor.shouldRetry());
            }
            pageId2++;
        }
        moreWorkToDo = cursorReady1 || cursorReady2;
    } while (moreWorkToDo);
    pagedFile1.close();
    pagedFile2.close();
    // Verify the file contents
    assertThat(fs.getFileSize(file2), is(file2sizeBytes));
    InputStream inputStream = fs.openAsInputStream(file2);
    for (int i = 0; i < file2sizeBytes; i++) {
        int b = inputStream.read();
        assertThat(b, is((int) 'b'));
    }
    assertThat(inputStream.read(), is(-1));
    inputStream.close();
    StoreChannel channel = fs.open(file("a"), "r");
    ByteBuffer bufB = ByteBuffer.allocate(recordSize);
    for (int i = 0; i < recordCount; i++) {
        bufA.clear();
        channel.read(bufA);
        bufA.flip();
        bufB.clear();
        generateRecordForId(i, bufB);
        assertThat(bufB.array(), byteArray(bufA.array()));
    }
}
Also used : AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 79 with StoreChannel

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

the class PageCacheTest method lastPageIdOfFileWithOneByteIsZero.

@Test
public void lastPageIdOfFileWithOneByteIsZero() throws IOException {
    StoreChannel channel = fs.create(file("a"));
    channel.write(ByteBuffer.wrap(new byte[] { 1 }));
    channel.close();
    configureStandardPageCache();
    try (PagedFile pagedFile = pageCache.map(file("a"), filePageSize)) {
        assertThat(pagedFile.getLastPageId(), is(0L));
    }
}
Also used : AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) StoreChannel(org.neo4j.io.fs.StoreChannel) DelegatingStoreChannel(org.neo4j.graphdb.mockfs.DelegatingStoreChannel) Test(org.junit.Test)

Example 80 with StoreChannel

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

the class StateRecoveryManagerTest method shouldReturnTheEmptyFileAsPreviouslyInactiveWhenActiveContainsCorruptEntry.

@Test
public void shouldReturnTheEmptyFileAsPreviouslyInactiveWhenActiveContainsCorruptEntry() throws Exception {
    // given
    EphemeralFileSystemAbstraction fsa = fileSystemRule.get();
    fsa.mkdir(testDir.directory());
    File fileA = fileA();
    StoreChannel channel = fsa.create(fileA);
    ByteBuffer buffer = writeLong(999);
    channel.writeAll(buffer);
    channel.force(false);
    File fileB = fileB();
    channel = fsa.create(fileB);
    channel.close();
    StateRecoveryManager<Long> manager = new StateRecoveryManager<>(fsa, new LongMarshal());
    // when
    final StateRecoveryManager.RecoveryStatus recoveryStatus = manager.recover(fileA, fileB);
    // then
    assertEquals(999L, recoveryStatus.recoveredState());
    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)

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