use of org.neo4j.kernel.impl.transaction.log.TransactionCursor in project neo4j by neo4j.
the class ReversedMultiFileTransactionCursorTest method log.
private static ThrowingFunction<LogPosition, TransactionCursor, IOException> log(int... transactionCounts) throws IOException {
long baseOffset = start().getByteOffset();
@SuppressWarnings("unchecked") ThrowingFunction<LogPosition, TransactionCursor, IOException> result = mock(ThrowingFunction.class);
AtomicLong txId = new AtomicLong(0);
CommittedTransactionRepresentation[][] logs = new CommittedTransactionRepresentation[transactionCounts.length][];
for (int logVersion = 0; logVersion < transactionCounts.length; logVersion++) {
logs[logVersion] = transactions(transactionCounts[logVersion], txId);
}
when(result.apply(any(LogPosition.class))).thenAnswer(invocation -> {
LogPosition position = invocation.getArgument(0);
if (position == null) {
// A mockito issue when calling the "when" methods, I believe
return null;
}
// For simplicity the offset means, in this test, the array offset
CommittedTransactionRepresentation[] transactions = logs[toIntExact(position.getLogVersion())];
CommittedTransactionRepresentation[] subset = copyOfRange(transactions, toIntExact(position.getByteOffset() - baseOffset), transactions.length);
ArrayUtil.reverse(subset);
return given(subset);
});
return result;
}
use of org.neo4j.kernel.impl.transaction.log.TransactionCursor in project neo4j by neo4j.
the class ReversedMultiFileTransactionCursorTest method shouldHandleEmptyLogsMidStream.
@Test
void shouldHandleEmptyLogsMidStream() throws Exception {
// GIVEN
TransactionCursor cursor = new ReversedMultiFileTransactionCursor(logFile, log(5, 0, 2, 0, 3), 4, start());
// WHEN
CommittedTransactionRepresentation[] reversed = exhaust(cursor);
// THEN
assertTransactionRange(reversed, 5 + 2 + 3, 0);
}
use of org.neo4j.kernel.impl.transaction.log.TransactionCursor in project neo4j by neo4j.
the class Recovery method init.
@Override
public void init() throws Throwable {
LogPosition recoveryFromPosition = spi.getPositionToRecoverFrom();
if (LogPosition.UNSPECIFIED.equals(recoveryFromPosition)) {
return;
}
monitor.recoveryRequired(recoveryFromPosition);
LogPosition recoveryToPosition;
CommittedTransactionRepresentation lastTransaction = null;
Visitor<CommittedTransactionRepresentation, Exception> recoveryVisitor = spi.startRecovery();
try (TransactionCursor transactionsToRecover = spi.getTransactions(recoveryFromPosition)) {
while (transactionsToRecover.next()) {
lastTransaction = transactionsToRecover.get();
long txId = lastTransaction.getCommitEntry().getTxId();
recoveryVisitor.visit(lastTransaction);
monitor.transactionRecovered(txId);
numberOfRecoveredTransactions++;
}
recoveryToPosition = transactionsToRecover.position();
}
if (recoveryToPosition.equals(LogPosition.UNSPECIFIED)) {
recoveryToPosition = recoveryFromPosition;
}
spi.allTransactionsRecovered(lastTransaction, recoveryToPosition);
recoveredLog = true;
spi.forceEverything();
}
use of org.neo4j.kernel.impl.transaction.log.TransactionCursor in project neo4j by neo4j.
the class ResponsePackerTest method shouldHaveFixedTargetTransactionIdEvenIfLastTransactionIdIsMoving.
@Test
public void shouldHaveFixedTargetTransactionIdEvenIfLastTransactionIdIsMoving() throws Exception {
// GIVEN
LogicalTransactionStore transactionStore = mock(LogicalTransactionStore.class);
long lastAppliedTransactionId = 5L;
TransactionCursor endlessCursor = new EndlessCursor(lastAppliedTransactionId + 1);
when(transactionStore.getTransactions(anyLong())).thenReturn(endlessCursor);
final long targetTransactionId = 8L;
final TransactionIdStore transactionIdStore = new DeadSimpleTransactionIdStore(targetTransactionId, 0, BASE_TX_COMMIT_TIMESTAMP, 0, 0);
ResponsePacker packer = new ResponsePacker(transactionStore, transactionIdStore, Suppliers.singleton(StoreIdTestFactory.newStoreIdForCurrentVersion()));
// WHEN
Response<Object> response = packer.packTransactionStreamResponse(requestContextStartingAt(5L), null);
final AtomicLong nextExpectedVisit = new AtomicLong(lastAppliedTransactionId);
response.accept(new Response.Handler() {
@Override
public void obligation(long txId) throws IOException {
fail("Should not be called");
}
@Override
public Visitor<CommittedTransactionRepresentation, Exception> transactions() {
return new Visitor<CommittedTransactionRepresentation, Exception>() {
@Override
public boolean visit(CommittedTransactionRepresentation element) {
// THEN
long txId = element.getCommitEntry().getTxId();
assertThat(txId, lessThanOrEqualTo(targetTransactionId));
assertEquals(nextExpectedVisit.incrementAndGet(), txId);
// Move the target transaction id forward one step, effectively always keeping it out of reach
transactionIdStore.setLastCommittedAndClosedTransactionId(transactionIdStore.getLastCommittedTransactionId() + 1, 0, BASE_TX_COMMIT_TIMESTAMP, 3, 4);
return true;
}
};
}
});
}
use of org.neo4j.kernel.impl.transaction.log.TransactionCursor in project neo4j by neo4j.
the class RemoteStore method getPullIndex.
/**
* Later stages of the startup process require at least one transaction to
* figure out the mapping between the transaction log and the consensus log.
*
* If there are no transaction logs then we can pull from and including
* the index which the metadata store points to. This would be the case
* for example with a backup taken during an idle period of the system.
*
* However, if there are transaction logs then we want to find out where
* they end and pull from there, excluding the last one so that we do not
* get duplicate entries.
*/
private long getPullIndex(File storeDir) throws IOException {
/* this is the metadata store */
ReadOnlyTransactionIdStore txIdStore = new ReadOnlyTransactionIdStore(pageCache, storeDir);
/* Clean as in clean shutdown. Without transaction logs this should be the truth,
* but otherwise it can be used as a starting point for scanning the logs. */
long lastCleanTxId = txIdStore.getLastCommittedTransactionId();
/* these are the transaction logs */
ReadOnlyTransactionStore txStore = new ReadOnlyTransactionStore(pageCache, fs, storeDir, new Monitors());
long lastTxId = BASE_TX_ID;
try (Lifespan ignored = new Lifespan(txStore)) {
TransactionCursor cursor;
try {
cursor = txStore.getTransactions(lastCleanTxId);
} catch (NoSuchTransactionException e) {
log.info("No transaction logs found. Will use metadata store as base for pull request.");
return lastCleanTxId;
}
while (cursor.next()) {
CommittedTransactionRepresentation tx = cursor.get();
lastTxId = tx.getCommitEntry().getTxId();
}
if (lastTxId < lastCleanTxId) {
throw new IllegalStateException("Metadata index was higher than transaction log index.");
}
// we don't want to pull a transaction we already have in the log, hence +1
return lastTxId + 1;
}
}
Aggregations