Search in sources :

Example 46 with StoreChannel

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

the class PhysicalFlushableChannelTest method shouldThrowIllegalStateExceptionAfterClosed.

@Test
public void shouldThrowIllegalStateExceptionAfterClosed() throws Exception {
    // GIVEN
    final File file = new File(directory.directory(), "file");
    StoreChannel storeChannel = fileSystemRule.get().open(file, "rw");
    PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, 1, (byte) -1);
    PhysicalFlushableChannel channel = new PhysicalFlushableChannel(versionedStoreChannel);
    // closing the WritableLogChannel, then the underlying channel is what PhysicalLogFile does
    channel.close();
    storeChannel.close();
    // WHEN just appending something to the buffer
    channel.put((byte) 0);
    // and wanting to empty that into the channel
    try {
        channel.prepareForFlush();
        fail("Should have thrown exception");
    } catch (IllegalStateException e) {
    // THEN we should get an IllegalStateException, not a ClosedChannelException
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) Test(org.junit.Test)

Example 47 with StoreChannel

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

the class PhysicalFlushableChannelTest method shouldBeAbleToWriteSmallNumberOfBytes.

@Test
public void shouldBeAbleToWriteSmallNumberOfBytes() throws IOException {
    final File firstFile = new File(directory.directory(), "file1");
    StoreChannel storeChannel = fileSystemRule.get().open(firstFile, "rw");
    PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(storeChannel, 1, (byte) -1);
    PhysicalFlushableChannel channel = new PhysicalFlushableChannel(versionedStoreChannel);
    int length = 26_145;
    byte[] bytes = generateBytes(length);
    channel.put(bytes, length);
    channel.close();
    byte[] writtenBytes = new byte[length];
    try (InputStream in = new FileInputStream(firstFile)) {
        in.read(writtenBytes);
    }
    assertArrayEquals(bytes, writtenBytes);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 48 with StoreChannel

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

the class PhysicalLogFileTest method shouldCloseChannelInFailedAttemptToReadHeaderAfterOpen.

@Test
public void shouldCloseChannelInFailedAttemptToReadHeaderAfterOpen() throws Exception {
    // GIVEN a file which returns 1/2 log header size worth of bytes
    File directory = new File("/dir");
    FileSystemAbstraction fs = mock(FileSystemAbstraction.class);
    PhysicalLogFiles logFiles = new PhysicalLogFiles(directory, fs);
    int logVersion = 0;
    File logFile = logFiles.getLogFileForVersion(logVersion);
    StoreChannel channel = mock(StoreChannel.class);
    when(channel.read(any(ByteBuffer.class))).thenReturn(LogHeader.LOG_HEADER_SIZE / 2);
    when(fs.fileExists(logFile)).thenReturn(true);
    when(fs.open(eq(logFile), anyString())).thenReturn(channel);
    // WHEN
    try {
        PhysicalLogFile.openForVersion(logFiles, fs, logVersion, false);
        fail("Should have failed");
    } catch (IncompleteLogHeaderException e) {
        // THEN good
        verify(channel).close();
    }
}
Also used : FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) IncompleteLogHeaderException(org.neo4j.kernel.impl.transaction.log.entry.IncompleteLogHeaderException) Test(org.junit.Test)

Example 49 with StoreChannel

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

the class PhysicalLogFileTest method shouldSuppressFailueToCloseChannelInFailedAttemptToReadHeaderAfterOpen.

@Test
public void shouldSuppressFailueToCloseChannelInFailedAttemptToReadHeaderAfterOpen() throws Exception {
    // GIVEN a file which returns 1/2 log header size worth of bytes
    File directory = new File("/dir");
    FileSystemAbstraction fs = mock(FileSystemAbstraction.class);
    PhysicalLogFiles logFiles = new PhysicalLogFiles(directory, fs);
    int logVersion = 0;
    File logFile = logFiles.getLogFileForVersion(logVersion);
    StoreChannel channel = mock(StoreChannel.class);
    when(channel.read(any(ByteBuffer.class))).thenReturn(LogHeader.LOG_HEADER_SIZE / 2);
    when(fs.fileExists(logFile)).thenReturn(true);
    when(fs.open(eq(logFile), anyString())).thenReturn(channel);
    doThrow(IOException.class).when(channel).close();
    // WHEN
    try {
        PhysicalLogFile.openForVersion(logFiles, fs, logVersion, false);
        fail("Should have failed");
    } catch (IncompleteLogHeaderException e) {
        // THEN good
        verify(channel).close();
        assertEquals(1, e.getSuppressed().length);
        assertTrue(e.getSuppressed()[0] instanceof IOException);
    }
}
Also used : FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) StoreChannel(org.neo4j.io.fs.StoreChannel) IOException(java.io.IOException) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) IncompleteLogHeaderException(org.neo4j.kernel.impl.transaction.log.entry.IncompleteLogHeaderException) Test(org.junit.Test)

Example 50 with StoreChannel

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

the class LogHeaderWriterTest method shouldWriteALogHeaderInAStoreChannel.

@Test
public void shouldWriteALogHeaderInAStoreChannel() throws IOException {
    // given
    final File file = File.createTempFile("WriteLogHeader", getClass().getSimpleName());
    final StoreChannel channel = fileSystemRule.get().open(file, "rw");
    // when
    writeLogHeader(channel, expectedLogVersion, expectedTxId);
    channel.close();
    // then
    final byte[] array = new byte[LOG_HEADER_SIZE];
    try (InputStream stream = fileSystemRule.get().openAsInputStream(file)) {
        int read = stream.read(array);
        assertEquals(LOG_HEADER_SIZE, read);
    }
    final ByteBuffer result = ByteBuffer.wrap(array);
    long encodedLogVersions = result.getLong();
    assertEquals(encodeLogVersion(expectedLogVersion), encodedLogVersions);
    byte logFormatVersion = decodeLogFormatVersion(encodedLogVersions);
    assertEquals(CURRENT_LOG_VERSION, logFormatVersion);
    long logVersion = decodeLogVersion(encodedLogVersions);
    assertEquals(expectedLogVersion, logVersion);
    long txId = result.getLong();
    assertEquals(expectedTxId, txId);
}
Also used : InputStream(java.io.InputStream) StoreChannel(org.neo4j.io.fs.StoreChannel) 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