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();
}
}
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);
}
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);
}
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;
}
}
Aggregations