Skip to content

Commit

Permalink
Better detection of multiple log versions
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint committed Sep 4, 2017
1 parent b272746 commit 0142842
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Expand Up @@ -74,6 +74,7 @@ private long[] sketchOutTransactionStartOffsets() throws IOException
long[] offsets = new long[10_000]; long[] offsets = new long[10_000];
int offsetCursor = 0; int offsetCursor = 0;


long logVersion = channel.getVersion();
long startOffset = channel.position(); long startOffset = channel.position();
while ( transactionCursor.next() ) while ( transactionCursor.next() )
{ {
Expand All @@ -82,11 +83,15 @@ private long[] sketchOutTransactionStartOffsets() throws IOException
offsets = Arrays.copyOf( offsets, offsetCursor * 2 ); offsets = Arrays.copyOf( offsets, offsetCursor * 2 );
} }
offsets[offsetCursor++] = startOffset; offsets[offsetCursor++] = startOffset;
long nextStartOffset = channel.position();
assert nextStartOffset > startOffset : "It looks like this channel moved over to another version";
startOffset = channel.position(); startOffset = channel.position();
} }


if ( channel.getVersion() != logVersion )
{
throw new IllegalArgumentException( "The channel which was passed in bridged multiple log versions, it started at version " +
logVersion + ", but continued through to version " + channel.getVersion() + ". This isn't supported" );
}

offsetsLength = offsetCursor; offsetsLength = offsetCursor;
chunkStartOffsetIndex = offsetCursor; chunkStartOffsetIndex = offsetCursor;
totalSize = channel.position(); totalSize = channel.position();
Expand Down
Expand Up @@ -41,7 +41,11 @@
import org.neo4j.test.rule.TestDirectory; import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.DefaultFileSystemRule; import org.neo4j.test.rule.fs.DefaultFileSystemRule;


import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import static org.neo4j.io.ByteUnit.mebiBytes; import static org.neo4j.io.ByteUnit.mebiBytes;
import static org.neo4j.kernel.impl.store.record.Record.NO_LABELS_FIELD; import static org.neo4j.kernel.impl.store.record.Record.NO_LABELS_FIELD;
import static org.neo4j.kernel.impl.transaction.log.GivenTransactionCursor.exhaust; import static org.neo4j.kernel.impl.transaction.log.GivenTransactionCursor.exhaust;
Expand Down Expand Up @@ -121,6 +125,27 @@ public void shouldHandleEmptyLog() throws Exception
assertEquals( 0, readTransactions.length ); assertEquals( 0, readTransactions.length );
} }


@Test
public void shouldDetectAndPreventChannelReadingMultipleLogVersions() throws Exception
{
// given
writeTransactions( 1, 1, 1 );
logFile.rotate();
writeTransactions( 1, 1, 1 );

// when
try ( ReadAheadLogChannel channel = (ReadAheadLogChannel) logFile.getReader( start( 0 ) ) )
{
new ReversedSingleFileTransactionCursor( channel, new VersionAwareLogEntryReader<>() );
fail( "Should've failed" );
}
catch ( IllegalArgumentException e )
{
// then good
assertThat( e.getMessage(), containsString( "multiple log versions" ) );
}
}

private CommittedTransactionRepresentation[] readAllFromReversedCursor() throws IOException private CommittedTransactionRepresentation[] readAllFromReversedCursor() throws IOException
{ {
try ( ReversedSingleFileTransactionCursor cursor = txCursor() ) try ( ReversedSingleFileTransactionCursor cursor = txCursor() )
Expand Down

0 comments on commit 0142842

Please sign in to comment.