Search in sources :

Example 21 with StoreId

use of org.neo4j.causalclustering.identity.StoreId in project neo4j by neo4j.

the class CoreStateDownloader method downloadSnapshot.

public synchronized void downloadSnapshot(MemberId source, CoreState coreState) throws StoreCopyFailedException {
    try {
        /* Extract some key properties before shutting it down. */
        boolean isEmptyStore = localDatabase.isEmpty();
        if (!isEmptyStore) {
            /* make sure it's recovered before we start messing with catchup */
            localDatabase.start();
            localDatabase.stop();
        }
        StoreId remoteStoreId = remoteStore.getStoreId(source);
        if (!isEmptyStore && !remoteStoreId.equals(localDatabase.storeId())) {
            throw new StoreCopyFailedException("StoreId mismatch and not empty");
        }
        startStopOnStoreCopy.stop();
        localDatabase.stopForStoreCopy();
        log.info("Downloading snapshot from core server at %s", source);
        /* The core snapshot must be copied before the store, because the store has a dependency on
             * the state of the state machines. The store will thus be at or ahead of the state machines,
             * in consensus log index, and application of commands will bring them in sync. Any such commands
             * that carry transactions will thus be ignored by the transaction/token state machines, since they
             * are ahead, and the correct decisions for their applicability have already been taken as encapsulated
             * in the copied store. */
        CoreSnapshot coreSnapshot = catchUpClient.makeBlockingRequest(source, new CoreSnapshotRequest(), new CatchUpResponseAdaptor<CoreSnapshot>() {

            @Override
            public void onCoreSnapshot(CompletableFuture<CoreSnapshot> signal, CoreSnapshot response) {
                signal.complete(response);
            }
        });
        if (isEmptyStore) {
            storeCopyProcess.replaceWithStoreFrom(source, remoteStoreId);
        } else {
            StoreId localStoreId = localDatabase.storeId();
            CatchupResult catchupResult = remoteStore.tryCatchingUp(source, localStoreId, localDatabase.storeDir());
            if (catchupResult == E_TRANSACTION_PRUNED) {
                log.info("Failed to pull transactions from " + source + ". They may have been pruned away.");
                localDatabase.delete();
                storeCopyProcess.replaceWithStoreFrom(source, localStoreId);
            } else if (catchupResult != SUCCESS_END_OF_STREAM) {
                throw new StoreCopyFailedException("Failed to download store: " + catchupResult);
            }
        }
        /* We install the snapshot after the store has been downloaded,
             * so that we are not left with a state ahead of the store. */
        coreState.installSnapshot(coreSnapshot);
        log.info("Core snapshot installed: " + coreSnapshot);
        /* Starting the database will invoke the commit process factory in
             * the EnterpriseCoreEditionModule, which has important side-effects. */
        log.info("Starting local database");
        localDatabase.start();
        coreStateMachines.installCommitProcess(localDatabase.getCommitProcess());
        startStopOnStoreCopy.start();
    } catch (StoreCopyFailedException e) {
        throw e;
    } catch (Throwable e) {
        throw new StoreCopyFailedException(e);
    }
}
Also used : StoreCopyFailedException(org.neo4j.causalclustering.catchup.storecopy.StoreCopyFailedException) StoreId(org.neo4j.causalclustering.identity.StoreId) CatchupResult(org.neo4j.causalclustering.catchup.CatchupResult)

Example 22 with StoreId

use of org.neo4j.causalclustering.identity.StoreId in project neo4j by neo4j.

the class RemoteStoreTest method shouldSetLastPulledTransactionId.

@Test
public void shouldSetLastPulledTransactionId() throws Exception {
    // given
    long lastFlushedTxId = 12;
    StoreId wantedStoreId = new StoreId(1, 2, 3, 4);
    MemberId localhost = new MemberId(UUID.randomUUID());
    StoreCopyClient storeCopyClient = mock(StoreCopyClient.class);
    when(storeCopyClient.copyStoreFiles(eq(localhost), eq(wantedStoreId), any(StoreFileStreams.class))).thenReturn(lastFlushedTxId);
    TxPullClient txPullClient = mock(TxPullClient.class);
    when(txPullClient.pullTransactions(eq(localhost), eq(wantedStoreId), anyLong(), any(TxPullResponseListener.class))).thenReturn(new TxPullRequestResult(SUCCESS_END_OF_STREAM, 13));
    TransactionLogCatchUpWriter writer = mock(TransactionLogCatchUpWriter.class);
    RemoteStore remoteStore = new RemoteStore(NullLogProvider.getInstance(), mock(FileSystemAbstraction.class), null, storeCopyClient, txPullClient, factory(writer), new Monitors());
    // when
    remoteStore.copy(localhost, wantedStoreId, new File("destination"));
    // then
    // the interface is defined as asking for the one preceding
    long previousTxId = lastFlushedTxId - 1;
    verify(txPullClient).pullTransactions(eq(localhost), eq(wantedStoreId), eq(previousTxId), any(TxPullResponseListener.class));
}
Also used : FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) TxPullResponseListener(org.neo4j.causalclustering.catchup.tx.TxPullResponseListener) TxPullRequestResult(org.neo4j.causalclustering.catchup.TxPullRequestResult) MemberId(org.neo4j.causalclustering.identity.MemberId) TransactionLogCatchUpWriter(org.neo4j.causalclustering.catchup.tx.TransactionLogCatchUpWriter) StoreId(org.neo4j.causalclustering.identity.StoreId) Monitors(org.neo4j.kernel.monitoring.Monitors) TxPullClient(org.neo4j.causalclustering.catchup.tx.TxPullClient) File(java.io.File) Test(org.junit.Test)

Example 23 with StoreId

use of org.neo4j.causalclustering.identity.StoreId in project neo4j by neo4j.

the class RemoteStoreTest method shouldCloseDownTxLogWriterIfTxStreamingFails.

@Test
public void shouldCloseDownTxLogWriterIfTxStreamingFails() throws Exception {
    // given
    StoreId storeId = new StoreId(1, 2, 3, 4);
    StoreCopyClient storeCopyClient = mock(StoreCopyClient.class);
    TxPullClient txPullClient = mock(TxPullClient.class);
    TransactionLogCatchUpWriter writer = mock(TransactionLogCatchUpWriter.class);
    RemoteStore remoteStore = new RemoteStore(NullLogProvider.getInstance(), mock(FileSystemAbstraction.class), null, storeCopyClient, txPullClient, factory(writer), new Monitors());
    doThrow(StoreCopyFailedException.class).when(txPullClient).pullTransactions(any(MemberId.class), eq(storeId), anyLong(), any(TransactionLogCatchUpWriter.class));
    // when
    try {
        remoteStore.copy(null, storeId, null);
    } catch (StoreCopyFailedException e) {
    // expected
    }
    // then
    verify(writer).close();
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) TransactionLogCatchUpWriter(org.neo4j.causalclustering.catchup.tx.TransactionLogCatchUpWriter) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) StoreId(org.neo4j.causalclustering.identity.StoreId) Monitors(org.neo4j.kernel.monitoring.Monitors) TxPullClient(org.neo4j.causalclustering.catchup.tx.TxPullClient) Test(org.junit.Test)

Example 24 with StoreId

use of org.neo4j.causalclustering.identity.StoreId in project neo4j by neo4j.

the class StoreFilesTest method mustReadStoreId.

@Test
public void mustReadStoreId() throws Exception {
    File dir = getBaseDir();
    File neostore = new File(dir, MetaDataStore.DEFAULT_NAME);
    ThreadLocalRandom rng = ThreadLocalRandom.current();
    long time = rng.nextLong();
    long randomNumber = rng.nextLong();
    long upgradeTime = rng.nextLong();
    long upgradeTransactionId = rng.nextLong();
    createOnPageCache(neostore);
    MetaDataStore.setRecord(pageCache, neostore, Position.TIME, time);
    MetaDataStore.setRecord(pageCache, neostore, Position.RANDOM_NUMBER, randomNumber);
    MetaDataStore.setRecord(pageCache, neostore, Position.STORE_VERSION, rng.nextLong());
    MetaDataStore.setRecord(pageCache, neostore, Position.UPGRADE_TIME, upgradeTime);
    MetaDataStore.setRecord(pageCache, neostore, Position.UPGRADE_TRANSACTION_ID, upgradeTransactionId);
    StoreId storeId = storeFiles.readStoreId(dir);
    assertThat(storeId.getCreationTime(), is(time));
    assertThat(storeId.getRandomId(), is(randomNumber));
    assertThat(storeId.getUpgradeTime(), is(upgradeTime));
    assertThat(storeId.getUpgradeId(), is(upgradeTransactionId));
}
Also used : StoreId(org.neo4j.causalclustering.identity.StoreId) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) File(java.io.File) Test(org.junit.Test)

Example 25 with StoreId

use of org.neo4j.causalclustering.identity.StoreId in project neo4j by neo4j.

the class TestStoreId method doReadStoreId.

private static StoreId doReadStoreId(File coreStoreDir, PageCache pageCache) throws IOException {
    File metadataStore = new File(coreStoreDir, MetaDataStore.DEFAULT_NAME);
    long creationTime = MetaDataStore.getRecord(pageCache, metadataStore, TIME);
    long randomNumber = MetaDataStore.getRecord(pageCache, metadataStore, RANDOM_NUMBER);
    long upgradeTime = MetaDataStore.getRecord(pageCache, metadataStore, UPGRADE_TIME);
    long upgradeId = MetaDataStore.getRecord(pageCache, metadataStore, UPGRADE_TRANSACTION_ID);
    return new StoreId(creationTime, randomNumber, upgradeTime, upgradeId);
}
Also used : StoreId(org.neo4j.causalclustering.identity.StoreId) File(java.io.File)

Aggregations

StoreId (org.neo4j.causalclustering.identity.StoreId)28 Test (org.junit.Test)14 Monitors (org.neo4j.kernel.monitoring.Monitors)9 File (java.io.File)6 CatchupServerProtocol (org.neo4j.causalclustering.catchup.CatchupServerProtocol)6 LogicalTransactionStore (org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore)6 TransactionIdStore (org.neo4j.kernel.impl.transaction.log.TransactionIdStore)6 MemberId (org.neo4j.causalclustering.identity.MemberId)5 NetworkReadableClosableChannelNetty4 (org.neo4j.causalclustering.messaging.NetworkReadableClosableChannelNetty4)4 TransactionLogCatchUpWriter (org.neo4j.causalclustering.catchup.tx.TransactionLogCatchUpWriter)3 TxPullClient (org.neo4j.causalclustering.catchup.tx.TxPullClient)3 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)3 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 CatchupResult (org.neo4j.causalclustering.catchup.CatchupResult)2 TxPullRequestResult (org.neo4j.causalclustering.catchup.TxPullRequestResult)2 StoreCopyFailedException (org.neo4j.causalclustering.catchup.storecopy.StoreCopyFailedException)2 TxPullResponseListener (org.neo4j.causalclustering.catchup.tx.TxPullResponseListener)2 UpstreamDatabaseSelectionException (org.neo4j.causalclustering.readreplica.UpstreamDatabaseSelectionException)2 CommittedTransactionRepresentation (org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation)2 NoSuchTransactionException (org.neo4j.kernel.impl.transaction.log.NoSuchTransactionException)2