public LogPosition newPosition() { return specified ? new LogPosition( logVersion, byteOffset ) : LogPosition.UNSPECIFIED; }
public static LogPosition start( long logVersion ) { return new LogPosition( logVersion, LOG_HEADER_SIZE ); }
@Parameterized.Parameters public static Collection<LogPosition[]> logPositions() { return Arrays.asList( new LogPosition[]{new LogPosition( 0, 1 ), new LogPosition( 0, 0 )}, new LogPosition[]{new LogPosition( 0, 11 ), new LogPosition( 0, 7 )}, new LogPosition[]{new LogPosition( 2, 1 ), new LogPosition( 2, 0 )}, new LogPosition[]{new LogPosition( 2, 17 ), new LogPosition( 2, 15 )}, new LogPosition[]{new LogPosition( 1, 1 ), new LogPosition( 0, 1 )}, new LogPosition[]{new LogPosition( 5, 1 ), new LogPosition( 3, 10 )}, new LogPosition[]{new LogPosition( Integer.MAX_VALUE, Integer.MAX_VALUE + 1L ), new LogPosition( Integer.MAX_VALUE, Integer.MAX_VALUE )}, new LogPosition[]{new LogPosition( Long.MAX_VALUE, Long.MAX_VALUE ), new LogPosition( Integer.MAX_VALUE + 1L, Long.MAX_VALUE )}, new LogPosition[]{new LogPosition( Long.MAX_VALUE, Long.MAX_VALUE ), new LogPosition( Long.MAX_VALUE, Long.MAX_VALUE - 1 )} ); }
LogPosition readLastTxLogPosition( DatabaseLayout migrationStructure ) throws IOException { long[] counters = readTxLogCounters( fileSystem, lastTxLogPositionFile( migrationStructure ), 2 ); return new LogPosition( counters[0], counters[1] ); }
@Test public void bigFileLatestCheckpointFindsStartAfter() throws Throwable { long firstTxAfterCheckpoint = Integer.MAX_VALUE + 4L; LogTailScanner tailScanner = new FirstTxIdConfigurableTailScanner( firstTxAfterCheckpoint, logFiles, reader, monitors ); LogEntryStart startEntry = new LogEntryStart( 1, 2, 3L, 4L, new byte[]{5, 6}, new LogPosition( endLogVersion, Integer.MAX_VALUE + 17L ) ); CheckPoint checkPoint = new CheckPoint( new LogPosition( endLogVersion, 16L ) ); LogTailInformation logTailInformation = tailScanner.checkpointTailInformation( endLogVersion, startEntry, endLogVersion, latestLogEntryVersion, checkPoint, false); assertLatestCheckPoint( true, true, firstTxAfterCheckpoint, endLogVersion, logTailInformation ); }
@Parameterized.Parameters( name = "{0}" ) public static Collection<Object[]> versions() { return Arrays.<Object[]>asList( new Object[]{ StandardV2_3.STORE_VERSION, new LogPosition( 3, 169 ), txInfoAcceptanceOnIdAndTimestamp( 39, UNKNOWN_TX_COMMIT_TIMESTAMP ) } ); }
@Test public void shouldRespectStartLogPosition() throws Exception { // GIVEN TransactionCursor cursor = new ReversedMultiFileTransactionCursor( log( 5, 6, 8 ), 2, new LogPosition( 1, LOG_HEADER_SIZE + 3 ) ); // WHEN CommittedTransactionRepresentation[] reversed = exhaust( cursor ); // THEN assertTransactionRange( reversed, 5 + 6 + 8, 5 + 3 ); }
@Test public void shouldBeAbleToWriteACheckPoint() throws Throwable { // Given FlushablePositionAwareChannel channel = mock( FlushablePositionAwareChannel.class, RETURNS_MOCKS ); Flushable flushable = mock( Flushable.class ); when( channel.prepareForFlush() ).thenReturn( flushable ); when( channel.putLong( anyLong() ) ).thenReturn( channel ); when( logFile.getWriter() ).thenReturn( channel ); BatchingTransactionAppender appender = life.add( createTransactionAppender() ); // When appender.checkPoint( new LogPosition( 1L, 2L ), LogCheckPointEvent.NULL ); // Then verify( channel, times( 1 ) ).putLong( 1L ); verify( channel, times( 1 ) ).putLong( 2L ); verify( channel, times( 1 ) ).prepareForFlush(); verify( flushable, times( 1 ) ).flush(); verifyZeroInteractions( databaseHealth ); }
@Test public void shouldReturnTheMarkedPosition() { // given final LogPositionMarker marker = new LogPositionMarker(); // when marker.mark( 1, 2 ); final LogPosition logPosition = marker.newPosition(); // given assertEquals(new LogPosition( 1, 2 ), logPosition); }
@Test public void shouldReturnTheTxValueTIfInTheCached() { // given final TransactionMetadataCache cache = new TransactionMetadataCache(); final LogPosition position = new LogPosition( 3, 4 ); final int txId = 42; final int masterId = 0; final int authorId = 1; final int checksum = 2; final long timestamp = System.currentTimeMillis(); // when cache.cacheTransactionMetadata( txId, position, masterId, authorId, checksum, timestamp ); final TransactionMetadataCache.TransactionMetadata metadata = cache.getTransactionMetadata( txId ); // then assertEquals( new TransactionMetadataCache.TransactionMetadata( masterId, authorId, position, checksum, timestamp ), metadata ); }
@Test public void shouldClearTheCache() { // given final TransactionMetadataCache cache = new TransactionMetadataCache(); final LogPosition position = new LogPosition( 3, 4 ); final int txId = 42; final int masterId = 0; final int authorId = 1; final int checksum = 2; final long timestamp = System.currentTimeMillis(); // when cache.cacheTransactionMetadata( txId, position, masterId, authorId, checksum, timestamp ); cache.clear(); final TransactionMetadataCache.TransactionMetadata metadata = cache.getTransactionMetadata( txId ); // then assertNull( metadata ); } }
private static ObjectLongMap<Class> getLogEntriesDistribution( LogFiles logFiles ) throws IOException { LogFile transactionLogFile = logFiles.getLogFile(); LogPosition fileStartPosition = new LogPosition( 0, LogHeader.LOG_HEADER_SIZE ); VersionAwareLogEntryReader<ReadableLogChannel> entryReader = new VersionAwareLogEntryReader<>(); MutableObjectLongMap<Class> multiset = new ObjectLongHashMap<>(); try ( ReadableLogChannel fileReader = transactionLogFile.getReader( fileStartPosition ) ) { LogEntry logEntry = entryReader.readLogEntry( fileReader ); while ( logEntry != null ) { multiset.addToValue( logEntry.getClass(), 1 ); logEntry = entryReader.readLogEntry( fileReader ); } } return multiset; }
@Test public void doNotPruneNonCorruptedLogs() throws IOException { life.start(); generateTransactionLogFiles( logFiles ); long highestLogVersion = logFiles.getHighestLogVersion(); long fileSizeBeforePrune = logFiles.getHighestLogFile().length(); LogPosition endOfLogsPosition = new LogPosition( highestLogVersion, fileSizeBeforePrune ); assertEquals( TOTAL_NUMBER_OF_LOG_FILES - 1, highestLogVersion ); logPruner.truncate( endOfLogsPosition ); assertEquals( TOTAL_NUMBER_OF_LOG_FILES, logFiles.logFiles().length ); assertEquals( fileSizeBeforePrune, logFiles.getHighestLogFile().length() ); assertTrue( ArrayUtil.isEmpty( databaseDirectory.listFiles( File::isDirectory ) ) ); }
@Test public void shouldFindLogPosition() throws NoSuchTransactionException { // given final long txId = 42L; final PhysicalLogicalTransactionStore.LogVersionLocator locator = new PhysicalLogicalTransactionStore.LogVersionLocator( txId ); final LogPosition position = new LogPosition( 1, 128 ); // when final boolean result = locator.visit( position, firstTxIdInLog, lastTxIdInLog ); // then assertFalse( result ); assertEquals( position, locator.getLogPosition() ); }
@Test public void shouldReturnLogPositionToRecoverFromIfNeeded() { // given LogPosition checkPointLogPosition = new LogPosition( 1L, 4242 ); when( tailScanner.getTailInformation() ) .thenReturn( new LogTailInformation( new CheckPoint( checkPointLogPosition ), true, 10L, logVersion, currentLogVersion, LogEntryVersion.CURRENT ) ); // when RecoveryStartInformation recoveryStartInformation = new RecoveryStartInformationProvider( tailScanner, monitor ).get(); // then verify( monitor ).commitsAfterLastCheckPoint( checkPointLogPosition, 10L ); assertEquals( checkPointLogPosition, recoveryStartInformation.getRecoveryPosition() ); assertEquals( 10L, recoveryStartInformation.getFirstTxIdAfterLastCheckPoint() ); assertTrue( recoveryStartInformation.isRecoveryRequired() ); }
private static boolean checkPointInTxLog( GraphDatabaseService db ) throws IOException { LogFiles logFiles = ((GraphDatabaseAPI)db).getDependencyResolver().resolveDependency( LogFiles.class ); LogFile logFile = logFiles.getLogFile(); try ( ReadableLogChannel reader = logFile.getReader( new LogPosition( 0, LOG_HEADER_SIZE ) ) ) { LogEntryReader<ReadableClosablePositionAwareChannel> logEntryReader = new VersionAwareLogEntryReader<>(); LogEntry entry; while ( (entry = logEntryReader.readLogEntry( reader )) != null ) { if ( entry instanceof CheckPoint ) { return true; } } return false; } }
@Test public void writeAndReadLastTxLogPosition() throws IOException { StoreMigrator migrator = newStoreMigrator(); LogPosition writtenLogPosition = new LogPosition( random.nextLong(), random.nextLong() ); migrator.writeLastTxLogPosition( directory.databaseLayout(), writtenLogPosition ); LogPosition readLogPosition = migrator.readLastTxLogPosition( directory.databaseLayout() ); assertEquals( writtenLogPosition, readLogPosition ); }
@Test public void shouldParseCheckPointEntry() throws IOException { // given final CheckPoint checkPoint = new CheckPoint( new LogPosition( 43, 44 ) ); final InMemoryClosableChannel channel = new InMemoryClosableChannel(); channel.putLong( checkPoint.getLogPosition().getLogVersion() ); channel.putLong( checkPoint.getLogPosition().getByteOffset() ); channel.getCurrentPosition( marker ); // when final LogEntryParser parser = version.entryParser( LogEntryByteCodes.CHECK_POINT ); final LogEntry logEntry = parser.parse( version, channel, marker, commandReader ); // then assertEquals( checkPoint, logEntry ); assertFalse( parser.skip() ); }
@Test public void shouldReadAStartLogEntry() throws IOException { // given LogEntryVersion version = LogEntryVersion.CURRENT; final LogEntryStart start = new LogEntryStart( version, 1, 2, 3, 4, new byte[]{5}, new LogPosition( 0, 31 ) ); final InMemoryClosableChannel channel = new InMemoryClosableChannel(); channel.put( version.byteCode() ); // version channel.put( LogEntryByteCodes.TX_START ); // type channel.putInt( start.getMasterId() ); channel.putInt( start.getLocalId() ); channel.putLong( start.getTimeWritten() ); channel.putLong( start.getLastCommittedTxWhenTransactionStarted() ); channel.putInt( start.getAdditionalHeader().length ); channel.put( start.getAdditionalHeader(), start.getAdditionalHeader().length ); // when final LogEntry logEntry = logEntryReader.readLogEntry( channel ); // then assertEquals( start, logEntry ); }
@Test public void shouldReadACheckPointLogEntry() throws IOException { // given LogEntryVersion version = LogEntryVersion.CURRENT; final LogPosition logPosition = new LogPosition( 42, 43 ); final CheckPoint checkPoint = new CheckPoint( version, logPosition ); final InMemoryClosableChannel channel = new InMemoryClosableChannel(); channel.put( version.byteCode() ); channel.put( LogEntryByteCodes.CHECK_POINT ); channel.putLong( logPosition.getLogVersion() ); channel.putLong( logPosition.getByteOffset() ); // when final LogEntry logEntry = logEntryReader.readLogEntry( channel ); // then assertEquals( checkPoint, logEntry ); }