use of org.neo4j.kernel.impl.transaction.TransactionRepresentation in project neo4j by neo4j.
the class LegacyLogEntryWriterTest method shouldWriteAllTheEntryInACommitToTheFile.
@Test
public void shouldWriteAllTheEntryInACommitToTheFile() throws IOException {
// given
final LogVersionedStoreChannel channel = mock(LogVersionedStoreChannel.class);
final LogEntryWriter logEntryWriter = mock(LogEntryWriter.class);
final LegacyLogEntryWriter writer = new LegacyLogEntryWriter(fs, liftToFactory(logEntryWriter));
final LogEntryStart start = new LogEntryStart(0, 1, 2L, 3L, EMPTY_ADDITIONAL_ARRAY, UNSPECIFIED);
final LogEntryCommand command = new LogEntryCommand(new Command.NodeCommand(nodeRecord, nodeRecord));
final LogEntryCommit commit = new OnePhaseCommit(42L, 43L);
// when
final IOCursor<LogEntry> cursor = mockCursor(start, command, commit);
writer.writeAllLogEntries(channel, cursor);
// then
verify(logEntryWriter, times(1)).writeStartEntry(0, 1, 2L, 3L, EMPTY_ADDITIONAL_ARRAY);
final TransactionRepresentation expected = new PhysicalTransactionRepresentation(Arrays.asList(command.getXaCommand()));
verify(logEntryWriter, times(1)).serialize(eq(expected));
verify(logEntryWriter, times(1)).writeCommitEntry(42L, 43L);
}
use of org.neo4j.kernel.impl.transaction.TransactionRepresentation 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.transaction.TransactionRepresentation 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);
}
}
}
use of org.neo4j.kernel.impl.transaction.TransactionRepresentation in project neo4j by neo4j.
the class RaftContentByteBufferMarshalTest method shouldSerializeTransactionRepresentation.
@Test
public void shouldSerializeTransactionRepresentation() throws Exception {
// given
CoreReplicatedContentMarshal serializer = new CoreReplicatedContentMarshal();
Collection<StorageCommand> commands = new ArrayList<>();
IndexCommand.AddNodeCommand addNodeCommand = new IndexCommand.AddNodeCommand();
addNodeCommand.init(0, 0, 0, 0);
commands.add(addNodeCommand);
byte[] extraHeader = new byte[0];
PhysicalTransactionRepresentation txIn = new PhysicalTransactionRepresentation(commands);
txIn.setHeader(extraHeader, -1, -1, 0, 0, 0, 0);
ReplicatedTransaction in = ReplicatedTransactionFactory.createImmutableReplicatedTransaction(txIn);
// when
ByteBuf buf = Unpooled.buffer();
serializer.marshal(in, new NetworkFlushableByteBuf(buf));
ReplicatedTransaction out = (ReplicatedTransaction) serializer.unmarshal(new NetworkReadableClosableChannelNetty4(buf));
TransactionRepresentation txOut = ReplicatedTransactionFactory.extractTransactionRepresentation(out, extraHeader);
// then
assertEquals(in, out);
assertEquals(txIn, txOut);
}
use of org.neo4j.kernel.impl.transaction.TransactionRepresentation in project neo4j by neo4j.
the class KernelTransactionsTest method shouldIncludeRandomBytesInAdditionalHeader.
@Test
public void shouldIncludeRandomBytesInAdditionalHeader() throws Throwable {
// Given
TransactionRepresentation[] transactionRepresentation = new TransactionRepresentation[1];
KernelTransactions registry = newKernelTransactions(newRememberingCommitProcess(transactionRepresentation));
// When
try (KernelTransaction transaction = getKernelTransaction(registry)) {
// Just pick anything that can flag that changes have been made to this transaction
((KernelTransactionImplementation) transaction).txState().nodeDoCreate(0);
transaction.success();
}
// Then
byte[] additionalHeader = transactionRepresentation[0].additionalHeader();
assertNotNull(additionalHeader);
assertTrue(additionalHeader.length > 0);
}
Aggregations