Search in sources :

Example 1 with ReadOnlyTransactionStore

use of org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore 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();
    }
}
Also used : TransactionToApply(org.neo4j.kernel.impl.api.TransactionToApply) CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) TransactionAppender(org.neo4j.kernel.impl.transaction.log.TransactionAppender) TransactionRepresentation(org.neo4j.kernel.impl.transaction.TransactionRepresentation) CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) LogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore) StorageEngine(org.neo4j.storageengine.api.StorageEngine) ReadOnlyTransactionStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore) DependencyResolver(org.neo4j.graphdb.DependencyResolver) ProgressListener(org.neo4j.helpers.progress.ProgressListener) TransactionRepresentationCommitProcess(org.neo4j.kernel.impl.api.TransactionRepresentationCommitProcess) Monitors(org.neo4j.kernel.monitoring.Monitors) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) PageCache(org.neo4j.io.pagecache.PageCache)

Example 2 with ReadOnlyTransactionStore

use of org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore in project neo4j by neo4j.

the class BackupServiceIT method checkLastCommittedTxIdInLogAndNeoStore.

private void checkLastCommittedTxIdInLogAndNeoStore(long txId, long txIdFromOrigin) throws Exception {
    // Assert last committed transaction can be found in tx log and is the last tx in the log
    LifeSupport life = new LifeSupport();
    PageCache pageCache = pageCacheRule.getPageCache(fileSystem);
    LogicalTransactionStore transactionStore = life.add(new ReadOnlyTransactionStore(pageCache, fileSystem, backupDir, monitors));
    life.start();
    try (IOCursor<CommittedTransactionRepresentation> cursor = transactionStore.getTransactions(txId)) {
        assertTrue(cursor.next());
        assertEquals(txId, cursor.get().getCommitEntry().getTxId());
        assertFalse(cursor.next());
    } finally {
        life.shutdown();
    }
    // Assert last committed transaction is correct in neostore
    assertEquals(txId, txIdFromOrigin);
}
Also used : CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) LogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore) PageCache(org.neo4j.io.pagecache.PageCache) ReadOnlyTransactionStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore)

Example 3 with ReadOnlyTransactionStore

use of org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore in project neo4j by neo4j.

the class CoreBootstrapperTest method shouldSetAllCoreState.

@Test
public void shouldSetAllCoreState() throws Exception {
    // given
    int nodeCount = 100;
    FileSystemAbstraction fileSystem = fileSystemRule.get();
    File classicNeo4jStore = RestoreClusterUtils.createClassicNeo4jStore(testDirectory.directory(), fileSystem, nodeCount, Standard.LATEST_NAME);
    PageCache pageCache = pageCacheRule.getPageCache(fileSystem);
    CoreBootstrapper bootstrapper = new CoreBootstrapper(classicNeo4jStore, pageCache, fileSystem, Config.defaults(), NullLogProvider.getInstance());
    // when
    Set<MemberId> membership = asSet(randomMember(), randomMember(), randomMember());
    CoreSnapshot snapshot = bootstrapper.bootstrap(membership);
    // then
    assertEquals(nodeCount, ((IdAllocationState) snapshot.get(CoreStateType.ID_ALLOCATION)).firstUnallocated(IdType.NODE));
    /* Bootstrapped state is created in RAFT land at index -1 and term -1. */
    assertEquals(0, snapshot.prevIndex());
    assertEquals(0, snapshot.prevTerm());
    /* Lock is initially not taken. */
    assertEquals(new ReplicatedLockTokenState(), snapshot.get(CoreStateType.LOCK_TOKEN));
    /* Raft has the bootstrapped set of members initially. */
    assertEquals(membership, ((RaftCoreState) snapshot.get(CoreStateType.RAFT_CORE_STATE)).committed().members());
    /* The session state is initially empty. */
    assertEquals(new GlobalSessionTrackerState(), snapshot.get(CoreStateType.SESSION_TRACKER));
    LastCommittedIndexFinder lastCommittedIndexFinder = new LastCommittedIndexFinder(new ReadOnlyTransactionIdStore(pageCache, classicNeo4jStore), new ReadOnlyTransactionStore(pageCache, fileSystem, classicNeo4jStore, new Monitors()), NullLogProvider.getInstance());
    long lastCommittedIndex = lastCommittedIndexFinder.getLastCommittedIndex();
    assertEquals(-1, lastCommittedIndex);
}
Also used : FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) LastCommittedIndexFinder(org.neo4j.causalclustering.core.state.machines.tx.LastCommittedIndexFinder) ReadOnlyTransactionStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore) GlobalSessionTrackerState(org.neo4j.causalclustering.core.replication.session.GlobalSessionTrackerState) MemberId(org.neo4j.causalclustering.identity.MemberId) CoreSnapshot(org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot) ReadOnlyTransactionIdStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionIdStore) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) Monitors(org.neo4j.kernel.monitoring.Monitors) ReplicatedLockTokenState(org.neo4j.causalclustering.core.state.machines.locks.ReplicatedLockTokenState) File(java.io.File) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.Test)

Example 4 with ReadOnlyTransactionStore

use of org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore 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;
    }
}
Also used : CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) ReadOnlyTransactionIdStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionIdStore) TransactionCursor(org.neo4j.kernel.impl.transaction.log.TransactionCursor) Monitors(org.neo4j.kernel.monitoring.Monitors) NoSuchTransactionException(org.neo4j.kernel.impl.transaction.log.NoSuchTransactionException) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) ReadOnlyTransactionStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore)

Aggregations

ReadOnlyTransactionStore (org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore)4 PageCache (org.neo4j.io.pagecache.PageCache)3 CommittedTransactionRepresentation (org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation)3 Monitors (org.neo4j.kernel.monitoring.Monitors)3 LogicalTransactionStore (org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore)2 ReadOnlyTransactionIdStore (org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionIdStore)2 LifeSupport (org.neo4j.kernel.lifecycle.LifeSupport)2 File (java.io.File)1 Test (org.junit.Test)1 GlobalSessionTrackerState (org.neo4j.causalclustering.core.replication.session.GlobalSessionTrackerState)1 ReplicatedLockTokenState (org.neo4j.causalclustering.core.state.machines.locks.ReplicatedLockTokenState)1 LastCommittedIndexFinder (org.neo4j.causalclustering.core.state.machines.tx.LastCommittedIndexFinder)1 CoreSnapshot (org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot)1 RaftCoreState (org.neo4j.causalclustering.core.state.snapshot.RaftCoreState)1 MemberId (org.neo4j.causalclustering.identity.MemberId)1 DependencyResolver (org.neo4j.graphdb.DependencyResolver)1 ProgressListener (org.neo4j.helpers.progress.ProgressListener)1 DefaultFileSystemAbstraction (org.neo4j.io.fs.DefaultFileSystemAbstraction)1 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)1 TransactionRepresentationCommitProcess (org.neo4j.kernel.impl.api.TransactionRepresentationCommitProcess)1