Search in sources :

Example 1 with ReadPastEndException

use of org.neo4j.storageengine.api.ReadPastEndException in project neo4j by neo4j.

the class VersionAwareLogEntryReader method readLogEntry.

@Override
public LogEntry readLogEntry(SOURCE channel) throws IOException {
    try {
        LogPositionMarker positionMarker = new LogPositionMarker();
        channel.getCurrentPosition(positionMarker);
        while (true) {
            LogEntryVersion version = null;
            LogEntryParser<LogEntry> entryReader;
            try {
                /*
                     * if the read type is negative than it is actually the log entry version
                     * so we need to read an extra byte which will contain the type
                     */
                byte typeCode = channel.get();
                byte versionCode = 0;
                if (typeCode < 0) {
                    versionCode = typeCode;
                    typeCode = channel.get();
                }
                version = byVersion(versionCode);
                entryReader = version.entryParser(typeCode);
            } catch (ReadPastEndException e) {
                // Make these exceptions slip by straight out to the outer handler
                throw e;
            } catch (Exception e) {
                // Tag all other exceptions with log position and other useful information
                LogPosition position = positionMarker.newPosition();
                e = withMessage(e, e.getMessage() + ". At position " + position + " and entry version " + version);
                throw launderedException(IOException.class, e);
            }
            LogEntry entry = entryReader.parse(version, channel, positionMarker, commandReaderFactory);
            if (!entryReader.skip()) {
                return entry;
            }
        }
    } catch (ReadPastEndException e) {
        return null;
    }
}
Also used : IOException(java.io.IOException) ReadPastEndException(org.neo4j.storageengine.api.ReadPastEndException) ReadPastEndException(org.neo4j.storageengine.api.ReadPastEndException) Exceptions.launderedException(org.neo4j.helpers.Exceptions.launderedException) IOException(java.io.IOException) LogPositionMarker(org.neo4j.kernel.impl.transaction.log.LogPositionMarker) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition)

Example 2 with ReadPastEndException

use of org.neo4j.storageengine.api.ReadPastEndException in project neo4j by neo4j.

the class EntryRecord method read.

public static EntryRecord read(ReadableChannel channel, ChannelMarshal<ReplicatedContent> contentMarshal) throws IOException, EndOfStreamException {
    try {
        long appendIndex = channel.getLong();
        long term = channel.getLong();
        ReplicatedContent content = contentMarshal.unmarshal(channel);
        return new EntryRecord(appendIndex, new RaftLogEntry(term, content));
    } catch (ReadPastEndException e) {
        throw new EndOfStreamException(e);
    }
}
Also used : EndOfStreamException(org.neo4j.causalclustering.messaging.EndOfStreamException) ReplicatedContent(org.neo4j.causalclustering.core.replication.ReplicatedContent) ReadPastEndException(org.neo4j.storageengine.api.ReadPastEndException)

Example 3 with ReadPastEndException

use of org.neo4j.storageengine.api.ReadPastEndException in project neo4j by neo4j.

the class ReadAheadChannelTest method shouldThrowExceptionForReadAfterEOFIfNotEnoughBytesExist.

@Test
public void shouldThrowExceptionForReadAfterEOFIfNotEnoughBytesExist() 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);
    assertEquals((byte) 1, channel.get());
    try {
        channel.get();
        fail("Should have thrown exception signalling end of file reached");
    } catch (ReadPastEndException endOfFile) {
    // outstanding
    }
    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)

Example 4 with ReadPastEndException

use of org.neo4j.storageengine.api.ReadPastEndException 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)

Example 5 with ReadPastEndException

use of org.neo4j.storageengine.api.ReadPastEndException in project neo4j by neo4j.

the class ReadAheadChannelTest method shouldHandleRunningOutOfBytesWhenRequestSpansMultipleFiles.

@Test
public void shouldHandleRunningOutOfBytesWhenRequestSpansMultipleFiles() throws Exception {
    // Given
    FileSystemAbstraction fileSystem = fileSystemRule.get();
    StoreChannel storeChannel1 = fileSystem.open(new File("foo.1"), "rw");
    ByteBuffer buffer = ByteBuffer.allocate(2);
    buffer.put((byte) 0);
    buffer.put((byte) 0);
    buffer.flip();
    storeChannel1.writeAll(buffer);
    storeChannel1.force(false);
    storeChannel1.close();
    buffer.flip();
    StoreChannel storeChannel2 = fileSystem.open(new File("foo.2"), "r");
    buffer.put((byte) 0);
    buffer.put((byte) 1);
    buffer.flip();
    storeChannel2.writeAll(buffer);
    storeChannel2.force(false);
    storeChannel2.close();
    storeChannel1 = fileSystem.open(new File("foo.1"), "r");
    final StoreChannel storeChannel2Copy = fileSystem.open(new File("foo.2"), "r");
    ReadAheadChannel<StoreChannel> channel = new ReadAheadChannel<StoreChannel>(storeChannel1) {

        @Override
        protected StoreChannel next(StoreChannel channel) throws IOException {
            return storeChannel2Copy;
        }
    };
    try {
        channel.getLong();
        fail("Should have thrown exception signalling end of file reached");
    } catch (ReadPastEndException endOfFile) {
    // outstanding
    }
    assertEquals(1, channel.getInt());
    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

ReadPastEndException (org.neo4j.storageengine.api.ReadPastEndException)6 File (java.io.File)4 Test (org.junit.Test)4 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)4 StoreChannel (org.neo4j.io.fs.StoreChannel)4 ByteBuffer (java.nio.ByteBuffer)3 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)3 IOException (java.io.IOException)1 ReplicatedContent (org.neo4j.causalclustering.core.replication.ReplicatedContent)1 EndOfStreamException (org.neo4j.causalclustering.messaging.EndOfStreamException)1 Exceptions.launderedException (org.neo4j.helpers.Exceptions.launderedException)1 LogPosition (org.neo4j.kernel.impl.transaction.log.LogPosition)1 LogPositionMarker (org.neo4j.kernel.impl.transaction.log.LogPositionMarker)1