Search in sources :

Example 36 with TransactionToApply

use of org.neo4j.kernel.impl.api.TransactionToApply in project neo4j by neo4j.

the class BatchingNeoStoresTest method apply.

private static void apply(TxState txState, CommandCreationContext commandCreationContext, RecordStorageEngine storageEngine) throws Exception {
    List<StorageCommand> commands = new ArrayList<>();
    try (RecordStorageReader storageReader = storageEngine.newReader()) {
        storageEngine.createCommands(commands, txState, storageReader, commandCreationContext, ResourceLocker.IGNORE, LockTracer.NONE, BASE_TX_ID, v -> v, NULL, INSTANCE);
        CommandsToApply apply = new TransactionToApply(new PhysicalTransactionRepresentation(commands, new byte[0], 0, 0, 0, 0, ANONYMOUS), NULL);
        storageEngine.apply(apply, TransactionApplicationMode.INTERNAL);
    }
}
Also used : CommandsToApply(org.neo4j.storageengine.api.CommandsToApply) TransactionToApply(org.neo4j.kernel.impl.api.TransactionToApply) RecordStorageReader(org.neo4j.internal.recordstorage.RecordStorageReader) StorageCommand(org.neo4j.storageengine.api.StorageCommand) ArrayList(java.util.ArrayList) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)

Example 37 with TransactionToApply

use of org.neo4j.kernel.impl.api.TransactionToApply in project neo4j by neo4j.

the class CommitProcessTracingIT method tracePageCacheAccessOnTransactionApply.

@Test
void tracePageCacheAccessOnTransactionApply() throws TransactionFailureException {
    var transaction = new PhysicalTransactionRepresentation(List.of(new Command.NodeCountsCommand(1, 2)), EMPTY_BYTE_ARRAY, 0, 0, 0, 0, ANONYMOUS);
    var pageCacheTracer = new DefaultPageCacheTracer();
    try (var cursorContext = new CursorContext(pageCacheTracer.createPageCursorTracer("tracePageCacheAccessOnTransactionApply"))) {
        assertZeroCursor(cursorContext);
        commitProcess.commit(new TransactionToApply(transaction, cursorContext), NULL, EXTERNAL);
        assertCursor(cursorContext, 3);
    }
}
Also used : TransactionToApply(org.neo4j.kernel.impl.api.TransactionToApply) CursorContext(org.neo4j.io.pagecache.context.CursorContext) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation) DefaultPageCacheTracer(org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer) Test(org.junit.jupiter.api.Test)

Example 38 with TransactionToApply

use of org.neo4j.kernel.impl.api.TransactionToApply in project neo4j by neo4j.

the class TransactionRepresentationFactory method nextTransaction.

TransactionToApply nextTransaction(long txId) {
    PhysicalTransactionRepresentation representation = new PhysicalTransactionRepresentation(createRandomCommands());
    representation.setHeader(new byte[0], currentTimeMillis(), txId, currentTimeMillis(), 42, ANONYMOUS);
    return new TransactionToApply(representation, NULL);
}
Also used : TransactionToApply(org.neo4j.kernel.impl.api.TransactionToApply) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)

Example 39 with TransactionToApply

use of org.neo4j.kernel.impl.api.TransactionToApply in project neo4j by neo4j.

the class BatchingTransactionAppender method append.

@Override
public long append(TransactionToApply batch, LogAppendEvent logAppendEvent) throws IOException {
    // Assigned base tx id just to make compiler happy
    long lastTransactionId = TransactionIdStore.BASE_TX_ID;
    // Synchronized with logFile to get absolute control over concurrent rotations happening
    synchronized (logFile) {
        // Assert that kernel is healthy before making any changes
        databaseHealth.assertHealthy(IOException.class);
        try (SerializeTransactionEvent serialiseEvent = logAppendEvent.beginSerializeTransaction()) {
            // Append all transactions in this batch to the log under the same logFile monitor
            TransactionToApply tx = batch;
            while (tx != null) {
                long transactionId = transactionIdStore.nextCommittingTransactionId();
                // If we're in a scenario where we're merely replicating transactions, i.e. transaction
                // id have already been generated by another entity we simply check that our id
                // that we generated match that id. If it doesn't we've run into a problem we can't ยด
                // really recover from and would point to a bug somewhere.
                matchAgainstExpectedTransactionIdIfAny(transactionId, tx);
                TransactionCommitment commitment = appendToLog(tx.transactionRepresentation(), transactionId, logAppendEvent, previousChecksum);
                previousChecksum = commitment.getTransactionChecksum();
                tx.commitment(commitment, transactionId);
                tx.logPosition(commitment.logPosition());
                tx = tx.next();
                lastTransactionId = transactionId;
            }
        }
    }
    // in this batch exist durably on disk.
    if (logFile.forceAfterAppend(logAppendEvent)) {
        // We got lucky and were the one forcing the log. It's enough if ones of all doing concurrent committers
        // checks the need for log rotation.
        boolean logRotated = logRotation.rotateLogIfNeeded(logAppendEvent);
        logAppendEvent.setLogRotated(logRotated);
    }
    // Mark all transactions as committed
    publishAsCommitted(batch);
    return lastTransactionId;
}
Also used : SerializeTransactionEvent(org.neo4j.kernel.impl.transaction.tracing.SerializeTransactionEvent) TransactionToApply(org.neo4j.kernel.impl.api.TransactionToApply)

Example 40 with TransactionToApply

use of org.neo4j.kernel.impl.api.TransactionToApply in project neo4j by neo4j.

the class TransactionLogAppendAndRotateIT method shouldKeepTransactionsIntactWhenConcurrentlyRotationAndAppending.

@Test
void shouldKeepTransactionsIntactWhenConcurrentlyRotationAndAppending() throws Throwable {
    // GIVEN
    LogVersionRepository logVersionRepository = new SimpleLogVersionRepository();
    LogFiles logFiles = LogFilesBuilder.builder(databaseLayout, fileSystem).withLogVersionRepository(logVersionRepository).withRotationThreshold(ByteUnit.mebiBytes(1)).withTransactionIdStore(new SimpleTransactionIdStore()).withLogEntryReader(logEntryReader()).withStoreId(StoreId.UNKNOWN).build();
    life.add(logFiles);
    final AtomicBoolean end = new AtomicBoolean();
    AllTheMonitoring monitoring = new AllTheMonitoring(end, 100);
    TransactionIdStore txIdStore = new SimpleTransactionIdStore();
    TransactionMetadataCache metadataCache = new TransactionMetadataCache();
    monitoring.setLogFile(logFiles.getLogFile());
    Health health = new DatabaseHealth(mock(DatabasePanicEventGenerator.class), NullLog.getInstance());
    LogRotation rotation = transactionLogRotation(logFiles, Clock.systemUTC(), health, monitoring);
    final TransactionAppender appender = life.add(new BatchingTransactionAppender(logFiles, rotation, metadataCache, txIdStore, health));
    // WHEN
    Race race = new Race();
    for (int i = 0; i < 4; i++) {
        race.addContestant(() -> {
            while (!end.get()) {
                try {
                    appender.append(new TransactionToApply(sillyTransaction(1_000), CursorContext.NULL), LogAppendEvent.NULL);
                } catch (Exception e) {
                    e.printStackTrace(System.out);
                    end.set(true);
                    fail(e.getMessage(), e);
                }
            }
        });
    }
    race.addContestant(endAfterMax(250, MILLISECONDS, end, monitoring));
    race.go();
    // THEN
    assertTrue(monitoring.numberOfRotations() > 0);
}
Also used : DatabaseHealth(org.neo4j.monitoring.DatabaseHealth) TransactionToApply(org.neo4j.kernel.impl.api.TransactionToApply) TransactionIdStore(org.neo4j.storageengine.api.TransactionIdStore) SimpleTransactionIdStore(org.neo4j.kernel.impl.transaction.SimpleTransactionIdStore) SimpleTransactionIdStore(org.neo4j.kernel.impl.transaction.SimpleTransactionIdStore) Health(org.neo4j.monitoring.Health) DatabaseHealth(org.neo4j.monitoring.DatabaseHealth) LogFiles(org.neo4j.kernel.impl.transaction.log.files.LogFiles) SimpleLogVersionRepository(org.neo4j.kernel.impl.transaction.SimpleLogVersionRepository) DatabasePanicEventGenerator(org.neo4j.kernel.monitoring.DatabasePanicEventGenerator) LogVersionRepository(org.neo4j.storageengine.api.LogVersionRepository) SimpleLogVersionRepository(org.neo4j.kernel.impl.transaction.SimpleLogVersionRepository) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Race(org.neo4j.test.Race) LogRotation(org.neo4j.kernel.impl.transaction.log.rotation.LogRotation) FileLogRotation.transactionLogRotation(org.neo4j.kernel.impl.transaction.log.rotation.FileLogRotation.transactionLogRotation) Test(org.junit.jupiter.api.Test)

Aggregations

TransactionToApply (org.neo4j.kernel.impl.api.TransactionToApply)46 TransactionRepresentation (org.neo4j.kernel.impl.transaction.TransactionRepresentation)14 Test (org.junit.jupiter.api.Test)11 PhysicalTransactionRepresentation (org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)11 CommittedTransactionRepresentation (org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation)9 Test (org.junit.Test)7 IOException (java.io.IOException)6 CursorContext (org.neo4j.io.pagecache.context.CursorContext)6 DbmsLogEntryWriterFactory (org.neo4j.kernel.database.DbmsLogEntryWriterFactory)6 TransactionFailureException (org.neo4j.kernel.api.exceptions.TransactionFailureException)4 TransactionId (org.neo4j.storageengine.api.TransactionId)4 TransactionCommitProcess (org.neo4j.kernel.impl.api.TransactionCommitProcess)3 CommitEvent (org.neo4j.kernel.impl.transaction.tracing.CommitEvent)3 ArrayList (java.util.ArrayList)2 DefaultPageCacheTracer (org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer)2 TransactionApplier (org.neo4j.kernel.impl.api.TransactionApplier)2 LockGroup (org.neo4j.kernel.impl.locking.LockGroup)2 FakeCommitment (org.neo4j.kernel.impl.transaction.log.FakeCommitment)2 LogEntryCommit (org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommit)2 LogEntryReader (org.neo4j.kernel.impl.transaction.log.entry.LogEntryReader)2