Search in sources :

Example 56 with MemberId

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

the class RemoteStoreTest method shouldCopyStoreFilesAndPullTransactions.

@Test
public void shouldCopyStoreFilesAndPullTransactions() throws Exception {
    // given
    StoreId storeId = new StoreId(1, 2, 3, 4);
    StoreCopyClient storeCopyClient = mock(StoreCopyClient.class);
    TxPullClient txPullClient = mock(TxPullClient.class);
    when(txPullClient.pullTransactions(any(), any(), anyLong(), any())).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
    MemberId localhost = new MemberId(UUID.randomUUID());
    remoteStore.copy(localhost, storeId, new File("destination"));
    // then
    verify(storeCopyClient).copyStoreFiles(eq(localhost), eq(storeId), any(StoreFileStreams.class));
    verify(txPullClient).pullTransactions(eq(localhost), eq(storeId), anyLong(), any(TxPullResponseListener.class));
}
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) TxPullResponseListener(org.neo4j.causalclustering.catchup.tx.TxPullResponseListener) Monitors(org.neo4j.kernel.monitoring.Monitors) TxPullRequestResult(org.neo4j.causalclustering.catchup.TxPullRequestResult) TxPullClient(org.neo4j.causalclustering.catchup.tx.TxPullClient) File(java.io.File) Test(org.junit.Test)

Example 57 with MemberId

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

the class RaftMessageDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> list) throws Exception {
    ReadableChannel channel = new NetworkReadableClosableChannelNetty4(buffer);
    ClusterId clusterId = ClusterId.Marshal.INSTANCE.unmarshal(channel);
    int messageTypeWire = channel.getInt();
    RaftMessages.Type[] values = RaftMessages.Type.values();
    RaftMessages.Type messageType = values[messageTypeWire];
    MemberId from = retrieveMember(channel);
    RaftMessages.RaftMessage result;
    if (messageType.equals(VOTE_REQUEST)) {
        MemberId candidate = retrieveMember(channel);
        long term = channel.getLong();
        long lastLogIndex = channel.getLong();
        long lastLogTerm = channel.getLong();
        result = new RaftMessages.Vote.Request(from, term, candidate, lastLogIndex, lastLogTerm);
    } else if (messageType.equals(VOTE_RESPONSE)) {
        long term = channel.getLong();
        boolean voteGranted = channel.get() == 1;
        result = new RaftMessages.Vote.Response(from, term, voteGranted);
    } else if (messageType.equals(APPEND_ENTRIES_REQUEST)) {
        // how many
        long term = channel.getLong();
        long prevLogIndex = channel.getLong();
        long prevLogTerm = channel.getLong();
        long leaderCommit = channel.getLong();
        long count = channel.getLong();
        RaftLogEntry[] entries = new RaftLogEntry[(int) count];
        for (int i = 0; i < count; i++) {
            long entryTerm = channel.getLong();
            final ReplicatedContent content = marshal.unmarshal(channel);
            entries[i] = new RaftLogEntry(entryTerm, content);
        }
        result = new RaftMessages.AppendEntries.Request(from, term, prevLogIndex, prevLogTerm, entries, leaderCommit);
    } else if (messageType.equals(APPEND_ENTRIES_RESPONSE)) {
        long term = channel.getLong();
        boolean success = channel.get() == 1;
        long matchIndex = channel.getLong();
        long appendIndex = channel.getLong();
        result = new RaftMessages.AppendEntries.Response(from, term, success, matchIndex, appendIndex);
    } else if (messageType.equals(NEW_ENTRY_REQUEST)) {
        ReplicatedContent content = marshal.unmarshal(channel);
        result = new RaftMessages.NewEntry.Request(from, content);
    } else if (messageType.equals(HEARTBEAT)) {
        long leaderTerm = channel.getLong();
        long commitIndexTerm = channel.getLong();
        long commitIndex = channel.getLong();
        result = new RaftMessages.Heartbeat(from, leaderTerm, commitIndex, commitIndexTerm);
    } else if (messageType.equals(HEARTBEAT_RESPONSE)) {
        result = new RaftMessages.HeartbeatResponse(from);
    } else if (messageType.equals(LOG_COMPACTION_INFO)) {
        long leaderTerm = channel.getLong();
        long prevIndex = channel.getLong();
        result = new RaftMessages.LogCompactionInfo(from, leaderTerm, prevIndex);
    } else {
        throw new IllegalArgumentException("Unknown message type");
    }
    list.add(new RaftMessages.ClusterIdAwareMessage(clusterId, result));
}
Also used : ReadableChannel(org.neo4j.storageengine.api.ReadableChannel) NetworkReadableClosableChannelNetty4(org.neo4j.causalclustering.messaging.NetworkReadableClosableChannelNetty4) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) MemberId(org.neo4j.causalclustering.identity.MemberId) ClusterId(org.neo4j.causalclustering.identity.ClusterId) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) ReplicatedContent(org.neo4j.causalclustering.core.replication.ReplicatedContent)

Example 58 with MemberId

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

the class ConnectToRandomCoreServerStrategy method upstreamDatabase.

@Override
public Optional<MemberId> upstreamDatabase() throws UpstreamDatabaseSelectionException {
    final CoreTopology coreTopology = topologyService.coreServers();
    if (coreTopology.members().size() == 0) {
        throw new UpstreamDatabaseSelectionException("No core servers available");
    }
    int skippedServers = random.nextInt(coreTopology.members().size());
    final Iterator<MemberId> iterator = coreTopology.members().keySet().iterator();
    MemberId member;
    do {
        member = iterator.next();
    } while (skippedServers-- > 0);
    return Optional.ofNullable(member);
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology)

Example 59 with MemberId

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

the class ReadReplicaStartupProcess method start.

@Override
public void start() throws IOException {
    boolean syncedWithUpstream = false;
    RetryStrategy.Timeout timeout = retryStrategy.newTimeout();
    int attempt = 0;
    while (!syncedWithUpstream) {
        attempt++;
        MemberId source = null;
        try {
            source = selectionStrategyPipeline.bestUpstreamDatabase();
            syncStoreWithUpstream(source);
            syncedWithUpstream = true;
        } catch (UpstreamDatabaseSelectionException e) {
            lastIssue = issueOf("finding upstream member", attempt);
            debugLog.warn(lastIssue);
        } catch (StoreCopyFailedException e) {
            lastIssue = issueOf(format("copying store files from %s", source), attempt);
            debugLog.warn(lastIssue);
        } catch (StreamingTransactionsFailedException e) {
            lastIssue = issueOf(format("streaming transactions from %s", source), attempt);
            debugLog.warn(lastIssue);
        } catch (StoreIdDownloadFailedException e) {
            lastIssue = issueOf(format("getting store id from %s", source), attempt);
            debugLog.warn(lastIssue);
        }
        try {
            Thread.sleep(timeout.getMillis());
            timeout.increment();
        } catch (InterruptedException e) {
            Thread.interrupted();
            lastIssue = "Interrupted while trying to start read replica";
            debugLog.warn(lastIssue);
            break;
        }
    }
    if (!syncedWithUpstream) {
        userLog.error(lastIssue);
        throw new RuntimeException(lastIssue);
    }
    try {
        localDatabase.start();
        txPulling.start();
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) StoreCopyFailedException(org.neo4j.causalclustering.catchup.storecopy.StoreCopyFailedException) StreamingTransactionsFailedException(org.neo4j.causalclustering.catchup.storecopy.StreamingTransactionsFailedException) StoreIdDownloadFailedException(org.neo4j.causalclustering.catchup.storecopy.StoreIdDownloadFailedException) RetryStrategy(org.neo4j.causalclustering.helper.RetryStrategy)

Example 60 with MemberId

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

the class UpstreamDatabaseStrategySelector method bestUpstreamDatabase.

public MemberId bestUpstreamDatabase() throws UpstreamDatabaseSelectionException {
    MemberId result = null;
    for (UpstreamDatabaseSelectionStrategy strategy : strategies) {
        log.debug("Trying selection strategy [%s]", strategy.toString());
        try {
            if (strategy.upstreamDatabase().isPresent()) {
                result = strategy.upstreamDatabase().get();
                break;
            }
        } catch (NoSuchElementException ex) {
        // Do nothing, this strategy failed
        }
    }
    if (result == null) {
        throw new UpstreamDatabaseSelectionException("Could not find an upstream database with which to connect.");
    }
    log.debug("Selected upstream database [%s]", result);
    return result;
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

MemberId (org.neo4j.causalclustering.identity.MemberId)114 Test (org.junit.Test)83 HashMap (java.util.HashMap)26 CoreTopology (org.neo4j.causalclustering.discovery.CoreTopology)16 CoreServerInfo (org.neo4j.causalclustering.discovery.CoreServerInfo)15 LeaderLocator (org.neo4j.causalclustering.core.consensus.LeaderLocator)13 ReadReplicaTopology (org.neo4j.causalclustering.discovery.ReadReplicaTopology)12 DirectNetworking (org.neo4j.causalclustering.core.consensus.DirectNetworking)10 RaftTestFixture (org.neo4j.causalclustering.core.consensus.RaftTestFixture)10 CoreTopologyService (org.neo4j.causalclustering.discovery.CoreTopologyService)10 Log (org.neo4j.logging.Log)10 ClusterId (org.neo4j.causalclustering.identity.ClusterId)9 ArrayList (java.util.ArrayList)8 UUID (java.util.UUID)8 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)8 TopologyService (org.neo4j.causalclustering.discovery.TopologyService)7 ByteBuf (io.netty.buffer.ByteBuf)6 File (java.io.File)6 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)6 HashSet (java.util.HashSet)5