use of org.neo4j.kernel.impl.api.TransactionToApply in project neo4j by neo4j.
the class ReplayableCommitProcessTest method shouldCommitTransactions.
@Test
public void shouldCommitTransactions() throws Exception {
// given
TransactionToApply newTx1 = mock(TransactionToApply.class);
TransactionToApply newTx2 = mock(TransactionToApply.class);
TransactionToApply newTx3 = mock(TransactionToApply.class);
StubLocalDatabase localDatabase = new StubLocalDatabase(1);
final ReplayableCommitProcess txListener = new ReplayableCommitProcess(localDatabase, localDatabase);
// when
txListener.commit(newTx1, NULL, EXTERNAL);
txListener.commit(newTx2, NULL, EXTERNAL);
txListener.commit(newTx3, NULL, EXTERNAL);
// then
verify(localDatabase.commitProcess, times(3)).commit(any(TransactionToApply.class), any(CommitEvent.class), any(TransactionApplicationMode.class));
}
use of org.neo4j.kernel.impl.api.TransactionToApply in project neo4j by neo4j.
the class ReplicatedTransactionStateMachineTest method createFakeTransactionCommitProcess.
private TransactionCommitProcess createFakeTransactionCommitProcess(long txId) throws TransactionFailureException {
TransactionCommitProcess localCommitProcess = mock(TransactionCommitProcess.class);
when(localCommitProcess.commit(any(TransactionToApply.class), any(CommitEvent.class), any(TransactionApplicationMode.class))).thenAnswer(invocation -> {
TransactionToApply txToApply = (TransactionToApply) invocation.getArguments()[0];
txToApply.commitment(new FakeCommitment(txId, mock(TransactionIdStore.class)), txId);
txToApply.commitment().publishAsCommitted();
txToApply.commitment().publishAsClosed();
txToApply.close();
return txId;
});
return localCommitProcess;
}
use of org.neo4j.kernel.impl.api.TransactionToApply in project neo4j by neo4j.
the class ApplyTransactionsCommand method applyTransactions.
private long applyTransactions(File fromPath, GraphDatabaseAPI toDb, long fromTxExclusive, long toTxInclusive, PrintStream out) throws IOException, TransactionFailureException {
DependencyResolver resolver = toDb.getDependencyResolver();
TransactionRepresentationCommitProcess commitProcess = new TransactionRepresentationCommitProcess(resolver.resolveDependency(TransactionAppender.class), resolver.resolveDependency(StorageEngine.class));
LifeSupport life = new LifeSupport();
try (DefaultFileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
PageCache pageCache = StandalonePageCacheFactory.createPageCache(fileSystem)) {
LogicalTransactionStore source = life.add(new ReadOnlyTransactionStore(pageCache, fileSystem, fromPath, new Monitors()));
life.start();
long lastAppliedTx = fromTxExclusive;
// Some progress if there are more than a couple of transactions to apply
ProgressListener progress = toTxInclusive - fromTxExclusive >= 100 ? textual(out).singlePart("Application progress", toTxInclusive - fromTxExclusive) : ProgressListener.NONE;
try (IOCursor<CommittedTransactionRepresentation> cursor = source.getTransactions(fromTxExclusive + 1)) {
while (cursor.next()) {
CommittedTransactionRepresentation transaction = cursor.get();
TransactionRepresentation transactionRepresentation = transaction.getTransactionRepresentation();
try {
commitProcess.commit(new TransactionToApply(transactionRepresentation), NULL, EXTERNAL);
progress.add(1);
} catch (final Throwable e) {
System.err.println("ERROR applying transaction " + transaction.getCommitEntry().getTxId());
throw e;
}
lastAppliedTx = transaction.getCommitEntry().getTxId();
if (lastAppliedTx == toTxInclusive) {
break;
}
}
}
return lastAppliedTx;
} finally {
life.shutdown();
}
}
use of org.neo4j.kernel.impl.api.TransactionToApply in project neo4j by neo4j.
the class BatchingTxApplier method queue.
/**
* Queues a transaction for application.
*
* @param tx The transaction to be queued for application.
*/
public void queue(CommittedTransactionRepresentation tx) throws Exception {
long receivedTxId = tx.getCommitEntry().getTxId();
long expectedTxId = lastQueuedTxId + 1;
if (receivedTxId != expectedTxId) {
log.warn("Out of order transaction. Received: %d Expected: %d", receivedTxId, expectedTxId);
return;
}
txQueue.queue(new TransactionToApply(tx.getTransactionRepresentation(), receivedTxId));
if (!stopped) {
lastQueuedTxId = receivedTxId;
monitor.txPullResponse(receivedTxId);
}
}
use of org.neo4j.kernel.impl.api.TransactionToApply in project neo4j by neo4j.
the class ReplicatedTransactionStateMachine method applyCommand.
@Override
public synchronized void applyCommand(ReplicatedTransaction replicatedTx, long commandIndex, Consumer<Result> callback) {
if (commandIndex <= lastCommittedIndex) {
log.debug("Ignoring transaction at log index %d since already committed up to %d", commandIndex, lastCommittedIndex);
return;
}
TransactionRepresentation tx;
byte[] extraHeader = encodeLogIndexAsTxHeader(commandIndex);
tx = ReplicatedTransactionFactory.extractTransactionRepresentation(replicatedTx, extraHeader);
int currentTokenId = lockTokenStateMachine.currentToken().id();
int txLockSessionId = tx.getLockSessionId();
if (currentTokenId != txLockSessionId && txLockSessionId != Locks.Client.NO_LOCK_SESSION_ID) {
callback.accept(Result.of(new TransactionFailureException(LockSessionExpired, "The lock session in the cluster has changed: [current lock session id:%d, tx lock session id:%d]", currentTokenId, txLockSessionId)));
} else {
try {
TransactionToApply transaction = new TransactionToApply(tx);
transaction.onClose(txId -> callback.accept(Result.of(txId)));
queue.queue(transaction);
} catch (Exception e) {
throw panicException(e);
}
}
}
Aggregations